Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CInput.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_CINPUT
19#define INCLUDED_CINPUT
20
21#include "gui/CGUISetting.h"
22#include "gui/CGUISprite.h"
23#include "gui/CGUIText.h"
28#include "maths/Rect.h"
29#include "ps/CStr.h"
30#include "ps/Input.h"
31
32#include <SDL_keycode.h>
33#include <list>
34#include <vector>
35
36class CCanvas2D;
37class CGUI;
38
39/**
40 * Text field where you can input and edit the text.
41 *
42 * It doesn't use IGUITextOwner, because we don't need
43 * any other features than word-wrapping, and we need to be
44 * able to rapidly change the string.
45 */
46class CInput : public IGUIObject, public IGUIScrollBarOwner
47{
49
50protected: // forwards
51 struct SRow;
52
53public:
54 CInput(CGUI& pGUI);
55
56 /**
57 * @see IGUIObject#ResetStates()
58 */
59 virtual void ResetStates();
60
61 // Check where the mouse is hovering, and get the appropriate text position.
62 // return is the text-position index.
64
65 // Same as above, but only on one row in X, and a given value, not the mouse's.
66 // wanted is filled with x if the row didn't extend as far as the mouse pos.
67 int GetXTextPosition(const std::list<SRow>::const_iterator& c, const float& x, float& wanted) const;
68
69protected:
70
72
73 /**
74 * @see IGUIObject#HandleMessage()
75 */
76 virtual void HandleMessage(SGUIMessage& Message);
77
78 /**
79 * Handle events manually to catch keyboard inputting.
80 */
81 virtual Input::Reaction ManuallyHandleKeys(const SDL_Event& ev);
82
83 /**
84 * Handle events manually to catch keys which change the text.
85 */
86 virtual void ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode);
87
88 /**
89 * Handle events manually to catch keys which don't change the text.
90 */
91 virtual void ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode);
92
93 /**
94 * Handle hotkey events (called by ManuallyHandleKeys)
95 */
96 virtual Input::Reaction ManuallyHandleHotkeyEvent(const SDL_Event& ev);
97
98 /**
99 * @see IGUIObject#HandleSizeChanged()
100 */
101 virtual void HandleSizeChanged();
102
103 /**
104 * Draws the Text
105 */
106 virtual void Draw(CCanvas2D& canvas);
107
108 void DrawContent(CCanvas2D& canvas);
109
110 /**
111 * Calculate m_CharacterPosition
112 * the main task for this function is to perfom word-wrapping
113 * You input from which character it has been changed, because
114 * if we add a character to the very last end, we don't want
115 * process everything all over again! Also notice you can
116 * specify a 'to' also, it will only be used though if a '\n'
117 * appears, because then the word-wrapping won't change after
118 * that.
119 */
120 void UpdateText(int from = 0, int to_before = -1, int to_after = -1);
121
122 /**
123 * Draws the text generated for placeholder.
124 *
125 * @param canvas Canvas to draw on.
126 * @param clipping Clipping rectangle, don't even add a parameter
127 * to get no clipping.
128 */
129 virtual void DrawPlaceholderText(CCanvas2D& canvas, const CRect& clipping = CRect());
130
131 /**
132 * Delete the current selection. Also places the pointer at the
133 * crack between the two segments kept.
134 */
135 void DeleteCurSelection();
136
137 /**
138 * Is text selected? It can be denote two ways, m_iBufferPos_Tail
139 * being -1 or the same as m_iBufferPos. This makes for clearer
140 * code.
141 */
142 bool SelectingText() const;
143
144 /// Get area of where text can be drawn.
145 float GetTextAreaWidth();
146
147 /// Called every time the auto-scrolling should be checked.
148 void UpdateAutoScroll();
149
150 /// Clear composed IME input when supported (SDL2 only).
151 void ClearComposedText();
152
153 /// Updates the buffer (cursor) position exposed to JS.
155protected:
156 /// Cursor position
158 /// Cursor position we started to select from. (-1 if not selecting)
159 /// (NB: Can be larger than m_iBufferPos if selecting from back to front.)
161
162 /// If we're composing text with an IME
164 /// The length and position of the current IME composition
166 /// The position to insert committed text
168
169 // the outer vector is lines, and the inner is X positions
170 // in a row. So that we can determine where characters are
171 // placed. It's important because we need to know where the
172 // pointer should be placed when the input control is pressed.
173 struct SRow
174 {
175 // Where the Row starts
177
178 // List of X values for each character.
179 std::vector<float> m_ListOfX;
180 };
181
182 /**
183 * List of rows to ease changing its size, so iterators stay valid.
184 * For one-liners only one row is used.
185 */
186 std::list<SRow> m_CharacterPositions;
187
188 // *** Things for a multi-lined input control *** //
189
190 /**
191 * When you change row with up/down, and the row you jump to does
192 * not have anything at that X position, then it will keep the
193 * m_WantedX position in mind when switching to the next row.
194 * It will keep on being used until it reach a row which meets the
195 * requirements.
196 * 0.0f means not in use.
197 */
199
200 /**
201 * If we are in the process of selecting a larger selection of text
202 * using the mouse click (hold) and drag, this is true.
203 */
205
206 /**
207 * Whether the cached text is currently valid (if not then SetupText will be called by Draw)
208 */
210
212
213 // *** Things for one-line input control *** //
215
216 /// Used to store the previous time for flashing the cursor.
218
219 /// Cursor blink rate in seconds, if greater than 0.0.
221
222 /// If the cursor should be drawn or not.
224
225 static const CStr EventNameTextEdit;
226 static const CStr EventNamePress;
227 static const CStr EventNameTab;
228
247};
248
249#endif // INCLUDED_CINPUT
#define GUI_OBJECT(obj)
Definition: IGUIObject.h:53
Definition: Canvas2D.h:36
An CGUIText object is a parsed string, divided into text-rendering components.
Definition: CGUIText.h:59
The main object that represents a whole GUI page.
Definition: CGUI.h:80
Text field where you can input and edit the text.
Definition: CInput.h:47
double m_PrevTime
Used to store the previous time for flashing the cursor.
Definition: CInput.h:217
virtual void ManuallyImmutableHandleKeyDownEvent(const SDL_Keycode keyCode)
Handle events manually to catch keys which don't change the text.
Definition: CInput.cpp:374
bool m_CursorVisState
If the cursor should be drawn or not.
Definition: CInput.h:223
CGUISimpleSetting< CStrW > m_Font
Definition: CInput.h:233
bool SelectingText() const
Is text selected? It can be denote two ways, m_iBufferPos_Tail being -1 or the same as m_iBufferPos.
Definition: CInput.cpp:2012
CGUISimpleSetting< CGUIColor > m_TextColor
Definition: CInput.h:244
int m_iBufferPos
Cursor position.
Definition: CInput.h:157
CGUISimpleSetting< bool > m_Readonly
Definition: CInput.h:238
static const CStr EventNamePress
Definition: CInput.h:226
void UpdateBufferPositionSetting()
Updates the buffer (cursor) position exposed to JS.
Definition: CInput.cpp:103
void UpdateAutoScroll()
Called every time the auto-scrolling should be checked.
Definition: CInput.cpp:2026
CGUISimpleSetting< CGUIColor > m_PlaceholderColor
Definition: CInput.h:246
CGUISimpleSetting< float > m_BufferZone
Definition: CInput.h:230
double m_CursorBlinkRate
Cursor blink rate in seconds, if greater than 0.0.
Definition: CInput.h:220
int m_iBufferPos_Tail
Cursor position we started to select from.
Definition: CInput.h:160
float GetTextAreaWidth()
Get area of where text can be drawn.
Definition: CInput.cpp:2018
virtual Input::Reaction ManuallyHandleHotkeyEvent(const SDL_Event &ev)
Handle hotkey events (called by ManuallyHandleKeys)
Definition: CInput.cpp:616
int GetXTextPosition(const std::list< SRow >::const_iterator &c, const float &x, float &wanted) const
Definition: CInput.cpp:1951
CGUIText m_GeneratedPlaceholderText
Definition: CInput.h:211
CGUISimpleSetting< bool > m_Mask
Definition: CInput.h:235
float m_WantedX
When you change row with up/down, and the row you jump to does not have anything at that X position,...
Definition: CInput.h:198
CGUISimpleSetting< CStrW > m_MaskChar
Definition: CInput.h:234
int GetMouseHoveringTextPosition() const
Definition: CInput.cpp:1892
CGUISimpleSetting< CStr > m_ScrollBarStyle
Definition: CInput.h:240
virtual void ResetStates()
Definition: CInput.cpp:882
virtual void DrawPlaceholderText(CCanvas2D &canvas, const CRect &clipping=CRect())
Draws the text generated for placeholder.
Definition: CInput.cpp:1533
CGUISimpleSetting< CGUIColor > m_TextColorSelected
Definition: CInput.h:245
void DrawContent(CCanvas2D &canvas)
Definition: CInput.cpp:1195
CGUISimpleSetting< CGUISpriteInstance > m_SpriteSelectArea
Definition: CInput.h:243
CGUISimpleSetting< bool > m_ScrollBar
Definition: CInput.h:239
int m_iInsertPos
The position to insert committed text.
Definition: CInput.h:167
CInput(CGUI &pGUI)
Definition: CInput.cpp:63
void DeleteCurSelection()
Delete the current selection.
Definition: CInput.cpp:1985
void ClearComposedText()
Clear composed IME input when supported (SDL2 only).
Definition: CInput.cpp:108
CGUISimpleSetting< std::int32_t > m_MaxLength
Definition: CInput.h:236
void UpdateText(int from=0, int to_before=-1, int to_after=-1)
Calculate m_CharacterPosition the main task for this function is to perfom word-wrapping You input fr...
Definition: CInput.cpp:1541
bool m_ComposingText
If we're composing text with an IME.
Definition: CInput.h:163
bool m_SelectingText
If we are in the process of selecting a larger selection of text using the mouse click (hold) and dra...
Definition: CInput.h:204
CGUISimpleSetting< CStrW > m_Caption
Definition: CInput.h:231
bool m_GeneratedPlaceholderTextValid
Whether the cached text is currently valid (if not then SetupText will be called by Draw)
Definition: CInput.h:209
virtual void HandleSizeChanged()
Definition: CInput.cpp:1177
CGUISimpleSetting< CGUISpriteInstance > m_Sprite
Definition: CInput.h:241
std::list< SRow > m_CharacterPositions
List of rows to ease changing its size, so iterators stay valid.
Definition: CInput.h:186
int m_iComposedLength
The length and position of the current IME composition.
Definition: CInput.h:165
virtual void ManuallyMutableHandleKeyDownEvent(const SDL_Keycode keyCode)
Handle events manually to catch keys which change the text.
Definition: CInput.cpp:262
void SetupGeneratedPlaceholderText()
Definition: CInput.cpp:610
static const CStr EventNameTab
Definition: CInput.h:227
CGUISimpleSetting< bool > m_MultiLine
Definition: CInput.h:237
CGUISimpleSetting< CGUIString > m_PlaceholderText
Definition: CInput.h:232
float m_HorizontalScroll
Definition: CInput.h:214
CGUISimpleSetting< CGUISpriteInstance > m_SpriteOverlay
Definition: CInput.h:242
virtual void HandleMessage(SGUIMessage &Message)
Definition: CInput.cpp:888
virtual Input::Reaction ManuallyHandleKeys(const SDL_Event &ev)
Handle events manually to catch keyboard inputting.
Definition: CInput.cpp:117
virtual void Draw(CCanvas2D &canvas)
Draws the Text.
Definition: CInput.cpp:1492
static const CStr EventNameTextEdit
Definition: CInput.h:225
int m_iComposedPos
Definition: CInput.h:165
CGUISimpleSetting< std::int32_t > m_BufferPosition
Definition: CInput.h:229
Rectangle class used for screen rectangles.
Definition: Rect.h:31
GUI object such as a button or an input-box.
Definition: IGUIObject.h:63
Base-class this if you want an object to contain one, or several, scroll-bars.
Definition: IGUIScrollBarOwner.h:38
Reaction
Definition: Input.h:39
Definition: CInput.h:174
int m_ListStart
Definition: CInput.h:176
std::vector< float > m_ListOfX
Definition: CInput.h:179
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: SGUIMessage.h:73