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