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