BsGUIButtonBase.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include "BsPrerequisites.h"
  3. #include "BsGUIElement.h"
  4. #include "BsImageSprite.h"
  5. #include "BsTextSprite.h"
  6. #include "BsGUIContent.h"
  7. #include "BsEvent.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Type of GUI button states.
  12. */
  13. enum class GUIButtonState
  14. {
  15. Normal = 0x01, /**< Normal state when button is not being iteracted with. */
  16. Hover = 0x02, /**< State when pointer is hovering over the button. */
  17. Active = 0x04, /**< State when button is being clicked. */
  18. Focused = 0x08, /**< State when button has been selected. */
  19. NormalOn = 0x11, /**< Normal state when button is not being iteracted with and is in "on" state. */
  20. HoverOn = 0x12, /**< State when pointer is hovering over the button and is in "on" state. */
  21. ActiveOn = 0x14, /**< State when button is being clicked and is in "on" state. */
  22. FocusedOn = 0x18 /**< State when button has been selected and is in "on" state. */
  23. };
  24. /**
  25. * @brief Base class for a clickable GUI button element.
  26. */
  27. class BS_EXPORT GUIButtonBase : public GUIElement
  28. {
  29. public:
  30. /**
  31. * @brief Change content displayed by the button.
  32. */
  33. void setContent(const GUIContent& content);
  34. /**
  35. * @brief Change the button "on" state. This state determines
  36. * whether the button uses normal or "on" fields specified
  37. * in the GUI style.
  38. */
  39. void _setOn(bool on);
  40. /**
  41. * @brief Retrieves the button "on" state. This state determines
  42. * whether the button uses normal or "on" fields specified
  43. * in the GUI style.
  44. */
  45. bool _isOn() const;
  46. /**
  47. * @copydoc GUIElement::_getOptimalSize
  48. */
  49. virtual Vector2I _getOptimalSize() const;
  50. /**
  51. * @brief Triggered when button is clicked.
  52. */
  53. Event<void()> onClick;
  54. /**
  55. * @brief Triggered when pointer hovers over the button.
  56. */
  57. Event<void()> onHover;
  58. /**
  59. * @brief Triggered when pointer that was previously hovering leaves the button.
  60. */
  61. Event<void()> onOut;
  62. protected:
  63. GUIButtonBase(const String& styleName, const GUIContent& content, const GUILayoutOptions& layoutOptions);
  64. virtual ~GUIButtonBase();
  65. /**
  66. * @copydoc GUIElement::getNumRenderElements
  67. */
  68. virtual UINT32 getNumRenderElements() const;
  69. /**
  70. * @copydoc GUIElement::getMaterial
  71. */
  72. virtual const GUIMaterialInfo& getMaterial(UINT32 renderElementIdx) const;
  73. /**
  74. * @copydoc GUIElement::getNumQuads
  75. */
  76. virtual UINT32 getNumQuads(UINT32 renderElementIdx) const;
  77. /**
  78. * @copydoc GUIElement::fillBuffer
  79. */
  80. virtual void fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad,
  81. UINT32 maxNumQuads, UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const;
  82. /**
  83. * @copydoc GUIElement::updateRenderElementsInternal
  84. */
  85. virtual void updateRenderElementsInternal();
  86. /**
  87. * @copydoc GUIElement::updateBounds
  88. */
  89. virtual void updateClippedBounds();
  90. /**
  91. * @copydoc GUIElement::mouseEvent
  92. */
  93. virtual bool mouseEvent(const GUIMouseEvent& ev);
  94. /**
  95. * @copydoc GUIElement::_getRenderElementDepth
  96. */
  97. virtual UINT32 _getRenderElementDepth(UINT32 renderElementIdx) const;
  98. /**
  99. * @brief Gets the text sprite descriptor used for creating/updating the internal text sprite.
  100. */
  101. TEXT_SPRITE_DESC getTextDesc() const;
  102. /**
  103. * @brief Change the internal button state, changing the button look depending on set style.
  104. */
  105. void setState(GUIButtonState state);
  106. /**
  107. * @brief Retrieves interal button state.
  108. */
  109. GUIButtonState getState() const { return mActiveState; }
  110. /**
  111. * @brief Returns the active sprite texture, depending on the current state.
  112. */
  113. const HSpriteTexture& getActiveTexture() const;
  114. private:
  115. ImageSprite* mImageSprite;
  116. ImageSprite* mContentImageSprite;
  117. TextSprite* mTextSprite;
  118. GUIButtonState mActiveState;
  119. IMAGE_SPRITE_DESC mImageDesc;
  120. GUIContent mContent;
  121. HEvent mLocStringUpdatedConn;
  122. };
  123. }