| [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 | |
|---|
| [8450] | 27 | #include <cassert> |
|---|
| [7919] | 28 | |
|---|
| [8450] | 29 | |
|---|
| [7919] | 30 | /// TAKE THIS OUT OF HERE. |
|---|
| 31 | #include "graphics_engine.h" |
|---|
| 32 | |
|---|
| [7779] | 33 | namespace OrxGui |
|---|
| [3655] | 34 | { |
|---|
| 35 | |
|---|
| [7779] | 36 | /** |
|---|
| 37 | * standard constructor |
|---|
| 38 | */ |
|---|
| 39 | GLGuiHandler::GLGuiHandler () |
|---|
| 40 | { |
|---|
| 41 | this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler"); |
|---|
| 42 | this->setName("GLGuiHandler"); |
|---|
| [3655] | 43 | |
|---|
| [7868] | 44 | |
|---|
| [8448] | 45 | EventHandler::getInstance()->withUNICODE(ES_MENU, true ); |
|---|
| [7919] | 46 | |
|---|
| [8324] | 47 | this->_cursor = NULL; |
|---|
| [7919] | 48 | for (unsigned int i = 0; i < EV_NUMBER; i++) |
|---|
| 49 | { |
|---|
| [8312] | 50 | this->subscribeEvent(ES_ALL, i); |
|---|
| [7919] | 51 | } |
|---|
| [7779] | 52 | } |
|---|
| [3655] | 53 | |
|---|
| [7779] | 54 | /** |
|---|
| 55 | * the singleton reference to this class |
|---|
| 56 | */ |
|---|
| 57 | GLGuiHandler* GLGuiHandler::singletonRef = NULL; |
|---|
| [5388] | 58 | |
|---|
| [7779] | 59 | /** |
|---|
| 60 | @brief standard deconstructor |
|---|
| 61 | */ |
|---|
| 62 | GLGuiHandler::~GLGuiHandler () |
|---|
| 63 | { |
|---|
| 64 | GLGuiHandler::singletonRef = NULL; |
|---|
| 65 | } |
|---|
| [5388] | 66 | |
|---|
| [7919] | 67 | void GLGuiHandler::activateCursor() |
|---|
| 68 | { |
|---|
| [8324] | 69 | if (this->_cursor == NULL) |
|---|
| 70 | this->_cursor = new GLGuiCursor(); |
|---|
| 71 | this->_cursor->show(); |
|---|
| 72 | this->_cursor->setMaxBorders(Vector2D(GraphicsEngine::getInstance()->getResolutionX(), GraphicsEngine::getInstance()->getResolutionY())); |
|---|
| [7919] | 73 | } |
|---|
| 74 | |
|---|
| 75 | void GLGuiHandler::deactivateCursor(bool deleteCursor) |
|---|
| 76 | { |
|---|
| [8324] | 77 | if (this->_cursor) |
|---|
| [7919] | 78 | { |
|---|
| 79 | if (deleteCursor) |
|---|
| [8324] | 80 | delete this->_cursor; |
|---|
| 81 | this->_cursor = NULL; |
|---|
| [7919] | 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| [7779] | 86 | void GLGuiHandler::activate() |
|---|
| 87 | { |
|---|
| 88 | EventHandler::getInstance()->pushState(ES_MENU); |
|---|
| [5388] | 89 | |
|---|
| [7919] | 90 | |
|---|
| 91 | |
|---|
| [7779] | 92 | } |
|---|
| [5388] | 93 | |
|---|
| [7779] | 94 | void GLGuiHandler::deactivate() |
|---|
| 95 | { |
|---|
| 96 | EventHandler::getInstance()->popState(); |
|---|
| [5388] | 97 | |
|---|
| [7919] | 98 | |
|---|
| [7779] | 99 | } |
|---|
| [5388] | 100 | |
|---|
| 101 | |
|---|
| [7779] | 102 | void GLGuiHandler::process(const Event &event) |
|---|
| 103 | { |
|---|
| [7919] | 104 | switch (event.type) |
|---|
| 105 | { |
|---|
| 106 | case EV_MOUSE_BUTTON_LEFT: |
|---|
| 107 | if (GLGuiWidget::focused() != NULL) |
|---|
| 108 | { |
|---|
| 109 | if (event.bPressed) |
|---|
| 110 | { |
|---|
| 111 | if (GLGuiWidget::focused()->clickable()) |
|---|
| [8035] | 112 | { |
|---|
| [8324] | 113 | Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
|---|
| [8035] | 114 | GLGuiWidget::focused()->click(cursorPos - GLGuiWidget::focused()->getAbsCoor2D()); |
|---|
| 115 | } |
|---|
| [7919] | 116 | } |
|---|
| 117 | else |
|---|
| 118 | { |
|---|
| 119 | if (GLGuiWidget::focused()->clickable()) |
|---|
| [8035] | 120 | { |
|---|
| [8324] | 121 | Vector2D cursorPos = (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
|---|
| [8035] | 122 | GLGuiWidget::focused()->release(cursorPos - GLGuiWidget::focused()->getAbsCoor2D()); |
|---|
| 123 | } |
|---|
| [7919] | 124 | } |
|---|
| 125 | } |
|---|
| 126 | break; |
|---|
| 127 | case EV_LEAVE_STATE: |
|---|
| 128 | if (GLGuiWidget::focused() != NULL) |
|---|
| 129 | GLGuiWidget::focused()->breakFocus(); |
|---|
| 130 | break; |
|---|
| [5388] | 131 | |
|---|
| [7919] | 132 | case EV_VIDEO_RESIZE: |
|---|
| [8324] | 133 | if (this->_cursor != NULL) |
|---|
| 134 | this->_cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h)); |
|---|
| [7919] | 135 | break; |
|---|
| 136 | } |
|---|
| [5388] | 137 | |
|---|
| [7919] | 138 | |
|---|
| 139 | |
|---|
| 140 | if (GLGuiWidget::focused()) |
|---|
| 141 | { |
|---|
| 142 | GLGuiWidget::focused()->processEvent(event); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| [7779] | 147 | } |
|---|
| [5406] | 148 | |
|---|
| [8035] | 149 | |
|---|
| 150 | Vector2D GLGuiHandler::cursorPositionOverFocusedWidget() const |
|---|
| 151 | { |
|---|
| [8324] | 152 | return (this->_cursor != NULL) ? this->_cursor->getAbsCoor2D() : Vector2D(0,0); |
|---|
| [8035] | 153 | } |
|---|
| 154 | |
|---|
| 155 | const Vector2D& GLGuiHandler::cursorPositionAbs() const |
|---|
| 156 | { |
|---|
| [8324] | 157 | if (this->_cursor) |
|---|
| 158 | return this->_cursor->getAbsCoor2D(); |
|---|
| [8035] | 159 | else |
|---|
| 160 | return Vector2D::nullVector(); |
|---|
| 161 | } |
|---|
| 162 | Vector2D GLGuiHandler::cursorPositionRel(const GLGuiWidget* const widget) const |
|---|
| 163 | { |
|---|
| 164 | assert (widget != NULL); |
|---|
| [8324] | 165 | if (this->_cursor) |
|---|
| 166 | return this->_cursor->getAbsCoor2D() - widget->getAbsCoor2D(); |
|---|
| [8035] | 167 | else |
|---|
| 168 | return Vector2D::nullVector(); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| [7779] | 172 | void GLGuiHandler::draw() |
|---|
| 173 | { |
|---|
| [7919] | 174 | // GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP); |
|---|
| [7779] | 175 | } |
|---|
| [5406] | 176 | |
|---|
| 177 | |
|---|
| [7779] | 178 | void GLGuiHandler::tick(float dt) |
|---|
| 179 | { |
|---|
| [7919] | 180 | |
|---|
| 181 | // CHECK THE COLLISIONS. |
|---|
| 182 | const std::list<BaseObject*>* objects = ClassList::getList(CL_GLGUI_WIDGET); |
|---|
| 183 | |
|---|
| [8324] | 184 | if (objects != NULL && this->_cursor != NULL) |
|---|
| [7919] | 185 | { |
|---|
| 186 | for (std::list<BaseObject*>::const_iterator it = objects->begin(); it != objects->end(); it++) |
|---|
| 187 | { |
|---|
| 188 | GLGuiWidget* widget = dynamic_cast<GLGuiWidget*>(*it); |
|---|
| 189 | |
|---|
| 190 | if (widget->isVisible() && |
|---|
| 191 | widget->focusable() && |
|---|
| [8324] | 192 | widget->focusOverWidget(this->_cursor)) |
|---|
| [7919] | 193 | { |
|---|
| 194 | // receiving Focus |
|---|
| 195 | if (GLGuiWidget::focused() != widget) |
|---|
| 196 | { |
|---|
| 197 | widget->giveFocus(); |
|---|
| 198 | } |
|---|
| 199 | return ; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | if (GLGuiWidget::focused() != NULL) |
|---|
| 203 | GLGuiWidget::focused()->breakFocus(); |
|---|
| 204 | } |
|---|
| [7779] | 205 | } |
|---|
| [5406] | 206 | } |
|---|