1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI |
---|
17 | |
---|
18 | #include "glgui_inputline.h" |
---|
19 | |
---|
20 | namespace OrxGui |
---|
21 | { |
---|
22 | ObjectListDefinition(GLGuiInputLine); |
---|
23 | /** |
---|
24 | * @brief standard constructor |
---|
25 | */ |
---|
26 | GLGuiInputLine::GLGuiInputLine () |
---|
27 | { |
---|
28 | this->init(); |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * @brief standard deconstructor |
---|
34 | */ |
---|
35 | GLGuiInputLine::~GLGuiInputLine() |
---|
36 | {} |
---|
37 | |
---|
38 | float GLGuiInputLine::repeatDelay = 0.3f; |
---|
39 | float GLGuiInputLine::repeatRate = 0.01f; |
---|
40 | |
---|
41 | |
---|
42 | /** |
---|
43 | * @brief initializes the GUI-element |
---|
44 | */ |
---|
45 | void GLGuiInputLine::init() |
---|
46 | { |
---|
47 | this->registerObject(this, GLGuiInputLine::_objectList); |
---|
48 | |
---|
49 | this->setFocusable(true); |
---|
50 | this->setClickable(true); |
---|
51 | this->setSelectable(true); |
---|
52 | |
---|
53 | this->_clearOnEnter = false; |
---|
54 | this->_text.setParent2D(this); |
---|
55 | this->_text.setRelCoor2D(4,4); |
---|
56 | this->_text.setFont("fonts/final_frontier.ttf", 20); |
---|
57 | this->_text.setLineWidth(400); |
---|
58 | this->_text.setDotsPosition(LimitedWidthText::Begin); |
---|
59 | this->_text.setColor(foregroundColor()); |
---|
60 | this->_text.setVisibility(false); |
---|
61 | this->resize(); |
---|
62 | |
---|
63 | this->delayNext = 0.0; |
---|
64 | this->pressedKey = SDLK_FIRST; |
---|
65 | this->pressedKeyName = SDLK_FIRST; |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief sets the Text of the InputLine |
---|
72 | * @param text The new Text. |
---|
73 | */ |
---|
74 | void GLGuiInputLine::setText(const std::string& text) |
---|
75 | { |
---|
76 | this->_text.setText(text); |
---|
77 | this->changedText(); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * @brief appends text to the InputLine |
---|
82 | * @param appendText the Text to append |
---|
83 | */ |
---|
84 | void GLGuiInputLine::append(const std::string& appendText) |
---|
85 | { |
---|
86 | this->_text.append(appendText); |
---|
87 | this->changedText(); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | /** |
---|
92 | * @brief appends a Character to the InputLine |
---|
93 | * @param character the Character to append. |
---|
94 | */ |
---|
95 | void GLGuiInputLine::appendCharacter(char character) |
---|
96 | { |
---|
97 | this->_text.appendCharacter(character); |
---|
98 | this->changedText(); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | /** |
---|
103 | * @brief Removes Characters from the InputLine |
---|
104 | * @param chars The count of characters to remove |
---|
105 | */ |
---|
106 | void GLGuiInputLine::removeCharacters(unsigned int chars) |
---|
107 | { |
---|
108 | this->_text.removeCharacters(chars); |
---|
109 | this->changedText(); |
---|
110 | } |
---|
111 | |
---|
112 | void GLGuiInputLine::clear() |
---|
113 | { |
---|
114 | this->_text.clear(); |
---|
115 | this->changedText(); |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * @brief If the Text has been changed this function is called. |
---|
120 | * |
---|
121 | * This Function also emits the Signal textChanged. |
---|
122 | */ |
---|
123 | void GLGuiInputLine::changedText() |
---|
124 | { |
---|
125 | this->resize(); |
---|
126 | this->setFrontColor(Color(1,1,1,1), true); |
---|
127 | this->textChanged.emit(this->_text.text()); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | /** |
---|
132 | * removes the focus from this Widget. |
---|
133 | */ |
---|
134 | void GLGuiInputLine::removedFocus() |
---|
135 | { |
---|
136 | GLGuiWidget::removedFocus(); |
---|
137 | this->pressedKey = 0; |
---|
138 | this->pressedKeyName = 0; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * @brief handles the EnterPush-Event. |
---|
143 | * |
---|
144 | * Emmits th EnterPushed Signal with the current Line, |
---|
145 | * and if _clearOnEnter is pushed clears the Line. |
---|
146 | */ |
---|
147 | void GLGuiInputLine::pushEnter() |
---|
148 | { |
---|
149 | this->enterPushed.emit(this->_text.text()); |
---|
150 | if (this->_clearOnEnter) |
---|
151 | this->clear(); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | /** |
---|
156 | * @brief Processes an Event. |
---|
157 | * @param event The event to be processed |
---|
158 | * @return true if the event was catched. |
---|
159 | */ |
---|
160 | bool GLGuiInputLine::processEvent(const Event& event) |
---|
161 | { |
---|
162 | if (event.bPressed) |
---|
163 | { |
---|
164 | if (event.type == SDLK_BACKSPACE) |
---|
165 | { |
---|
166 | this->delayNext = GLGuiInputLine::repeatDelay; |
---|
167 | this->pressedKey = SDLK_BACKSPACE; |
---|
168 | this->pressedKeyName = SDLK_BACKSPACE; |
---|
169 | this->removeCharacters(1); |
---|
170 | return true; |
---|
171 | } |
---|
172 | else if(event.type == SDLK_RETURN || event.type == SDLK_KP_ENTER) |
---|
173 | { |
---|
174 | this->pushEnter(); |
---|
175 | return true; |
---|
176 | } |
---|
177 | // any other keyboard key |
---|
178 | else if (likely(event.type < 127)) |
---|
179 | { |
---|
180 | this->delayNext = GLGuiInputLine::repeatDelay; |
---|
181 | |
---|
182 | this->appendCharacter(event.x); |
---|
183 | this->pressedKey = event.type; |
---|
184 | this->pressedKeyName = event.x; |
---|
185 | return true; |
---|
186 | } |
---|
187 | } |
---|
188 | else // if(!event.bPressed) |
---|
189 | { |
---|
190 | if (this->pressedKey == event.type) |
---|
191 | { |
---|
192 | this->pressedKeyName = 0; |
---|
193 | this->pressedKey = 0; |
---|
194 | this->delayNext = 0.0; |
---|
195 | return true; |
---|
196 | } |
---|
197 | } |
---|
198 | return false; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | /** |
---|
203 | * @brief Resizes the Widget to the new Size-constraints. |
---|
204 | */ |
---|
205 | void GLGuiInputLine::resize() |
---|
206 | { |
---|
207 | this->_text.setRelCoor2D(borderLeft(), borderTop()); |
---|
208 | this->setSize2D( this->_text.getSize2D() + Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom())); |
---|
209 | GLGuiWidget::resize(); |
---|
210 | /* this->frontRect().setTopLeft(borderLeft(), borderTop()); |
---|
211 | this->frontRect().setSize(this->getSize2D() - Vector2D(borderLeft() + borderRight(), borderTop() + borderBottom()));*/ |
---|
212 | } |
---|
213 | |
---|
214 | void GLGuiInputLine::updateFrontColor() |
---|
215 | { |
---|
216 | this->_text.setColor(foregroundColor()); |
---|
217 | } |
---|
218 | |
---|
219 | void GLGuiInputLine::hiding() |
---|
220 | { |
---|
221 | this->_text.setVisibility(false); |
---|
222 | } |
---|
223 | |
---|
224 | void GLGuiInputLine::showing() |
---|
225 | { |
---|
226 | this->_text.setVisibility(true); |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * @brief ticks the InputLine |
---|
231 | * @param dt the time passed. |
---|
232 | */ |
---|
233 | void GLGuiInputLine::tick(float dt) |
---|
234 | { |
---|
235 | GLGuiWidget::tick(dt); |
---|
236 | if (this->delayNext > 0.0) |
---|
237 | this->delayNext -= dt; |
---|
238 | else if (this->pressedKey != SDLK_FIRST ) |
---|
239 | { |
---|
240 | this->delayNext = GLGuiInputLine::repeatRate; |
---|
241 | switch (this->pressedKey) |
---|
242 | { |
---|
243 | case SDLK_BACKSPACE: |
---|
244 | this->removeCharacters(1); |
---|
245 | break; |
---|
246 | default: |
---|
247 | { |
---|
248 | if (likely(this->pressedKey < 127)) |
---|
249 | this->appendCharacter(this->pressedKeyName); |
---|
250 | } |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | * @brief draws the GLGuiInputLine |
---|
259 | */ |
---|
260 | void GLGuiInputLine::draw() const |
---|
261 | { |
---|
262 | this->beginDraw(); |
---|
263 | GLGuiWidget::draw(); |
---|
264 | |
---|
265 | // this->frontMaterial().select(); |
---|
266 | // GLGuiWidget::drawRect(this->frontRect()); |
---|
267 | |
---|
268 | this->endDraw(); |
---|
269 | } |
---|
270 | } |
---|