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()) |
---|
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()) |
---|
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 | this->selectNext(); |
---|
241 | break; |
---|
242 | } |
---|
243 | |
---|
244 | if (GLGuiWidget::selected() != NULL) |
---|
245 | { |
---|
246 | GLGuiWidget::selected()->processEvent(event); |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const |
---|
255 | { |
---|
256 | return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0); |
---|
257 | } |
---|
258 | |
---|
259 | const Vector2D& GLGuiHandler::cursorPositionAbs() const |
---|
260 | { |
---|
261 | if (this->_cursor) |
---|
262 | return this->_cursor->getAbsCoor2D(); |
---|
263 | else |
---|
264 | return Vector2D::nullVector(); |
---|
265 | } |
---|
266 | |
---|
267 | Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const |
---|
268 | { |
---|
269 | assert (widget != NULL); |
---|
270 | if (this->_cursor) |
---|
271 | return this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D(); |
---|
272 | else |
---|
273 | return Vector2D::nullVector(); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | void GLGuiHandler::checkFocus() |
---|
278 | { |
---|
279 | // CHECK THE COLLISIONS. |
---|
280 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
---|
281 | |
---|
282 | if (objects != NULL && this->_cursor != NULL) |
---|
283 | { |
---|
284 | for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++) |
---|
285 | { |
---|
286 | GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it); |
---|
287 | |
---|
288 | if (widget->isVisible() && |
---|
289 | widget->focusable() && |
---|
290 | widget->focusOverWidget(this->_cursor)) |
---|
291 | { |
---|
292 | // receiving Focus |
---|
293 | if (GLGuiWidget::mouseFocused() != widget) |
---|
294 | { |
---|
295 | widget->giveMouseFocus(); |
---|
296 | } |
---|
297 | return ; |
---|
298 | } |
---|
299 | } |
---|
300 | if (GLGuiWidget::mouseFocused() != NULL) |
---|
301 | GLGuiWidget::mouseFocused()->breakMouseFocus(); |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | void GLGuiHandler::draw() |
---|
306 | { |
---|
307 | // GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP); |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | void GLGuiHandler::tick(float dt) |
---|
312 | { |
---|
313 | // do not change if we already clicked into a Widget. |
---|
314 | // if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed()) |
---|
315 | // return ; |
---|
316 | |
---|
317 | this->checkFocus(); |
---|
318 | } |
---|
319 | } |
---|