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