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