BsGUIToggle.h 5.4 KB

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