[4838] | 1 | /*! |
---|
[5391] | 2 | * @file glgui_widget.h |
---|
| 3 | * The gl_widget of the openglGUI |
---|
| 4 | */ |
---|
[1853] | 5 | |
---|
[5362] | 6 | #ifndef _GLGUI_WIDGET_H |
---|
| 7 | #define _GLGUI_WIDGET_H |
---|
[1853] | 8 | |
---|
[5362] | 9 | #include "element_2d.h" |
---|
[7919] | 10 | #include "rect2D.h" |
---|
| 11 | |
---|
[6295] | 12 | #include "material.h" |
---|
| 13 | |
---|
[7919] | 14 | #include "event.h" |
---|
[7779] | 15 | #include "signal_connector.h" |
---|
[5690] | 16 | |
---|
[7919] | 17 | #include "glincl.h" |
---|
| 18 | |
---|
| 19 | #include <vector> |
---|
| 20 | |
---|
[4838] | 21 | // FORWARD DECLARATION |
---|
[5365] | 22 | class Material; |
---|
[3543] | 23 | |
---|
[7779] | 24 | namespace OrxGui |
---|
[5391] | 25 | { |
---|
| 26 | |
---|
[7919] | 27 | class GLGuiCursor; |
---|
| 28 | |
---|
[7779] | 29 | //! if the Element should be visible by default. |
---|
[5387] | 30 | #define GLGUI_WIDGET_DEFAULT_VISIBLE false |
---|
| 31 | |
---|
[7779] | 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 | { |
---|
[5366] | 38 | public: |
---|
[8035] | 39 | GLGuiWidget(GLGuiWidget* parent = NULL); |
---|
[5366] | 40 | virtual ~GLGuiWidget(); |
---|
[2036] | 41 | |
---|
[5366] | 42 | void show(); |
---|
[6431] | 43 | void hide(); |
---|
[5364] | 44 | |
---|
[8035] | 45 | void setParentWidget(GLGuiWidget* parent); |
---|
| 46 | GLGuiWidget* parent() const { return this->_parent; } |
---|
[7868] | 47 | |
---|
[7919] | 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 |
---|
[8035] | 66 | void click(const Vector2D& pos); |
---|
| 67 | void release(const Vector2D& pos); |
---|
[7919] | 68 | bool clickable() const { return this->_clickable; }; |
---|
| 69 | void setClickable(bool clickable = true) { this->_clickable = clickable; }; |
---|
| 70 | |
---|
[8035] | 71 | static void connect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver, Slot executor); |
---|
| 72 | void connect(Signal& signal, BaseObject* receiver, Slot executor); |
---|
[7868] | 73 | |
---|
[8035] | 74 | void disconnect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver); |
---|
[7919] | 75 | |
---|
[8035] | 76 | |
---|
[7919] | 77 | /// MATERIAL (looks) |
---|
[8035] | 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; }; |
---|
[7868] | 82 | |
---|
[8035] | 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; }; |
---|
[7868] | 87 | |
---|
[8035] | 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 | |
---|
[7919] | 110 | /** @param the Event to process. @returns true if the Event has been consumed*/ |
---|
| 111 | virtual bool processEvent(const Event& event) { }; |
---|
| 112 | |
---|
[8035] | 113 | protected: |
---|
[7919] | 114 | |
---|
[8035] | 115 | /// LOOKS |
---|
| 116 | virtual void resize(); |
---|
[7919] | 117 | |
---|
[8035] | 118 | // if something was clickt on the GUI-widget. |
---|
| 119 | virtual void clicking(const Vector2D& pos); |
---|
| 120 | virtual void releasing(const Vector2D& pos); |
---|
[7919] | 121 | virtual void receivedFocus(); |
---|
| 122 | virtual void removedFocus(); |
---|
[5391] | 123 | |
---|
[7919] | 124 | virtual void destroyed(); |
---|
[5391] | 125 | |
---|
[7855] | 126 | |
---|
[8035] | 127 | inline void beginDraw() const { glPushMatrix(); glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); }; |
---|
[6287] | 128 | inline void endDraw() const { glPopMatrix(); }; |
---|
| 129 | |
---|
[7779] | 130 | private: |
---|
| 131 | void init(); |
---|
| 132 | |
---|
[7855] | 133 | |
---|
[7919] | 134 | private: |
---|
[8035] | 135 | GLGuiWidget* _parent; //!< The parent of this Widget. |
---|
| 136 | |
---|
[7919] | 137 | /// LOOKS |
---|
[8035] | 138 | Material _backMat; |
---|
| 139 | Rect2D _backRect; |
---|
[7855] | 140 | |
---|
[8035] | 141 | Material _frontMat; |
---|
| 142 | Rect2D _frontRect; |
---|
[7855] | 143 | |
---|
[8035] | 144 | float _borderSize; |
---|
[3245] | 145 | |
---|
[7919] | 146 | /// EVENTS |
---|
| 147 | bool _focusable; //!< If this widget can receive focus. |
---|
| 148 | bool _clickable; //!< if this widget can be clicked upon. |
---|
[5391] | 149 | |
---|
[7919] | 150 | bool _pushed; |
---|
| 151 | |
---|
[8035] | 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. |
---|
[7779] | 157 | }; |
---|
| 158 | } |
---|
[5362] | 159 | #endif /* _GLGUI_WIDGET_H */ |
---|