1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #pragma once |
4 | |
5 | #include "BsPrerequisites.h" |
6 | #include "GUI/BsGUIButtonBase.h" |
7 | #include "GUI/BsGUIToggleGroup.h" |
8 | #include "2D/BsImageSprite.h" |
9 | #include "2D/BsTextSprite.h" |
10 | #include "GUI/BsGUIContent.h" |
11 | #include "Utility/BsEvent.h" |
12 | |
13 | namespace bs |
14 | { |
15 | /** @addtogroup GUI |
16 | * @{ |
17 | */ |
18 | |
19 | /** GUI element representing a toggle (on/off) button. */ |
20 | class BS_EXPORT GUIToggle : public GUIButtonBase |
21 | { |
22 | public: |
23 | /** Returns type name of the GUI element used for finding GUI element styles. */ |
24 | static const String& getGUITypeName(); |
25 | |
26 | /** |
27 | * Creates a new toggle button with the specified label. |
28 | * |
29 | * @param[in] text Label to display in the button, if any. |
30 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
31 | * GUIWidget the element is used on. If not specified default style is used. |
32 | */ |
33 | static GUIToggle* create(const HString& text, const String& styleName = StringUtil::BLANK); |
34 | |
35 | /** |
36 | * Creates a new toggle button with the specified label. |
37 | * |
38 | * @param[in] text Label to display in the button, if any. |
39 | * @param[in] options Options that allow you to control how is the element positioned and sized. |
40 | * This will override any similar options set by style. |
41 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
42 | * GUIWidget the element is used on. If not specified default style is used. |
43 | */ |
44 | static GUIToggle* create(const HString& text, const GUIOptions& options, |
45 | const String& styleName = StringUtil::BLANK); |
46 | |
47 | /** |
48 | * Creates a new toggle button with the specified label. |
49 | * |
50 | * @param[in] text Label to display in the button, if any. |
51 | * @param[in] toggleGroup Toggle group this button belongs to. |
52 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
53 | * GUIWidget the element is used on. If not specified default style is used. |
54 | */ |
55 | static GUIToggle* create(const HString& text, SPtr<GUIToggleGroup> toggleGroup, |
56 | const String& styleName = StringUtil::BLANK); |
57 | |
58 | /** |
59 | * Creates a new toggle button with the specified label. |
60 | * |
61 | * @param[in] text Label to display in the button, if any. |
62 | * @param[in] toggleGroup Toggle group this button belongs to. |
63 | * @param[in] options Options that allow you to control how is the element positioned and sized. |
64 | * This will override any similar options set by style. |
65 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
66 | * GUIWidget the element is used on. If not specified default style is used. |
67 | */ |
68 | static GUIToggle* create(const HString& text, SPtr<GUIToggleGroup> toggleGroup, |
69 | const GUIOptions& options, const String& styleName = StringUtil::BLANK); |
70 | |
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] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
76 | * GUIWidget the element is used on. If not specified default style is used. |
77 | */ |
78 | static GUIToggle* create(const GUIContent& content, const String& styleName = StringUtil::BLANK); |
79 | |
80 | /** |
81 | * Creates a new toggle button with the specified label. |
82 | * |
83 | * @param[in] content Content to display in the button, if any. |
84 | * @param[in] options Options that allow you to control how is the element positioned and sized. |
85 | * This will override any similar options set by style. |
86 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
87 | * GUIWidget the element is used on. If not specified default style is used. |
88 | */ |
89 | static GUIToggle* create(const GUIContent& content, const GUIOptions& options, |
90 | const String& styleName = StringUtil::BLANK); |
91 | |
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] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
98 | * GUIWidget the element is used on. If not specified default style is used. |
99 | */ |
100 | static GUIToggle* create(const GUIContent& content, SPtr<GUIToggleGroup> toggleGroup, |
101 | const String& styleName = StringUtil::BLANK); |
102 | |
103 | /** |
104 | * Creates a new toggle button with the specified label. |
105 | * |
106 | * @param[in] content Content to display in the button, if any. |
107 | * @param[in] toggleGroup Toggle group this button belongs to. |
108 | * @param[in] options Options that allow you to control how is the element positioned and sized. |
109 | * This will override any similar options set by style. |
110 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
111 | * GUIWidget the element is used on. If not specified default style is used. |
112 | */ |
113 | static GUIToggle* create(const GUIContent& content, SPtr<GUIToggleGroup> toggleGroup, |
114 | const GUIOptions& options, const String& styleName = StringUtil::BLANK); |
115 | |
116 | /** |
117 | * Creates a toggle group that you may provide to GUIToggle upon construction. Toggles sharing the same group will |
118 | * only have a single element active at a time. |
119 | * |
120 | * @param[in] allowAllOff If true all of the toggle buttons can be turned off, if false one will always be turned |
121 | * on. |
122 | */ |
123 | static SPtr<GUIToggleGroup> createToggleGroup(bool allowAllOff = false); |
124 | |
125 | /** Checks the toggle, making it active. */ |
126 | void toggleOn() { _toggleOn(false); } |
127 | |
128 | /** Unchecks the toggle, making it inactive. */ |
129 | void toggleOff() { _toggleOff(false); } |
130 | |
131 | /** Checks is the toggle currently on. */ |
132 | bool isToggled() const { return mIsToggled; } |
133 | |
134 | /** Triggered whenever the button is toggled on or off. */ |
135 | Event<void(bool)> onToggled; |
136 | |
137 | public: // ***** INTERNAL ****** |
138 | /** @name Internal |
139 | * @{ |
140 | */ |
141 | |
142 | /** @copydoc GUIButtonBase::_getElementType */ |
143 | ElementType _getElementType() const override { return ElementType::Toggle; } |
144 | |
145 | /** Sets a toggle group of the toggle button. Toggling one button in a group will automatically untoggle others. */ |
146 | void _setToggleGroup(SPtr<GUIToggleGroup> toggleGroup); |
147 | |
148 | /** Checks the toggle, making it active and optionally triggering the onToggled event. */ |
149 | virtual void _toggleOn(bool triggerEvent); |
150 | |
151 | /** Unchecks the toggle, making it inactive and optionally triggering the onToggled event. */ |
152 | virtual void _toggleOff(bool triggerEvent); |
153 | |
154 | /** @} */ |
155 | protected: |
156 | virtual ~GUIToggle(); |
157 | |
158 | protected: |
159 | GUIToggle(const String& styleName, const GUIContent& content, |
160 | SPtr<GUIToggleGroup> toggleGroup, const GUIDimensions& dimensions); |
161 | |
162 | /** @copydoc GUIButtonBase::_mouseEvent */ |
163 | bool _mouseEvent(const GUIMouseEvent& ev) override; |
164 | |
165 | /** @copydoc GUIButtonBase::_commandEvent */ |
166 | bool _commandEvent(const GUICommandEvent& ev) override; |
167 | |
168 | protected: |
169 | SPtr<GUIToggleGroup> mToggleGroup; |
170 | bool mIsToggled; |
171 | }; |
172 | |
173 | /** @} */ |
174 | } |