TGUI  1.0-alpha
RadioButton.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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
36namespace tgui
37{
41 class TGUI_API RadioButton : public ClickableWidget
42 {
43 public:
44
45 typedef std::shared_ptr<RadioButton> Ptr;
46 typedef std::shared_ptr<const RadioButton> ConstPtr;
47
48
56 RadioButton(const char* typeName = "RadioButton", bool initRenderer = true);
57
58
66
67
77
78
84 const RadioButtonRenderer* getSharedRenderer() const;
85
92
93
100 void setSize(const Layout2d& size) override;
101 using Widget::setSize;
102
103
112 Vector2f getFullSize() const override;
113
114
125 Vector2f getWidgetOffset() const override;
126
127
135 void setEnabled(bool enabled) override;
136
137
144 virtual void setChecked(bool checked);
145
146
151 bool isChecked() const
152 {
153 return m_checked;
154 }
155
156
163 void setText(const String& text);
164
165
172 const String& getText() const;
173
174
181 void setTextClickable(bool acceptTextClick);
182
183
188 bool isTextClickable() const;
189
190
197 bool isMouseOnWidget(Vector2f pos) const override;
198
202 void leftMouseReleased(Vector2f pos) override;
203
207 void keyPressed(const Event::KeyEvent& event) override;
208
209
217 void draw(BackendRenderTarget& target, RenderStates states) const override;
218
219
221 protected:
222
232 Signal& getSignal(String signalName) override;
233
234
241 void rendererChanged(const String& property) override;
242
243
247 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
248
249
253 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
254
255
259 void updateTextSize() override;
260
261
263 // This function is called when the mouse enters the widget. If requested, a callback will be send.
265 void mouseEnteredWidget() override;
266
267
269 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
271 void mouseLeftWidget() override;
272
273
275 // Returns the size without the borders
277 Vector2f getInnerSize() const;
278
279
281 // Returns the check color that is being used in the current state
283 const Color& getCurrentCheckColor() const;
284
285
287 // Returns the background color that is being used in the current state
289 const Color& getCurrentBackgroundColor() const;
290
291
293 // Returns the border color that is being used in the current state
295 const Color& getCurrentBorderColor() const;
296
297
299 // Resets the sizes of the textures if they are used
301 virtual void updateTextureSizes();
302
303
305 // Updates the text color of the label depending on the current state
307 void updateTextColor();
308
309
311 // Makes a copy of the widget
313 Widget::Ptr clone() const override;
314
315
317 public:
318
319 SignalBool onCheck = {"Checked"};
320 SignalBool onUncheck = {"Unchecked"};
321 SignalBool onChange = {"Changed"};
322
323
325 protected:
326
327 // This is the checked flag. When the radio button is checked then this variable will be true.
328 bool m_checked = false;
329
330 // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
331 bool m_allowTextClick = true;
332
333 // This will contain the text that is written next to radio button.
334 Text m_text;
335
336 Sprite m_spriteUnchecked;
337 Sprite m_spriteChecked;
338 Sprite m_spriteUncheckedHover;
339 Sprite m_spriteCheckedHover;
340 Sprite m_spriteUncheckedDisabled;
341 Sprite m_spriteCheckedDisabled;
342 Sprite m_spriteUncheckedFocused;
343 Sprite m_spriteCheckedFocused;
344
345 // Cached renderer properties
346 Borders m_bordersCached;
347 TextStyles m_textStyleCached;
348 TextStyles m_textStyleCheckedCached;
349 Color m_checkColorCached;
350 Color m_checkColorHoverCached;
351 Color m_checkColorDisabledCached;
352 Color m_borderColorCached;
353 Color m_borderColorHoverCached;
354 Color m_borderColorDisabledCached;
355 Color m_borderColorFocusedCached;
356 Color m_borderColorCheckedCached;
357 Color m_borderColorCheckedHoverCached;
358 Color m_borderColorCheckedDisabledCached;
359 Color m_borderColorCheckedFocusedCached;
360 Color m_backgroundColorCached;
361 Color m_backgroundColorHoverCached;
362 Color m_backgroundColorDisabledCached;
363 Color m_backgroundColorCheckedCached;
364 Color m_backgroundColorCheckedHoverCached;
365 Color m_backgroundColorCheckedDisabledCached;
366 float m_textDistanceRatioCached = 0.2f;
367 };
368
370}
371
373
374#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:63
Class to store the position or size of a widget.
Definition: Layout.hpp:284
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.
bool isChecked() const
Returns whether the radio button is checked or not.
Definition: RadioButton.hpp:151
void setEnabled(bool enabled) override
Enables or disables the widget.
std::shared_ptr< RadioButton > Ptr
Shared widget pointer.
Definition: RadioButton.hpp:45
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.
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...
std::shared_ptr< const RadioButton > ConstPtr
Shared constant widget pointer.
Definition: RadioButton.hpp:46
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.
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.
RadioButtonRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
const String & getText() const
Returns the text of the radio button.
Vector2f getFullSize() const override
Returns the full size of the radio button.
RadioButtonRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static RadioButton::Ptr copy(RadioButton::ConstPtr radioButton)
Makes a copy of another 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.
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.
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:58
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:79
Wrapper for text styles.
Definition: TextStyle.hpp:58
Definition: Text.hpp:48
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
KeyPressed event parameters.
Definition: Event.hpp:167
States used for drawing.
Definition: RenderStates.hpp:39