TGUI  0.9.1
RadioButton.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 
36 namespace 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 
173  void setTextSize(unsigned int size) override;
174 
175 
182  unsigned int getTextSize() const override;
183 
184 
191  void setTextClickable(bool acceptTextClick);
192 
193 
198  bool isTextClickable() const;
199 
200 
207  bool isMouseOnWidget(Vector2f pos) const override;
208 
212  void leftMouseReleased(Vector2f pos) override;
213 
217  void keyPressed(const Event::KeyEvent& event) override;
218 
219 
227  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
228 
229 
231  protected:
232 
242  Signal& getSignal(String signalName) override;
243 
244 
251  void rendererChanged(const String& property) override;
252 
253 
257  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
258 
259 
263  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
264 
265 
267  // This function is called when the mouse enters the widget. If requested, a callback will be send.
269  void mouseEnteredWidget() override;
270 
271 
273  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
275  void mouseLeftWidget() override;
276 
277 
279  // Returns the size without the borders
281  Vector2f getInnerSize() const;
282 
283 
285  // Returns the check color that is being used in the current state
287  const Color& getCurrentCheckColor() const;
288 
289 
291  // Returns the background color that is being used in the current state
293  const Color& getCurrentBackgroundColor() const;
294 
295 
297  // Returns the border color that is being used in the current state
299  const Color& getCurrentBorderColor() const;
300 
301 
303  // Resets the sizes of the textures if they are used
305  virtual void updateTextureSizes();
306 
307 
309  // Updates the text color of the label depending on the current state
311  void updateTextColor();
312 
313 
315  // Makes a copy of the widget
317  Widget::Ptr clone() const override
318  {
319  return std::make_shared<RadioButton>(*this);
320  }
321 
322 
324  public:
325 
326  SignalBool onCheck = {"Checked"};
327  SignalBool onUncheck = {"Unchecked"};
328  SignalBool onChange = {"Changed"};
329 
330 
332  protected:
333 
334  // This is the checked flag. When the radio button is checked then this variable will be true.
335  bool m_checked = false;
336 
337  // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
338  bool m_allowTextClick = true;
339 
340  // This will contain the text that is written next to radio button.
341  Text m_text;
342 
343  Sprite m_spriteUnchecked;
344  Sprite m_spriteChecked;
345  Sprite m_spriteUncheckedHover;
346  Sprite m_spriteCheckedHover;
347  Sprite m_spriteUncheckedDisabled;
348  Sprite m_spriteCheckedDisabled;
349  Sprite m_spriteUncheckedFocused;
350  Sprite m_spriteCheckedFocused;
351 
352  // Cached renderer properties
353  Borders m_bordersCached;
354  TextStyles m_textStyleCached;
355  TextStyles m_textStyleCheckedCached;
356  Color m_checkColorCached;
357  Color m_checkColorHoverCached;
358  Color m_checkColorDisabledCached;
359  Color m_borderColorCached;
360  Color m_borderColorHoverCached;
361  Color m_borderColorDisabledCached;
362  Color m_borderColorFocusedCached;
363  Color m_borderColorCheckedCached;
364  Color m_borderColorCheckedHoverCached;
365  Color m_borderColorCheckedDisabledCached;
366  Color m_borderColorCheckedFocusedCached;
367  Color m_backgroundColorCached;
368  Color m_backgroundColorHoverCached;
369  Color m_backgroundColorDisabledCached;
370  Color m_backgroundColorCheckedCached;
371  Color m_backgroundColorCheckedHoverCached;
372  Color m_backgroundColorCheckedDisabledCached;
373  float m_textDistanceRatioCached = 0.2f;
374  };
375 
377 }
378 
380 
381 #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:52
Class to store the position or size of a widget.
Definition: Layout.hpp:262
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
void setTextSize(unsigned int size) override
Changes the character size of the text.
void setTextClickable(bool acceptTextClick)
Allows (or disallows) the radio button to be checked by clicking on the text next to it.
const String & getText() const
Returns the text of the radio button.
void draw(BackendRenderTargetBase &target, RenderStates states) const override
Draw the widget to a render target.
RadioButtonRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: RadioButton.hpp:317
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 rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
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.
Vector2f getFullSize() const override
Returns the full size of the radio button.
static RadioButton::Ptr copy(RadioButton::ConstPtr radioButton)
Makes a copy of another radio button.
RadioButtonRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void mouseEnteredWidget() override
This function is called when the mouse enters the widget.
unsigned int getTextSize() const override
Returns the character size of the text.
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.
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:49
Wrapper class to store strings.
Definition: String.hpp:74
Wrapper for text styles.
Definition: TextStyle.hpp:58
Definition: Text.hpp:44
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