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