Pyrogenesis  trunk
CInput.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 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.h"
22 
23 /**
24  * Text field where you can input and edit the text.
25  *
26  * It doesn't use IGUITextOwner, because we don't need
27  * any other features than word-wrapping, and we need to be
28  * able to rapidly change the string.
29  *
30  * @see IGUIObject
31  */
32 class CInput : public IGUIScrollBarOwner
33 {
35 
36 protected: // forwards
37  struct SRow;
38 
39 public:
40  CInput();
41  virtual ~CInput();
42 
43  /**
44  * @see IGUIObject#ResetStates()
45  */
47 
48  // Check where the mouse is hovering, and get the appropriate text position.
49  // return is the text-position index.
50  int GetMouseHoveringTextPosition() const;
51 
52  // Same as above, but only on one row in X, and a given value, not the mouse's.
53  // wanted is filled with x if the row didn't extend as far as the mouse pos.
54  int GetXTextPosition(const std::list<SRow>::const_iterator& c, const float& x, float& wanted) const;
55 
56 protected:
57  /**
58  * @see IGUIObject#HandleMessage()
59  */
60  virtual void HandleMessage(SGUIMessage& Message);
61 
62  /**
63  * Handle events manually to catch keyboard inputting.
64  */
65  virtual InReaction ManuallyHandleEvent(const SDL_Event_* ev);
66 
67  /**
68  * Handle hotkey events (called by ManuallyHandleEvent)
69  */
71 
72  /**
73  * @see IGUIObject#UpdateCachedSize()
74  */
75  virtual void UpdateCachedSize();
76 
77  /**
78  * Draws the Text
79  */
80  virtual void Draw();
81 
82  /**
83  * Calculate m_CharacterPosition
84  * the main task for this function is to perfom word-wrapping
85  * You input from which character it has been changed, because
86  * if we add a character to the very last end, we don't want
87  * process everything all over again! Also notice you can
88  * specify a 'to' also, it will only be used though if a '\n'
89  * appears, because then the word-wrapping won't change after
90  * that.
91  */
92  void UpdateText(int from = 0, int to_before = -1, int to_after = -1);
93 
94  /**
95  * Delete the current selection. Also places the pointer at the
96  * crack between the two segments kept.
97  */
98  void DeleteCurSelection();
99 
100  /**
101  * Is text selected? It can be denote two ways, m_iBufferPos_Tail
102  * being -1 or the same as m_iBufferPos. This makes for clearer
103  * code.
104  */
105  bool SelectingText() const;
106 
107  /// Get area of where text can be drawn.
108  float GetTextAreaWidth();
109 
110  /// Called every time the auto-scrolling should be checked.
111  void UpdateAutoScroll();
112 
113  /// Clear composed IME input when supported (SDL2 only).
114  void ClearComposedText();
115 
116  /// Updates the buffer (cursor) position exposed to JS.
118 protected:
119  /// Cursor position
121  /// Cursor position we started to select from. (-1 if not selecting)
122  /// (NB: Can be larger than m_iBufferPos if selecting from back to front.)
124 
125  /// If we're composing text with an IME
127  /// The length and position of the current IME composition
129  /// The position to insert committed text
131 
132  // the outer vector is lines, and the inner is X positions
133  // in a row. So that we can determine where characters are
134  // placed. It's important because we need to know where the
135  // pointer should be placed when the input control is pressed.
136  struct SRow
137  {
138  int m_ListStart; /// Where does the Row starts
139  std::vector<float> m_ListOfX; /// List of X values for each character.
140  };
141 
142  /**
143  * List of rows to ease changing its size, so iterators stay valid.
144  * For one-liners only one row is used.
145  */
146  std::list<SRow> m_CharacterPositions;
147 
148  // *** Things for a multi-lined input control *** //
149 
150  /**
151  * When you change row with up/down, and the row you jump to does
152  * not have anything at that X position, then it will keep the
153  * m_WantedX position in mind when switching to the next row.
154  * It will keep on being used until it reach a row which meets the
155  * requirements.
156  * 0.0f means not in use.
157  */
158  float m_WantedX;
159 
160  /**
161  * If we are in the process of selecting a larger selection of text
162  * using the mouse click (hold) and drag, this is true.
163  */
165 
166  // *** Things for one-line input control *** //
168 
169  /// Used to store the previous time for flashing the cursor.
170  double m_PrevTime;
171 
172  /// Cursor blink rate in seconds, if greater than 0.0.
174 
175  /// If the cursor should be drawn or not.
177 };
178 
179 #endif // INCLUDED_CINPUT
double m_CursorBlinkRate
Cursor blink rate in seconds, if greater than 0.0.
Definition: CInput.h:173
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:158
CInput()
Definition: CInput.cpp:44
float m_HorizontalScroll
Definition: CInput.h:167
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:1992
virtual void Draw()
Draws the Text.
Definition: CInput.cpp:1093
virtual InReaction ManuallyHandleEvent(const SDL_Event_ *ev)
Handle events manually to catch keyboard inputting.
Definition: CInput.cpp:95
int m_iInsertPos
The position to insert committed text.
Definition: CInput.h:130
Definition: CInput.h:136
void DeleteCurSelection()
Delete the current selection.
Definition: CInput.cpp:1963
bool m_CursorVisState
If the cursor should be drawn or not.
Definition: CInput.h:176
float GetTextAreaWidth()
Get area of where text can be drawn.
Definition: CInput.cpp:1998
int m_iComposedPos
Definition: CInput.h:128
Text field where you can input and edit the text.
Definition: CInput.h:32
Definition: libsdl.h:51
int GetXTextPosition(const std::list< SRow >::const_iterator &c, const float &x, float &wanted) const
Definition: CInput.cpp:1929
int GetMouseHoveringTextPosition() const
Definition: CInput.cpp:1857
std::list< SRow > m_CharacterPositions
List of rows to ease changing its size, so iterators stay valid.
Definition: CInput.h:146
double m_PrevTime
Used to store the previous time for flashing the cursor.
Definition: CInput.h:170
virtual ~CInput()
Definition: CInput.cpp:75
InReaction
Definition: input.h:34
int m_iComposedLength
The length and position of the current IME composition.
Definition: CInput.h:128
virtual void ResetStates()
Definition: CInput.h:46
int m_ListStart
Definition: CInput.h:138
void UpdateAutoScroll()
Called every time the auto-scrolling should be checked.
Definition: CInput.cpp:2011
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:1484
void ClearComposedText()
Clear composed IME input when supported (SDL2 only).
Definition: CInput.cpp:85
virtual void ResetStates()
Definition: IGUIScrollBarOwner.cpp:32
int m_iBufferPos_Tail
Cursor position we started to select from.
Definition: CInput.h:123
virtual InReaction ManuallyHandleHotkeyEvent(const SDL_Event_ *ev)
Handle hotkey events (called by ManuallyHandleEvent)
Definition: CInput.cpp:531
void UpdateBufferPositionSetting()
Updates the buffer (cursor) position exposed to JS.
Definition: CInput.cpp:79
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:164
std::vector< float > m_ListOfX
Where does the Row starts.
Definition: CInput.h:139
virtual void HandleMessage(SGUIMessage &Message)
Definition: CInput.cpp:787
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: GUIbase.h:91
bool m_ComposingText
If we&#39;re composing text with an IME.
Definition: CInput.h:126
Base-class this if you want an object to contain one, or several, scroll-bars.
Definition: IGUIScrollBarOwner.h:33
#define GUI_OBJECT(obj)
Definition: GUIbase.h:48
int m_iBufferPos
Cursor position.
Definition: CInput.h:120
virtual void UpdateCachedSize()
Definition: CInput.cpp:1075