| [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 |  | 
|---|
| [9869] | 25 | #include "loading/resource_manager.h" | 
|---|
 | 26 |  | 
|---|
| [8711] | 27 | #include <cassert> | 
|---|
| [7919] | 28 |  | 
|---|
| [8717] | 29 | #include "debug.h" | 
|---|
 | 30 |  | 
|---|
| [7919] | 31 |  | 
|---|
 | 32 | /// TAKE THIS OUT OF HERE. | 
|---|
 | 33 | #include "graphics_engine.h" | 
|---|
 | 34 |  | 
|---|
| [7779] | 35 | namespace OrxGui | 
|---|
| [3655] | 36 | { | 
|---|
| [9869] | 37 |   ObjectListDefinition(GLGuiHandler); | 
|---|
| [7779] | 38 |   /** | 
|---|
 | 39 |    * standard constructor | 
|---|
 | 40 |    */ | 
|---|
 | 41 |   GLGuiHandler::GLGuiHandler () | 
|---|
 | 42 |   { | 
|---|
| [9869] | 43 |     this->registerObject(this, GLGuiHandler::_objectList); | 
|---|
| [7779] | 44 |     this->setName("GLGuiHandler"); | 
|---|
| [3655] | 45 |  | 
|---|
| [9656] | 46 |     this->_resolution = Vector2D(GraphicsEngine::getInstance()->getResolutionX(), GraphicsEngine::getInstance()->getResolutionY()); | 
|---|
| [7868] | 47 |  | 
|---|
| [8448] | 48 |     EventHandler::getInstance()->withUNICODE(ES_MENU, true ); | 
|---|
| [7919] | 49 |  | 
|---|
| [8324] | 50 |     this->_cursor = NULL; | 
|---|
| [7919] | 51 |     for (unsigned int i = 0; i < EV_NUMBER; i++) | 
|---|
 | 52 |     { | 
|---|
| [9656] | 53 |       this->subscribeEvent(ES_GAME, i); | 
|---|
 | 54 |       this->subscribeEvent(ES_GAME_MENU, i); | 
|---|
 | 55 |       this->subscribeEvent(ES_MENU, i); | 
|---|
| [7919] | 56 |     } | 
|---|
| [7779] | 57 |   } | 
|---|
| [3655] | 58 |  | 
|---|
| [7779] | 59 |   /** | 
|---|
 | 60 |    *  the singleton reference to this class | 
|---|
 | 61 |    */ | 
|---|
 | 62 |   GLGuiHandler* GLGuiHandler::singletonRef = NULL; | 
|---|
| [5388] | 63 |  | 
|---|
| [7779] | 64 |   /** | 
|---|
 | 65 |      @brief standard deconstructor | 
|---|
 | 66 |    */ | 
|---|
 | 67 |   GLGuiHandler::~GLGuiHandler () | 
|---|
 | 68 |   { | 
|---|
 | 69 |     GLGuiHandler::singletonRef = NULL; | 
|---|
 | 70 |   } | 
|---|
| [5388] | 71 |  | 
|---|
| [7919] | 72 |   void GLGuiHandler::activateCursor() | 
|---|
 | 73 |   { | 
|---|
| [8324] | 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())); | 
|---|
| [9235] | 78 |  | 
|---|
| [10317] | 79 |     _cursor->loadTextureSequence(Resources::ResourceManager::getInstance()->mainGlobalPath().name() + "/" + "textures/reap_mouse/reap_mouse_##.png", 1, 49); | 
|---|
| [9235] | 80 |  | 
|---|
| [7919] | 81 |   } | 
|---|
 | 82 |  | 
|---|
 | 83 |   void GLGuiHandler::deactivateCursor(bool deleteCursor) | 
|---|
 | 84 |   { | 
|---|
| [8324] | 85 |     if (this->_cursor) | 
|---|
| [7919] | 86 |     { | 
|---|
 | 87 |       if (deleteCursor) | 
|---|
| [8324] | 88 |         delete this->_cursor; | 
|---|
 | 89 |       this->_cursor = NULL; | 
|---|
| [7919] | 90 |     } | 
|---|
 | 91 |   } | 
