[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 | |
---|
[8450] | 30 | #include <cassert> |
---|
[7919] | 31 | |
---|
[8450] | 32 | |
---|
[7919] | 33 | /// TAKE THIS OUT OF HERE. |
---|
| 34 | #include "graphics_engine.h" |
---|
[9235] | 35 | #include "loading/resource_manager.h" |
---|
[7919] | 36 | |
---|
[7779] | 37 | namespace OrxGui |
---|
[3655] | 38 | { |
---|
| 39 | |
---|
[7779] | 40 | /** |
---|
| 41 | * standard constructor |
---|
| 42 | */ |
---|
| 43 | GLGuiHandler::GLGuiHandler () |
---|
| 44 | { |
---|
| 45 | this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler"); |
---|
| 46 | this->setName("GLGuiHandler"); |
---|
[3655] | 47 | |
---|
[7868] | 48 | |
---|
[8448] | 49 | EventHandler::getInstance()->withUNICODE(ES_MENU, true ); |
---|
[7919] | 50 | |
---|
[8324] | 51 | this->_cursor = NULL; |
---|
[7919] | 52 | for (unsigned int i = 0; i < EV_NUMBER; i++) |
---|
| 53 | { |
---|
[8312] | 54 | this->subscribeEvent(ES_ALL, 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 | |
---|
| 92 | |
---|
[7779] | 93 | void GLGuiHandler::activate() |
---|
| 94 | { |
---|
[9006] | 95 | //EventHandler::getInstance()->pushState(ES_MENU); |
---|
[5388] | 96 | |
---|
[7919] | 97 | |
---|
| 98 | |
---|
[7779] | 99 | } |
---|
[5388] | 100 | |
---|
[7779] | 101 | void GLGuiHandler::deactivate() |
---|
| 102 | { |
---|
[9006] | 103 | //EventHandler::getInstance()->popState(); |
---|
[5388] | 104 | |
---|
[7919] | 105 | |
---|
[7779] | 106 | } |
---|
[5388] | 107 | |
---|
[8717] | 108 | void GLGuiHandler::selectNext() |
---|
| 109 | { |
---|
| 110 | // retrieve Objects. |
---|
| 111 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
---|
[5388] | 112 | |
---|
[8717] | 113 | if (objects) |
---|
| 114 | { |
---|
| 115 | std::list<BaseObject*>::const_iterator it ; |
---|
| 116 | std::list<BaseObject*>::const_iterator currentIt = objects->end(); |
---|
| 117 | |
---|
| 118 | if (GLGuiWidget::selected() != NULL) |
---|
| 119 | { |
---|
| 120 | it = std::find(objects->begin(), objects->end(), GLGuiWidget::selected()); |
---|
| 121 | if (it != objects->end()) |
---|
| 122 | { |
---|
| 123 | currentIt = it; |
---|
| 124 | it++; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | else |
---|
| 128 | { |
---|
| 129 | it = objects->begin(); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | bool cycledOnce = false; |
---|
| 133 | |
---|
| 134 | for (; it != currentIt; ++it) |
---|
| 135 | { |
---|
| 136 | if (it == objects->end() && !cycledOnce) |
---|
| 137 | { |
---|
| 138 | it = objects->begin(); |
---|
| 139 | cycledOnce = true; |
---|
| 140 | } |
---|
| 141 | |
---|
[8742] | 142 | if (dynamic_cast<GLGuiWidget*>(*it)->selectable() && dynamic_cast<GLGuiWidget*>(*it)->isVisible()) |
---|
[8717] | 143 | { |
---|
| 144 | dynamic_cast<GLGuiWidget*>(*it)->select(); |
---|
| 145 | return; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | } |
---|
| 150 | else |
---|
| 151 | { |
---|
| 152 | PRINTF(0)("NO GUI-ELEMENTS EXISTING\n"); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | void GLGuiHandler::selectPrevious() |
---|
| 157 | { |
---|
| 158 | // retrieve Objects. |
---|
| 159 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
---|
| 160 | |
---|
| 161 | if (objects) |
---|
| 162 | { |
---|
| 163 | std::list<BaseObject*>::const_iterator it ; |
---|
| 164 | std::list<BaseObject*>::const_iterator currentIt = objects->begin(); |
---|
| 165 | |
---|
| 166 | if (GLGuiWidget::selected() != NULL) |
---|
| 167 | { |
---|
| 168 | it = std::find(objects->begin(), objects->end(), GLGuiWidget::selected()); |
---|
| 169 | if (it != objects->end()) |
---|
| 170 | { |
---|
| 171 | currentIt = it; |
---|
| 172 | it--; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | else |
---|
| 176 | { |
---|
| 177 | it = objects->end(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | bool cycledOnce = false; |
---|
| 181 | |
---|
| 182 | for (; it != currentIt; --it) |
---|
| 183 | { |
---|
| 184 | if (it == objects->end() && !cycledOnce) |
---|
| 185 | { |
---|
| 186 | --it ; |
---|
| 187 | cycledOnce = true; |
---|
| 188 | } |
---|
| 189 | |
---|
[8742] | 190 | if (dynamic_cast<GLGuiWidget*>(*it)->selectable() && dynamic_cast<GLGuiWidget*>(*it)->isVisible()) |
---|
[8717] | 191 | { |
---|
| 192 | dynamic_cast<GLGuiWidget*>(*it)->select(); |
---|
| 193 | return; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | } |
---|
| 198 | else |
---|
| 199 | { |
---|
| 200 | PRINTF(0)("NO GUI-ELEMENTS EXISTING\n"); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | |
---|
[7779] | 206 | void GLGuiHandler::process(const Event &event) |
---|
| 207 | { |
---|
[7919] | 208 | switch (event.type) |
---|
| 209 | { |
---|
[9021] | 210 | case EV_MOUSE_MOTION: |
---|
| 211 | this->checkFocus(); |
---|
| 212 | break; |
---|
| 213 | |
---|
[7919] | 214 | case EV_MOUSE_BUTTON_LEFT: |
---|
[8717] | 215 | if (GLGuiWidget::mouseFocused() != NULL && event.bPressed) |
---|
[7919] | 216 | { |
---|
[8717] | 217 | // if clickable select the Widget. |
---|
| 218 | if (GLGuiWidget::mouseFocused()->clickable()) |
---|
[7919] | 219 | { |
---|
[8717] | 220 | Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
---|
| 221 | GLGuiWidget::mouseFocused()->select(); |
---|
| 222 | GLGuiWidget::mouseFocused()->click(cursorPos - GLGuiWidget::mouseFocused()->getAbsCoor2D()); |
---|
[7919] | 223 | } |
---|
[8717] | 224 | } |
---|
| 225 | else if (GLGuiWidget::selected() != NULL && !event.bPressed) |
---|
| 226 | { |
---|
| 227 | if (GLGuiWidget::selected()->clickable()) |
---|
[7919] | 228 | { |
---|
[8717] | 229 | Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
---|
| 230 | GLGuiWidget::selected()->release(cursorPos - GLGuiWidget::selected()->getAbsCoor2D()); |
---|
[7919] | 231 | } |
---|
| 232 | } |
---|
[8717] | 233 | |
---|
[7919] | 234 | break; |
---|
| 235 | case EV_LEAVE_STATE: |
---|
[8717] | 236 | if (GLGuiWidget::selected() != NULL) |
---|
| 237 | GLGuiWidget::selected()->unselect(); |
---|
| 238 | |
---|
| 239 | if (GLGuiWidget::mouseFocused() != NULL) |
---|
| 240 | GLGuiWidget::mouseFocused()->breakMouseFocus(); |
---|
[7919] | 241 | break; |
---|
[5388] | 242 | |
---|
[7919] | 243 | case EV_VIDEO_RESIZE: |
---|
[8324] | 244 | if (this->_cursor != NULL) |
---|
| 245 | this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h)); |
---|
[7919] | 246 | break; |
---|
[8717] | 247 | case SDLK_TAB: |
---|
| 248 | if (event.bPressed) |
---|
[8743] | 249 | { |
---|
| 250 | if (EventHandler::getInstance()->isPressed(SDLK_LSHIFT) || EventHandler::getInstance()->isPressed(SDLK_RSHIFT)) |
---|
| 251 | this->selectPrevious(); |
---|
| 252 | else |
---|
| 253 | this->selectNext(); |
---|
| 254 | } |
---|
[8717] | 255 | break; |
---|
[7919] | 256 | } |
---|
[5388] | 257 | |
---|
[9021] | 258 | |
---|
| 259 | // Send the Event to the Widget below. |
---|
[8717] | 260 | if (GLGuiWidget::selected() != NULL) |
---|
[7919] | 261 | { |
---|
[8717] | 262 | GLGuiWidget::selected()->processEvent(event); |
---|
[7919] | 263 | } |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | |
---|
[7779] | 267 | } |
---|
[5406] | 268 | |
---|
[8035] | 269 | |
---|
[9240] | 270 | const Vector2D& GLGuiHandler::cursorPositionOverFocusedWidget() const |
---|
[8035] | 271 | { |
---|
[9240] | 272 | return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D::nullVector(); |
---|
[8035] | 273 | } |
---|
| 274 | |
---|
| 275 | const Vector2D& GLGuiHandler::cursorPositionAbs() const |
---|
| 276 | { |
---|
[8324] | 277 | if (this->_cursor) |
---|
| 278 | return this->_cursor->getAbsCoor2D(); |
---|
[8035] | 279 | else |
---|
| 280 | return Vector2D::nullVector(); |
---|
| 281 | } |
---|
[8717] | 282 | |
---|
[8035] | 283 | Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const |
---|
| 284 | { |
---|
| 285 | assert (widget != NULL); |
---|
[8324] | 286 | if (this->_cursor) |
---|
| 287 | return this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D(); |
---|
[8035] | 288 | else |
---|
| 289 | return Vector2D::nullVector(); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | |
---|
[8717] | 293 | void GLGuiHandler::checkFocus() |
---|
[7779] | 294 | { |
---|
[8743] | 295 | // CHECK THE COLLISIONS. |
---|
[7919] | 296 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
---|
| 297 | |
---|
[8324] | 298 | if (objects != NULL && this->_cursor != NULL) |
---|
[7919] | 299 | { |
---|
| 300 | for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++) |
---|
| 301 | { |
---|
| 302 | GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it); |
---|
| 303 | |
---|
| 304 | if (widget->isVisible() && |
---|
| 305 | widget->focusable() && |
---|
[8324] | 306 | widget->focusOverWidget(this->_cursor)) |
---|
[7919] | 307 | { |
---|
| 308 | // receiving Focus |
---|
[8717] | 309 | if (GLGuiWidget::mouseFocused() != widget) |
---|
[7919] | 310 | { |
---|
[8717] | 311 | widget->giveMouseFocus(); |
---|
[7919] | 312 | } |
---|
| 313 | return ; |
---|
| 314 | } |
---|
| 315 | } |
---|
[8717] | 316 | if (GLGuiWidget::mouseFocused() != NULL) |
---|
| 317 | GLGuiWidget::mouseFocused()->breakMouseFocus(); |
---|
[7919] | 318 | } |
---|
[7779] | 319 | } |
---|
[8717] | 320 | |
---|
| 321 | void GLGuiHandler::draw() |
---|
| 322 | { |
---|
| 323 | // GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | void GLGuiHandler::tick(float dt) |
---|
| 328 | { |
---|
| 329 | // do not change if we already clicked into a Widget. |
---|
| 330 | // if (GLGuiWidget::selected() != NULL && GLGuiWidget::selected()->pushed()) |
---|
| 331 | // return ; |
---|
| 332 | |
---|
| 333 | this->checkFocus(); |
---|
| 334 | } |
---|
[5406] | 335 | } |
---|