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