1 | /* |
---|
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: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI |
---|
17 | |
---|
18 | #include "glgui_handler.h" |
---|
19 | #include "event_handler.h" |
---|
20 | |
---|
21 | #include "glgui_mainwidget.h" |
---|
22 | #include "glgui_cursor.h" |
---|
23 | |
---|
24 | #include "class_list.h" |
---|
25 | |
---|
26 | |
---|
27 | /// TAKE THIS OUT OF HERE. |
---|
28 | #include "graphics_engine.h" |
---|
29 | |
---|
30 | namespace OrxGui |
---|
31 | { |
---|
32 | |
---|
33 | /** |
---|
34 | * standard constructor |
---|
35 | */ |
---|
36 | GLGuiHandler::GLGuiHandler () |
---|
37 | { |
---|
38 | this->setClassID(CL_GLGUI_HANDLER, "GLGuiHandler"); |
---|
39 | this->setName("GLGuiHandler"); |
---|
40 | |
---|
41 | |
---|
42 | EventHandler::getInstance()->withUNICODE(ES_ALL, true ); |
---|
43 | |
---|
44 | this->cursor = NULL; |
---|
45 | for (unsigned int i = 0; i < EV_NUMBER; i++) |
---|
46 | { |
---|
47 | this->subscribeEvent(ES_ALL, i); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * the singleton reference to this class |
---|
53 | */ |
---|
54 | GLGuiHandler* GLGuiHandler::singletonRef = NULL; |
---|
55 | |
---|
56 | /** |
---|
57 | @brief standard deconstructor |
---|
58 | */ |
---|
59 | GLGuiHandler::~GLGuiHandler () |
---|
60 | { |
---|
61 | GLGuiHandler::singletonRef = NULL; |
---|
62 | } |
---|
63 | |
---|
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 | |
---|
83 | void GLGuiHandler::activate() |
---|
84 | { |
---|
85 | EventHandler::getInstance()->pushState(ES_MENU); |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | void GLGuiHandler::deactivate() |
---|
92 | { |
---|
93 | EventHandler::getInstance()->popState(); |
---|
94 | |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | void GLGuiHandler::process(const Event &event) |
---|
100 | { |
---|
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()) |
---|
109 | { |
---|
110 | Vector2D cursorPos = (this->cursor != NULL) ? this->cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
---|
111 | GLGuiWidget::focused()->click(cursorPos - GLGuiWidget::focused()->getAbsCoor2D()); |
---|
112 | } |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | if (GLGuiWidget::focused()->clickable()) |
---|
117 | { |
---|
118 | Vector2D cursorPos = (this->cursor != NULL) ? this->cursor->getAbsCoor2D() : Vector2D(event.x, event.y); |
---|
119 | GLGuiWidget::focused()->release(cursorPos - GLGuiWidget::focused()->getAbsCoor2D()); |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | break; |
---|
124 | case EV_LEAVE_STATE: |
---|
125 | if (GLGuiWidget::focused() != NULL) |
---|
126 | GLGuiWidget::focused()->breakFocus(); |
---|
127 | break; |
---|
128 | |
---|
129 | case EV_VIDEO_RESIZE: |
---|
130 | if (this->cursor != NULL) |
---|
131 | this->cursor->setMaxBorders(Vector2D(event.resize.w, event.resize.h)); |
---|
132 | break; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | |
---|
137 | if (GLGuiWidget::focused()) |
---|
138 | { |
---|
139 | GLGuiWidget::focused()->processEvent(event); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
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 | |
---|
169 | void GLGuiHandler::draw() |
---|
170 | { |
---|
171 | // GLGuiMainWidget::getInstance()->draw2D(E2D_LAYER_TOP); |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | void GLGuiHandler::tick(float dt) |
---|
176 | { |
---|
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 | } |
---|
202 | } |
---|
203 | } |
---|