TGUI  1.6.1
Loading...
Searching...
No Matches
RadioButton.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_RADIO_BUTTON_HPP
26#define TGUI_RADIO_BUTTON_HPP
27
28#include <TGUI/Renderers/RadioButtonRenderer.hpp>
29#include <TGUI/Widgets/ClickableWidget.hpp>
30#include <TGUI/Text.hpp>
31
33
34TGUI_MODULE_EXPORT namespace tgui
35{
39 class TGUI_API RadioButton : public ClickableWidget
40 {
41 public:
42
43 using Ptr = std::shared_ptr<RadioButton>;
44 using ConstPtr = std::shared_ptr<const RadioButton>;
45
46 static constexpr const char StaticWidgetType[] = "RadioButton";
47
55 RadioButton(const char* typeName = StaticWidgetType, bool initRenderer = true);
56
62 TGUI_NODISCARD static RadioButton::Ptr create();
63
71 TGUI_NODISCARD static RadioButton::Ptr copy(const RadioButton::ConstPtr& radioButton);
72
77 TGUI_NODISCARD RadioButtonRenderer* getSharedRenderer() override;
78 TGUI_NODISCARD const RadioButtonRenderer* getSharedRenderer() const override;
79
85 TGUI_NODISCARD RadioButtonRenderer* getRenderer() override;
86
92 void setSize(const Layout2d& size) override;
93 using Widget::setSize;
94
102 TGUI_NODISCARD Vector2f getFullSize() const override;
103
113 TGUI_NODISCARD Vector2f getWidgetOffset() const override;
114
122 void setEnabled(bool enabled) override;
123
130 virtual void setChecked(bool checked);
131
136 TGUI_NODISCARD bool isChecked() const
137 {
138 return m_checked;
139 }
140
146 void setText(const String& text);
147
153 TGUI_NODISCARD const String& getText() const;
154
160 void setTextClickable(bool acceptTextClick);
161
165 TGUI_NODISCARD bool isTextClickable() const;
166
172 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
173
177 void leftMouseReleased(Vector2f pos) override;
178
182 void keyPressed(const Event::KeyEvent& event) override;
183
193 bool canHandleKeyPress(const Event::KeyEvent& event) override;
194
201 void draw(BackendRenderTarget& target, RenderStates states) const override;
202
204 protected:
205
215 TGUI_NODISCARD Signal& getSignal(String signalName) override;
216
222 void rendererChanged(const String& property) override;
223
227 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
228
232 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
233
237 void updateTextSize() override;
238
240 // This function is called when the mouse enters the widget. If requested, a callback will be send.
242 void mouseEnteredWidget() override;
243
245 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
247 void mouseLeftWidget() override;
248
250 // Returns the size without the borders
252 TGUI_NODISCARD Vector2f getInnerSize() const;
253
255 // Returns the check color that is being used in the current state
257 TGUI_NODISCARD const Color& getCurrentCheckColor() const;
258
260 // Returns the background color that is being used in the current state
262 TGUI_NODISCARD const Color& getCurrentBackgroundColor() const;
263
265 // Returns the border color that is being used in the current state
267 TGUI_NODISCARD const Color& getCurrentBorderColor() const;
268
270 // Resets the sizes of the textures if they are used
272 virtual void updateTextureSizes();
273
275 // Updates the text color of the label depending on the current state
277 void updateTextColor();
278
280 // Makes a copy of the widget
282 TGUI_NODISCARD Widget::Ptr clone() const override;
283
285 public:
286
287 SignalBool onCheck = {"Checked"};
288 SignalBool onUncheck = {"Unchecked"};
289 SignalBool onChange = {"Changed"};
290
292 protected:
293
294 // This is the checked flag. When the radio button is checked then this variable will be true.
295 bool m_checked = false;
296
297 // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
298 bool m_allowTextClick = true;
299
300 // This will contain the text that is written next to radio button.
301 Text m_text;
302
303 Sprite m_spriteUnchecked;
304 Sprite m_spriteChecked;
305 Sprite m_spriteUncheckedHover;
306 Sprite m_spriteCheckedHover;
307 Sprite m_spriteUncheckedDisabled;
308 Sprite m_spriteCheckedDisabled;
309 Sprite m_spriteUncheckedFocused;
310 Sprite m_spriteCheckedFocused;
311
312 // Cached renderer properties
313 Borders m_bordersCached;
314 TextStyles m_textStyleCached;
315 TextStyles m_textStyleCheckedCached;
316 Color m_checkColorCached;
317 Color m_checkColorHoverCached;
318 Color m_checkColorDisabledCached;
319 Color m_borderColorCached;
320 Color m_borderColorHoverCached;
321 Color m_borderColorDisabledCached;
322 Color m_borderColorFocusedCached;
323 Color m_borderColorCheckedCached;
324 Color m_borderColorCheckedHoverCached;
325 Color m_borderColorCheckedDisabledCached;
326 Color m_borderColorCheckedFocusedCached;
327 Color m_backgroundColorCached;
328 Color m_backgroundColorHoverCached;
329 Color m_backgroundColorDisabledCached;
330 Color m_backgroundColorCheckedCached;
331 Color m_backgroundColorCheckedHoverCached;
332 Color m_backgroundColorCheckedDisabledCached;
333 float m_textDistanceRatioCached = 0.2f;
334 };
335
337}
338
340
341#endif // TGUI_RADIO_BUTTON_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Clickable widget.
Definition ClickableWidget.hpp:38
Wrapper for colors.
Definition Color.hpp:73
Class to store the position or size of a widget.
Definition Layout.hpp:323
Definition Outline.hpp:38
Definition RadioButtonRenderer.hpp:35
Radio button widget.
Definition RadioButton.hpp:40
void setText(const String &text)
Changes the text of the radio button.
virtual void setChecked(bool checked)
Checks or unchecks the radio button.
bool isChecked() const
Returns whether the radio button is checked or not.
Definition RadioButton.hpp:136
void setEnabled(bool enabled) override
Enables or disables the widget.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void setTextClickable(bool acceptTextClick)
Allows (or disallows) the radio button to be checked by clicking on the text next to it.
std::shared_ptr< RadioButton > Ptr
Shared widget pointer.
Definition RadioButton.hpp:43
RadioButtonRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void setSize(const Layout2d &size) override
Changes the size of the radio button.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void mouseLeftWidget() override
This function is called when the mouse leaves the widget.
Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
static RadioButton::Ptr copy(const RadioButton::ConstPtr &radioButton)
Makes a copy of another radio button.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
const String & getText() const
Returns the text of the radio button.
Vector2f getFullSize() const override
Returns the full size of the radio button.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
void mouseEnteredWidget() override
This function is called when the mouse enters the widget.
std::shared_ptr< const RadioButton > ConstPtr
Shared constant widget pointer.
Definition RadioButton.hpp:44
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
static RadioButton::Ptr create()
Creates a new radio button widget.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
RadioButtonRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool isTextClickable() const
Returns whether the radio button can be checked by clicking on the text next to it.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
Wrapper for text styles.
Definition TextStyle.hpp:55
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:48
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
KeyPressed event parameters.
Definition Event.hpp:168
States used for drawing.
Definition RenderStates.hpp:38