File: GameControllerTextInput.h

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 (108 lines) | stat: -rw-r--r-- 2,241 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef GAME_CONTROLLER_TEXTINPUT_H
#define GAME_CONTROLLER_TEXTINPUT_H

#include <string>
#include <vector>

#include <SDL_rect.h>

class Action;
struct GameControllerTextInput {
public:
	void ViewResize();
	void Draw();

	void SetPos(float px, float py) {
		inputTextPosX  = px;
		inputTextPosY  = py;
	}
	void SetSize(float sx, float sy) {
		inputTextSizeX = sx;
		inputTextSizeY = sy;
	}


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

		editText = utf8Text;
		editingPos = utf8Text.length();
		return 0;
	}


	void PromptInput(const std::string* inputPrefix) {
		if (inputPrefix != nullptr)
			userInputPrefix = *inputPrefix;

		userWriting = true;
		userChatting = true;
		ignoreNextChar = false;

		userPrompt = "Say: ";
		userInput = userInputPrefix;
		writingPos = (int)userInput.length();
	}

	void PromptLabel() {
		userWriting = true;
		userPrompt = "Label: ";
		ignoreNextChar = true;
	}


	bool SendPromptInput();
	bool SendLabelInput();
	void ClearInput() {
		userChatting = false;

		userInput = "";
		writingPos = 0;
	}


	bool CheckHandlePasteCommand(const std::string& rawLine);

	bool ConsumePressedKey(int key, const std::vector<Action>& actions);
	bool ConsumeReleasedKey(int key) const;

private:
	void PasteClipboard();

	bool HandleChatCommand(int key, const std::string& command);
	bool HandleEditCommand(int key, const std::string& command);
	bool HandlePasteCommand(int key, const std::string& rawLine);
	bool ProcessKeyPressAction(int key, const Action& action);

public:
	/// current writing/editing positions
	int writingPos = 0;
	int editingPos = 0;

	float inputTextPosX = 0.0f;
	float inputTextPosY = 0.0f;
	float inputTextSizeX = 0.0f;
	float inputTextSizeY = 0.0f;

	bool userWriting = false; // true for any text-input (chat, labels)
	bool userChatting = false; // true for chat-actions

	bool ignoreNextChar = false;
	bool drawTextCaret = true;

	SDL_Rect textEditingWindow;

	std::string userInput;
	std::string userInputPrefix;
	std::string userPrompt;
	std::string editText;
};

extern GameControllerTextInput gameTextInput;

#endif