|---|
 | 92 |  | 
|---|
| [9656] | 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 |   } | 
|---|
| [7919] | 99 |  | 
|---|
| [9656] | 100 |  | 
|---|
| [7779] | 101 |   void GLGuiHandler::activate() | 
|---|
 | 102 |   { | 
|---|
| [9006] | 103 |     //EventHandler::getInstance()->pushState(ES_MENU); | 
|---|
| [5388] | 104 |  | 
|---|
| [7919] | 105 |  | 
|---|
 | 106 |  | 
|---|
| [7779] | 107 |   } | 
|---|
| [5388] | 108 |  | 
|---|
| [7779] | 109 |   void GLGuiHandler::deactivate() | 
|---|
 | 110 |   { | 
|---|
| [9006] | 111 |     //EventHandler::getInstance()->popState(); | 
|---|
| [5388] | 112 |  | 
|---|
| [7919] | 113 |  | 
|---|
| [7779] | 114 |   } | 
|---|
| [5388] | 115 |  | 
|---|
| [8717] | 116 |   void GLGuiHandler::selectNext() | 
|---|
 | 117 |   { | 
|---|
 | 118 |     // retrieve Objects. | 
|---|
| [9869] | 119 |     ObjectList<GLGuiWidget>::const_iterator it, currentIt; | 
|---|
 | 120 |     currentIt = GLGuiWidget::objectList().end(); | 
|---|
| [5388] | 121 |  | 
|---|
| [9869] | 122 |     if (GLGuiWidget::selected() != NULL) | 
|---|
| [8717] | 123 |     { | 
|---|
| [9869] | 124 |       it = std::find(GLGuiWidget::objectList().begin(), GLGuiWidget::objectList().end(), GLGuiWidget::selected()); | 
|---|
 | 125 |       if (it != GLGuiWidget::objectList().end()) | 
|---|
| [8717] | 126 |       { | 
|---|
| [9869] | 127 |         currentIt = it; | 
|---|
 | 128 |         it++; | 
|---|
| [8717] | 129 |       } | 
|---|
| [9869] | 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) | 
|---|
| [8717] | 141 |       { | 
|---|
| [9869] | 142 |         it = GLGuiWidget::objectList().begin(); | 
|---|
 | 143 |         cycledOnce = true; | 
|---|
| [8717] | 144 |       } | 
|---|
 | 145 |  | 
|---|
| [9869] | 146 |       if ((*it)->selectable() && (*it)->isVisible()) | 
|---|
| [8717] | 147 |       { | 
|---|
| [9869] | 148 |         (*it)->select(); | 
|---|
 | 149 |         return; | 
|---|
| [8717] | 150 |       } | 
|---|
| [9869] | 151 |     } | 
|---|
| [8717] | 152 |  | 
|---|
 | 153 |   } | 
|---|
 | 154 |  | 
|---|
 | 155 |   void GLGuiHandler::selectPrevious() | 
