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