File: GameControllerTextInput.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (438 lines) | stat: -rw-r--r-- 12,535 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <cassert>
#include <SDL_keyboard.h>

#include "GameControllerTextInput.h"
#include "Action.h"
#include "ConsoleHistory.h"
#include "Game.h"
#include "InMapDraw.h"
#include "WordCompletion.h"
#include "Game/UI/GuiHandler.h"
#include "Game/UI/KeyCodes.h"
#include "Rendering/Fonts/glFont.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/RenderDataBuffer.hpp"
#include "Rendering/GlobalRendering.h"
#include "System/StringUtil.h"
#include "System/Log/ILog.h"
#include "System/Platform/Clipboard.h"

static constexpr float4 const  defColor(1.0f, 1.0f, 1.0f, 1.0f);
static constexpr float4 const allyColor(0.5f, 1.0f, 0.5f, 1.0f);
static constexpr float4 const specColor(1.0f, 1.0f, 0.5f, 1.0f);

GameControllerTextInput gameTextInput;

void GameControllerTextInput::ViewResize() {
	// inputTextSizeX and inputTextSizeY aren't actually used by anything
	// so we assume those values are bad, and we could simply ignore the X component
	// that said, the width of the SDL TextInputRect doesn't seem to matter either, as
	// it tends to be printed in groups of 10 characters.
	textEditingWindow.x =        inputTextPosX  * globalRendering->viewSizeX;
	textEditingWindow.y = (1.0 - inputTextPosY) * globalRendering->viewSizeY;
	// textEditingWindow.w = globalRendering->viewSizeX - textEditingWindow.y; // remaining width
	textEditingWindow.w = inputTextSizeX * globalRendering->viewSizeX;
	textEditingWindow.h = inputTextSizeY * globalRendering->viewSizeY;

	SDL_SetTextInputRect(&textEditingWindow);
}

void GameControllerTextInput::Draw() {
	if (!userWriting)
		return;

	const float fontScale = 1.0f; // TODO: make configurable again
	const float fontSize = fontScale * font->GetSize();

	const std::string userString = userPrompt + userInput + editText;

	if (drawTextCaret) {
		int curCaretIdx = writingPos + editingPos; // advanced by GetNextChar
		int prvCaretIdx = curCaretIdx;

		char32_t c = utf8::GetNextChar(userInput + editText, curCaretIdx);

		// make caret always visible
		if (c == 0)
			c = ' ';


		const float caretRelPos = fontSize * font->GetTextWidth(userString.substr(0, userPrompt.length() + prvCaretIdx)) * globalRendering->pixelX;
		const float caretHeight = fontSize * font->GetLineHeight() * globalRendering->pixelY;
		const float caretWidth  = fontSize * font->GetCharacterWidth(c) * globalRendering->pixelX;

		const float caretScrPos = inputTextPosX + caretRelPos;
		const float caretIllum = 0.5f * (1.0f + fastmath::sin(spring_now().toMilliSecsf() * 0.015f));

		GL::RenderDataBufferC* buffer = GL::GetRenderBufferC();
		Shader::IProgramObject* shader = buffer->GetShader();

		shader->Enable();
		shader->SetUniformMatrix4x4<float>("u_movi_mat", false, CMatrix44f::Identity());
		shader->SetUniformMatrix4x4<float>("u_proj_mat", false, CMatrix44f::ClipOrthoProj01(globalRendering->supportClipSpaceControl * 1.0f));
		buffer->SafeAppend({{caretScrPos             , inputTextPosY              , 0.0f}, {caretIllum, caretIllum, caretIllum, 0.75f}}); // tl
		buffer->SafeAppend({{caretScrPos             , inputTextPosY + caretHeight, 0.0f}, {caretIllum, caretIllum, caretIllum, 0.75f}}); // bl
		buffer->SafeAppend({{caretScrPos + caretWidth, inputTextPosY + caretHeight, 0.0f}, {caretIllum, caretIllum, caretIllum, 0.75f}}); // br

		buffer->SafeAppend({{caretScrPos + caretWidth, inputTextPosY + caretHeight, 0.0f}, {caretIllum, caretIllum, caretIllum, 0.75f}}); // br
		buffer->SafeAppend({{caretScrPos + caretWidth, inputTextPosY              , 0.0f}, {caretIllum, caretIllum, caretIllum, 0.75f}}); // tr
		buffer->SafeAppend({{caretScrPos             , inputTextPosY              , 0.0f}, {caretIllum, caretIllum, caretIllum, 0.75f}}); // tl
		buffer->Submit(GL_TRIANGLES);
		shader->Disable();
	}

	// setup the color
	const float4* textColor = &defColor;

	if (userInput.length() < 2) {
		textColor = &defColor;
	} else if ((userInput.find_first_of("aA") == 0) && (userInput[1] == ':')) {
		textColor = &allyColor;
	} else if ((userInput.find_first_of("sS") == 0) && (userInput[1] == ':')) {
		textColor = &specColor;
	} else {
		textColor = &defColor;
	}


	// draw the text
	font->SetColors(textColor, nullptr);
	font->glPrint(inputTextPosX, inputTextPosY, fontSize, FONT_DESCENDER | (FONT_OUTLINE * guihandler->GetOutlineFonts()) | FONT_NORM | FONT_BUFFERED, userString);
	font->DrawBufferedGL4();
}


