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