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_handler.h" |
---|
19 | #include "event_handler.h" |
---|
20 | #include "key_names.h" |
---|
21 | |
---|
22 | #include "glgui_mainwidget.h" |
---|
23 | #include "glgui_cursor.h" |
---|
24 | |
---|
25 | #include "class_list.h" |
---|
26 | #include <cassert> |
---|
27 | |
---|
28 | #include "debug.h" |
---|
29 | |
---|
30 | #include <cassert> |
---|
31 | |
---|
32 | |
---|
33 | /// TAKE THIS OUT OF HERE. |
---|
34 | #include "graphics_engine.h" |
---|
35 | |
---|
36 | namespace OrxGui |
---|
37 | { |
---|
38 | |
---|
39 | /** |
---|
40 | * standard constructor |
---|
41 | */ |
---|
42 | GLGuiHandler::GLGuiHandler () |
---|
43 | { |
---|
44 | this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler"); |
---|
45 | this->setName("GLGuiHandler"); |
---|
46 | |
---|
47 | |
---|
48 | EventHandler::getInstance()->withUNICODE(ES_MENU, true ); |
---|
49 | |
---|
50 | this->_cursor = NULL; |
---|
51 | for (unsigned int i = 0; i < EV_NUMBER; i++) |
---|
52 | { |
---|
53 | this->subscribeEvent(ES_ALL, i); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * the singleton reference to this class |
---|
59 | */ |
---|
60 | GLGuiHandler* GLGuiHandler::singletonRef = NULL; |
---|
61 | |
---|
62 | /** |
---|
63 | @brief standard deconstructor |
---|
64 | */ |
---|
65 | GLGuiHandler::~GLGuiHandler () |
---|
66 | { |
---|
67 | GLGuiHandler::singletonRef = NULL; |
---|
68 | } |
---|
69 | |
---|
70 | void GLGuiHandler::activateCursor() |
---|
71 | { |
---|
72 | if (this->_cursor == NULL) |
---|
73 | this->_cursor = new GLGuiCursor(); |
---|
74 | this->_cursor->show(); |
---|
75 | this->_cursor->setMaxBorders(Vector2D(GraphicsEngine::getInstance()->getResolutionX(), GraphicsEngine::getInstance()->getResolutionY())); |
---|
76 | } |
---|
77 | |
---|
78 | void GLGuiHandler::deactivateCursor(bool deleteCursor) |
---|
79 | { |
---|
80 | if (this->_cursor) |
---|
81 | { |
---|
82 | if (deleteCursor) |
---|
83 | delete this->_cursor; |
---|
84 | this->_cursor = NULL; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void GLGuiHandler::activate() |
---|
90 | { |
---|
91 | //EventHandler::getInstance()->pushState(ES_MENU); |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | void GLGuiHandler::deactivate() |
---|
98 | { |
---|
99 | //EventHandler::getInstance()->popState(); |
---|
100 | |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | void GLGuiHandler::selectNext() |
---|
105 | { |
---|
106 | // retrieve Objects. |
---|
107 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
---|
108 | |
---|
109 | if (objects) |
---|
110 | { |
---|
111 | std::list<BaseObject*>::const_iterator it ; |
---|
112 | std::list<BaseObject*>::const_iterator currentIt = objects->end(); |
---|
113 | |
---|
114 | if (GLGuiWidget::selected() != NULL) |
---|
115 | { |
---|
116 | it = std::find(objects->begin(), objects->end(), GLGuiWidget::selected()); |
---|
117 | if (it != objects->end()) |
---|
118 | { |
---|
119 | currentIt = it; |
---|
120 | it++; |
---|
121 | } |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | it = objects->begin(); |
---|
126 | } |
---|
127 | |
---|
128 | bool cycledOnce = false; |
---|
129 | |
---|
130 | for (; it != currentIt; ++it) |
---|
131 | { |
---|
132 | if (it == objects->end() && !cycledOnce) |
---|
133 | { |
---|
134 | it = objects->begin(); |
---|
135 | cycledOnce = true; |
---|
136 | } |
---|
137 | |
---|
138 | if (dynamic_cast<GLGuiWidget*>(*it)->selectable() && dynamic_cast<GLGuiWidget*>(*it)->isVisible()) |
---|
139 | { |
---|
140 | dynamic_cast<GLGuiWidget*>(*it)->select(); |
---|
141 | return; |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | PRINTF(0)("NO GUI-ELEMENTS EXISTING\n"); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | void GLGuiHandler::selectPrevious() |
---|
153 | { |
---|
154 | // retrieve Objects. |
---|
155 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
---|
156 | |
---|
157 | if (objects) |
---|
158 | { |
---|
159 | std::list<BaseObject*>::const_iterator it ; |
---|
160 | std::list<BaseObject*>::const_iterator currentIt = objects->begin(); |
---|
161 | |
---|
162 | if (GLGuiWidget::selected() != NULL) |
---|
163 | { |
---|
164 | it = std::find(objects->begin(), objects->end(), GLGuiWidget::selected()); |
---|
165 | if (it != objects->end()) |
---|
166 | { |
---|
167 | currentIt = it; |
---|
168 | it--; |
---|
169 | } |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | it = objects->end(); |
---|
174 | } |
---|
175 | |
---|
176 | bool cycledOnce = false; |
---|
177 | |
---|
178 | for (; it != currentIt; --it) |
---|
179 | { |
---|
180 | if (it == objects->end() && !cycledOnce) |
---|
181 | { |
---|
182 | --it ; |
---|
183 | cycledOnce = true; |
---|
184 | } |
---|
185 | |
---|
186 | if (dynamic_cast<GLGuiWidget*>(*it)->selectable() && dynamic_cast<GLGuiWidget*>(*it)->isVisible()) |
---|
187 | { |
---|
188 | dynamic_cast<GLGuiWidget*>(*it)->select(); |
---|
189 | return; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | } |
---|
194 | else |
---|
195 | { |
---|
196 | PRINTF(0)("NO GUI-ELEMENTS EXISTING\n"); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | void GLGuiHandler::process(const Event &event) |
---|
203 | { |
---|
204 | switch (event.type) |
---|
205 | { |
---|
206 | case EV_MOUSE_MOTION: |
---|
207 | this->checkFocus(); |
---|
208 | break; |
---|
209 | |
---|
210 | case EV_MOUSE_BUTTON_LEFT: |
---|
211 | if (GLGuiWidget::mouseFocused() != NULL && event.bPressed) |
---|
212 | { |
---|
213 | // if clickable select the Widget. |
---|
214 | if (GLGuiWidget::mouseFocused()->clickable()) |
---|
215 | { |
---|
216 | Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
---|
217 | GLGuiWidget::mouseFocused()->select(); |
---|
218 | GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D()); |
---|
219 | } |
---|
220 | } |
---|
221 | else if (GLGuiWidget::selected() != NULL && !event.bPressed) |
---|
222 | { |
---|
223 | if (GLGuiWidget::selected()->clickable()) |
---|
224 | { |
---|
225 | Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
---|
226 | GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D()); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | break; |
---|
231 | case EV_LEAVE_STATE: |
---|
232 | if (GLGuiWidget::selected() != NULL) |
---|
233 | GLGuiWidget::selected()->unselect(); |
---|
234 | |
---|
235 | if (GLGuiWidget::mouseFocused() != NULL) |
---|
236 | GLGuiWidget::mouseFocused()->breakMouseFocus(); |
---|
237 | break; |
---|
238 | |
---|
239 | case EV_VIDEO_RESIZE: |
---|
240 | if (this->_cursor != NULL) |
---|
241 | this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h)); |
---|
242 | break; |
---|
243 | case SDLK_TAB: |
---|
244 | if (event.bPressed) |
---|
245 | { |
---|
246 | if (EventHandler::getInstance()->isPressed(SDLK_LSHIFT) || EventHandler::getInstance()->isPressed(SDLK_RSHIFT)) |
---|
247 | this->selectPrevious(); |
---|
248 | else |
---|
249 | this->selectNext(); |
---|
250 | } |
---|
251 | break; |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | // Send the Event to the Widget below. |
---|
256 | if (GLGuiWidget::selected() != NULL) |
---|
257 | { |
---|
258 | GLGuiWidget::selected()->processEvent(event); |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const |
---|
267 | { |
---|
268 | return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0); |
---|
269 | } |
---|
270 | |
---|
271 | const Vector2D& GLGuiHandler::cursorPositionAbs() const |
---|
272 | { |
---|
273 | if (this->_cursor) |
---|
274 | return this->_cursor->getAbsCoor2D(); |
---|
275 | else |
---|
276 | return Vector2D::nullVector(); |
---|
277 | } |
---|
278 | |
---|
279 | Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const |
---|
280 | { |
---|
281 | assert (widget != NULL); |
---|
282 | if (this->_cursor) |
---|
283 | return this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D(); |
---|
284 | else |
---|
285 | return Vector2D::nullVector(); |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | void GLGuiHandler::checkFocus() |
---|
290 | { |
---|
291 | // CHECK THE COLLISIONS. |
---|
292 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
---|
293 | |
---|
294 | if (objects != NULL && this->_cursor != NULL) |
---|
295 | { |
---|
296 | for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++) |
---|
297 | { |
---|
298 | GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it); |
---|
299 | |
---|
300 | if (widget->isVisible() && |
---|
301 | widget->focusable() && |
---|
302 | widget->focusOverWidget(this->_cursor)) |
---|
303 | { |
---|
304 | // receiving Focus |
---|
305 | if (GLGuiWidget::mouseFocused() != widget) |
---|
306 | { |
---|
307 | widget->giveMouseFocus(); |
---|
308 | } |
---|
309 | return ; |
---|
310 | } |
---|
311 | } |
---|
312 | if (GLGuiWidget::mouseFocused() != NULL) |
---|
313 | GLGuiWidget::mouseFocused()->breakMouseFocus(); |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | void GLGuiHandler::draw() |
---|
318 | { |
---|
319 | // GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP); |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | void GLGuiHandler::tick(float dt) |
---|
324 | { |
---|
325 | // do not change if we already clicked into a Widget. |
---|
326 | // if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed()) |
---|
327 | // return ; |
---|
328 | |
---|
329 | this->checkFocus(); |
---|
330 | } |
---|
331 | } |
---|