| 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_style.h" |
|---|
| 12 | #include "material.h" |
|---|
| 13 | #include "rect2D.h" |
|---|
| 14 | |
|---|
| 15 | #include "event.h" |
|---|
| 16 | #include "signal_connector.h" |
|---|
| 17 | |
|---|
| 18 | namespace OrxGui |
|---|
| 19 | { |
|---|
| 20 | |
|---|
| 21 | class GLGuiCursor; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 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 |
|---|
| 29 | { |
|---|
| 30 | public: |
|---|
| 31 | GLGuiWidget(GLGuiWidget* parent = NULL); |
|---|
| 32 | virtual ~GLGuiWidget(); |
|---|
| 33 | |
|---|
| 34 | void show(); |
|---|
| 35 | void hide(); |
|---|
| 36 | |
|---|
| 37 | void setParentWidget(GLGuiWidget* parent); |
|---|
| 38 | GLGuiWidget* parent() const { return this->_parent; } |
|---|
| 39 | |
|---|
| 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 |
|---|
| 58 | void click(const Vector2D& pos); |
|---|
| 59 | void release(const Vector2D& pos); |
|---|
| 60 | bool clickable() const { return this->_clickable; }; |
|---|
| 61 | void setClickable(bool clickable = true) { this->_clickable = clickable; }; |
|---|
| 62 | |
|---|
| 63 | static void connect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver, Slot executor); |
|---|
| 64 | void connect(Signal& signal, BaseObject* receiver, Slot executor); |
|---|
| 65 | |
|---|
| 66 | void disconnect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | /// MATERIAL (looks) |
|---|
| 70 | Material& backMaterial() { return this->_backMat; }; |
|---|
| 71 | const Material& backMaterial() const { return this->_backMat; }; |
|---|
| 72 | Rect2D& backRect() { return this->_backRect; }; |
|---|
| 73 | const Rect2D& backRect() const { return this->_backRect; }; |
|---|
| 74 | |
|---|
| 75 | void setFrontColor(const Color& frontColor, bool instantaniously = false); |
|---|
| 76 | const Color& frontColor() const { return this->_frontColor; }; |
|---|
| 77 | |
|---|
| 78 | /** @brief sets all borders to the same value. */ |
|---|
| 79 | void setBorderSize(float borderSize); |
|---|
| 80 | void setBorderLeft(float borderLeft); |
|---|
| 81 | void setBorderRight(float borderRight); |
|---|
| 82 | void setBorderTop(float borderTop); |
|---|
| 83 | void setBorderBottom(float borderBottom); |
|---|
| 84 | |
|---|
| 85 | float borderLeft() const { return this->_borderLeft; }; |
|---|
| 86 | float borderRight() const { return this->_borderRight; }; |
|---|
| 87 | float borderTop() const { return this->_borderTop; }; |
|---|
| 88 | float borderBottom() const { return this->_borderBottom; }; |
|---|
| 89 | |
|---|
| 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 | { |
|---|
| 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 tick(float dt); |
|---|
| 110 | virtual void draw() const; |
|---|
| 111 | |
|---|
| 112 | /** @param the Event to process. @returns true if the Event has been consumed*/ |
|---|
| 113 | virtual bool processEvent(const Event& event) { return false; }; |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | protected: |
|---|
| 117 | /// LOOKS |
|---|
| 118 | virtual void resize(); |
|---|
| 119 | |
|---|
| 120 | virtual void hiding() {}; |
|---|
| 121 | virtual void showing() {}; |
|---|
| 122 | virtual void updateFrontColor() {}; |
|---|
| 123 | |
|---|
| 124 | inline void beginDraw() const { glPushMatrix(); glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); }; |
|---|
| 125 | inline void endDraw() const { glPopMatrix(); }; |
|---|
| 126 | |
|---|
| 127 | /// EVENTS |
|---|
| 128 | // if something was clickt on the GUI-widget. |
|---|
| 129 | virtual void clicking(const Vector2D& pos); |
|---|
| 130 | virtual void releasing(const Vector2D& pos); |
|---|
| 131 | virtual void receivedFocus(); |
|---|
| 132 | virtual void removedFocus(); |
|---|
| 133 | |
|---|
| 134 | virtual void destroyed(); |
|---|
| 135 | |
|---|
| 136 | private: |
|---|
| 137 | void init(); |
|---|
| 138 | |
|---|
| 139 | private: |
|---|
| 140 | GLGuiWidget* _parent; //!< The parent of this Widget. |
|---|
| 141 | |
|---|
| 142 | /// LOOKS |
|---|
| 143 | Material _backMat; |
|---|
| 144 | Rect2D _backRect; |
|---|
| 145 | |
|---|
| 146 | Color _frontColor; |
|---|
| 147 | Color* _toFrontColor; |
|---|
| 148 | |
|---|
| 149 | float _borderLeft; |
|---|
| 150 | float _borderRight; |
|---|
| 151 | float _borderTop; |
|---|
| 152 | float _borderBottom; |
|---|
| 153 | |
|---|
| 154 | Vector2D _minSize; |
|---|
| 155 | |
|---|
| 156 | GLGuiStyle _style; |
|---|
| 157 | |
|---|
| 158 | /// EVENTS |
|---|
| 159 | bool _focusable; //!< If this widget can receive focus. |
|---|
| 160 | bool _clickable; //!< if this widget can be clicked upon. |
|---|
| 161 | |
|---|
| 162 | bool _pushed; |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | static GLGuiWidget* _selected; //!< The currently selected Widget. |
|---|
| 166 | static GLGuiWidget* _focused; //!< The currently Focused Widget. |
|---|
| 167 | |
|---|
| 168 | static GLGuiWidget* _inputGrabber; //!< The Widget that grabs input. |
|---|
| 169 | }; |
|---|
| 170 | } |
|---|
| 171 | #endif /* _GLGUI_WIDGET_H */ |
|---|