|---|
 | 156 |   { | 
|---|
| [9869] | 157 |     ObjectList<GLGuiWidget>::const_iterator it, currentIt; | 
|---|
 | 158 |     currentIt = GLGuiWidget::objectList().begin(); | 
|---|
| [8717] | 159 |  | 
|---|
 | 160 |       if (GLGuiWidget::selected() != NULL) | 
|---|
 | 161 |       { | 
|---|
| [9869] | 162 |         it = std::find(GLGuiWidget::objectList().begin(), GLGuiWidget::objectList().end(), GLGuiWidget::selected()); | 
|---|
 | 163 |         if (it != GLGuiWidget::objectList().end()) | 
|---|
| [8717] | 164 |         { | 
|---|
 | 165 |           currentIt = it; | 
|---|
 | 166 |           it--; | 
|---|
 | 167 |         } | 
|---|
 | 168 |       } | 
|---|
 | 169 |       else | 
|---|
 | 170 |       { | 
|---|
| [9869] | 171 |         it = GLGuiWidget::objectList().end(); | 
|---|
| [8717] | 172 |       } | 
|---|
 | 173 |  | 
|---|
 | 174 |       bool cycledOnce = false; | 
|---|
 | 175 |  | 
|---|
 | 176 |       for (; it != currentIt; --it) | 
|---|
 | 177 |       { | 
|---|
| [9869] | 178 |         if (it == GLGuiWidget::objectList().end() && !cycledOnce) | 
|---|
| [8717] | 179 |         { | 
|---|
 | 180 |           --it ; | 
|---|
 | 181 |           cycledOnce = true; | 
|---|
 | 182 |         } | 
|---|
 | 183 |  | 
|---|
| [9869] | 184 |         if ((*it)->selectable() && (*it)->isVisible()) | 
|---|
| [8717] | 185 |         { | 
|---|
| [9869] | 186 |           (*it)->select(); | 
|---|
| [8717] | 187 |           return; | 
|---|
 | 188 |         } | 
|---|
 | 189 |       } | 
|---|
 | 190 |  | 
|---|
 | 191 |   } | 
|---|
 | 192 |  | 
|---|
 | 193 |  | 
|---|
 | 194 |  | 
|---|
| [7779] | 195 |   void GLGuiHandler::process(const Event &event) | 
|---|
 | 196 |   { | 
|---|
| [7919] | 197 |     switch (event.type) | 
|---|
 | 198 |     { | 
|---|
| [9021] | 199 |       case EV_MOUSE_MOTION: | 
|---|
| [9869] | 200 |       this->checkFocus(); | 
|---|
 | 201 |       break; | 
|---|
| [9021] | 202 |  | 
|---|
| [7919] | 203 |       case  EV_MOUSE_BUTTON_LEFT: | 
|---|
| [9869] | 204 |       if (GLGuiWidget::mouseFocused() != NULL && event.bPressed) | 
|---|
 | 205 |       { | 
|---|
 | 206 |         // if clickable select the Widget. | 
|---|
 | 207 |         if (GLGuiWidget::mouseFocused()->clickable()) | 
|---|
| [7919] | 208 |         { | 
|---|
| [9869] | 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()); | 
|---|
| [8717] | 212 |         } | 
|---|
| [9869] | 213 |       } | 
|---|
 | 214 |       else if (GLGuiWidget::selected() != NULL && !event.bPressed) | 
|---|
 | 215 |       { | 
|---|
 | 216 |         if (GLGuiWidget::selected()->clickable()) | 
|---|
| [8717] | 217 |         { | 
|---|
| [9869] | 218 |           Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); | 
|---|
 | 219 |           GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D()); | 
|---|
| [7919] | 220 |         } | 
|---|
| [9869] | 221 |       } | 
|---|
| [8717] | 222 |  | 
|---|
| [9869] | 223 |       break; | 
|---|
| [7919] | 224 |       case EV_LEAVE_STATE: | 
|---|
| [9869] | 225 |       if (GLGuiWidget::selected() != NULL) | 
|---|
 | 226 |         GLGuiWidget::selected()->unselect(); | 
|---|
| [8717] | 227 |  | 
|---|
| [9869] | 228 |       if (GLGuiWidget::mouseFocused() != NULL) | 
|---|
 | 229 |         GLGuiWidget::mouseFocused()->breakMouseFocus(); | 
|---|
 | 230 |       break; | 
|---|
| [5388] | 231 |  | 
|---|
| [7919] | 232 |       case EV_VIDEO_RESIZE: | 
|---|
| [9869] | 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; | 
|---|
| [9656] | 237 |  | 
|---|
| [8717] | 238 |       case SDLK_TAB: | 
|---|
| [9869] | 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; | 
|---|
| [7919] | 247 |     } | 
|---|
| [5388] | 248 |  | 
|---|
| [9021] | 249 |  | 
|---|
 | 250 |     // Send the Event to the Widget below. | 
