[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 | |
---|
[8619] | 11 | #include "glgui_defs.h" |
---|
| 12 | |
---|
[6295] | 13 | #include "material.h" |
---|
[8448] | 14 | #include "rect2D.h" |
---|
[6295] | 15 | |
---|
[7919] | 16 | #include "event.h" |
---|
[7779] | 17 | #include "signal_connector.h" |
---|
[5690] | 18 | |
---|
[8619] | 19 | class Font; |
---|
| 20 | |
---|
[7779] | 21 | namespace OrxGui |
---|
[5391] | 22 | { |
---|
| 23 | |
---|
[7919] | 24 | class GLGuiCursor; |
---|
| 25 | |
---|
[5387] | 26 | |
---|
[7779] | 27 | //! This is widget part of the openglGUI class |
---|
| 28 | /** |
---|
| 29 | * A widget is the main class of all the elements of th GUI. |
---|
| 30 | */ |
---|
| 31 | class GLGuiWidget : public Element2D |
---|
[8448] | 32 | { |
---|
[5366] | 33 | public: |
---|
[8035] | 34 | GLGuiWidget(GLGuiWidget* parent = NULL); |
---|
[5366] | 35 | virtual ~GLGuiWidget(); |
---|
[2036] | 36 | |
---|
[5366] | 37 | void show(); |
---|
[6431] | 38 | void hide(); |
---|
[5364] | 39 | |
---|
[8035] | 40 | void setParentWidget(GLGuiWidget* parent); |
---|
| 41 | GLGuiWidget* parent() const { return this->_parent; } |
---|
[7868] | 42 | |
---|
[7919] | 43 | /// FOCUS |
---|
| 44 | /** @brief gives focus to this widget */ |
---|
| 45 | void giveFocus(); |
---|
| 46 | void breakFocus(); |
---|
| 47 | /** @returns true if the widget is focusable */ |
---|
| 48 | bool focusable() const { return this->_focusable; }; |
---|
| 49 | /** @param focusable sets if the Widget should be focusable */ |
---|
| 50 | void setFocusable(bool focusable = true) { this->_focusable = focusable; }; |
---|
| 51 | /** @returns true if the position is inside of the Widget. @param position the position to check */ |
---|
| 52 | bool focusOverWidget(const Vector2D& position) const; |
---|
| 53 | /** @brief overloaded function, that returns true if the cursor is on top of the Widget */ |
---|
| 54 | bool focusOverWidget(const OrxGui::GLGuiCursor* const cursor) const; |
---|
| 55 | |
---|
| 56 | /** @returns the currently focused Widget (NULL if none is selected) */ |
---|
| 57 | static GLGuiWidget* focused() { return GLGuiWidget::_focused; }; |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /// CLICK |
---|
[8035] | 61 | void click(const Vector2D& pos); |
---|
| 62 | void release(const Vector2D& pos); |
---|
[7919] | 63 | bool clickable() const { return this->_clickable; }; |
---|
| 64 | void setClickable(bool clickable = true) { this->_clickable = clickable; }; |
---|
| 65 | |
---|
[8035] | 66 | static void connect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver, Slot executor); |
---|
| 67 | void connect(Signal& signal, BaseObject* receiver, Slot executor); |
---|
[7868] | 68 | |
---|
[8035] | 69 | void disconnect(GLGuiWidget* sender, Signal& signal, BaseObject* receiver); |
---|
[7919] | 70 | |
---|
[8619] | 71 | OrxGui::State state() const { return this->_state; }; |
---|
[8035] | 72 | |
---|
| 73 | Rect2D& backRect() { return this->_backRect; }; |
---|
| 74 | const Rect2D& backRect() const { return this->_backRect; }; |
---|
[7868] | 75 | |
---|
[8448] | 76 | void setFrontColor(const Color& frontColor, bool instantaniously = false); |
---|
[7868] | 77 | |
---|
[8619] | 78 | void setWidgetSize(const Vector2D& size); |
---|
| 79 | void setWidgetSize(float x, float y); |
---|
[8035] | 80 | |
---|
[8619] | 81 | void animateBack(); |
---|
[8115] | 82 | |
---|
[8619] | 83 | /// STYLE |
---|
| 84 | //////////////////////////////// |
---|
| 85 | /// Retrieve Current Values. /// |
---|
| 86 | //////////////////////////////// |
---|
| 87 | /** @returns current left borderWidth */ |
---|
| 88 | inline float borderLeft() const { return _currentStyle._borderLeft; } |
---|
| 89 | /** @returns current right borderWidth */ |
---|
| 90 | inline float borderRight() const { return _currentStyle._borderRight; } |
---|
| 91 | /** @returns current top borderWidth */ |
---|
| 92 | inline float borderTop() const { return _currentStyle._borderTop; } |
---|
| 93 | /** @returns burrent bottom borderWidth */ |
---|
| 94 | inline float borderBottom() const { return _currentStyle._borderBottom; } |
---|
[8115] | 95 | |
---|
[8035] | 96 | |
---|
[8619] | 97 | /** @returns current textSize */ |
---|
| 98 | inline float textSize() const { return _currentStyle._textSize; } |
---|
| 99 | /** @returns the Background Color */ |
---|
| 100 | inline const Color& backgroundColor() const { return _currentStyle._background.diffuseColor(); } |
---|
| 101 | /** @returns the current Background Material. */ |
---|
| 102 | inline const Material& background() const { return _currentStyle._background; } |
---|
| 103 | /** @returns the current background Texture. */ |
---|
| 104 | inline const Texture& backgroundTexture() const { return _currentStyle._background.diffuseTexture(); } |
---|
| 105 | /** @returns the current foreground Color */ |
---|
| 106 | inline const Color& foregroundColor() const { return _currentStyle._foreground.diffuseColor(); } |
---|
| 107 | /** @returns the current ForeGroung Material. */ |
---|
| 108 | inline const Material& foreground() const { return _currentStyle._foreground; } |
---|
[8035] | 109 | |
---|
| 110 | |
---|
[8619] | 111 | /** @returns FeaturePosition */ |
---|
| 112 | inline FeaturePosition featurePosition() const { return _featurePosition; } |
---|
| 113 | /** @returns the font */ |
---|
| 114 | inline const Font* const font() const { return _font; } |
---|
| 115 | /** @returns true if the Element is Animated */ |
---|
| 116 | inline bool animating() const { return _animating; } |
---|
| 117 | /** @returns true if State-Changes are animated */ |
---|
| 118 | inline bool animatedStateChanges() const { return _animatedStateChanges; } |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | /////////////////////////////////////////////////////////////// |
---|
| 122 | /// Retrieve Values for the Saved Values inside the States. /// |
---|
| 123 | /////////////////////////////////////////////////////////////// |
---|
| 124 | /** @returns left borderWidth @param state the State to retrieve from */ |
---|
| 125 | inline float borderLeft(OrxGui::State state) const { return _style[state]._borderLeft; } |
---|
| 126 | /** @returns right borderWidth @param state the State to retrieve from */ |
---|
| 127 | inline float borderRight(OrxGui::State state) const { return _style[state]._borderRight; } |
---|
| 128 | /** @returns top borderWidth @param state the State to retrieve from */ |
---|
| 129 | inline float borderTop(OrxGui::State state) const { return _style[state]._borderTop; } |
---|
| 130 | /** @returns bottom borderWidth @param state the State to retrieve from */ |
---|
| 131 | inline float borderBottom(OrxGui::State state) const { return _style[state]._borderBottom; } |
---|
| 132 | |
---|
| 133 | /** @returns textSize @param state the State to retrieve from */ |
---|
| 134 | inline float textSize(OrxGui::State state) const { return _style[state]._textSize; } |
---|
| 135 | /** @returns the Background Color @param state the State to retrieve from */ |
---|
| 136 | inline const Color& backgroundColor(OrxGui::State state) const { return _style[state]._background.diffuseColor(); } |
---|
| 137 | /** @returns the Background Material. @param state the state to retrieve from */ |
---|
| 138 | inline const Material& background(OrxGui::State state) const { return _style[state]._background; } |
---|
| 139 | /** @returns background Texture. @param state the State to retrieve from */ |
---|
| 140 | inline const Texture& backgroundTexture(OrxGui::State state) const { return _style[state]._background.diffuseTexture(); } |
---|
| 141 | /** @returns the foreground Color @param state the State to retrieve from */ |
---|
| 142 | inline const Color& foregroundColor(OrxGui::State state) const { return _style[state]._foreground.diffuseColor(); } |
---|
| 143 | /** @returns the ForeGroung Material. @param state the state to retrieve from */ |
---|
| 144 | inline const Material& foreground(OrxGui::State state) const { return _style[state]._foreground; } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | /// SETUP |
---|
| 148 | void resetStyle(); |
---|
| 149 | void loadParams(const TiXmlElement* root); |
---|
| 150 | |
---|
| 151 | void setBorderLeft(float value); |
---|
| 152 | void setBorderLeft(float value, OrxGui::State state); |
---|
| 153 | void setBorderLeftS(float value, const std::string& stateName); |
---|
| 154 | |
---|
| 155 | void setBorderRight(float value); |
---|
| 156 | void setBorderRight(float value, OrxGui::State state); |
---|
| 157 | void setBorderRightS(float value, const std::string& stateName); |
---|
| 158 | |
---|
| 159 | void setBorderTop(float value); |
---|
| 160 | void setBorderTop(float value, OrxGui::State state); |
---|
| 161 | void setBorderTopS(float value, const std::string& stateName); |
---|
| 162 | |
---|
| 163 | void setBorderBottom(float value); |
---|
| 164 | void setBorderBottom(float value, OrxGui::State state); |
---|
| 165 | void setBorderBottomS(float value, const std::string& stateName); |
---|
| 166 | |
---|
| 167 | void setTextSize(float value); |
---|
| 168 | void setTextSize(float value, OrxGui::State state); |
---|
| 169 | void setTextSizeS(float value, const std::string& stateName); |
---|
| 170 | |
---|
| 171 | void setBackgroundColor(const Color& color); |
---|
| 172 | void setBackgroundColor(const Color& color, OrxGui::State state); |
---|
| 173 | void setBackgroundColorS(float r, float g, float b, float a, const std::string& stateName); |
---|
| 174 | |
---|
| 175 | void setBackgroundTexture(const Texture& texture); |
---|
| 176 | void setBackgroundTexture(const std::string& textureName); |
---|
| 177 | void setBackgroundTexture(const Texture& texture, OrxGui::State state); |
---|
| 178 | void setBackgroundTexture(const std::string& textureName, const std::string& stateName); |
---|
| 179 | |
---|
| 180 | void setForegroundColor(const Color& color); |
---|
| 181 | void setForegroundColor(const Color& color, OrxGui::State state); |
---|
| 182 | void setForegroundColorS(float r, float g, float b, float a, const std::string& stateName); |
---|
| 183 | |
---|
| 184 | void loadBackgroundMaterial(const Material& material); |
---|
| 185 | void loadBackgroundMaterial(const Material& material, OrxGui::State state); |
---|
| 186 | void loadBackgroundMaterial(const TiXmlElement* element); |
---|
| 187 | void loadBackgroundMaterial(const TiXmlElement* element, OrxGui::State state); |
---|
| 188 | void loadBackgroundMaterialS(const TiXmlElement* element, const std::string& stateName); |
---|
| 189 | |
---|
| 190 | void loadForegroundMaterial(const Material& material); |
---|
| 191 | void loadForegroundMaterial(const Material& material, OrxGui::State state); |
---|
| 192 | void loadForegroundMaterial(const TiXmlElement* element, OrxGui::State state); |
---|
| 193 | void loadForegroundMaterialS(const TiXmlElement* element, const std::string& stateName); |
---|
| 194 | |
---|
| 195 | void setFeaturePosition(FeaturePosition featurePosition); |
---|
| 196 | void setFeaturePositionS(const std::string& featurePosition); |
---|
| 197 | |
---|
| 198 | void setFont(Font* font); |
---|
| 199 | void setFont(const std::string& fontName); |
---|
| 200 | |
---|
| 201 | void setAnimatedStateChanges(bool animated); |
---|
| 202 | void switchState(OrxGui::State state); |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | |
---|
[8448] | 206 | inline void drawRect(const Rect2D& rect) const |
---|
| 207 | { |
---|
[8035] | 208 | glBegin(GL_QUADS); |
---|
| 209 | glTexCoord2i(0,0); glVertex2d(rect.left(), rect.top()); |
---|
| 210 | glTexCoord2i(0,1); glVertex2d(rect.left(), rect.bottom()); |
---|
| 211 | glTexCoord2i(1,1); glVertex2d(rect.right(), rect.bottom()); |
---|
| 212 | glTexCoord2i(1,0); glVertex2d(rect.right(), rect.top()); |
---|
| 213 | glEnd(); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | virtual void update() {}; |
---|
[8448] | 218 | virtual void tick(float dt); |
---|
[8035] | 219 | virtual void draw() const; |
---|
| 220 | |
---|
[7919] | 221 | /** @param the Event to process. @returns true if the Event has been consumed*/ |
---|
[8148] | 222 | virtual bool processEvent(const Event& event) { return false; }; |
---|
[7919] | 223 | |
---|
[8619] | 224 | bool getState(const std::string& stateName, OrxGui::State* state); |
---|
[8448] | 225 | |
---|
[8035] | 226 | protected: |
---|
| 227 | /// LOOKS |
---|
| 228 | virtual void resize(); |
---|
[7919] | 229 | |
---|
[8115] | 230 | virtual void hiding() {}; |
---|
| 231 | virtual void showing() {}; |
---|
[8448] | 232 | virtual void updateFrontColor() {}; |
---|
| 233 | |
---|
| 234 | inline void beginDraw() const { glPushMatrix(); glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); }; |
---|
| 235 | inline void endDraw() const { glPopMatrix(); }; |
---|
| 236 | |
---|
| 237 | /// EVENTS |
---|
[8035] | 238 | // if something was clickt on the GUI-widget. |
---|
| 239 | virtual void clicking(const Vector2D& pos); |
---|
| 240 | virtual void releasing(const Vector2D& pos); |
---|
[7919] | 241 | virtual void receivedFocus(); |
---|
| 242 | virtual void removedFocus(); |
---|
[5391] | 243 | |
---|
[7919] | 244 | virtual void destroyed(); |
---|
[5391] | 245 | |
---|
[8619] | 246 | virtual void debug() const; |
---|
| 247 | |
---|
[7779] | 248 | private: |
---|
| 249 | void init(); |
---|
| 250 | |
---|
[7919] | 251 | private: |
---|
[8619] | 252 | static GLGuiWidget* _selected; //!< The currently selected Widget. |
---|
| 253 | static GLGuiWidget* _focused; //!< The currently Focused Widget. |
---|
| 254 | static GLGuiWidget* _inputGrabber; //!< The Widget that grabs input. |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | /// WIDGET |
---|
[8035] | 258 | GLGuiWidget* _parent; //!< The parent of this Widget. |
---|
| 259 | |
---|
[7919] | 260 | /// LOOKS |
---|
[8035] | 261 | Rect2D _backRect; |
---|
[8140] | 262 | Vector2D _minSize; |
---|
| 263 | |
---|
[8448] | 264 | |
---|
[7919] | 265 | /// EVENTS |
---|
[8619] | 266 | OrxGui::State _state; |
---|
| 267 | |
---|
[7919] | 268 | bool _focusable; //!< If this widget can receive focus. |
---|
| 269 | bool _clickable; //!< if this widget can be clicked upon. |
---|
[5391] | 270 | |
---|
[7919] | 271 | bool _pushed; |
---|
| 272 | |
---|
[8035] | 273 | |
---|
| 274 | |
---|
[8619] | 275 | /// STYLE - Variables. |
---|
| 276 | typedef struct |
---|
| 277 | { |
---|
| 278 | float _borderLeft; //!< The Distance to the left Border of the widget, before any internal Element starts. |
---|
| 279 | float _borderRight; //!< The Distance to the right Border of the widget, before any internal Element starts. |
---|
| 280 | float _borderTop; //!< The Distance to the top Border of the widget, before any internal Element starts |
---|
| 281 | float _borderBottom; //!< The Distance to the bottom Border of the widget, before any internal Element starts |
---|
| 282 | |
---|
| 283 | float _textSize; //!< The TextSize of the Widget. |
---|
| 284 | |
---|
| 285 | Material _background; //!< The Background Material of the Widget. |
---|
| 286 | |
---|
| 287 | Material _foreground; //!< The foreground Material of the Widget. |
---|
| 288 | } |
---|
| 289 | StatedStyle; |
---|
| 290 | |
---|
| 291 | |
---|
| 292 | StatedStyle _style[GLGUI_STATE_COUNT]; //!< Styles configured for different States |
---|
| 293 | |
---|
| 294 | FeaturePosition _featurePosition; //!< The Position a Feature will be layed at (checkbox(box), slider(text),...) |
---|
| 295 | Font* _font; //!< The Font used in the current Widget. |
---|
| 296 | |
---|
| 297 | |
---|
| 298 | /// ANIMATION STUFF: |
---|
| 299 | bool _animatedStateChanges; //!< If the Transitions between States are Animated automatically. |
---|
| 300 | |
---|
| 301 | bool _animating; //!< If the Widget is animated at the moment (Texture might be an AnimatedTexture.) |
---|
| 302 | float _animationCycle; |
---|
| 303 | float _animationDuration; |
---|
| 304 | StatedStyle _currentStyle; |
---|
| 305 | |
---|
[7779] | 306 | }; |
---|
| 307 | } |
---|
[5362] | 308 | #endif /* _GLGUI_WIDGET_H */ |
---|