BsGUIButtonBase.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsGUIElement.h"
  6. #include "BsImageSprite.h"
  7. #include "BsTextSprite.h"
  8. #include "BsGUIContent.h"
  9. #include "BsEvent.h"
  10. namespace BansheeEngine
  11. {
  12. /** @addtogroup Implementation
  13. * @{
  14. */
  15. /** Base class for a clickable GUI button element. */
  16. class BS_EXPORT GUIButtonBase : public GUIElement
  17. {
  18. public:
  19. /** Change content displayed by the button. */
  20. void setContent(const GUIContent& content);
  21. /** Triggered when button is clicked. */
  22. Event<void()> onClick;
  23. /** Triggered when pointer hovers over the button. */
  24. Event<void()> onHover;
  25. /** Triggered when pointer that was previously hovering leaves the button. */
  26. Event<void()> onOut;
  27. /** Triggered when button is clicked twice in rapid succession. */
  28. Event<void()> onDoubleClick;
  29. public: // ***** INTERNAL ******
  30. /** @name Internal
  31. * @{
  32. */
  33. /**
  34. * Change the button "on" state. This state determines whether the button uses normal or "on" fields specified in
  35. * the GUI style.
  36. */
  37. void _setOn(bool on);
  38. /**
  39. * Retrieves the button "on" state. This state determines whether the button uses normal or "on" fields specified
  40. * in the GUI style.
  41. */
  42. bool _isOn() const;
  43. /** Change the internal button state, changing the button look depending on set style. */
  44. void _setState(GUIElementState state);
  45. /** @copydoc GUIElement::_getOptimalSize */
  46. Vector2I _getOptimalSize() const override;
  47. /** @copydoc GUIElement::_getRenderElementDepthRange */
  48. UINT32 _getRenderElementDepthRange() const override;
  49. /** @} */
  50. protected:
  51. GUIButtonBase(const String& styleName, const GUIContent& content, const GUIDimensions& dimensions);
  52. virtual ~GUIButtonBase();
  53. /** @copydoc GUIElement::_getNumRenderElements */
  54. UINT32 _getNumRenderElements() const override;
  55. /** @copydoc GUIElement::_getMaterial */
  56. const SpriteMaterialInfo& _getMaterial(UINT32 renderElementIdx) const override;
  57. /** @copydoc GUIElement::_getMeshSize() */
  58. void _getMeshSize(UINT32 renderElementIdx, UINT32& numVertices, UINT32& numIndices) const override;
  59. /** @copydoc GUIElement::_fillBuffer */
  60. void _fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 vertexOffset, UINT32 indexOffset,
  61. UINT32 maxNumVerts, UINT32 maxNumIndices, UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const override;
  62. /** @copydoc GUIElement::updateRenderElementsInternal */
  63. void updateRenderElementsInternal() override;
  64. /** @copydoc GUIElement::_mouseEvent */
  65. bool _mouseEvent(const GUIMouseEvent& ev) override;
  66. /** @copydoc GUIElement::_getRenderElementDepth */
  67. UINT32 _getRenderElementDepth(UINT32 renderElementIdx) const override;
  68. /** @copydoc GUIElement::_getTooltip */
  69. WString _getTooltip() const override;
  70. /** Creates or destroys the content image sprite depending if there is a content image for the active state. */
  71. void refreshContentSprite();
  72. /** Gets the text sprite descriptor used for creating/updating the internal text sprite. */
  73. TEXT_SPRITE_DESC getTextDesc() const;
  74. /** Retrieves internal button state. */
  75. GUIElementState getState() const { return mActiveState; }
  76. /** Returns the active sprite texture, depending on the current state. */
  77. const HSpriteTexture& getActiveTexture() const;
  78. /** Returns the active text color, depending on the current state. */
  79. Color getActiveTextColor() const;
  80. private:
  81. ImageSprite* mImageSprite;
  82. ImageSprite* mContentImageSprite;
  83. TextSprite* mTextSprite;
  84. GUIElementState mActiveState;
  85. IMAGE_SPRITE_DESC mImageDesc;
  86. GUIContent mContent;
  87. };
  88. /** @} */
  89. }