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