TGUI  1.3-dev
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
26#ifndef TGUI_RADIO_BUTTON_HPP
27#define TGUI_RADIO_BUTTON_HPP
28
29
30#include <TGUI/Renderers/RadioButtonRenderer.hpp>
31#include <TGUI/Widgets/ClickableWidget.hpp>
32#include <TGUI/Text.hpp>
33
35
36TGUI_MODULE_EXPORT namespace tgui
37{
41 class TGUI_API RadioButton : public ClickableWidget
42 {
43 public:
44
45 using Ptr = std::shared_ptr<RadioButton>;
46 using ConstPtr = std::shared_ptr<const RadioButton>;
47
48 static constexpr const char StaticWidgetType[] = "RadioButton";
49
50
58 RadioButton(const char* typeName = StaticWidgetType, bool initRenderer = true);
59
60
67 TGUI_NODISCARD static RadioButton::Ptr create();
68
69
78 TGUI_NODISCARD static RadioButton::Ptr copy(const RadioButton::ConstPtr& radioButton);
79
80
85 TGUI_NODISCARD RadioButtonRenderer* getSharedRenderer() override;
86 TGUI_NODISCARD const RadioButtonRenderer* getSharedRenderer() const override;
87
93 TGUI_NODISCARD RadioButtonRenderer* getRenderer() override;
94
95
102 void setSize(const Layout2d& size) override;
103 using Widget::setSize;
104
105
114 TGUI_NODISCARD Vector2f getFullSize() const override;
115
116
127 TGUI_NODISCARD Vector2f getWidgetOffset() const override;
128
129
137 void setEnabled(bool enabled) override;
138
139
146 virtual void setChecked(bool checked);
147
148
153 TGUI_NODISCARD bool isChecked() const
154 {
155 return m_checked;
156 }
157
158
165 void setText(const String& text);
166
167
174 TGUI_NODISCARD const String& getText() const;
175
176
183 void setTextClickable(bool acceptTextClick);
184
185
190 TGUI_NODISCARD bool isTextClickable() const;
191
192
199 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
200
204 void leftMouseReleased(Vector2f pos) override;
205
209 void keyPressed(const Event::KeyEvent& event) override;
210
220 bool canHandleKeyPress(const Event::KeyEvent& event) override;
221
222
230 void draw(BackendRenderTarget& target, RenderStates states) const override;
231
232
234 protected:
235
245 TGUI_NODISCARD Signal& getSignal(String signalName) override;
246
247
254 void rendererChanged(const String& property) override;
255
256
260 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
261
262
266 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
267
268
272 void updateTextSize() override;
273
274
276 // This function is called when the mouse enters the widget. If requested, a callback will be send.
278 void mouseEnteredWidget() override;
279
280
282 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
284 void mouseLeftWidget() override;
285
286
288 // Returns the size without the borders
290 TGUI_NODISCARD Vector2f getInnerSize() const;
291
292
294 // Returns the check color that is being used in the current state
296 TGUI_NODISCARD const Color& getCurrentCheckColor() const;
297
298
300 // Returns the background color that is being used in the current state
302 TGUI_NODISCARD const Color& getCurrentBackgroundColor() const;
303
304
306 // Returns the border color that is being used in the current state
308 TGUI_NODISCARD const Color& getCurrentBorderColor() const;
309
310
312 // Resets the sizes of the textures if they are used
314 virtual void updateTextureSizes();
315
316
318 // Updates the text color of the label depending on the current state
320 void updateTextColor();
321
322
324 // Makes a copy of the widget
326 TGUI_NODISCARD Widget::Ptr clone() const override;
327
328
330 public:
331
332 SignalBool onCheck = {"Checked"};
333 SignalBool onUncheck = {"Unchecked"};
334 SignalBool onChange = {"Changed"};
335
336
338 protected:
339
340 // This is the checked flag. When the radio button is checked then this variable will be true.
341 bool m_checked = false;
342
343 // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
344 bool m_allowTextClick = true;
345
346 // This will contain the text that is written next to radio button.
347 Text m_text;
348
349 Sprite m_spriteUnchecked;
350 Sprite m_spriteChecked;
351 Sprite m_spriteUncheckedHover;
352 Sprite m_spriteCheckedHover;
353 Sprite m_spriteUncheckedDisabled;
354 Sprite m_spriteCheckedDisabled;
355 Sprite m_spriteUncheckedFocused;
356 Sprite m_spriteCheckedFocused;
357
358 // Cached renderer properties
359 Borders m_bordersCached;
360 TextStyles m_textStyleCached;
361 TextStyles m_textStyleCheckedCached;
362 Color m_checkColorCached;
363 Color m_checkColorHoverCached;
364 Color m_checkColorDisabledCached;
365 Color m_borderColorCached;
366 Color m_borderColorHoverCached;
367 Color m_borderColorDisabledCached;
368 Color m_borderColorFocusedCached;
369 Color m_borderColorCheckedCached;
370 Color m_borderColorCheckedHoverCached;
371 Color m_borderColorCheckedDisabledCached;
372 Color m_borderColorCheckedFocusedCached;
373 Color m_backgroundColorCached;
374 Color m_backgroundColorHoverCached;
375 Color m_backgroundColorDisabledCached;
376 Color m_backgroundColorCheckedCached;
377 Color m_backgroundColorCheckedHoverCached;
378 Color m_backgroundColorCheckedDisabledCached;
379 float m_textDistanceRatioCached = 0.2f;
380 };
381
383}
384
386
387#endif // TGUI_RADIO_BUTTON_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Clickable widget.
Definition ClickableWidget.hpp:40
Wrapper for colors.
Definition Color.hpp:72
Class to store the position or size of a widget.
Definition Layout.hpp:305
Definition Outline.hpp:39
Definition RadioButtonRenderer.hpp:37
Radio button widget.
Definition RadioButton.hpp:42
void setText(const String &text)
Changes the text of the radio button.
virtual void setChecked(bool checked)
Checks or unchecks the radio button.
static TGUI_NODISCARD RadioButton::Ptr copy(const RadioButton::ConstPtr &radioButton)
Makes a copy of another radio button.
TGUI_NODISCARD RadioButtonRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD 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 setEnabled(bool enabled) override
Enables or disables the widget.
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:45
TGUI_NODISCARD 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 setSize(const Layout2d &size) override
Changes the size of the radio button.
TGUI_NODISCARD bool isChecked() const
Returns whether the radio button is checked or not.
Definition RadioButton.hpp:153
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
TGUI_NODISCARD const String & getText() const
Returns the text of the radio button.
void mouseLeftWidget() override
This function is called when the mouse leaves the widget.
TGUI_NODISCARD Vector2f getFullSize() const override
Returns the full size of the radio button.
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
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.
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:46
static TGUI_NODISCARD RadioButton::Ptr create()
Creates a new radio button widget.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
TGUI_NODISCARD bool isTextClickable() const
Returns whether the radio button can be checked by clicking on the text next to it.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
TGUI_NODISCARD RadioButtonRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
TGUI_NODISCARD Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
Definition Sprite.hpp:48
Wrapper class to store strings.
Definition String.hpp:101
Wrapper for text styles.
Definition TextStyle.hpp:57
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:50
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
KeyPressed event parameters.
Definition Event.hpp:169
States used for drawing.
Definition RenderStates.hpp:39