[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 | |
---|
[7919] | 25 | #include "class_list.h" |
---|
[8711] | 26 | #include <cassert> |
---|
[7919] | 27 | |
---|
[8717] | 28 | #include "debug.h" |
---|
| 29 | |
---|
[7919] | 30 | |
---|
| 31 | /// TAKE THIS OUT OF HERE. |
---|
| 32 | #include "graphics_engine.h" |
---|
[9235] | 33 | #include "loading/resource_manager.h" |
---|
[7919] | 34 | |
---|
[7779] | 35 | namespace OrxGui |
---|
[3655] | 36 | { |
---|
| 37 | |
---|
[7779] | 38 | /** |
---|
| 39 | * standard constructor |
---|
| 40 | */ |
---|
| 41 | GLGuiHandler::GLGuiHandler () |
---|
| 42 | { |
---|
| 43 | this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler"); |
---|
| 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 | |
---|
| 79 | _cursor->loadTextureSequence(ResourceManager::getInstance()->getDataDir() + "/" + "maps/reap_mouse/reap_mouse_##.png", 1, 49); |
---|
| 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. |
---|
| 119 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
---|
[5388] | 120 | |
---|
[8717] | 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 | |
---|
[8742] | 150 | if (dynamic_cast<GLGuiWidget*>(*it)->selectable() && dynamic_cast<GLGuiWidget*>(*it)->isVisible()) |
---|
[8717] | 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 | |
---|
[8742] | 198 | if (dynamic_cast<GLGuiWidget*>(*it)->selectable() && dynamic_cast<GLGuiWidget*>(*it)->isVisible()) |
---|
[8717] | 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 | |
---|
[7779] | 214 | void GLGuiHandler::process(const Event &event) |
---|
| 215 | { |
---|
[7919] | 216 | switch (event.type) |
---|
| 217 | { |
---|
[9021] | 218 | case EV_MOUSE_MOTION: |
---|
| 219 | this->checkFocus(); |
---|
| 220 | break; |
---|
| 221 | |
---|
[7919] | 222 | case EV_MOUSE_BUTTON_LEFT: |
---|
[8717] | 223 | if (GLGuiWidget::mouseFocused() != NULL && event.bPressed) |
---|
[7919] | 224 | { |
---|
[8717] | 225 | // if clickable select the Widget. |
---|
| 226 | if (GLGuiWidget::mouseFocused()->clickable()) |
---|
[7919] | 227 | { |
---|
[8717] | 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()); |
---|
[7919] | 231 | } |
---|
[8717] | 232 | } |
---|
| 233 | else if (GLGuiWidget::selected() != NULL && !event.bPressed) |
---|
| 234 | { |
---|
| 235 | if (GLGuiWidget::selected()->clickable()) |
---|
[7919] | 236 | { |
---|
[8717] | 237 | Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
---|
| 238 | GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D()); |
---|
[7919] | 239 | } |
---|
| 240 | } |
---|
[8717] | 241 | |
---|
[7919] | 242 | break; |
---|
| 243 | case EV_LEAVE_STATE: |
---|
[8717] | 244 | if (GLGuiWidget::selected() != NULL) |
---|
| 245 | GLGuiWidget::selected()->unselect(); |
---|
| 246 | |
---|
| 247 | if (GLGuiWidget::mouseFocused() != NULL) |
---|
| 248 | GLGuiWidget::mouseFocused()->breakMouseFocus(); |
---|
[7919] | 249 | break; |
---|
[5388] | 250 | |
---|
[7919] | 251 | case EV_VIDEO_RESIZE: |
---|
[8324] | 252 | if (this->_cursor != NULL) |
---|
| 253 | this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h)); |
---|
[9656] | 254 | this->_resolution = Vector2D(event.resize.w, event.resize.h); |
---|
[7919] | 255 | break; |
---|
[9656] | 256 | |
---|
[8717] | 257 | case SDLK_TAB: |
---|
| 258 | if (event.bPressed) |
---|
[8743] | 259 | { |
---|
| 260 | if (EventHandler::getInstance()->isPressed(SDLK_LSHIFT) || EventHandler::getInstance()->isPressed(SDLK_RSHIFT)) |
---|
| 261 | this->selectPrevious(); |
---|
| 262 | else |
---|
| 263 | this->selectNext(); |
---|
| 264 | } |
---|
[8717] | 265 | break; |
---|
[7919] | 266 | } |
---|
[5388] | 267 | |
---|
[9021] | 268 | |
---|
| 269 | // Send the Event to the Widget below. |
---|
[8717] | 270 | if (GLGuiWidget::selected() != NULL) |
---|
[7919] | 271 | { |
---|
[8717] | 272 | GLGuiWidget::selected()->processEvent(event); |
---|
[7919] | 273 | } |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | |
---|
[7779] | 277 | } |
---|
[5406] | 278 | |
---|
[8035] | 279 | |
---|
[9240] | 280 | const Vector2D& GLGuiHandler::cursorPositionOverFocusedWidget() const |
---|
[8035] | 281 | { |
---|
[9240] | 282 | return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D::nullVector(); |
---|
[8035] | 283 | } |
---|
| 284 | |
---|
| 285 | const Vector2D& GLGuiHandler::cursorPositionAbs() const |
---|
| 286 | { |
---|
[8324] | 287 | if (this->_cursor) |
---|
| 288 | return this->_cursor->getAbsCoor2D(); |
---|
[8035] | 289 | else |
---|
| 290 | return Vector2D::nullVector(); |
---|
| 291 | } |
---|
[8717] | 292 | |
---|
[8035] | 293 | Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const |
---|
| 294 | { |
---|
| 295 | assert (widget != NULL); |
---|
[8324] | 296 | if (this->_cursor) |
---|
| 297 | return this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D(); |
---|
[8035] | 298 | else |
---|
| 299 | return Vector2D::nullVector(); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | |
---|
[8717] | 303 | void GLGuiHandler::checkFocus() |
---|
[7779] | 304 | { |
---|
[8743] | 305 | // CHECK THE COLLISIONS. |
---|
[7919] | 306 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
---|
| 307 | |
---|
[8324] | 308 | if (objects != NULL && this->_cursor != NULL) |
---|
[7919] | 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() && |
---|
[8324] | 316 | widget->focusOverWidget(this->_cursor)) |
---|
[7919] | 317 | { |
---|
| 318 | // receiving Focus |
---|
[8717] | 319 | if (GLGuiWidget::mouseFocused() != widget) |
---|
[7919] | 320 | { |
---|
[8717] | 321 | widget->giveMouseFocus(); |
---|
[7919] | 322 | } |
---|
| 323 | return ; |
---|
| 324 | } |
---|
| 325 | } |
---|
[8717] | 326 | if (GLGuiWidget::mouseFocused() != NULL) |
---|
| 327 | GLGuiWidget::mouseFocused()->breakMouseFocus(); |
---|
[7919] | 328 | } |
---|
[7779] | 329 | } |
---|
[8717] | 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 | } |
---|
[5406] | 345 | } |
---|