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