Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CInput.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 /*
19 GUI Object - Input [box]
20 
21 --Overview--
22 
23  GUI Object representing a text field you can edit.
24 
25 --More info--
26 
27  Check GUI.h
28 
29 */
30 
31 #ifndef INCLUDED_CINPUT
32 #define INCLUDED_CINPUT
33 
34 //--------------------------------------------------------
35 // Includes / Compiler directives
36 //--------------------------------------------------------
37 #include "GUI.h"
38 
39 // TODO Gee: Remove
40 class IGUIScrollBar;
41 
42 //--------------------------------------------------------
43 // Macros
44 //--------------------------------------------------------
45 
46 //--------------------------------------------------------
47 // Types
48 //--------------------------------------------------------
49 
50 //--------------------------------------------------------
51 // Declarations
52 //--------------------------------------------------------
53 
54 /**
55  * Text field where you can input and edit the text.
56  *
57  * It doesn't use IGUITextOwner, because we don't need
58  * any other features than word-wrapping, and we need to be
59  * able to rapidly change the string.
60  *
61  * @see IGUIObject
62  */
63 class CInput : public IGUIScrollBarOwner
64 {
66 
67 protected: // forwards
68  struct SRow;
69 
70 public:
71  CInput();
72  virtual ~CInput();
73 
74  /**
75  * @see IGUIObject#ResetStates()
76  */
78 
79  // Check where the mouse is hovering, and get the appropriate text position.
80  // return is the text-position index.
81  // const in philosophy, but I need to retrieve the caption in a non-const way.
83 
84  // Same as above, but only on one row in X, and a given value, not the mouse's
85  // wanted is filled with x if the row didn't extend as far as we
86  int GetXTextPosition(const std::list<SRow>::iterator &c, const float &x, float &wanted);
87 
88 protected:
89  /**
90  * @see IGUIObject#HandleMessage()
91  */
92  virtual void HandleMessage(SGUIMessage &Message);
93 
94  /**
95  * Handle events manually to catch keyboard inputting.
96  */
97  virtual InReaction ManuallyHandleEvent(const SDL_Event_* ev);
98 
99  /**
100  * Handle hotkey events (called by ManuallyHandleEvent)
101  */
103 
104  /**
105  * @see IGUIObject#UpdateCachedSize()
106  */
107  virtual void UpdateCachedSize();
108 
109  /**
110  * Draws the Text
111  */
112  virtual void Draw();
113 
114  // Calculate m_CharacterPosition
115  // the main task for this function is to perfom word-wrapping
116  // You input from which character it has been changed, because
117  // if we add a character to the very last end, we don't want
118  // process everything all over again! Also notice you can
119  // specify a 'to' also, it will only be used though if a '\n'
120  // appears, because then the word-wrapping won't change after
121  // that.
122  void UpdateText(int from=0, int to_before=-1, int to_after=-1);
123 
124  // Delete the current selection. Also places the pointer at the
125  // crack between the two segments kept.
126  void DeleteCurSelection();
127 
128  // Is text selected? It can be denote two ways, m_iBufferPos_Tail
129  // being -1 or the same as m_iBufferPos. This makes for clearer
130  // code.
131  bool SelectingText() const;
132 
133  // Get area of where text can be drawn.
134  float GetTextAreaWidth();
135 
136  // Called every time the auto-scrolling should be checked.
137  void UpdateAutoScroll();
138 
139 protected:
140  // Cursor position
141  // (the second one is for selection of larger areas, -1 if not used)
142  // A note on 'Tail', it was first called 'To', and the natural order
143  // of X and X_To was X then X_To. Now Tail is called so, because it
144  // can be placed both before and after, but the important things is
145  // that m_iBufferPos is ALWAYS where the edit pointer is. Yes, there
146  // is an edit pointer even though you select a larger area. For instance
147  // if you want to resize the selection with Shift+Left/Right, there
148  // are always two ways a selection can look. Check any OS GUI and you'll
149  // see.
152 
153  // the outer vector is lines, and the inner is X positions
154  // in a row. So that we can determine where characters are
155  // placed. It's important because we need to know where the
156  // pointer should be placed when the input control is pressed.
157  struct SRow
158  {
159  int m_ListStart; // Where does the Row starts
160  std::vector<float> m_ListOfX; // List of X values for each character.
161  };
162 
163  // List of rows, list because I use a lot of iterators, and change
164  // its size continuously, it's easier and safer if I know the
165  // iterators never gets invalidated.
166  // For one-liners, only one row is used.
167  std::list< SRow > m_CharacterPositions;
168 
169  // *** Things for a multi-lined input control *** //
170 
171  // This is when you change row with up/down, and the row you jump
172  // to doesn't have anything at that X position, then it will
173  // keep the WantedX position in mind when switching to the next
174  // row. It will keep on being used until it reach a row which meets
175  // the requirements.
176  // 0.0f means not in use.
177  float m_WantedX;
178 
179  // If we are in the process of selecting a larger selection of text
180  // using the mouse click (hold) and drag, this is true.
182 
183  // *** Things for one-line input control *** //
185 
186  // Used to store the previous time for flashing the cursor.
187  double m_PrevTime;
188 
189  // Cursor blink rate in seconds, if greater than 0.0.
191 
192  // If the cursor should be drawn or not.
194 };
195 
196 #endif
double m_CursorBlinkRate
Definition: CInput.h:190
float m_WantedX
Definition: CInput.h:177
CInput()
Definition: CInput.cpp:46
float m_HorizontalScroll
Definition: CInput.h:184
bool SelectingText() const
Definition: CInput.cpp:1891
virtual void Draw()
Draws the Text.
Definition: CInput.cpp:997
virtual InReaction ManuallyHandleEvent(const SDL_Event_ *ev)
Handle events manually to catch keyboard inputting.
Definition: CInput.cpp:78
void DeleteCurSelection()
Definition: CInput.cpp:1863
bool m_CursorVisState
Definition: CInput.h:193
float GetTextAreaWidth()
Definition: CInput.cpp:1897
Text field where you can input and edit the text.
Definition: CInput.h:63
double m_PrevTime
Definition: CInput.h:187
virtual ~CInput()
Definition: CInput.cpp:74
InReaction
Definition: input.h:34
int GetXTextPosition(const std::list< SRow >::iterator &c, const float &x, float &wanted)
Definition: CInput.cpp:1830
virtual void ResetStates()
Definition: CInput.h:77
int m_ListStart
Definition: CInput.h:159
void UpdateAutoScroll()
Definition: CInput.cpp:1910
void UpdateText(int from=0, int to_before=-1, int to_after=-1)
Definition: CInput.cpp:1378
virtual void ResetStates()
int m_iBufferPos_Tail
Definition: CInput.h:150
std::list< SRow > m_CharacterPositions
Definition: CInput.h:167
virtual InReaction ManuallyHandleHotkeyEvent(const SDL_Event_ *ev)
Handle hotkey events (called by ManuallyHandleEvent)
Definition: CInput.cpp:448
bool m_SelectingText
Definition: CInput.h:181
The GUI Scroll-bar, used everywhere there is a scroll-bar in the game.
std::vector< float > m_ListOfX
Definition: CInput.h:160
virtual void HandleMessage(SGUIMessage &Message)
Definition: CInput.cpp:697
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: GUIbase.h:106
Base-class this if you want an object to contain one, or several, scroll-bars.
#define GUI_OBJECT(obj)
Definition: GUIbase.h:62
int m_iBufferPos
Definition: CInput.h:150
virtual void UpdateCachedSize()
Definition: CInput.cpp:979
int GetMouseHoveringTextPosition()
Definition: CInput.cpp:1749