int GameControllerTextInput::SetInputText(const std::string& utf8Text) {
	if (!userWriting)
		return 0;

	const std::string text = ignoreNextChar ? utf8Text.substr(utf8::NextChar(utf8Text, 0)) : utf8Text;

	userInput.insert(writingPos = Clamp<int>(writingPos, 0, userInput.length()), text);
	editText = "";

	writingPos += text.length();
	editingPos = 0;
	return 0;
}


bool GameControllerTextInput::SendPromptInput() {
	if (userWriting)
		return false;
	if (!userChatting)
		return false;

	std::string msg = userInput;
	std::string pfx;

	if ((userInput.find_first_of("aAsS") == 0) && (userInput[1] == ':')) {
		pfx = userInput.substr(0, 2);
		msg = userInput.substr(2);
	}
	if ((msg[0] == '/') && (msg[1] == '/'))
		msg = msg.substr(1);

	userInput = pfx + msg;
	return true;
}

bool GameControllerTextInput::SendLabelInput() {
	if (userWriting)
		return false;

	if (userInput.size() > 200) {
		// avoid troubles with long lines
		userInput = userInput.substr(0, 200);
		writingPos = (int)userInput.length();
	}

	inMapDrawer->SendWaitingInput(userInput);
	return true;
}


void GameControllerTextInput::PasteClipboard() {
	const CClipboard clipboard;
	const std::string text = clipboard.GetContents();

	userInput.insert(writingPos, text);
	writingPos += text.length();
}


bool GameControllerTextInput::HandleChatCommand(int key, const std::string& command) {
	switch (hashString(command.c_str())) {
		case hashString("chatswitchall"): {
			if ((userInput.find_first_of("aAsS") == 0) && (userInput[1] == ':')) {
				userInput = userInput.substr(2);
				writingPos = std::max(0, writingPos - 2);
			}

			userInputPrefix = "";
			return true;
		} break;

		case hashString("chatswitchally"): {
			if ((userInput.find_first_of("aA") == 0) && (userInput[1] == ':')) {
				// already in ally chat, toggle it off
				userInput = userInput.substr(2);
				userInputPrefix = "";
				writingPos = std::max(0, writingPos - 2);
			}
			else if ((userInput.find_first_of("sS") == 0) && (userInput[1] == ':')) {
				// already in spec chat, switch to ally chat
				userInput[0] = 'a';
				userInputPrefix = "a:";
			} else {
				userInput = "a:" + userInput;
				userInputPrefix = "a:";
				writingPos += 2;
			}

			return true;
		} break;

		case hashString("chatswitchspec"): {
			if ((userInput.find_first_of("sS") == 0) && (userInput[1] == ':')) {
				// already in spec chat, toggle it off
				userInput = userInput.substr(2);
				userInputPrefix = "";
				writingPos = std::max(0, writingPos - 2);
			}
			else if ((userInput.find_first_of("aA") == 0) && (userInput[1] == ':')) {
				// already in ally chat, switch to spec chat
				userInput[0] = 's';
				userInputPrefix = "s:";
			} else {
				userInput = "s:" + userInput;
				userInputPrefix = "s:";
				writingPos += 2;
			}

			return true;
		} break;
	}

	// unknown chat-command
	return false;
}


