Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/gui/gl/glgui_widget.h @ 8671

Last change on this file since 8671 was 8667, checked in by bensch, 18 years ago

widgets are selectable now

File size: 13.7 KB
Line 
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
19class Font;
20
21namespace 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 mouse - focus to this widget */
45    void giveMouseFocus();
46    void breakMouseFocus();
47
48    /** @returns true if the widget is focusable */
49    bool focusable() const { return this->_focusable; };
50    /** @param focusable sets if the Widget should be focusable */
51    void setFocusable(bool focusable = true) { this->_focusable = focusable; };
52    /** @returns true if the position is inside of the Widget. @param position the position to check */
53    bool focusOverWidget(const Vector2D& position) const;
54    /** @brief overloaded function, that returns true if the cursor is on top of the Widget */
55    bool focusOverWidget(const OrxGui::GLGuiCursor* const cursor) const;
56
57    /** @returns the currently mouse - focused Widget (NULL if none is focused). */
58    static GLGuiWidget* mouseFocused() { return GLGuiWidget::_mouseFocused; };
59
60    /// SELECT
61    void select();
62    void unselect();
63    /** @returns true if the Widget is selectable */
64    bool selectable() const { return this->_selectable; }
65    /** @param selectable true if the widget should be selectable */
66    void setSelectable(bool selectable) { this->_selectable = selectable; }
67
68    /** @returns the currently Selected Widget (NULL if none is selected). */
69    static GLGuiWidget* selected() { return GLGuiWidget::_selected; };
70
71    /// CLICK
72    bool pushed() { return _pushed; };
73    void click(const Vector2D& pos);
74    void release(const Vector2D& pos);
75    bool clickable() const { return this->_clickable; };
76    void setClickable(bool clickable = true) { this->_clickable = clickable; };
77
78    static void connect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver, Slot executor);
79    void connect(Signal& signal, BaseObject* receiver, Slot executor);
80
81    void disconnect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver);
82
83    OrxGui::State state() const { return this->_state; };
84
85    Rect2D& backRect() { return this->_backRect; };
86    const Rect2D& backRect() const { return this->_backRect; };
87
88    void setFrontColor(const Color& frontColor, bool instantaniously = false);
89
90    void setWidgetSize(const Vector2D& size);
91    void setWidgetSize(float x, float y);
92
93    void animateBack();
94
95    ///////////////////////////////////////////////////////////////////////////////////
96    /// STYLE /////////////////////////////////////////////////////////////////////////
97    ////////////////////////////////
98    /// Retrieve Current Values. ///
99    ////////////////////////////////
100    /** @returns current left borderWidth */
101    inline float borderLeft() const { return _currentStyle._borderLeft; }
102    /** @returns current right borderWidth */
103    inline float borderRight() const { return _currentStyle._borderRight; }
104    /** @returns current top borderWidth */
105    inline float borderTop() const { return _currentStyle._borderTop; }
106    /** @returns burrent bottom borderWidth */
107    inline float borderBottom() const { return _currentStyle._borderBottom; }
108
109
110    /** @returns current textSize */
111    inline float textSize() const { return _currentStyle._textSize; }
112    /** @returns the Background Color */
113    inline const Color& backgroundColor() const { return _currentStyle._background.diffuseColor(); }
114    /** @returns the current Background Material. */
115    inline const Material& background() const { return _currentStyle._background; }
116    /** @returns the current background Texture. */
117    inline const Texture& backgroundTexture() const { return _currentStyle._background.diffuseTexture(); }
118    /** @returns the current foreground Color */
119    inline const Color& foregroundColor() const { return _currentStyle._foreground.diffuseColor(); }
120    /** @returns the current ForeGroung Material. */
121    inline const Material& foreground() const { return _currentStyle._foreground; }
122
123
124    /** @returns FeaturePosition */
125    inline FeaturePosition featurePosition() const { return _featurePosition; }
126    /** @returns the font */
127    inline const Font* const font() const { return _font; }
128    /** @returns true if the Element is Animated */
129    inline bool animating() const { return _animating; }
130    /** @returns true if State-Changes are animated */
131    inline bool animatedStateChanges() const { return _animatedStateChanges; }
132
133
134    ///////////////////////////////////////////////////////////////
135    /// Retrieve Values for the Saved Values inside the States. ///
136    ///////////////////////////////////////////////////////////////
137    /** @returns left borderWidth @param state the State to retrieve from */
138    inline float borderLeft(OrxGui::State state) const { return _style[state]._borderLeft; }
139    /** @returns right borderWidth @param state the State to retrieve from */
140    inline float borderRight(OrxGui::State state) const { return _style[state]._borderRight; }
141    /** @returns top borderWidth @param state the State to retrieve from */
142    inline float borderTop(OrxGui::State state) const { return _style[state]._borderTop; }
143    /** @returns bottom borderWidth @param state the State to retrieve from */
144    inline float borderBottom(OrxGui::State state) const { return _style[state]._borderBottom; }
145
146    /** @returns textSize @param state the State to retrieve from */
147    inline float textSize(OrxGui::State state) const { return _style[state]._textSize; }
148    /** @returns the Background Color @param state the State to retrieve from */
149    inline const Color& backgroundColor(OrxGui::State state) const { return _style[state]._background.diffuseColor(); }
150    /** @returns the Background Material. @param state the state to retrieve from */
151    inline const Material& background(OrxGui::State state) const { return _style[state]._background; }
152    /** @returns background Texture. @param state the State to retrieve from */
153    inline const Texture& backgroundTexture(OrxGui::State state) const { return _style[state]._background.diffuseTexture(); }
154    /** @returns the foreground Color @param state the State to retrieve from */
155    inline const Color& foregroundColor(OrxGui::State state) const { return _style[state]._foreground.diffuseColor(); }
156    /** @returns the ForeGroung Material. @param state the state to retrieve from */
157    inline const Material& foreground(OrxGui::State state) const { return _style[state]._foreground; }
158
159
160    /// SETUP
161    void resetStyle();
162    void loadParams(const TiXmlElement* root);
163
164    void setBorderLeft(float value);
165    void setBorderLeft(float value, OrxGui::State state);
166    void setBorderLeftS(float value, const std::string& stateName);
167
168    void setBorderRight(float value);
169    void setBorderRight(float value, OrxGui::State state);
170    void setBorderRightS(float value, const std::string& stateName);
171
172    void setBorderTop(float value);
173    void setBorderTop(float value, OrxGui::State state);
174    void setBorderTopS(float value, const std::string& stateName);
175
176    void setBorderBottom(float value);
177    void setBorderBottom(float value, OrxGui::State state);
178    void setBorderBottomS(float value, const std::string& stateName);
179
180    void setTextSize(float value);
181    void setTextSize(float value, OrxGui::State state);
182    void setTextSizeS(float value, const std::string& stateName);
183
184    void setBackgroundColor(const Color& color);
185    void setBackgroundColor(const Color& color, OrxGui::State state);
186    void setBackgroundColorS(float r, float g, float b, float a, const std::string& stateName);
187
188    void setBackgroundTexture(const Texture& texture);
189    void setBackgroundTexture(const std::string& textureName);
190    void setBackgroundTexture(const Texture& texture, OrxGui::State state);
191    void setBackgroundTexture(const std::string& textureName, const std::string& stateName);
192
193    void setForegroundColor(const Color& color);
194    void setForegroundColor(const Color& color, OrxGui::State state);
195    void setForegroundColorS(float r, float g, float b, float a, const std::string& stateName);
196
197    void loadBackgroundMaterial(const Material& material);
198    void loadBackgroundMaterial(const Material& material, OrxGui::State state);
199    void loadBackgroundMaterial(const TiXmlElement* element);
200    void loadBackgroundMaterial(const TiXmlElement* element, OrxGui::State state);
201    void loadBackgroundMaterialS(const TiXmlElement* element, const std::string& stateName);
202
203    void loadForegroundMaterial(const Material& material);
204    void loadForegroundMaterial(const Material& material, OrxGui::State state);
205    void loadForegroundMaterial(const TiXmlElement* element, OrxGui::State state);
206    void loadForegroundMaterialS(const TiXmlElement* element, const std::string& stateName);
207
208    void setFeaturePosition(FeaturePosition featurePosition);
209    void setFeaturePositionS(const std::string& featurePosition);
210
211    void setFont(Font* font);
212    void setFont(const std::string& fontName);
213
214    void setAnimatedStateChanges(bool animated);
215    void switchState(OrxGui::State state);
216    ///////////////////////////////////////////////////////////////////////////////////
217
218
219    inline void drawRect(const Rect2D& rect) const
220    {
221      glBegin(GL_QUADS);
222      glTexCoord2i(0,0); glVertex2d(rect.left(), rect.top());
223      glTexCoord2i(0,1); glVertex2d(rect.left(), rect.bottom());
224      glTexCoord2i(1,1); glVertex2d(rect.right(), rect.bottom());
225      glTexCoord2i(1,0); glVertex2d(rect.right(), rect.top());
226      glEnd();
227    }
228
229
230    virtual void update() {};
231    virtual void tick(float dt);
232    virtual void draw() const;
233
234    /** @param the Event to process. @returns true if the Event has been consumed*/
235    virtual bool processEvent(const Event& event) { return false; };
236
237    bool getState(const std::string& stateName, OrxGui::State* state);
238
239  protected:
240    /// RENDERING
241    inline void beginDraw() const { glPushMatrix(); glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); };
242    inline void endDraw() const { glPopMatrix(); };
243
244
245    /// LOOKS
246    virtual void resize();
247
248    virtual void hiding() {};
249    virtual void showing() {};
250
251    virtual void updateFrontColor() {};
252
253
254    /// EVENTS
255    // mouse clicking
256    virtual void clicking(const Vector2D& pos);
257    virtual void releasing(const Vector2D& pos, bool focused);
258    // mouse focusing
259    virtual void receivedFocus();
260    virtual void removedFocus();
261    // selecting either with the mouse by clicking, or by the keybord traversing to it.
262    virtual void selecting();
263    virtual void unselecting();
264    // destroying the Widget.
265    virtual void destroying();
266
267
268    virtual void debug(unsigned int level) const;
269
270  private:
271    void init();
272
273  private:
274    static GLGuiWidget*            _selected;         //!< The currently selected Widget.
275    static GLGuiWidget*            _mouseFocused;     //!< The currently Focused Widget (mouse-focus).
276    static GLGuiWidget*            _inputGrabber;     //!< The Widget that grabs input (keyboard-focus).
277
278
279    /// WIDGET
280    GLGuiWidget*                   _parent;           //!< The parent of this Widget.
281
282    /// LOOKS
283    Rect2D                         _backRect;
284    Vector2D                       _minSize;
285
286
287    /// EVENTS
288    OrxGui::State                  _state;
289    bool                           _pushed;
290
291    bool                           _focusable;        //!< If this widget can receive focus.
292    bool                           _clickable;        //!< if this widget can be clicked upon.
293    bool                           _selectable;       //!< If this widget can be selected.
294
295
296
297
298    /// STYLE - Variables.
299    typedef struct
300    {
301      float                        _borderLeft;           //!< The Distance to the left Border of the widget, before any internal Element starts.
302      float                        _borderRight;          //!< The Distance to the right Border of the widget, before any internal Element starts.
303      float                        _borderTop;            //!< The Distance to the top Border of the widget, before any internal Element starts
304      float                        _borderBottom;         //!< The Distance to the bottom Border of the widget, before any internal Element starts
305
306      float                        _textSize;             //!< The TextSize of the Widget.
307
308      Material                     _background;           //!< The Background Material of the Widget.
309
310      Material                     _foreground;           //!< The foreground Material of the Widget.
311    }
312    StatedStyle;
313
314
315    StatedStyle                    _style[GLGUI_STATE_COUNT]; //!< Styles configured for different States
316
317    FeaturePosition                _featurePosition;      //!< The Position a Feature will be layed at (checkbox(box), slider(text),...)
318    Font*                          _font;                 //!< The Font used in the current Widget.
319
320
321    /// ANIMATION STUFF:
322    bool                           _animatedStateChanges; //!< If the Transitions between States are Animated automatically.
323
324    bool                           _animating;            //!< If the Widget is animated at the moment (Texture might be an AnimatedTexture.)
325    float                          _animationCycle;
326    float                          _animationDuration;
327    StatedStyle                    _currentStyle;
328
329  };
330}
331#endif /* _GLGUI_WIDGET_H */
Note: See TracBrowser for help on using the repository browser.