TGUI  0.10-beta
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 const RadioButtonRenderer* getRenderer() const;
93
94
101 void setSize(const Layout2d& size) override;
102 using Widget::setSize;
103
104
113 Vector2f getFullSize() const override;
114
115
126 Vector2f getWidgetOffset() const override;
127
128
135 virtual void setChecked(bool checked);
136
137
142 bool isChecked() const
143 {
144 return m_checked;
145 }
146
147
154 void setText(const String& text);
155
156
163 const String& getText() const;
164
165
172 void setTextClickable(bool acceptTextClick);
173
174
179 bool isTextClickable() const;
180
181
188 bool isMouseOnWidget(Vector2f pos) const override;
189
193 void leftMouseReleased(Vector2f pos) override;
194
198 void keyPressed(const Event::KeyEvent& event) override;
199
200
208 void draw(BackendRenderTarget& target, RenderStates states) const override;
209
210
212 protected:
213
223 Signal& getSignal(String signalName) override;
224
225
232 void rendererChanged(const String& property) override;
233
234
238 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
239
240
244 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
245
246
250 void updateTextSize() override;
251
252
254 // This function is called when the mouse enters the widget. If requested, a callback will be send.
256 void mouseEnteredWidget() override;
257
258
260 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
262 void mouseLeftWidget() override;
263
264
266 // Returns the size without the borders
268 Vector2f getInnerSize() const;
269
270
272 // Returns the check color that is being used in the current state
274 const Color& getCurrentCheckColor() const;
275
276
278 // Returns the background color that is being used in the current state
280 const Color& getCurrentBackgroundColor() const;
281
282
284 // Returns the border color that is being used in the current state
286 const Color& getCurrentBorderColor() const;
287
288
290 // Resets the sizes of the textures if they are used
292 virtual void updateTextureSizes();
293
294
296 // Updates the text color of the label depending on the current state
298 void updateTextColor();
299
300
302 // Makes a copy of the widget
304 Widget::Ptr clone() const override;
305
306
308 public:
309
310 SignalBool onCheck = {"Checked"};
311 SignalBool onUncheck = {"Unchecked"};
312 SignalBool onChange = {"Changed"};
313
314
316 protected:
317
318 // This is the checked flag. When the radio button is checked then this variable will be true.
319 bool m_checked = false;
320
321 // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
322 bool m_allowTextClick = true;
323
324 // This will contain the text that is written next to radio button.
325 Text m_text;
326
327 Sprite m_spriteUnchecked;
328 Sprite m_spriteChecked;
329 Sprite m_spriteUncheckedHover;
330 Sprite m_spriteCheckedHover;
331 Sprite m_spriteUncheckedDisabled;
332 Sprite m_spriteCheckedDisabled;
333 Sprite m_spriteUncheckedFocused;
334 Sprite m_spriteCheckedFocused;
335
336 // Cached renderer properties
337 Borders m_bordersCached;
338 TextStyles m_textStyleCached;
339 TextStyles m_textStyleCheckedCached;
340 Color m_checkColorCached;
341 Color m_checkColorHoverCached;
342 Color m_checkColorDisabledCached;
343 Color m_borderColorCached;
344 Color m_borderColorHoverCached;
345 Color m_borderColorDisabledCached;
346 Color m_borderColorFocusedCached;
347 Color m_borderColorCheckedCached;
348 Color m_borderColorCheckedHoverCached;
349 Color m_borderColorCheckedDisabledCached;
350 Color m_borderColorCheckedFocusedCached;
351 Color m_backgroundColorCached;
352 Color m_backgroundColorHoverCached;
353 Color m_backgroundColorDisabledCached;
354 Color m_backgroundColorCheckedCached;
355 Color m_backgroundColorCheckedHoverCached;
356 Color m_backgroundColorCheckedDisabledCached;
357 float m_textDistanceRatioCached = 0.2f;
358 };
359
361}
362
364
365#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:142
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