TGUI 1.10
Loading...
Searching...
No Matches
RadioButton.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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
126 void setMaxWidth(float maxWidth);
127
136 TGUI_NODISCARD float getMaxWidth() const;
137
145 void setEnabled(bool enabled) override;
146
153 virtual void setChecked(bool checked);
154
159 TGUI_NODISCARD bool isChecked() const
160 {
161 return m_checked;
162 }
163
169 void setText(const String& text);
170
176 TGUI_NODISCARD const String& getText() const;
177
183 void setTextClickable(bool acceptTextClick);
184
188 TGUI_NODISCARD bool isTextClickable() const;
189
195 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
196
200 void leftMouseReleased(Vector2f pos) override;
201
205 void keyPressed(const Event::KeyEvent& event) override;
206
216 bool canHandleKeyPress(const Event::KeyEvent& event) override;
217
224 void draw(BackendRenderTarget& target, RenderStates states) const override;
225
227 protected:
228
238 TGUI_NODISCARD Signal& getSignal(String signalName) override;
239
245 void rendererChanged(const String& property) override;
246
250 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
251
255 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
256
260 void updateTextSize() override;
261
263 // This function is called when the mouse enters the widget. If requested, a callback will be send.
265 void mouseEnteredWidget() override;
266
268 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
270 void mouseLeftWidget() override;
271
273 // Returns the size without the borders
275 TGUI_NODISCARD Vector2f getInnerSize() const;
276
278 // Returns the check color that is being used in the current state
280 TGUI_NODISCARD const Color& getCurrentCheckColor() const;
281
283 // Returns the background color that is being used in the current state
285 TGUI_NODISCARD const Color& getCurrentBackgroundColor() const;
286
288 // Returns the border color that is being used in the current state
290 TGUI_NODISCARD const Color& getCurrentBorderColor() const;
291
293 // Resets the sizes of the textures if they are used
295 virtual void updateTextureSizes();
296
298 // Updates the text color of the label depending on the current state
300 void updateTextColor();
301
303 // Makes a copy of the widget
305 TGUI_NODISCARD Widget::Ptr clone() const override;
306
308 public:
309
310 SignalBool onCheck = {"Checked"};
311 SignalBool onUncheck = {"Unchecked"};
312 SignalBool onChange = {"Changed"};
313
315 protected:
316
317 // This is the checked flag. When the radio button is checked then this variable will be true.
318 bool m_checked = false;
319
320 // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
321 bool m_allowTextClick = true;
322
323 // This will contain the text that is written next to radio button.
324 String m_caption;
325 Text m_text;
326
327 float m_maxWidth = 0;
328
329 Sprite m_spriteUnchecked;
330 Sprite m_spriteChecked;
331 Sprite m_spriteUncheckedHover;
332 Sprite m_spriteCheckedHover;
333 Sprite m_spriteUncheckedDisabled;
334 Sprite m_spriteCheckedDisabled;
335 Sprite m_spriteUncheckedFocused;
336 Sprite m_spriteCheckedFocused;
337
338 // Cached renderer properties
339 Borders m_bordersCached;
340 TextStyles m_textStyleCached;
341 TextStyles m_textStyleCheckedCached;
342 Color m_checkColorCached;
343 Color m_checkColorHoverCached;
344 Color m_checkColorDisabledCached;
345 Color m_borderColorCached;
346 Color m_borderColorHoverCached;
347 Color m_borderColorDisabledCached;
348 Color m_borderColorFocusedCached;
349 Color m_borderColorCheckedCached;
350 Color m_borderColorCheckedHoverCached;
351 Color m_borderColorCheckedDisabledCached;
352 Color m_borderColorCheckedFocusedCached;
353 Color m_backgroundColorCached;
354 Color m_backgroundColorHoverCached;
355 Color m_backgroundColorDisabledCached;
356 Color m_backgroundColorCheckedCached;
357 Color m_backgroundColorCheckedHoverCached;
358 Color m_backgroundColorCheckedDisabledCached;
359 float m_textDistanceRatioCached = 0.2f;
360 };
361
363}
364
366
367#endif // TGUI_RADIO_BUTTON_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:73
Class to store the position or size of a widget.
Definition Layout.hpp:323
Definition RadioButtonRenderer.hpp:35
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:159
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.
static constexpr const char StaticWidgetType[]
Type name of the widget.
Definition RadioButton.hpp:46
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.
SignalBool onUncheck
Radio button was unchecked. Optional parameter: bool which is always false.
Definition RadioButton.hpp:311
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 setMaxWidth(float maxWidth)
Sets the maximum width that is used to determine when to split the text over multiple lines.
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.
SignalBool onCheck
Radio button was checked. Optional parameter: bool which is always true.
Definition RadioButton.hpp:310
bool isTextClickable() const
Returns whether the radio button can be checked by clicking on the text next to it.
SignalBool onChange
Radio button was checked or unchecked. Optional parameter: bool indicating whether it is checked.
Definition RadioButton.hpp:312
float getMaxWidth() const
Returns the maximum width that is used to determine when to split the text over multiple lines.
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
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
SignalTyped< bool > SignalBool
Signal with one "bool" as optional unbound parameter.
Definition Signal.hpp:425
KeyPressed event parameters.
Definition Event.hpp:168
States used for drawing.
Definition RenderStates.hpp:38