TGUI  0.8.8
RadioButton.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 
50  // Default constructor
52  RadioButton();
53 
54 
62 
63 
73 
74 
80  const RadioButtonRenderer* getSharedRenderer() const;
81 
88  const RadioButtonRenderer* getRenderer() const;
89 
90 
97  void setSize(const Layout2d& size) override;
98  using Widget::setSize;
99 
100 
109  Vector2f getFullSize() const override;
110 
111 
122  Vector2f getWidgetOffset() const override;
123 
124 
131  virtual void setChecked(bool checked);
132 
133 
138  bool isChecked() const
139  {
140  return m_checked;
141  }
142 
143 
150  void setText(const sf::String& text);
151 
152 
159  const sf::String& getText() const;
160 
161 
169  void setTextSize(unsigned int size) override;
170 
171 
178  unsigned int getTextSize() const override;
179 
180 
187  void setTextClickable(bool acceptTextClick);
188 
189 
194  bool isTextClickable() const;
195 
196 
203  bool mouseOnWidget(Vector2f pos) const override;
204 
208  void leftMouseReleased(Vector2f pos) override;
209 
213  void keyPressed(const sf::Event::KeyEvent& event) override;
214 
215 
223  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
224 
225 
227  protected:
228 
238  Signal& getSignal(std::string signalName) override;
239 
240 
247  void rendererChanged(const std::string& property) override;
248 
249 
253  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
254 
255 
259  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) 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  return std::make_shared<RadioButton>(*this);
316  }
317 
318 
320  public:
321 
322  SignalBool onCheck = {"Checked"};
323  SignalBool onUncheck = {"Unchecked"};
324  SignalBool onChange = {"Changed"};
325 
326 
328  protected:
329 
330  // This is the checked flag. When the radio button is checked then this variable will be true.
331  bool m_checked = false;
332 
333  // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
334  bool m_allowTextClick = true;
335 
336  // This will contain the text that is written next to radio button.
337  Text m_text;
338 
339  Sprite m_spriteUnchecked;
340  Sprite m_spriteChecked;
341  Sprite m_spriteUncheckedHover;
342  Sprite m_spriteCheckedHover;
343  Sprite m_spriteUncheckedDisabled;
344  Sprite m_spriteCheckedDisabled;
345  Sprite m_spriteUncheckedFocused;
346  Sprite m_spriteCheckedFocused;
347 
348  // Cached renderer properties
349  Borders m_bordersCached;
350  TextStyle m_textStyleCached;
351  TextStyle m_textStyleCheckedCached;
352  Color m_checkColorCached;
353  Color m_checkColorHoverCached;
354  Color m_checkColorDisabledCached;
355  Color m_borderColorCached;
356  Color m_borderColorHoverCached;
357  Color m_borderColorDisabledCached;
358  Color m_borderColorFocusedCached;
359  Color m_borderColorCheckedCached;
360  Color m_borderColorCheckedHoverCached;
361  Color m_borderColorCheckedDisabledCached;
362  Color m_borderColorCheckedFocusedCached;
363  Color m_backgroundColorCached;
364  Color m_backgroundColorHoverCached;
365  Color m_backgroundColorDisabledCached;
366  Color m_backgroundColorCheckedCached;
367  Color m_backgroundColorCheckedHoverCached;
368  Color m_backgroundColorCheckedDisabledCached;
369  float m_textDistanceRatioCached = 0.2f;
370  };
371 
373 }
374 
376 
377 #endif // TGUI_RADIO_BUTTON_HPP
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
void mouseLeftWidget() override
This function is called when the mouse leaves the widget.
const sf::String & getText() const
Returns the text of the radio button.
static RadioButton::Ptr create()
Creates a new radio button widget.
void setSize(const Layout2d &size) override
Changes the size of the 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.
Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
Definition: Text.hpp:43
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
bool isChecked() const
Returns whether the radio button is checked or not.
Definition: RadioButton.hpp:138
Wrapper for colors.
Definition: Color.hpp:49
RadioButtonRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
void setTextSize(unsigned int size) override
Changes the character size of the text.
std::shared_ptr< const RadioButton > ConstPtr
Shared constant widget pointer.
Definition: RadioButton.hpp:46
Vector2f getFullSize() const override
Returns the full size of the radio button.
Definition: Sprite.hpp:46
Definition: RadioButtonRenderer.hpp:37
static RadioButton::Ptr copy(RadioButton::ConstPtr radioButton)
Makes a copy of another radio button.
void setText(const sf::String &text)
Changes the text of the radio button.
Radio button widget.
Definition: RadioButton.hpp:42
Definition: Vector2f.hpp:39
Definition: Outline.hpp:39
virtual void setChecked(bool checked)
Checks or unchecks the radio button.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void setTextClickable(bool acceptTextClick)
Allows (or disallows) the radio button to be checked by clicking on the text next to it.
bool isTextClickable() const
Returns whether the radio button can be checked by clicking on the text next to it.
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
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 load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
Clickable widget.
Definition: ClickableWidget.hpp:40
std::shared_ptr< RadioButton > Ptr
Shared widget pointer.
Definition: RadioButton.hpp:45
unsigned int getTextSize() const override
Returns the character size of the text.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: RadioButton.hpp:313
Wrapper for text styles.
Definition: TextStyle.hpp:47