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