TGUI 1.13
Loading...
Searching...
No Matches
RadioButton.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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/Text.hpp>
30#include <TGUI/Widgets/ClickableWidget.hpp>
31
33
34namespace tgui
35{
39 class TGUI_API RadioButton : public ClickableWidget
40 {
41 public:
42 using Ptr = std::shared_ptr<RadioButton>;
43 using ConstPtr = std::shared_ptr<const RadioButton>;
44
45 static constexpr char StaticWidgetType[] = "RadioButton";
46
54 explicit RadioButton(const char* typeName = StaticWidgetType, bool initRenderer = true);
55
61 [[nodiscard]] static RadioButton::Ptr create();
62
70 [[nodiscard]] static RadioButton::Ptr copy(const RadioButton::ConstPtr& radioButton);
71
76 [[nodiscard]] RadioButtonRenderer* getSharedRenderer() override;
77 [[nodiscard]] const RadioButtonRenderer* getSharedRenderer() const override;
78
84 [[nodiscard]] RadioButtonRenderer* getRenderer() override;
85
91 void setSize(const Layout2d& size) override;
92 using Widget::setSize;
93
101 [[nodiscard]] Vector2f getFullSize() const override;
102
112 [[nodiscard]] Vector2f getWidgetOffset() const override;
113
125 void setMaxWidth(float maxWidth);
126
135 [[nodiscard]] float getMaxWidth() const;
136
144 void setEnabled(bool enabled) override;
145
152 virtual void setChecked(bool checked);
153
158 [[nodiscard]] bool isChecked() const
159 {
160 return m_checked;
161 }
162
168 void setText(const String& text);
169
175 [[nodiscard]] const String& getText() const;
176
182 void setTextClickable(bool acceptTextClick);
183
187 [[nodiscard]] bool isTextClickable() const;
188
194 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
195
199 void leftMouseReleased(Vector2f pos) override;
200
204 void keyPressed(const Event::KeyEvent& event) override;
205
215 bool canHandleKeyPress(const Event::KeyEvent& event) override;
216
223 void draw(BackendRenderTarget& target, RenderStates states) const override;
224
226
227 protected:
237 [[nodiscard]] Signal& getSignal(String signalName) override;
238
244 void rendererChanged(const String& property) override;
245
249 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
250
254 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
255
259 void updateTextSize() override;
260
262 // This function is called when the mouse enters the widget. If requested, a callback will be send.
264 void mouseEnteredWidget() override;
265
267 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
269 void mouseLeftWidget() override;
270
272 // Returns the size without the borders
274 [[nodiscard]] Vector2f getInnerSize() const;
275
277 // Returns the check color that is being used in the current state
279 [[nodiscard]] const Color& getCurrentCheckColor() const;
280
282 // Returns the background color that is being used in the current state
284 [[nodiscard]] const Color& getCurrentBackgroundColor() const;
285
287 // Returns the border color that is being used in the current state
289 [[nodiscard]] const Color& getCurrentBorderColor() const;
290
292 // Resets the sizes of the textures if they are used
294 virtual void updateTextureSizes();
295
297 // Updates the text color of the label depending on the current state
299 void updateTextColor();
300
302 // Makes a copy of the widget
304 [[nodiscard]] Widget::Ptr clone() const override;
305
307
308 public:
309 SignalBool onCheck = {"Checked"};
310 SignalBool onUncheck = {"Unchecked"};
311 SignalBool onChange = {"Changed"};
312
314
315 protected:
316 // This is the checked flag. When the radio button is checked then this variable will be true.
317 bool m_checked = false;
318
319 // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
320 bool m_allowTextClick = true;
321
322 // This will contain the text that is written next to radio button.
323 String m_caption;
324 Text m_text;
325
326 float m_maxWidth = 0;
327
328 Sprite m_spriteUnchecked;
329 Sprite m_spriteChecked;
330 Sprite m_spriteUncheckedHover;
331 Sprite m_spriteCheckedHover;
332 Sprite m_spriteUncheckedDisabled;
333 Sprite m_spriteCheckedDisabled;
334 Sprite m_spriteUncheckedFocused;
335 Sprite m_spriteCheckedFocused;
336
337 // Cached renderer properties
338 Borders m_bordersCached;
339 TextStyles m_textStyleCached;
340 TextStyles m_textStyleCheckedCached;
341 Color m_checkColorCached;
342 Color m_checkColorHoverCached;
343 Color m_checkColorDisabledCached;
344 Color m_borderColorCached;
345 Color m_borderColorHoverCached;
346 Color m_borderColorDisabledCached;
347 Color m_borderColorFocusedCached;
348 Color m_borderColorCheckedCached;
349 Color m_borderColorCheckedHoverCached;
350 Color m_borderColorCheckedDisabledCached;
351 Color m_borderColorCheckedFocusedCached;
352 Color m_backgroundColorCached;
353 Color m_backgroundColorHoverCached;
354 Color m_backgroundColorDisabledCached;
355 Color m_backgroundColorCheckedCached;
356 Color m_backgroundColorCheckedHoverCached;
357 Color m_backgroundColorCheckedDisabledCached;
358 float m_textDistanceRatioCached = 0.2f;
359 };
360} // namespace tgui
361
362#endif // TGUI_RADIO_BUTTON_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:63
Class to store the position or size of a widget.
Definition Layout.hpp:320
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:158
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:42
RadioButtonRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition RadioButton.hpp:45
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.
SignalBool onUncheck
Radio button was unchecked. Optional parameter: bool which is always false.
Definition RadioButton.hpp:310
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:43
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:309
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:311
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:59
Definition Sprite.hpp:45
Wrapper class to store strings.
Definition String.hpp:94
Wrapper for text styles.
Definition TextStyle.hpp:55
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:53
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
SignalTyped< bool > SignalBool
Signal with one "bool" as optional unbound parameter.
Definition Signal.hpp:410
KeyPressed event parameters.
Definition Event.hpp:166
States used for drawing.
Definition RenderStates.hpp:38