BsGUIButtonBase.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * @copydoc GUIElement::setTint
  36. */
  37. virtual void setTint(const Color& color);
  38. /**
  39. * @brief Change the button "on" state. This state determines
  40. * whether the button uses normal or "on" fields specified
  41. * in the GUI style.
  42. */
  43. void _setOn(bool on);
  44. /**
  45. * @brief Retrieves the button "on" state. This state determines
  46. * whether the button uses normal or "on" fields specified
  47. * in the GUI style.
  48. */
  49. bool _isOn() const;
  50. /**
  51. * @copydoc GUIElement::_getOptimalSize
  52. */
  53. virtual Vector2I _getOptimalSize() const;
  54. /**
  55. * @copydoc GUIElement::_getRenderElementDepthRange
  56. */
  57. virtual UINT32 _getRenderElementDepthRange() const;
  58. /**
  59. * @brief Triggered when button is clicked.
  60. */
  61. Event<void()> onClick;
  62. /**
  63. * @brief Triggered when pointer hovers over the button.
  64. */
  65. Event<void()> onHover;
  66. /**
  67. * @brief Triggered when pointer that was previously hovering leaves the button.
  68. */
  69. Event<void()> onOut;
  70. /**
  71. * @brief Triggered when button is clicked twice in rapid succession.
  72. */
  73. Event<void()> onDoubleClick;
  74. protected:
  75. GUIButtonBase(const String& styleName, const GUIContent& content, const GUIDimensions& dimensions);
  76. virtual ~GUIButtonBase();
  77. /**
  78. * @copydoc GUIElement::getNumRenderElements
  79. */
  80. virtual UINT32 _getNumRenderElements() const;
  81. /**
  82. * @copydoc GUIElement::getMaterial
  83. */
  84. virtual const GUIMaterialInfo& _getMaterial(UINT32 renderElementIdx) const;
  85. /**
  86. * @copydoc GUIElement::getNumQuads
  87. */
  88. virtual UINT32 _getNumQuads(UINT32 renderElementIdx) const;
  89. /**
  90. * @copydoc GUIElement::fillBuffer
  91. */
  92. virtual void _fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad,
  93. UINT32 maxNumQuads, UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const;
  94. /**
  95. * @copydoc GUIElement::updateRenderElementsInternal
  96. */
  97. virtual void updateRenderElementsInternal();
  98. /**
  99. * @copydoc GUIElement::updateBounds
  100. */
  101. virtual void updateClippedBounds();
  102. /**
  103. * @copydoc GUIElement::mouseEvent
  104. */
  105. virtual bool _mouseEvent(const GUIMouseEvent& ev);
  106. /**
  107. * @copydoc GUIElement::_getRenderElementDepth
  108. */
  109. virtual UINT32 _getRenderElementDepth(UINT32 renderElementIdx) const;
  110. /**
  111. * @brief Gets the text sprite descriptor used for creating/updating the internal text sprite.
  112. */
  113. TEXT_SPRITE_DESC getTextDesc() const;
  114. /**
  115. * @brief Change the internal button state, changing the button look depending on set style.
  116. */
  117. void setState(GUIButtonState state);
  118. /**
  119. * @brief Retrieves interal button state.
  120. */
  121. GUIButtonState getState() const { return mActiveState; }
  122. /**
  123. * @brief Returns the active sprite texture, depending on the current state.
  124. */
  125. const HSpriteTexture& getActiveTexture() const;
  126. /**
  127. * @brief Returns the active text color, depending on the current state.
  128. */
  129. Color getActiveTextColor() const;
  130. private:
  131. ImageSprite* mImageSprite;
  132. ImageSprite* mContentImageSprite;
  133. TextSprite* mTextSprite;
  134. GUIButtonState mActiveState;
  135. Color mColor;
  136. IMAGE_SPRITE_DESC mImageDesc;
  137. GUIContent mContent;
  138. HEvent mLocStringUpdatedConn;
  139. };
  140. }