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 | #include "rect2D.h" |
---|
11 | |
---|
12 | #include "material.h" |
---|
13 | |
---|
14 | #include "event.h" |
---|
15 | #include "signal_connector.h" |
---|
16 | |
---|
17 | #include "glincl.h" |
---|
18 | |
---|
19 | #include <vector> |
---|
20 | |
---|
21 | // FORWARD DECLARATION |
---|
22 | class Material; |
---|
23 | |
---|
24 | namespace OrxGui |
---|
25 | { |
---|
26 | |
---|
27 | class GLGuiCursor; |
---|
28 | |
---|
29 | //! if the Element should be visible by default. |
---|
30 | #define GLGUI_WIDGET_DEFAULT_VISIBLE false |
---|
31 | |
---|
32 | //! This is widget part of the openglGUI class |
---|
33 | /** |
---|
34 | * A widget is the main class of all the elements of th GUI. |
---|
35 | */ |
---|
36 | class GLGuiWidget : public Element2D |
---|
37 | { |
---|
38 | public: |
---|
39 | GLGuiWidget(GLGuiWidget* parent = NULL); |
---|
40 | virtual ~GLGuiWidget(); |
---|
41 | |
---|
42 | void show(); |
---|
43 | void hide(); |
---|
44 | |
---|
45 | void setParentWidget(GLGuiWidget* parent); |
---|
46 | GLGuiWidget* parent() const { return this->_parent; } |
---|
47 | |
---|
48 | /// FOCUS |
---|
49 | /** @brief gives focus to this widget */ |
---|
50 | void giveFocus(); |
---|
51 | void breakFocus(); |
---|
52 | /** @returns true if the widget is focusable */ |
---|
53 | bool focusable() const { return this->_focusable; }; |
---|
54 | /** @param focusable sets if the Widget should be focusable */ |
---|
55 | void setFocusable(bool focusable = true) { this->_focusable = focusable; }; |
---|
56 | /** @returns true if the position is inside of the Widget. @param position the position to check */ |
---|
57 | bool focusOverWidget(const Vector2D& position) const; |
---|
58 | /** @brief overloaded function, that returns true if the cursor is on top of the Widget */ |
---|
59 | bool focusOverWidget(const OrxGui::GLGuiCursor* const cursor) const; |
---|
60 | |
---|
61 | /** @returns the currently focused Widget (NULL if none is selected) */ |
---|
62 | static GLGuiWidget* focused() { return GLGuiWidget::_focused; }; |
---|
63 | |
---|
64 | |
---|
65 | /// CLICK |
---|
66 | void click(const Vector2D& pos); |
---|
67 | void release(const Vector2D& pos); |
---|
68 | bool clickable() const { return this->_clickable; }; |
---|
69 | void setClickable(bool clickable = true) { this->_clickable = clickable; }; |
---|
70 | |
---|
71 | static void connect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver, Slot executor); |
---|
72 | void connect(Signal& signal, BaseObject* receiver, Slot executor); |
---|
73 | |
---|
74 | void disconnect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver); |
---|
75 | |
---|
76 | |
---|
77 | /// MATERIAL (looks) |
---|
78 | Material& backMaterial() { return this->_backMat; }; |
---|
79 | const Material& backMaterial() const { return this->_backMat; }; |
---|
80 | Rect2D& backRect() { return this->_backRect; }; |
---|
81 | const Rect2D& backRect() const { return this->_backRect; }; |
---|
82 | |
---|
83 | Material& frontMaterial() { return this->_frontMat; }; |
---|
84 | const Material& frontMaterial() const { return this->_frontMat; }; |
---|
85 | Rect2D& frontRect() { return this->_frontRect; }; |
---|
86 | const Rect2D& frontRect() const { return this->_frontRect; }; |
---|
87 | |
---|
88 | float borderSize() const { return this->_borderSize; }; |
---|
89 | void setBorderSize(float borderSize); |
---|
90 | |
---|
91 | void setWidgetSize(const Vector2D& size); |
---|
92 | void setWidgetSize(float x, float y); |
---|
93 | |
---|
94 | |
---|
95 | void setBackgroundColor(float x, float y, float z) { this->backMaterial().setDiffuse(x,y,z); }; |
---|
96 | |
---|
97 | inline void drawRect(const Rect2D& rect) const { |
---|
98 | glBegin(GL_QUADS); |
---|
99 | glTexCoord2i(0,0); glVertex2d(rect.left(), rect.top()); |
---|
100 | glTexCoord2i(0,1); glVertex2d(rect.left(), rect.bottom()); |
---|
101 | glTexCoord2i(1,1); glVertex2d(rect.right(), rect.bottom()); |
---|
102 | glTexCoord2i(1,0); glVertex2d(rect.right(), rect.top()); |
---|
103 | glEnd(); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | virtual void update() {}; |
---|
108 | virtual void draw() const; |
---|
109 | |
---|
110 | /** @param the Event to process. @returns true if the Event has been consumed*/ |
---|
111 | virtual bool processEvent(const Event& event) { }; |
---|
112 | |
---|
113 | protected: |
---|
114 | |
---|
115 | /// LOOKS |
---|
116 | virtual void resize(); |
---|
117 | |
---|
118 | // if something was clickt on the GUI-widget. |
---|
119 | virtual void clicking(const Vector2D& pos); |
---|
120 | virtual void releasing(const Vector2D& pos); |
---|
121 | virtual void receivedFocus(); |
---|
122 | virtual void removedFocus(); |
---|
123 | |
---|
124 | virtual void destroyed(); |
---|
125 | |
---|
126 | |
---|
127 | inline void beginDraw() const { glPushMatrix(); glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); }; |
---|
128 | inline void endDraw() const { glPopMatrix(); }; |
---|
129 | |
---|
130 | private: |
---|
131 | void init(); |
---|
132 | |
---|
133 | |
---|
134 | private: |
---|
135 | GLGuiWidget* _parent; //!< The parent of this Widget. |
---|
136 | |
---|
137 | /// LOOKS |
---|
138 | Material _backMat; |
---|
139 | Rect2D _backRect; |
---|
140 | |
---|
141 | Material _frontMat; |
---|
142 | Rect2D _frontRect; |
---|
143 | |
---|
144 | float _borderSize; |
---|
145 | |
---|
146 | /// EVENTS |
---|
147 | bool _focusable; //!< If this widget can receive focus. |
---|
148 | bool _clickable; //!< if this widget can be clicked upon. |
---|
149 | |
---|
150 | bool _pushed; |
---|
151 | |
---|
152 | |
---|
153 | static GLGuiWidget* _selected; //!< The currently selected Widget. |
---|
154 | static GLGuiWidget* _focused; //!< The currently Focused Widget. |
---|
155 | |
---|
156 | static GLGuiWidget* _inputGrabber; //!< The Widget that grabs input. |
---|
157 | }; |
---|
158 | } |
---|
159 | #endif /* _GLGUI_WIDGET_H */ |
---|