[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" |
---|
[8769] | 14 | #include "font.h" |
---|
| 15 | |
---|
[8448] | 16 | #include "rect2D.h" |
---|
[6295] | 17 | |
---|
[7919] | 18 | #include "event.h" |
---|
[9406] | 19 | #include "sigslot/signal.h" |
---|
[5690] | 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 |
---|
[8717] | 44 | /** @brief gives mouse - focus to this widget */ |
---|
| 45 | void giveMouseFocus(); |
---|
| 46 | void breakMouseFocus(); |
---|
| 47 | |
---|
[7919] | 48 | /** @returns true if the widget is focusable */ |
---|
| 49 | bool focusable() const { return this->_focusable; }; |
---|
| 50 | /** @returns true if the position is inside of the Widget. @param position the position to check */ |
---|
| 51 | bool focusOverWidget(const Vector2D& position) const; |
---|
| 52 | /** @brief overloaded function, that returns true if the cursor is on top of the Widget */ |
---|
| 53 | bool focusOverWidget(const OrxGui::GLGuiCursor* const cursor) const; |
---|
| 54 | |
---|
[8717] | 55 | /** @returns the currently mouse - focused Widget (NULL if none is focused). */ |
---|
| 56 | static GLGuiWidget* mouseFocused() { return GLGuiWidget::_mouseFocused; }; |
---|
[7919] | 57 | |
---|
[8717] | 58 | /// SELECT |
---|
| 59 | void select(); |
---|
| 60 | void unselect(); |
---|
| 61 | /** @returns true if the Widget is selectable */ |
---|
| 62 | bool selectable() const { return this->_selectable; } |
---|
[7919] | 63 | |
---|
[8717] | 64 | /** @returns the currently Selected Widget (NULL if none is selected). */ |
---|
| 65 | static GLGuiWidget* selected() { return GLGuiWidget::_selected; }; |
---|
| 66 | |
---|
[7919] | 67 | /// CLICK |
---|
[8717] | 68 | bool pushed() { return _pushed; }; |
---|
[8035] | 69 | void click(const Vector2D& pos); |
---|
| 70 | void release(const Vector2D& pos); |
---|
[7919] | 71 | bool clickable() const { return this->_clickable; }; |
---|
| 72 | |
---|
[8619] | 73 | OrxGui::State state() const { return this->_state; }; |
---|
[8035] | 74 | |
---|
| 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); |
---|
[7868] | 79 | |
---|
[8619] | 80 | void setWidgetSize(const Vector2D& size); |
---|
| 81 | void setWidgetSize(float x, float y); |
---|
[8035] | 82 | |
---|
[8619] | 83 | void animateBack(); |
---|
[8115] | 84 | |
---|
[8717] | 85 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 86 | /// STYLE ///////////////////////////////////////////////////////////////////////// |
---|
[8619] | 87 | //////////////////////////////// |
---|
| 88 | /// Retrieve Current Values. /// |
---|
| 89 | //////////////////////////////// |
---|
| 90 | /** @returns current left borderWidth */ |
---|
| 91 | inline float borderLeft() const { return _currentStyle._borderLeft; } |
---|
| 92 | /** @returns current right borderWidth */ |
---|
| 93 | inline float borderRight() const { return _currentStyle._borderRight; } |
---|
| 94 | /** @returns current top borderWidth */ |
---|
| 95 | inline float borderTop() const { return _currentStyle._borderTop; } |
---|
| 96 | /** @returns burrent bottom borderWidth */ |
---|
| 97 | inline float borderBottom() const { return _currentStyle._borderBottom; } |
---|
[8115] | 98 | |
---|
[8035] | 99 | |
---|
[8619] | 100 | /** @returns current textSize */ |
---|
| 101 | inline float textSize() const { return _currentStyle._textSize; } |
---|
| 102 | /** @returns the Background Color */ |
---|
| 103 | inline const Color& backgroundColor() const { return _currentStyle._background.diffuseColor(); } |
---|
| 104 | /** @returns the current Background Material. */ |
---|
| 105 | inline const Material& background() const { return _currentStyle._background; } |
---|
| 106 | /** @returns the current background Texture. */ |
---|
| 107 | inline const Texture& backgroundTexture() const { return _currentStyle._background.diffuseTexture(); } |
---|
| 108 | /** @returns the current foreground Color */ |
---|
| 109 | inline const Color& foregroundColor() const { return _currentStyle._foreground.diffuseColor(); } |
---|
| 110 | /** @returns the current ForeGroung Material. */ |
---|
| 111 | inline const Material& foreground() const { return _currentStyle._foreground; } |
---|
[8035] | 112 | |
---|
| 113 | |
---|
[8619] | 114 | /** @returns FeaturePosition */ |
---|
| 115 | inline FeaturePosition featurePosition() const { return _featurePosition; } |
---|
[8983] | 116 | /** @returns the constant font */ |
---|
| 117 | inline const Font& font() const { return _font; } |
---|
[8619] | 118 | /** @returns the font */ |
---|
[8983] | 119 | inline Font& font() { return _font; } |
---|
[8619] | 120 | /** @returns true if the Element is Animated */ |
---|
| 121 | inline bool animating() const { return _animating; } |
---|
| 122 | /** @returns true if State-Changes are animated */ |
---|
| 123 | inline bool animatedStateChanges() const { return _animatedStateChanges; } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | /////////////////////////////////////////////////////////////// |
---|
| 127 | /// Retrieve Values for the Saved Values inside the States. /// |
---|
| 128 | /////////////////////////////////////////////////////////////// |
---|
| 129 | /** @returns left borderWidth @param state the State to retrieve from */ |
---|
| 130 | inline float borderLeft(OrxGui::State state) const { return _style[state]._borderLeft; } |
---|
| 131 | /** @returns right borderWidth @param state the State to retrieve from */ |
---|
| 132 | inline float borderRight(OrxGui::State state) const { return _style[state]._borderRight; } |
---|
| 133 | /** @returns top borderWidth @param state the State to retrieve from */ |
---|
| 134 | inline float borderTop(OrxGui::State state) const { return _style[state]._borderTop; } |
---|
| 135 | /** @returns bottom borderWidth @param state the State to retrieve from */ |
---|
| 136 | inline float borderBottom(OrxGui::State state) const { return _style[state]._borderBottom; } |
---|
| 137 | |
---|
| 138 | /** @returns textSize @param state the State to retrieve from */ |
---|
| 139 | inline float textSize(OrxGui::State state) const { return _style[state]._textSize; } |
---|
| 140 | /** @returns the Background Color @param state the State to retrieve from */ |
---|
| 141 | inline const Color& backgroundColor(OrxGui::State state) const { return _style[state]._background.diffuseColor(); } |
---|
| 142 | /** @returns the Background Material. @param state the state to retrieve from */ |
---|
| 143 | inline const Material& background(OrxGui::State state) const { return _style[state]._background; } |
---|
| 144 | /** @returns background Texture. @param state the State to retrieve from */ |
---|
| 145 | inline const Texture& backgroundTexture(OrxGui::State state) const { return _style[state]._background.diffuseTexture(); } |
---|
| 146 | /** @returns the foreground Color @param state the State to retrieve from */ |
---|
| 147 | inline const Color& foregroundColor(OrxGui::State state) const { return _style[state]._foreground.diffuseColor(); } |
---|
| 148 | /** @returns the ForeGroung Material. @param state the state to retrieve from */ |
---|
| 149 | inline const Material& foreground(OrxGui::State state) const { return _style[state]._foreground; } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | /// SETUP |
---|
| 153 | void resetStyle(); |
---|
| 154 | void loadParams(const TiXmlElement* root); |
---|
| 155 | |
---|
| 156 | void setBorderLeft(float value); |
---|
| 157 | void setBorderLeft(float value, OrxGui::State state); |
---|
| 158 | void setBorderLeftS(float value, const std::string& stateName); |
---|
| 159 | |
---|
| 160 | void setBorderRight(float value); |
---|
| 161 | void setBorderRight(float value, OrxGui::State state); |
---|
| 162 | void setBorderRightS(float value, const std::string& stateName); |
---|
| 163 | |
---|
| 164 | void setBorderTop(float value); |
---|
| 165 | void setBorderTop(float value, OrxGui::State state); |
---|
| 166 | void setBorderTopS(float value, const std::string& stateName); |
---|
| 167 | |
---|
| 168 | void setBorderBottom(float value); |
---|
| 169 | void setBorderBottom(float value, OrxGui::State state); |
---|
| 170 | void setBorderBottomS(float value, const std::string& stateName); |
---|
| 171 | |
---|
| 172 | void setTextSize(float value); |
---|
| 173 | void setTextSize(float value, OrxGui::State state); |
---|
| 174 | void setTextSizeS(float value, const std::string& stateName); |
---|
| 175 | |
---|
| 176 | void setBackgroundColor(const Color& color); |
---|
| 177 | void setBackgroundColor(const Color& color, OrxGui::State state); |
---|
| 178 | void setBackgroundColorS(float r, float g, float b, float a, const std::string& stateName); |
---|
| 179 | |
---|
| 180 | void setBackgroundTexture(const Texture& texture); |
---|
| 181 | void setBackgroundTexture(const std::string& textureName); |
---|
| 182 | void setBackgroundTexture(const Texture& texture, OrxGui::State state); |
---|
| 183 | void setBackgroundTexture(const std::string& textureName, const std::string& stateName); |
---|
| 184 | |
---|
| 185 | void setForegroundColor(const Color& color); |
---|
| 186 | void setForegroundColor(const Color& color, OrxGui::State state); |
---|
| 187 | void setForegroundColorS(float r, float g, float b, float a, const std::string& stateName); |
---|
| 188 | |
---|
[8990] | 189 | void setForegroundTexture(const Texture& texture); |
---|
| 190 | void setForegroundTexture(const std::string& textureName); |
---|
| 191 | void setForegroundTexture(const Texture& texture, OrxGui::State state); |
---|
| 192 | void setForegroundTexture(const std::string& textureName, const std::string& stateName); |
---|
| 193 | |
---|
[8619] | 194 | void loadBackgroundMaterial(const Material& material); |
---|
| 195 | void loadBackgroundMaterial(const Material& material, OrxGui::State state); |
---|
| 196 | void loadBackgroundMaterial(const TiXmlElement* element); |
---|
| 197 | void loadBackgroundMaterial(const TiXmlElement* element, OrxGui::State state); |
---|
| 198 | void loadBackgroundMaterialS(const TiXmlElement* element, const std::string& stateName); |
---|
| 199 | |
---|
| 200 | void loadForegroundMaterial(const Material& material); |
---|
| 201 | void loadForegroundMaterial(const Material& material, OrxGui::State state); |
---|
| 202 | void loadForegroundMaterial(const TiXmlElement* element, OrxGui::State state); |
---|
| 203 | void loadForegroundMaterialS(const TiXmlElement* element, const std::string& stateName); |
---|
| 204 | |
---|
| 205 | void setFeaturePosition(FeaturePosition featurePosition); |
---|
| 206 | void setFeaturePositionS(const std::string& featurePosition); |
---|
| 207 | |
---|
[9656] | 208 | virtual void setFont(const Font& font); |
---|
| 209 | void setFont(const std::string& fontName, unsigned int renderSize = FONT_DEFAULT_RENDER_SIZE); |
---|
[8619] | 210 | |
---|
| 211 | void setAnimatedStateChanges(bool animated); |
---|
| 212 | void switchState(OrxGui::State state); |
---|
[8717] | 213 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[8619] | 214 | |
---|
| 215 | |
---|
[8448] | 216 | inline void drawRect(const Rect2D& rect) const |
---|
| 217 | { |
---|
[8035] | 218 | glBegin(GL_QUADS); |
---|
| 219 | glTexCoord2i(0,0); glVertex2d(rect.left(), rect.top()); |
---|
| 220 | glTexCoord2i(0,1); glVertex2d(rect.left(), rect.bottom()); |
---|
| 221 | glTexCoord2i(1,1); glVertex2d(rect.right(), rect.bottom()); |
---|
| 222 | glTexCoord2i(1,0); glVertex2d(rect.right(), rect.top()); |
---|
| 223 | glEnd(); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | virtual void update() {}; |
---|
[8448] | 228 | virtual void tick(float dt); |
---|
[8035] | 229 | virtual void draw() const; |
---|
| 230 | |
---|
[7919] | 231 | /** @param the Event to process. @returns true if the Event has been consumed*/ |
---|
[8148] | 232 | virtual bool processEvent(const Event& event) { return false; }; |
---|
[7919] | 233 | |
---|
[8619] | 234 | bool getState(const std::string& stateName, OrxGui::State* state); |
---|
[8448] | 235 | |
---|
[8035] | 236 | protected: |
---|
[8717] | 237 | /** @param focusable sets if the Widget should be focusable */ |
---|
| 238 | void setFocusable(bool focusable = true) { this->_focusable = focusable; }; |
---|
| 239 | /** @param selectable true if the widget should be selectable */ |
---|
| 240 | void setSelectable(bool selectable) { this->_selectable = selectable; } |
---|
| 241 | /** @param focusable true if the widget should be focusable */ |
---|
| 242 | void setClickable(bool clickable = true) { this->_clickable = clickable; }; |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | /// RENDERING |
---|
| 246 | inline void beginDraw() const { glPushMatrix(); glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); }; |
---|
| 247 | inline void endDraw() const { glPopMatrix(); }; |
---|
| 248 | |
---|
| 249 | |
---|
[8035] | 250 | /// LOOKS |
---|
| 251 | virtual void resize(); |
---|
[7919] | 252 | |
---|
[8115] | 253 | virtual void hiding() {}; |
---|
| 254 | virtual void showing() {}; |
---|
[8717] | 255 | |
---|
[8448] | 256 | virtual void updateFrontColor() {}; |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | /// EVENTS |
---|
[8717] | 260 | // mouse clicking |
---|
[8035] | 261 | virtual void clicking(const Vector2D& pos); |
---|
[8717] | 262 | virtual void releasing(const Vector2D& pos, bool focused); |
---|
| 263 | // mouse focusing |
---|
[7919] | 264 | virtual void receivedFocus(); |
---|
| 265 | virtual void removedFocus(); |
---|
[8717] | 266 | // selecting either with the mouse by clicking, or by the keybord traversing to it. |
---|
| 267 | virtual void selecting(); |
---|
| 268 | virtual void unselecting(); |
---|
| 269 | // destroying the Widget. |
---|
| 270 | virtual void destroying(); |
---|
[5391] | 271 | |
---|
[9656] | 272 | // unparent the widget and from this widget seen as parent |
---|
| 273 | virtual void removeChildWidget(GLGuiWidget* widget) {}; |
---|
[5391] | 274 | |
---|
[9656] | 275 | |
---|
[8717] | 276 | virtual void debug(unsigned int level) const; |
---|
[8619] | 277 | |
---|
[7779] | 278 | private: |
---|
| 279 | void init(); |
---|
| 280 | |
---|
[7919] | 281 | private: |
---|
[8619] | 282 | static GLGuiWidget* _selected; //!< The currently selected Widget. |
---|
[8717] | 283 | static GLGuiWidget* _mouseFocused; //!< The currently Focused Widget (mouse-focus). |
---|
| 284 | static GLGuiWidget* _inputGrabber; //!< The Widget that grabs input (keyboard-focus). |
---|
[8619] | 285 | |
---|
| 286 | |
---|
| 287 | /// WIDGET |
---|
[8035] | 288 | GLGuiWidget* _parent; //!< The parent of this Widget. |
---|
| 289 | |
---|
[7919] | 290 | /// LOOKS |
---|
[8035] | 291 | Rect2D _backRect; |
---|
[8140] | 292 | Vector2D _minSize; |
---|
| 293 | |
---|
[8448] | 294 | |
---|
[7919] | 295 | /// EVENTS |
---|
[8619] | 296 | OrxGui::State _state; |
---|
[8717] | 297 | bool _pushed; |
---|
[8619] | 298 | |
---|
[7919] | 299 | bool _focusable; //!< If this widget can receive focus. |
---|
| 300 | bool _clickable; //!< if this widget can be clicked upon. |
---|
[8717] | 301 | bool _selectable; //!< If this widget can be selected. |
---|
[5391] | 302 | |
---|
[7919] | 303 | |
---|
[8035] | 304 | |
---|
| 305 | |
---|
[8619] | 306 | /// STYLE - Variables. |
---|
| 307 | typedef struct |
---|
| 308 | { |
---|
[8717] | 309 | float _borderLeft; //!< The Distance to the left Border of the widget, before any internal Element starts. |
---|
| 310 | float _borderRight; //!< The Distance to the right Border of the widget, before any internal Element starts. |
---|
| 311 | float _borderTop; //!< The Distance to the top Border of the widget, before any internal Element starts |
---|
| 312 | float _borderBottom; //!< The Distance to the bottom Border of the widget, before any internal Element starts |
---|
[8619] | 313 | |
---|
[8717] | 314 | float _textSize; //!< The TextSize of the Widget. |
---|
[8619] | 315 | |
---|
[8717] | 316 | Material _background; //!< The Background Material of the Widget. |
---|
[8619] | 317 | |
---|
[8717] | 318 | Material _foreground; //!< The foreground Material of the Widget. |
---|
[8619] | 319 | } |
---|
| 320 | StatedStyle; |
---|
| 321 | |
---|
| 322 | |
---|
[8717] | 323 | StatedStyle _style[GLGUI_STATE_COUNT]; //!< Styles configured for different States |
---|
[8619] | 324 | |
---|
[8717] | 325 | FeaturePosition _featurePosition; //!< The Position a Feature will be layed at (checkbox(box), slider(text),...) |
---|
[8769] | 326 | Font _font; //!< The Font used in the current Widget. |
---|
[8619] | 327 | |
---|
[9656] | 328 | static Font* _defaultFont; //!< The default Font. |
---|
[8619] | 329 | |
---|
| 330 | /// ANIMATION STUFF: |
---|
[8717] | 331 | bool _animatedStateChanges; //!< If the Transitions between States are Animated automatically. |
---|
[8619] | 332 | |
---|
[8717] | 333 | bool _animating; //!< If the Widget is animated at the moment (Texture might be an AnimatedTexture.) |
---|
| 334 | float _animationCycle; |
---|
| 335 | float _animationDuration; |
---|
| 336 | StatedStyle _currentStyle; |
---|
[8619] | 337 | |
---|
[7779] | 338 | }; |
---|
| 339 | } |
---|
[5362] | 340 | #endif /* _GLGUI_WIDGET_H */ |
---|