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 | if (parent != NULL) |
---|
37 | parent->addChild2D(this); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | /** |
---|
42 | * @brief standard deconstructor |
---|
43 | */ |
---|
44 | GLGuiWidget::~GLGuiWidget() |
---|
45 | { |
---|
46 | if (this == GLGuiWidget::_focused) |
---|
47 | GLGuiWidget::_focused = NULL; |
---|
48 | } |
---|
49 | |
---|
50 | GLGuiWidget* GLGuiWidget::_selected = NULL; |
---|
51 | GLGuiWidget* GLGuiWidget::_focused = NULL; |
---|
52 | GLGuiWidget* GLGuiWidget::_inputGrabber = NULL; |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | * initializes the GUI-element |
---|
58 | */ |
---|
59 | void GLGuiWidget::init() |
---|
60 | { |
---|
61 | this->setClassID(CL_GLGUI_WIDGET, "GLGuiWidget"); |
---|
62 | |
---|
63 | this->_focusable = false; |
---|
64 | this->_clickable = false; |
---|
65 | this->_pushed = false; |
---|
66 | |
---|
67 | this->setVisibility(GLGUI_WIDGET_DEFAULT_VISIBLE); |
---|
68 | |
---|
69 | this->_backMat.setDiffuse(1.0, 1.0, 1.0); |
---|
70 | this->_frontMat.setDiffuse(1.0, 0.0, 0.0); |
---|
71 | this->_borderSize = 1.0; |
---|
72 | |
---|
73 | this->widgetSignals.resize(SignalCount, SignalConnector()); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /** @brief gives focus to this widget */ |
---|
78 | void GLGuiWidget::giveFocus() |
---|
79 | { |
---|
80 | if (GLGuiWidget::focused() != NULL) |
---|
81 | GLGuiWidget::focused()->breakFocus(); |
---|
82 | GLGuiWidget::_focused = this; |
---|
83 | this->receivedFocus(); |
---|
84 | }; |
---|
85 | |
---|
86 | void GLGuiWidget::breakFocus() |
---|
87 | { |
---|
88 | if (GLGuiWidget::_focused == this) |
---|
89 | { |
---|
90 | GLGuiWidget::_focused = NULL; |
---|
91 | this->_pushed = false; |
---|
92 | this->removedFocus(); |
---|
93 | } |
---|
94 | }; |
---|
95 | |
---|
96 | |
---|
97 | bool GLGuiWidget::focusOverWidget(const Vector2D& position) const |
---|
98 | { |
---|
99 | return (this->getAbsCoor2D().x < position.x && this->getAbsCoor2D().x + this->getSizeX2D() > position.x && |
---|
100 | this->getAbsCoor2D().y < position.y && this->getAbsCoor2D().y + this->getSizeY2D() > position.y); |
---|
101 | } |
---|
102 | |
---|
103 | bool GLGuiWidget::focusOverWidget(const GLGuiCursor* const cursor) const |
---|
104 | { |
---|
105 | return this->focusOverWidget(cursor->getAbsCoor2D()); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | void GLGuiWidget::setBorderSize(float borderSize) |
---|
110 | { |
---|
111 | this->_borderSize = borderSize; |
---|
112 | this->resize(); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | void GLGuiWidget::resize() |
---|
117 | { |
---|
118 | this->backRect().setSize(this->getSize2D()); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | void GLGuiWidget::click(const Vector2D& pos) |
---|
123 | { |
---|
124 | assert (!this->_pushed); |
---|
125 | this->widgetSignals[Signal_click]("none"); |
---|
126 | this->_pushed = true; |
---|
127 | |
---|
128 | this->clicked(pos); |
---|
129 | } |
---|
130 | |
---|
131 | void GLGuiWidget::release(const Vector2D& pos) |
---|
132 | { |
---|
133 | if (this->_pushed) |
---|
134 | { |
---|
135 | this->widgetSignals[Signal_release]("none"); |
---|
136 | |
---|
137 | this->released(pos); |
---|
138 | this->_pushed = false; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | void GLGuiWidget::clicked(const Vector2D& pos) |
---|
144 | { |
---|
145 | this->frontMaterial().setDiffuse(0, 0, 1); |
---|
146 | |
---|
147 | } |
---|
148 | |
---|
149 | void GLGuiWidget::released(const Vector2D& pos) |
---|
150 | { |
---|
151 | this->frontMaterial().setDiffuse(0,1,0); |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | void GLGuiWidget::receivedFocus() |
---|
156 | { |
---|
157 | this->frontMaterial().setDiffuse(0, 1, 0); |
---|
158 | } |
---|
159 | |
---|
160 | void GLGuiWidget::removedFocus() |
---|
161 | { |
---|
162 | this->frontMaterial().setDiffuse(1, 0, 0); |
---|
163 | |
---|
164 | } |
---|
165 | |
---|
166 | void GLGuiWidget::destroyed() |
---|
167 | {} |
---|
168 | ; |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | void GLGuiWidget::connect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver, Slot executor) |
---|
174 | { |
---|
175 | sender->connect(signal, receiver, executor); |
---|
176 | } |
---|
177 | |
---|
178 | void GLGuiWidget::connect(Signal& signal, BaseObject* receiver, Slot executor) |
---|
179 | { |
---|
180 | signal.push_back(SignalConnector(receiver, executor)); |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | /** |
---|
185 | * @brief connects a Signal to the Gui-Elements' Event. |
---|
186 | * @param sinalType the Type of Signal to set. @see GLGuiSignalType |
---|
187 | * @param signal the name of the Signal |
---|
188 | */ |
---|
189 | void GLGuiWidget::connectSignal(SignalType signalType, BaseObject* object, const Executor* signal) |
---|
190 | { |
---|
191 | if (signalType >= this->widgetSignals.size()) |
---|
192 | return; |
---|
193 | |
---|
194 | // if (this->widgetSignals[signalType] != NULL) |
---|
195 | // PRINTF(2)("Already connected a Signal to '%s::%s' type %s... overwriting\n", this->getClassName(), this->getName(), "TEST"); |
---|
196 | |
---|
197 | this->widgetSignals[signalType] = SignalConnector(object, signal); |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * @brief removes a Signal from a Gui-ELements' Event |
---|
202 | * @param signalType the type of Signal to remove. |
---|
203 | */ |
---|
204 | void GLGuiWidget::disconnectSignal(SignalType signalType) |
---|
205 | { |
---|
206 | if (signalType >= this->widgetSignals.size()) |
---|
207 | return; |
---|
208 | |
---|
209 | this->widgetSignals[signalType] = SignalConnector(); |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | void GLGuiWidget::show() |
---|
214 | { |
---|
215 | this->setVisibility(true); |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | void GLGuiWidget::hide() |
---|
220 | { |
---|
221 | this->setVisibility(false); |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | /** |
---|
226 | * USE THIS FUNCTION ONLY FROM DERIVED CLASS |
---|
227 | */ |
---|
228 | void GLGuiWidget::draw() const |
---|
229 | { |
---|
230 | this->backMaterial().select(); |
---|
231 | this->drawRect(this->backRect()); |
---|
232 | } |
---|
233 | |
---|
234 | } |
---|