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