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 "loading/resource_manager.h" |
---|
26 | |
---|
27 | #include <cassert> |
---|
28 | |
---|
29 | #include "debug.h" |
---|
30 | |
---|
31 | |
---|
32 | /// TAKE THIS OUT OF HERE. |
---|
33 | #include "graphics_engine.h" |
---|
34 | |
---|
35 | namespace OrxGui |
---|
36 | { |
---|
37 | ObjectListDefinition(GLGuiHandler); |
---|
38 | /** |
---|
39 | * standard constructor |
---|
40 | */ |
---|
41 | GLGuiHandler::GLGuiHandler () |
---|
42 | { |
---|
43 | this->registerObject(this, GLGuiHandler::_objectList); |
---|
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(Resources::ResourceManager::getInstance()->mainGlobalPath().name() + "/" + "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 | ObjectList<GLGuiWidget>::const_iterator it, currentIt; |
---|
120 | currentIt = GLGuiWidget::objectList().end(); |
---|
121 | |
---|
122 | if (GLGuiWidget::selected() != NULL) |
---|
123 | { |
---|
124 | it = std::find(GLGuiWidget::objectList().begin(), GLGuiWidget::objectList().end(), GLGuiWidget::selected()); |
---|
125 | if (it != GLGuiWidget::objectList().end()) |
---|
126 | { |
---|
127 | currentIt = it; |
---|
128 | it++; |
---|
129 | } |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | it = GLGuiWidget::objectList().begin(); |
---|
134 | } |
---|
135 | |
---|
136 | bool cycledOnce = false; |
---|
137 | |
---|
138 | for (; it != currentIt; ++it) |
---|
139 | { |
---|
140 | if (it == GLGuiWidget::objectList().end() && !cycledOnce) |
---|
141 | { |
---|
142 | it = GLGuiWidget::objectList().begin(); |
---|
143 | cycledOnce = true; |
---|
144 | } |
---|
145 | |
---|
146 | if ((*it)->selectable() && (*it)->isVisible()) |
---|
147 | { |
---|
148 | (*it)->select(); |
---|
149 | return; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | void GLGuiHandler::selectPrevious() |
---|
156 | { |
---|
157 | ObjectList<GLGuiWidget>::const_iterator it, currentIt; |
---|
158 | currentIt = GLGuiWidget::objectList().begin(); |
---|
159 | |
---|
160 | if (GLGuiWidget::selected() != NULL) |
---|
161 | { |
---|
162 | it = std::find(GLGuiWidget::objectList().begin(), GLGuiWidget::objectList().end(), GLGuiWidget::selected()); |
---|
163 | if (it != GLGuiWidget::objectList().end()) |
---|
164 | { |
---|
165 | currentIt = it; |
---|
166 | it--; |
---|
167 | } |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | it = GLGuiWidget::objectList().end(); |
---|
172 | } |
---|
173 | |
---|
174 | bool cycledOnce = false; |
---|
175 | |
---|
176 | for (; it != currentIt; --it) |
---|
177 | { |
---|
178 | if (it == GLGuiWidget::objectList().end() && !cycledOnce) |
---|
179 | { |
---|
180 | --it ; |
---|
181 | cycledOnce = true; |
---|
182 | } |
---|
183 | |
---|
184 | if ((*it)->selectable() && (*it)->isVisible()) |
---|
185 | { |
---|
186 | (*it)->select(); |
---|
187 | return; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | void GLGuiHandler::process(const Event &event) |
---|
196 | { |
---|
197 | switch (event.type) |
---|
198 | { |
---|
199 | case EV_MOUSE_MOTION: |
---|
200 | this->checkFocus(); |
---|
201 | break; |
---|
202 | |
---|
203 | case EV_MOUSE_BUTTON_LEFT: |
---|
204 | if (GLGuiWidget::mouseFocused() != NULL && event.bPressed) |
---|
205 | { |
---|
206 | // if clickable select the Widget. |
---|
207 | if (GLGuiWidget::mouseFocused()->clickable()) |
---|
208 | { |
---|
209 | Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
---|
210 | GLGuiWidget::mouseFocused()->select(); |
---|
211 | GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D()); |
---|
212 | } |
---|
213 | } |
---|
214 | else if (GLGuiWidget::selected() != NULL && !event.bPressed) |
---|
215 | { |
---|
216 | if (GLGuiWidget::selected()->clickable()) |
---|
217 | { |
---|
218 | Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
---|
219 | GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D()); |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | break; |
---|
224 | case EV_LEAVE_STATE: |
---|
225 | if (GLGuiWidget::selected() != NULL) |
---|
226 | GLGuiWidget::selected()->unselect(); |
---|
227 | |
---|
228 | if (GLGuiWidget::mouseFocused() != NULL) |
---|
229 | GLGuiWidget::mouseFocused()->breakMouseFocus(); |
---|
230 | break; |
---|
231 | |
---|
232 | case EV_VIDEO_RESIZE: |
---|
233 | if (this->_cursor != NULL) |
---|
234 | this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h)); |
---|
235 | this->_resolution = Vector2D(event.resize.w, event.resize.h); |
---|
236 | break; |
---|
237 | |
---|
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 | |
---|
250 | // Send the Event to the Widget below. |
---|
251 | if (GLGuiWidget::selected() != NULL) |
---|
252 | { |
---|
253 | GLGuiWidget::selected()->processEvent(event); |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | const Vector2D& GLGuiHandler::cursorPositionOverFocusedWidget() const |
---|
262 | { |
---|
263 | return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D::nullVector(); |
---|
264 | } |
---|
265 | |
---|
266 | const Vector2D& GLGuiHandler::cursorPositionAbs() const |
---|
267 | { |
---|
268 | if (this->_cursor) |
---|
269 | return this->_cursor->getAbsCoor2D(); |
---|
270 | else |
---|
271 | return Vector2D::nullVector(); |
---|
272 | } |
---|
273 | |
---|
274 | Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const |
---|
275 | { |
---|
276 | assert (widget != NULL); |
---|
277 | if (this->_cursor) |
---|
278 | return this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D(); |
---|
279 | else |
---|
280 | return Vector2D::nullVector(); |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | void GLGuiHandler::checkFocus() |
---|
285 | { |
---|
286 | // CHECK THE COLLISIONS. |
---|
287 | if (this->_cursor != NULL) |
---|
288 | { |
---|
289 | for (ObjectList<GLGuiWidget>::const_iterator it = GLGuiWidget::objectList().begin(); |
---|
290 | it != GLGuiWidget::objectList().end(); |
---|
291 | it++) |
---|
292 | { |
---|
293 | GLGuiWidget* widget = (*it); |
---|
294 | |
---|
295 | if (widget->isVisible() && |
---|
296 | widget->focusable() && |
---|
297 | widget->focusOverWidget(this->_cursor)) |
---|
298 | { |
---|
299 | // receiving Focus |
---|
300 | if (GLGuiWidget::mouseFocused() != widget) |
---|
301 | { |
---|
302 | widget->giveMouseFocus(); |
---|
303 | } |
---|
304 | return ; |
---|
305 | } |
---|
306 | } |
---|
307 | if (GLGuiWidget::mouseFocused() != NULL) |
---|
308 | GLGuiWidget::mouseFocused()->breakMouseFocus(); |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | void GLGuiHandler::draw() |
---|
313 | { |
---|
314 | // GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP); |
---|
315 | } |
---|
316 | |
---|
317 | |
---|
318 | void GLGuiHandler::tick(float dt) |
---|
319 | { |
---|
320 | // do not change if we already clicked into a Widget. |
---|
321 | // if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed()) |
---|
322 | // return ; |
---|
323 | |
---|
324 | this->checkFocus(); |
---|
325 | } |
---|
326 | } |
---|