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_widget.h" |
---|
19 | |
---|
20 | #include "glgui_cursor.h" |
---|
21 | |
---|
22 | #include "material.h" |
---|
23 | |
---|
24 | #include "debug.h" |
---|
25 | |
---|
26 | namespace OrxGui |
---|
27 | { |
---|
28 | |
---|
29 | /** |
---|
30 | * @brief standard constructor |
---|
31 | */ |
---|
32 | GLGuiWidget::GLGuiWidget (GLGuiWidget* parent) |
---|
33 | { |
---|
34 | this->init(); |
---|
35 | |
---|
36 | this->setParentWidget(parent); |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | /** |
---|
41 | * @brief standard deconstructor |
---|
42 | */ |
---|
43 | GLGuiWidget::~GLGuiWidget() |
---|
44 | { |
---|
45 | if (this == GLGuiWidget::_focused) |
---|
46 | GLGuiWidget::_focused = NULL; |
---|
47 | |
---|
48 | if (this->_toFrontColor) |
---|
49 | delete this->_toFrontColor; |
---|
50 | } |
---|
51 | |
---|
52 | GLGuiWidget* GLGuiWidget::_selected = NULL; |
---|
53 | GLGuiWidget* GLGuiWidget::_focused = NULL; |
---|
54 | GLGuiWidget* GLGuiWidget::_inputGrabber = NULL; |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | /** |
---|
59 | * initializes the GUI-element |
---|
60 | */ |
---|
61 | void GLGuiWidget::init() |
---|
62 | { |
---|
63 | this->setClassID(CL_GLGUI_WIDGET, "GLGuiWidget"); |
---|
64 | |
---|
65 | this->_focusable = false; |
---|
66 | this->_clickable = false; |
---|
67 | this->_pushed = false; |
---|
68 | |
---|
69 | this->setVisibility(GLGUI_WIDGET_DEFAULT_VISIBLE); |
---|
70 | |
---|
71 | this->_backMat.setDiffuseColor(Color(1.0, 0.5, 0.4, 1.0)); |
---|
72 | this->_backMat.setDiffuseMap("gui_element_background.png"); |
---|
73 | this->_frontColor = Color(1.0, 0.0, 0.0); |
---|
74 | this->_toFrontColor = NULL; |
---|
75 | |
---|
76 | |
---|
77 | this->_borderLeft = 15.0; |
---|
78 | this->_borderRight = 1.0; |
---|
79 | this->_borderTop = 1.0; |
---|
80 | this->_borderBottom = 1.0; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | void GLGuiWidget::setParentWidget(GLGuiWidget* parent) |
---|
85 | { |
---|
86 | this->_parent = parent; |
---|
87 | |
---|
88 | if (parent != NULL) |
---|
89 | parent->addChild2D(this); |
---|
90 | } |
---|
91 | |
---|
92 | /** @brief gives focus to this widget */ |
---|
93 | void GLGuiWidget::giveFocus() |
---|
94 | { |
---|
95 | if (GLGuiWidget::focused() != NULL) |
---|
96 | GLGuiWidget::focused()->breakFocus(); |
---|
97 | GLGuiWidget::_focused = this; |
---|
98 | this->receivedFocus(); |
---|
99 | }; |
---|
100 | |
---|
101 | void GLGuiWidget::breakFocus() |
---|
102 | { |
---|
103 | if (GLGuiWidget::_focused == this) |
---|
104 | { |
---|
105 | GLGuiWidget::_focused = NULL; |
---|
106 | this->_pushed = false; |
---|
107 | this->removedFocus(); |
---|
108 | } |
---|
109 | }; |
---|
110 | |
---|
111 | |
---|
112 | bool GLGuiWidget::focusOverWidget(const Vector2D& position) const |
---|
113 | { |
---|
114 | return (this->getAbsCoor2D().x < position.x && this->getAbsCoor2D().x + this->getSizeX2D() > position.x && |
---|
115 | this->getAbsCoor2D().y < position.y && this->getAbsCoor2D().y + this->getSizeY2D() > position.y); |
---|
116 | } |
---|
117 | |
---|
118 | bool GLGuiWidget::focusOverWidget(const GLGuiCursor* const cursor) const |
---|
119 | { |
---|
120 | return this->focusOverWidget(cursor->getAbsCoor2D()); |
---|
121 | } |
---|
122 | |
---|
123 | void GLGuiWidget::setFrontColor(const Color& frontColor, bool instantaniously) |
---|
124 | { |
---|
125 | if (instantaniously) |
---|
126 | { |
---|
127 | this->_frontColor = frontColor; |
---|
128 | if (this->_toFrontColor != NULL) |
---|
129 | { |
---|
130 | delete this->_toFrontColor; |
---|
131 | this->_toFrontColor = NULL; |
---|
132 | } |
---|
133 | } |
---|
134 | else if (!this->_toFrontColor) |
---|
135 | this->_toFrontColor = new Color(frontColor); |
---|
136 | else |
---|
137 | *this->_toFrontColor = frontColor; |
---|
138 | //this->_frontColor = frontColor; |
---|
139 | //this->updateFrontColor(); |
---|
140 | }; |
---|
141 | |
---|
142 | |
---|
143 | void GLGuiWidget::setBorderSize(float borderSize) |
---|
144 | { |
---|
145 | this->_borderLeft = borderSize; |
---|
146 | this->_borderRight = borderSize; |
---|
147 | this->_borderTop = borderSize; |
---|
148 | this->_borderBottom = borderSize; |
---|
149 | this->resize(); |
---|
150 | } |
---|
151 | |
---|
152 | void GLGuiWidget::setBorderLeft(float borderLeft) |
---|
153 | { |
---|
154 | this->_borderLeft = borderLeft; |
---|
155 | this->resize(); |
---|
156 | } |
---|
157 | void GLGuiWidget::setBorderRight(float borderRight) |
---|
158 | { |
---|
159 | this->_borderRight = borderRight; |
---|
160 | this->resize(); |
---|
161 | } |
---|
162 | void GLGuiWidget::setBorderTop(float borderTop) |
---|
163 | { |
---|
164 | this->_borderTop = borderTop; |
---|
165 | this->resize(); |
---|
166 | } |
---|
167 | void GLGuiWidget::setBorderBottom(float borderBottom) |
---|
168 | { |
---|
169 | this->_borderBottom = borderBottom; |
---|
170 | this->resize(); |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | void GLGuiWidget::resize() |
---|
176 | { |
---|
177 | this->backRect().setSize(this->getSize2D()); |
---|
178 | if (this->parent() != NULL) |
---|
179 | this->parent()->resize(); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | void GLGuiWidget::click(const Vector2D& pos) |
---|
184 | { |
---|
185 | assert (!this->_pushed); |
---|
186 | this->_pushed = true; |
---|
187 | |
---|
188 | this->clicking(pos); |
---|
189 | } |
---|
190 | |
---|
191 | void GLGuiWidget::release(const Vector2D& pos) |
---|
192 | { |
---|
193 | if (this->_pushed) |
---|
194 | { |
---|
195 | this->releasing(pos); |
---|
196 | this->_pushed = false; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | void GLGuiWidget::clicking(const Vector2D& pos) |
---|
202 | { |
---|
203 | this->setFrontColor(Color(0, 0, 1)); |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | void GLGuiWidget::releasing(const Vector2D& pos) |
---|
208 | { |
---|
209 | this->setFrontColor(Color(0,1,0)); |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | void GLGuiWidget::receivedFocus() |
---|
214 | { |
---|
215 | this->setFrontColor(Color(0, 1, 0)); |
---|
216 | } |
---|
217 | |
---|
218 | void GLGuiWidget::removedFocus() |
---|
219 | { |
---|
220 | this->setFrontColor(Color(1, 0, 0)); |
---|
221 | |
---|
222 | } |
---|
223 | |
---|
224 | void GLGuiWidget::destroyed() |
---|
225 | {} |
---|
226 | ; |
---|
227 | |
---|
228 | |
---|
229 | void GLGuiWidget::setWidgetSize(const Vector2D& size) |
---|
230 | { |
---|
231 | this->setSize2D(size); |
---|
232 | this->resize(); |
---|
233 | |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | void GLGuiWidget::setWidgetSize(float x, float y) |
---|
238 | { |
---|
239 | this->setWidgetSize(Vector2D(x, y)); |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | void GLGuiWidget::connect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver, Slot executor) |
---|
245 | { |
---|
246 | sender->connect(signal, receiver, executor); |
---|
247 | } |
---|
248 | |
---|
249 | void GLGuiWidget::connect(Signal& signal, BaseObject* receiver, Slot executor) |
---|
250 | { |
---|
251 | signal.push_back(SignalConnector(receiver, executor)); |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | void GLGuiWidget::show() |
---|
256 | { |
---|
257 | this->setVisibility(true); |
---|
258 | this->showing(); |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | |
---|
263 | void GLGuiWidget::hide() |
---|
264 | { |
---|
265 | this->setVisibility(false); |
---|
266 | this->hiding(); |
---|
267 | } |
---|
268 | |
---|
269 | void GLGuiWidget::tick(float dt) |
---|
270 | { |
---|
271 | if (this->_toFrontColor) |
---|
272 | { |
---|
273 | this->_frontColor.slerp(*_toFrontColor, dt*3.0); |
---|
274 | this->updateFrontColor(); |
---|
275 | if (this->_frontColor.dist(*_toFrontColor) < .1) |
---|
276 | { |
---|
277 | delete _toFrontColor; |
---|
278 | _toFrontColor = NULL; |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | /** |
---|
285 | * USE THIS FUNCTION ONLY FROM DERIVED CLASS |
---|
286 | */ |
---|
287 | void GLGuiWidget::draw() const |
---|
288 | { |
---|
289 | this->backMaterial().select(); |
---|
290 | this->drawRect(this->backRect()); |
---|
291 | this->backMaterial().unselect(); |
---|
292 | } |
---|
293 | |
---|
294 | } |
---|