TGUI  1.0-beta
Loading...
Searching...
No Matches
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 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
68
69
78 static RadioButton::Ptr copy(const RadioButton::ConstPtr& radioButton);
79
80
86 const RadioButtonRenderer* getSharedRenderer() const;
87
94
95
102 void setSize(const Layout2d& size) override;
103 using Widget::setSize;
104
105
114 Vector2f getFullSize() const override;
115
116
127 Vector2f getWidgetOffset() const override;
128
129
137 void setEnabled(bool enabled) override;
138
139
146 virtual void setChecked(bool checked);
147
148
153 bool isChecked() const
154 {
155 return m_checked;
156 }
157
158
165 void setText(const String& text);
166
167
174 const String& getText() const;
175
176
183 void setTextClickable(bool acceptTextClick);
184
185
190 bool isTextClickable() const;
191
192
199 bool isMouseOnWidget(Vector2f pos) const override;
200
204 void leftMouseReleased(Vector2f pos) override;
205
209 void keyPressed(const Event::KeyEvent& event) override;
210
211
219 void draw(BackendRenderTarget& target, RenderStates states) const override;
220
221
223 protected:
224
234 Signal& getSignal(String signalName) override;
235
236
243 void rendererChanged(const String& property) override;
244
245
249 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
250
251
255 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
256
257
261 void updateTextSize() override;
262
263
265 // This function is called when the mouse enters the widget. If requested, a callback will be send.
267 void mouseEnteredWidget() override;
268
269
271 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
273 void mouseLeftWidget() override;
274
275
277 // Returns the size without the borders
279 Vector2f getInnerSize() const;
280
281
283 // Returns the check color that is being used in the current state
285 const Color& getCurrentCheckColor() const;
286
287
289 // Returns the background color that is being used in the current state
291 const Color& getCurrentBackgroundColor() const;
292
293
295 // Returns the border color that is being used in the current state
297 const Color& getCurrentBorderColor() const;
298
299
301 // Resets the sizes of the textures if they are used
303 virtual void updateTextureSizes();
304
305
307 // Updates the text color of the label depending on the current state
309 void updateTextColor();
310
311
313 // Makes a copy of the widget
315 Widget::Ptr clone() const override;
316
317
319 public:
320
321 SignalBool onCheck = {"Checked"};
322 SignalBool onUncheck = {"Unchecked"};
323 SignalBool onChange = {"Changed"};
324
325
327 protected:
328
329 // This is the checked flag. When the radio button is checked then this variable will be true.
330 bool m_checked = false;
331
332 // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
333 bool m_allowTextClick = true;
334
335 // This will contain the text that is written next to radio button.
336 Text m_text;
337
338 Sprite m_spriteUnchecked;
339 Sprite m_spriteChecked;
340 Sprite m_spriteUncheckedHover;
341 Sprite m_spriteCheckedHover;
342 Sprite m_spriteUncheckedDisabled;
343 Sprite m_spriteCheckedDisabled;
344 Sprite m_spriteUncheckedFocused;
345 Sprite m_spriteCheckedFocused;
346
347 // Cached renderer properties
348 Borders m_bordersCached;
349 TextStyles m_textStyleCached;
350 TextStyles m_textStyleCheckedCached;
351 Color m_checkColorCached;
352 Color m_checkColorHoverCached;
353 Color m_checkColorDisabledCached;
354 Color m_borderColorCached;
355 Color m_borderColorHoverCached;
356 Color m_borderColorDisabledCached;
357 Color m_borderColorFocusedCached;
358 Color m_borderColorCheckedCached;
359 Color m_borderColorCheckedHoverCached;
360 Color m_borderColorCheckedDisabledCached;
361 Color m_borderColorCheckedFocusedCached;
362 Color m_backgroundColorCached;
363 Color m_backgroundColorHoverCached;
364 Color m_backgroundColorDisabledCached;
365 Color m_backgroundColorCheckedCached;
366 Color m_backgroundColorCheckedHoverCached;
367 Color m_backgroundColorCheckedDisabledCached;
368 float m_textDistanceRatioCached = 0.2f;
369 };
370
372}
373
375
376#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:153
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: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.
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.
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.
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.
std::shared_ptr< const RadioButton > ConstPtr
Shared constant widget pointer.
Definition: RadioButton.hpp:46
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
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