// can only be called by CGame (via ConsumePressedKey)
bool GameControllerTextInput::HandleEditCommand(int key, const std::string& command) {
	switch (hashString(command.c_str())) {
		case hashString("edit_return"): {
			userWriting = false;
			writingPos = 0;

			if (userChatting) {
				std::string cmd;

				if ((userInput.find_first_of("aAsS") == 0) && (userInput[1] == ':')) {
					cmd = userInput.substr(2);
				} else {
					cmd = userInput;
				}

				if (game->ProcessCommandText(key, cmd)) {
					// execute an action
					gameConsoleHistory.AddLine(cmd);
					ClearInput();
				}
			}

			SDL_StopTextInput();
			return true;
		} break;

		case hashString("edit_escape"): {
			if (userChatting || inMapDrawer->IsWantLabel()) {
				if (userChatting)
					gameConsoleHistory.AddLine(userInput);

				userWriting = false;

				ClearInput();
				inMapDrawer->SetWantLabel(false);
			}

			SDL_StopTextInput();
			return true;
		} break;

		case hashString("edit_complete"): {
			std::string head = userInput.substr(0, writingPos);
			std::string tail = userInput.substr(writingPos);

			// NB: sets head to the first partial match
			const std::vector<std::string>& partials = wordCompletion.Complete(head);

			userInput = head + tail;
			writingPos = head.length();

			if (!partials.empty()) {
				std::string msg;
				for (const std::string& match: partials) {
					msg.append("  ");
					msg.append(match);
				}
				LOG("%s", msg.c_str());
			}

			return true;
		} break;


		case hashString("edit_backspace"): {
			if (!userInput.empty() && (writingPos > 0)) {
				const int prev = utf8::PrevChar(userInput, writingPos);
				userInput.erase(prev, writingPos - prev);
				writingPos = prev;
			}

			return true;
		} break;

		case hashString("edit_delete"): {
			if (!userInput.empty() && (writingPos < (int)userInput.size()))
				userInput.erase(writingPos, utf8::CharLen(userInput, writingPos));

			return true;
		} break;


		case hashString("edit_home"): {
			writingPos = 0;
			return true;
		} break;

		case hashString("edit_end"): {
			writingPos = userInput.length();
			return true;
		} break;


		case hashString("edit_prev_char"): {
			writingPos = utf8::PrevChar(userInput, writingPos);
			return true;
		} break;

		case hashString("edit_next_char"): {
			writingPos = utf8::NextChar(userInput, writingPos);
			return true;
		} break;


		case hashString("edit_prev_word"): {
			//TODO: does not seem to work correctly with utf-8
			// prev word
			const char* s = userInput.c_str();
			int p = writingPos;
			while ((p > 0) && !isalnum(s[p - 1])) { p--; }
			while ((p > 0) &&  isalnum(s[p - 1])) { p--; }
			writingPos = p;
			return true;
		} break;

		case hashString("edit_next_word"): {
			// TODO: does not seem to work correctly with utf-8
			const size_t len = userInput.length();
			const char* s = userInput.c_str();
			size_t p = writingPos;
			while ((p < len) && !isalnum(s[p])) { p++; }
			while ((p < len) &&  isalnum(s[p])) { p++; }
			writingPos = p;
			return true;
		} break;


		case hashString("edit_prev_line"): {
			if (userChatting) {
				userInput = gameConsoleHistory.PrevLine(userInput);
				writingPos = (int)userInput.length();
				return true;
			}
		} break;

		case hashString("edit_next_line"): {
			if (userChatting) {
				userInput = gameConsoleHistory.NextLine(userInput);
				writingPos = (int)userInput.length();
				return true;
			}
		} break;
	}

	// unknown edit-command
	return false;
}


bool GameControllerTextInput::HandlePasteCommand(int key, const std::string& rawLine) {
	// we cannot use extra commands because tokenization strips multiple
	// spaces or even trailing spaces, the text should be copied verbatim
	const std::string pastecommand = "pastetext ";

	if (rawLine.length() > pastecommand.length()) {
		userInput.insert(writingPos, rawLine.substr(pastecommand.length(), rawLine.length() - pastecommand.length()));
		writingPos += (rawLine.length() - pastecommand.length());
	} else {
		PasteClipboard();
	}

	return true;
}

bool GameControllerTextInput::CheckHandlePasteCommand(const std::string& rawLine) {
	if (!userWriting)
		return false;

	return (HandlePasteCommand(0, rawLine));
}


bool GameControllerTextInput::ProcessKeyPressAction(int key, const Action& action) {
	assert(userWriting);

	if (action.command == "pastetext")
		return (HandlePasteCommand(key, action.rawline));

	if (action.command.find("edit_") == 0)
		return (HandleEditCommand(key, action.command));

	if (action.command.find("chatswitch") == 0)
		return (HandleChatCommand(key, action.command));

	return false;
}


bool GameControllerTextInput::ConsumePressedKey(int key, const std::vector<Action>& actions) {
	if (!userWriting)
		return false;

	for (const Action& action: actions) {
		if (!ProcessKeyPressAction(key, action))
			continue;

		// the key was used, ignore it (ex: alt+a)
		ignoreNextChar = keyCodes.IsPrintable(key);
		break;
	}

	return true;
}


bool GameControllerTextInput::ConsumeReleasedKey(int key) const {
	if (!userWriting)
		return false;
	if (keyCodes.IsPrintable(key))
		return true;

	return (key == SDLK_RETURN || key == SDLK_BACKSPACE || key == SDLK_DELETE || key == SDLK_HOME || key == SDLK_END || key == SDLK_RIGHT || key == SDLK_LEFT);
}