|---|
| [8717] | 251 |     if (GLGuiWidget::selected() != NULL) | 
|---|
| [7919] | 252 |     { | 
|---|
| [8717] | 253 |       GLGuiWidget::selected()->processEvent(event); | 
|---|
| [7919] | 254 |     } | 
|---|
 | 255 |  | 
|---|
 | 256 |  | 
|---|
 | 257 |  | 
|---|
| [7779] | 258 |   } | 
|---|
| [5406] | 259 |  | 
|---|
| [8035] | 260 |  | 
|---|
| [9240] | 261 |   const Vector2D& GLGuiHandler::cursorPositionOverFocusedWidget() const | 
|---|
| [8035] | 262 |   { | 
|---|
| [9240] | 263 |     return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D::nullVector(); | 
|---|
| [8035] | 264 |   } | 
|---|
 | 265 |  | 
|---|
 | 266 |   const Vector2D& GLGuiHandler::cursorPositionAbs() const | 
|---|
 | 267 |   { | 
|---|
| [8324] | 268 |     if (this->_cursor) | 
|---|
 | 269 |       return this->_cursor->getAbsCoor2D(); | 
|---|
| [8035] | 270 |     else | 
|---|
 | 271 |       return Vector2D::nullVector(); | 
|---|
 | 272 |   } | 
|---|
| [8717] | 273 |  | 
|---|
| [8035] | 274 |   Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const | 
|---|
 | 275 |   { | 
|---|
 | 276 |     assert (widget != NULL); | 
|---|
| [8324] | 277 |     if (this->_cursor) | 
|---|
 | 278 |       return  this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D(); | 
|---|
| [8035] | 279 |     else | 
|---|
 | 280 |       return Vector2D::nullVector(); | 
|---|
 | 281 |   } | 
|---|
 | 282 |  | 
|---|
 | 283 |  | 
|---|
| [8717] | 284 |   void GLGuiHandler::checkFocus() | 
|---|
| [7779] | 285 |   { | 
|---|
| [8743] | 286 |     // CHECK THE COLLISIONS. | 
|---|
| [9869] | 287 |     if (this->_cursor != NULL) | 
|---|
| [7919] | 288 |     { | 
|---|
| [9869] | 289 |       for (ObjectList<GLGuiWidget>::const_iterator it = GLGuiWidget::objectList().begin(); | 
|---|
 | 290 |            it != GLGuiWidget::objectList().end(); | 
|---|
 | 291 |            it++) | 
|---|
| [7919] | 292 |       { | 
|---|
| [9869] | 293 |         GLGuiWidget* widget = (*it); | 
|---|
| [7919] | 294 |  | 
|---|
 | 295 |         if (widget->isVisible() && | 
|---|
 | 296 |             widget->focusable() && | 
|---|
| [8324] | 297 |             widget->focusOverWidget(this->_cursor)) | 
|---|
| [7919] | 298 |         { | 
|---|
 | 299 |           // receiving Focus | 
|---|
| [8717] | 300 |           if (GLGuiWidget::mouseFocused() != widget) | 
|---|
| [7919] | 301 |           { | 
|---|
| [8717] | 302 |             widget->giveMouseFocus(); | 
|---|
| [7919] | 303 |           } | 
|---|
 | 304 |           return ; | 
|---|
 | 305 |         } | 
|---|
 | 306 |       } | 
|---|
| [8717] | 307 |       if (GLGuiWidget::mouseFocused() != NULL) | 
|---|
 | 308 |         GLGuiWidget::mouseFocused()->breakMouseFocus(); | 
|---|
| [7919] | 309 |     } | 
|---|
| [7779] | 310 |   } | 
|---|
| [8717] | 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 |   } | 
|---|
| [5406] | 326 | } | 
|---|