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