1 | /*! |
---|
2 | * @file glgui_widget.h |
---|
3 | * The gl_widget of the openglGUI |
---|
4 | */ |
---|
5 | |
---|
6 | #ifndef _GLGUI_WIDGET_H |
---|
7 | #define _GLGUI_WIDGET_H |
---|
8 | |
---|
9 | #include "element_2d.h" |
---|
10 | |
---|
11 | #include "glgui_defs.h" |
---|
12 | |
---|
13 | #include "material.h" |
---|
14 | #include "rect2D.h" |
---|
15 | |
---|
16 | #include "event.h" |
---|
17 | #include "signal_connector.h" |
---|
18 | |
---|
19 | class Font; |
---|
20 | |
---|
21 | namespace OrxGui |
---|
22 | { |
---|
23 | |
---|
24 | class GLGuiCursor; |
---|
25 | |
---|
26 | |
---|
27 | //! This is widget part of the openglGUI class |
---|
28 | /** |
---|
29 | * A widget is the main class of all the elements of th GUI. |
---|
30 | */ |
---|
31 | class GLGuiWidget : public Element2D |
---|
32 | { |
---|
33 | public: |
---|
34 | GLGuiWidget(GLGuiWidget* parent = NULL); |
---|
35 | virtual ~GLGuiWidget(); |
---|
36 | |
---|
37 | void show(); |
---|
38 | void hide(); |
---|
39 | |
---|
40 | void setParentWidget(GLGuiWidget* parent); |
---|
41 | GLGuiWidget* parent() const { return this->_parent; } |
---|
42 | |
---|
43 | /// FOCUS |
---|
44 | /** @brief gives focus to this widget */ |
---|
45 | void giveFocus(); |
---|
46 | void breakFocus(); |
---|
47 | /** @returns true if the widget is focusable */ |
---|
48 | bool focusable() const { return this->_focusable; }; |
---|
49 | /** @param focusable sets if the Widget should be focusable */ |
---|
50 | void setFocusable(bool focusable = true) { this->_focusable = focusable; }; |
---|
51 | /** @returns true if the position is inside of the Widget. @param position the position to check */ |
---|
52 | bool focusOverWidget(const Vector2D& position) const; |
---|
53 | /** @brief overloaded function, that returns true if the cursor is on top of the Widget */ |
---|
54 | bool focusOverWidget(const OrxGui::GLGuiCursor* const cursor) const; |
---|
55 | |
---|
56 | /** @returns the currently focused Widget (NULL if none is selected) */ |
---|
57 | static GLGuiWidget* focused() { return GLGuiWidget::_focused; }; |
---|
58 | |
---|
59 | |
---|
60 | /// CLICK |
---|
61 | void click(const Vector2D& pos); |
---|
62 | void release(const Vector2D& pos); |
---|
63 | bool clickable() const { return this->_clickable; }; |
---|
64 | void setClickable(bool clickable = true) { this->_clickable = clickable; }; |
---|
65 | |
---|
66 | static void connect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver, Slot executor); |
---|
67 | void connect(Signal& signal, BaseObject* receiver, Slot executor); |
---|
68 | |
---|
69 | void disconnect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver); |
---|
70 | |
---|
71 | OrxGui::State state() const { return this->_state; }; |
---|
72 | |
---|
73 | Rect2D& backRect() { return this->_backRect; }; |
---|
74 | const Rect2D& backRect() const { return this->_backRect; }; |
---|
75 | |
---|
76 | void setFrontColor(const Color& frontColor, bool instantaniously = false); |
---|
77 | |
---|
78 | void setWidgetSize(const Vector2D& size); |
---|
79 | void setWidgetSize(float x, float y); |
---|
80 | |
---|
81 | void animateBack(); |
---|
82 | |
---|
83 | /// STYLE |
---|
84 | //////////////////////////////// |
---|
85 | /// Retrieve Current Values. /// |
---|
86 | //////////////////////////////// |
---|
87 | /** @returns current left borderWidth */ |
---|
88 | inline float borderLeft() const { return _currentStyle._borderLeft; } |
---|
89 | /** @returns current right borderWidth */ |
---|
90 | inline float borderRight() const { return _currentStyle._borderRight; } |
---|
91 | /** @returns current top borderWidth */ |
---|
92 | inline float borderTop() const { return _currentStyle._borderTop; } |
---|
93 | /** @returns burrent bottom borderWidth */ |
---|
94 | inline float borderBottom() const { return _currentStyle._borderBottom; } |
---|
95 | |
---|
96 | |
---|
97 | /** @returns current textSize */ |
---|
98 | inline float textSize() const { return _currentStyle._textSize; } |
---|
99 | /** @returns the Background Color */ |
---|
100 | inline const Color& backgroundColor() const { return _currentStyle._background.diffuseColor(); } |
---|
101 | /** @returns the current Background Material. */ |
---|
102 | inline const Material& background() const { return _currentStyle._background; } |
---|
103 | /** @returns the current background Texture. */ |
---|
104 | inline const Texture& backgroundTexture() const { return _currentStyle._background.diffuseTexture(); } |
---|
105 | /** @returns the current foreground Color */ |
---|
106 | inline const Color& foregroundColor() const { return _currentStyle._foreground.diffuseColor(); } |
---|
107 | /** @returns the current ForeGroung Material. */ |
---|
108 | inline const Material& foreground() const { return _currentStyle._foreground; } |
---|
109 | |
---|
110 | |
---|
111 | /** @returns FeaturePosition */ |
---|
112 | inline FeaturePosition featurePosition() const { return _featurePosition; } |
---|
113 | /** @returns the font */ |
---|
114 | inline const Font* const font() const { return _font; } |
---|
115 | /** @returns true if the Element is Animated */ |
---|
116 | inline bool animating() const { return _animating; } |
---|
117 | /** @returns true if State-Changes are animated */ |
---|
118 | inline bool animatedStateChanges() const { return _animatedStateChanges; } |
---|
119 | |
---|
120 | |
---|
121 | /////////////////////////////////////////////////////////////// |
---|
122 | /// Retrieve Values for the Saved Values inside the States. /// |
---|
123 | /////////////////////////////////////////////////////////////// |
---|
124 | /** @returns left borderWidth @param state the State to retrieve from */ |
---|
125 | inline float borderLeft(OrxGui::State state) const { return _style[state]._borderLeft; } |
---|
126 | /** @returns right borderWidth @param state the State to retrieve from */ |
---|
127 | inline float borderRight(OrxGui::State state) const { return _style[state]._borderRight; } |
---|
128 | /** @returns top borderWidth @param state the State to retrieve from */ |
---|
129 | inline float borderTop(OrxGui::State state) const { return _style[state]._borderTop; } |
---|
130 | /** @returns bottom borderWidth @param state the State to retrieve from */ |
---|
131 | inline float borderBottom(OrxGui::State state) const { return _style[state]._borderBottom; } |
---|
132 | |
---|
133 | /** @returns textSize @param state the State to retrieve from */ |
---|
134 | inline float textSize(OrxGui::State state) const { return _style[state]._textSize; } |
---|
135 | /** @returns the Background Color @param state the State to retrieve from */ |
---|
136 | inline const Color& backgroundColor(OrxGui::State state) const { return _style[state]._background.diffuseColor(); } |
---|
137 | /** @returns the Background Material. @param state the state to retrieve from */ |
---|
138 | inline const Material& background(OrxGui::State state) const { return _style[state]._background; } |
---|
139 | /** @returns background Texture. @param state the State to retrieve from */ |
---|
140 | inline const Texture& backgroundTexture(OrxGui::State state) const { return _style[state]._background.diffuseTexture(); } |
---|
141 | /** @returns the foreground Color @param state the State to retrieve from */ |
---|
142 | inline const Color& foregroundColor(OrxGui::State state) const { return _style[state]._foreground.diffuseColor(); } |
---|
143 | /** @returns the ForeGroung Material. @param state the state to retrieve from */ |
---|
144 | inline const Material& foreground(OrxGui::State state) const { return _style[state]._foreground; } |
---|
145 | |
---|
146 | |
---|
147 | /// SETUP |
---|
148 | void resetStyle(); |
---|
149 | void loadParams(const TiXmlElement* root); |
---|
150 | |
---|
151 | void setBorderLeft(float value); |
---|
152 | void setBorderLeft(float value, OrxGui::State state); |
---|
153 | void setBorderLeftS(float value, const std::string& stateName); |
---|
154 | |
---|
155 | void setBorderRight(float value); |
---|
156 | void setBorderRight(float value, OrxGui::State state); |
---|
157 | void setBorderRightS(float value, const std::string& stateName); |
---|
158 | |
---|
159 | void setBorderTop(float value); |
---|
160 | void setBorderTop(float value, OrxGui::State state); |
---|
161 | void setBorderTopS(float value, const std::string& stateName); |
---|
162 | |
---|
163 | void setBorderBottom(float value); |
---|
164 | void setBorderBottom(float value, OrxGui::State state); |
---|
165 | void setBorderBottomS(float value, const std::string& stateName); |
---|
166 | |
---|
167 | void setTextSize(float value); |
---|
168 | void setTextSize(float value, OrxGui::State state); |
---|
169 | void setTextSizeS(float value, const std::string& stateName); |
---|
170 | |
---|
171 | void setBackgroundColor(const Color& color); |
---|
172 | void setBackgroundColor(const Color& color, OrxGui::State state); |
---|
173 | void setBackgroundColorS(float r, float g, float b, float a, const std::string& stateName); |
---|
174 | |
---|
175 | void setBackgroundTexture(const Texture& texture); |
---|
176 | void setBackgroundTexture(const std::string& textureName); |
---|
177 | void setBackgroundTexture(const Texture& texture, OrxGui::State state); |
---|
178 | void setBackgroundTexture(const std::string& textureName, const std::string& stateName); |
---|
179 | |
---|
180 | void setForegroundColor(const Color& color); |
---|
181 | void setForegroundColor(const Color& color, OrxGui::State state); |
---|
182 | void setForegroundColorS(float r, float g, float b, float a, const std::string& stateName); |
---|
183 | |
---|
184 | void loadBackgroundMaterial(const Material& material); |
---|
185 | void loadBackgroundMaterial(const Material& material, OrxGui::State state); |
---|
186 | void loadBackgroundMaterial(const TiXmlElement* element); |
---|
187 | void loadBackgroundMaterial(const TiXmlElement* element, OrxGui::State state); |
---|
188 | void loadBackgroundMaterialS(const TiXmlElement* element, const std::string& stateName); |
---|
189 | |
---|
190 | void loadForegroundMaterial(const Material& material); |
---|
191 | void loadForegroundMaterial(const Material& material, OrxGui::State state); |
---|
192 | void loadForegroundMaterial(const TiXmlElement* element, OrxGui::State state); |
---|
193 | void loadForegroundMaterialS(const TiXmlElement* element, const std::string& stateName); |
---|
194 | |
---|
195 | void setFeaturePosition(FeaturePosition featurePosition); |
---|
196 | void setFeaturePositionS(const std::string& featurePosition); |
---|
197 | |
---|
198 | void setFont(Font* font); |
---|
199 | void setFont(const std::string& fontName); |
---|
200 | |
---|
201 | void setAnimatedStateChanges(bool animated); |
---|
202 | void switchState(OrxGui::State state); |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | inline void drawRect(const Rect2D& rect) const |
---|
207 | { |
---|
208 | glBegin(GL_QUADS); |
---|
209 | glTexCoord2i(0,0); glVertex2d(rect.left(), rect.top()); |
---|
210 | glTexCoord2i(0,1); glVertex2d(rect.left(), rect.bottom()); |
---|
211 | glTexCoord2i(1,1); glVertex2d(rect.right(), rect.bottom()); |
---|
212 | glTexCoord2i(1,0); glVertex2d(rect.right(), rect.top()); |
---|
213 | glEnd(); |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | virtual void update() {}; |
---|
218 | virtual void tick(float dt); |
---|
219 | virtual void draw() const; |
---|
220 | |
---|
221 | /** @param the Event to process. @returns true if the Event has been consumed*/ |
---|
222 | virtual bool processEvent(const Event& event) { return false; }; |
---|
223 | |
---|
224 | bool getState(const std::string& stateName, OrxGui::State* state); |
---|
225 | |
---|
226 | protected: |
---|
227 | /// LOOKS |
---|
228 | virtual void resize(); |
---|
229 | |
---|
230 | virtual void hiding() {}; |
---|
231 | virtual void showing() {}; |
---|
232 | virtual void updateFrontColor() {}; |
---|
233 | |
---|
234 | inline void beginDraw() const { glPushMatrix(); glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); }; |
---|
235 | inline void endDraw() const { glPopMatrix(); }; |
---|
236 | |
---|
237 | /// EVENTS |
---|
238 | // if something was clickt on the GUI-widget. |
---|
239 | virtual void clicking(const Vector2D& pos); |
---|
240 | virtual void releasing(const Vector2D& pos); |
---|
241 | virtual void receivedFocus(); |
---|
242 | virtual void removedFocus(); |
---|
243 | |
---|
244 | virtual void destroyed(); |
---|
245 | |
---|
246 | virtual void debug() const; |
---|
247 | |
---|
248 | private: |
---|
249 | void init(); |
---|
250 | |
---|
251 | private: |
---|
252 | static GLGuiWidget* _selected; //!< The currently selected Widget. |
---|
253 | static GLGuiWidget* _focused; //!< The currently Focused Widget. |
---|
254 | static GLGuiWidget* _inputGrabber; //!< The Widget that grabs input. |
---|
255 | |
---|
256 | |
---|
257 | /// WIDGET |
---|
258 | GLGuiWidget* _parent; //!< The parent of this Widget. |
---|
259 | |
---|
260 | /// LOOKS |
---|
261 | Rect2D _backRect; |
---|
262 | Vector2D _minSize; |
---|
263 | |
---|
264 | |
---|
265 | /// EVENTS |
---|
266 | OrxGui::State _state; |
---|
267 | |
---|
268 | bool _focusable; //!< If this widget can receive focus. |
---|
269 | bool _clickable; //!< if this widget can be clicked upon. |
---|
270 | |
---|
271 | bool _pushed; |
---|
272 | |
---|
273 | |
---|
274 | |
---|
275 | /// STYLE - Variables. |
---|
276 | typedef struct |
---|
277 | { |
---|
278 | float _borderLeft; //!< The Distance to the left Border of the widget, before any internal Element starts. |
---|
279 | float _borderRight; //!< The Distance to the right Border of the widget, before any internal Element starts. |
---|
280 | float _borderTop; //!< The Distance to the top Border of the widget, before any internal Element starts |
---|
281 | float _borderBottom; //!< The Distance to the bottom Border of the widget, before any internal Element starts |
---|
282 | |
---|
283 | float _textSize; //!< The TextSize of the Widget. |
---|
284 | |
---|
285 | Material _background; //!< The Background Material of the Widget. |
---|
286 | |
---|
287 | Material _foreground; //!< The foreground Material of the Widget. |
---|
288 | } |
---|
289 | StatedStyle; |
---|
290 | |
---|
291 | |
---|
292 | StatedStyle _style[GLGUI_STATE_COUNT]; //!< Styles configured for different States |
---|
293 | |
---|
294 | FeaturePosition _featurePosition; //!< The Position a Feature will be layed at (checkbox(box), slider(text),...) |
---|
295 | Font* _font; //!< The Font used in the current Widget. |
---|
296 | |
---|
297 | |
---|
298 | /// ANIMATION STUFF: |
---|
299 | bool _animatedStateChanges; //!< If the Transitions between States are Animated automatically. |
---|
300 | |
---|
301 | bool _animating; //!< If the Widget is animated at the moment (Texture might be an AnimatedTexture.) |
---|
302 | float _animationCycle; |
---|
303 | float _animationDuration; |
---|
304 | StatedStyle _currentStyle; |
---|
305 | |
---|
306 | }; |
---|
307 | } |
---|
308 | #endif /* _GLGUI_WIDGET_H */ |
---|