BsGUIToggle.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "BsGUIButtonBase.h"
  6. #include "BsGUIToggleGroup.h"
  7. #include "BsImageSprite.h"
  8. #include "BsTextSprite.h"
  9. #include "BsGUIContent.h"
  10. #include "BsEvent.h"
  11. namespace BansheeEngine
  12. {
  13. /** @addtogroup GUI
  14. * @{
  15. */
  16. /** GUI element representing a toggle (on/off) button. */
  17. class BS_EXPORT GUIToggle : public GUIButtonBase
  18. {
  19. public:
  20. /** Returns type name of the GUI element used for finding GUI element styles. */
  21. static const String& getGUITypeName();
  22. /**
  23. * Creates a new toggle button with the specified label.
  24. *
  25. * @param[in] text Label to display in the button, if any.
  26. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  27. * GUIWidget the element is used on. If not specified default style is used.
  28. */
  29. static GUIToggle* create(const HString& text, const String& styleName = StringUtil::BLANK);
  30. /**
  31. * Creates a new toggle button with the specified label.
  32. *
  33. * @param[in] text Label to display in the button, if any.
  34. * @param[in] options Options that allow you to control how is the element positioned and sized.
  35. * This will override any similar options set by style.
  36. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  37. * GUIWidget the element is used on. If not specified default style is used.
  38. */
  39. static GUIToggle* create(const HString& text, const GUIOptions& options,
  40. const String& styleName = StringUtil::BLANK);
  41. /**
  42. * Creates a new toggle button with the specified label.
  43. *
  44. * @param[in] text Label to display in the button, if any.
  45. * @param[in] toggleGroup Toggle group this button belongs to.
  46. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  47. * GUIWidget the element is used on. If not specified default style is used.
  48. */
  49. static GUIToggle* create(const HString& text, SPtr<GUIToggleGroup> toggleGroup,
  50. const String& styleName = StringUtil::BLANK);
  51. /**
  52. * Creates a new toggle button with the specified label.
  53. *
  54. * @param[in] text Label to display in the button, if any.
  55. * @param[in] toggleGroup Toggle group this button belongs to.
  56. * @param[in] options Options that allow you to control how is the element positioned and sized.
  57. * This will override any similar options set by style.
  58. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  59. * GUIWidget the element is used on. If not specified default style is used.
  60. */
  61. static GUIToggle* create(const HString& text, SPtr<GUIToggleGroup> toggleGroup,
  62. const GUIOptions& options, const String& styleName = StringUtil::BLANK);
  63. /**
  64. * Creates a new toggle button with the specified label.
  65. *
  66. * @param[in] content Content to display in the button, if any.
  67. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  68. * GUIWidget the element is used on. If not specified default style is used.
  69. */
  70. static GUIToggle* create(const GUIContent& content, const String& styleName = StringUtil::BLANK);
  71. /**
  72. * Creates a new toggle button with the specified label.
  73. *
  74. * @param[in] content Content to display in the button, if any.
  75. * @param[in] options Options that allow you to control how is the element positioned and sized.
  76. * This will override any similar options set by style.
  77. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  78. * GUIWidget the element is used on. If not specified default style is used.
  79. */
  80. static GUIToggle* create(const GUIContent& content, const GUIOptions& options,
  81. const String& styleName = StringUtil::BLANK);
  82. /**
  83. * Creates a new toggle button with the specified label.
  84. *
  85. * @param[in] content Content to display in the button, if any.
  86. * @param[in] toggleGroup Toggle group this button belongs to.
  87. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  88. * GUIWidget the element is used on. If not specified default style is used.
  89. */
  90. static GUIToggle* create(const GUIContent& content, SPtr<GUIToggleGroup> toggleGroup,
  91. const String& styleName = StringUtil::BLANK);
  92. /**
  93. * Creates a new toggle button with the specified label.
  94. *
  95. * @param[in] content Content to display in the button, if any.
  96. * @param[in] toggleGroup Toggle group this button belongs to.
  97. * @param[in] options Options that allow you to control how is the element positioned and sized.
  98. * This will override any similar options set by style.
  99. * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
  100. * GUIWidget the element is used on. If not specified default style is used.
  101. */
  102. static GUIToggle* create(const GUIContent& content, SPtr<GUIToggleGroup> toggleGroup,
  103. const GUIOptions& options, const String& styleName = StringUtil::BLANK);
  104. /**
  105. * Creates a toggle group that you may provide to GUIToggle upon construction. Toggles sharing the same group will
  106. * only have a single element active at a time.
  107. *
  108. * @param[in] allowAllOff If true all of the toggle buttons can be turned off, if false one will always be turned
  109. * on.
  110. */
  111. static SPtr<GUIToggleGroup> createToggleGroup(bool allowAllOff = false);
  112. /** Checks the toggle, making it active. */
  113. virtual void toggleOn();
  114. /** Unchecks the toggle, making it inactive. */
  115. virtual void toggleOff();
  116. /** Checks is the toggle currently on. */
  117. bool isToggled() const { return mIsToggled; }
  118. /** Triggered whenever the button is toggled on or off. */
  119. Event<void(bool)> onToggled;
  120. public: // ***** INTERNAL ******
  121. /** @name Internal
  122. * @{
  123. */
  124. /** @copydoc GUIButtonBase::_getElementType */
  125. virtual ElementType _getElementType() const override { return ElementType::Toggle; }
  126. /** Sets a toggle group of the toggle button. Toggling one button in a group will automatically untoggle others. */
  127. void _setToggleGroup(SPtr<GUIToggleGroup> toggleGroup);
  128. /** @} */
  129. protected:
  130. virtual ~GUIToggle();
  131. protected:
  132. GUIToggle(const String& styleName, const GUIContent& content,
  133. SPtr<GUIToggleGroup> toggleGroup, const GUIDimensions& dimensions);
  134. /** @copydoc GUIButtonBase::_mouseEvent */
  135. virtual bool _mouseEvent(const GUIMouseEvent& ev) override;
  136. protected:
  137. SPtr<GUIToggleGroup> mToggleGroup;
  138. bool mIsToggled;
  139. };
  140. /** @} */
  141. }