TGUI  0.8-dev
RadioButton.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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 {
53  class TGUI_API RadioButton : public ClickableWidget
54  {
55  public:
56 
57  typedef std::shared_ptr<RadioButton> Ptr;
58  typedef std::shared_ptr<const RadioButton> ConstPtr;
59 
60 
62  // Default constructor
64  RadioButton();
65 
66 
73  static RadioButton::Ptr create();
74 
75 
84  static RadioButton::Ptr copy(RadioButton::ConstPtr radioButton);
85 
86 
94  {
95  return aurora::downcast<RadioButtonRenderer*>(m_renderer.get());
96  }
97 
98 
105  virtual void setSize(const Layout2d& size) override;
107 
108 
117  virtual sf::Vector2f getFullSize() const override;
118 
119 
130  virtual sf::Vector2f getWidgetOffset() const override;
131 
132 
139  virtual void check();
140 
141 
146  virtual void uncheck();
147 
148 
155  bool isChecked() const
156  {
157  return m_checked;
158  }
159 
160 
167  void setText(const sf::String& text);
168 
169 
176  const sf::String& getText() const;
177 
178 
186  void setTextSize(unsigned int size);
187 
188 
195  unsigned int getTextSize() const;
196 
197 
204  void setTextClickable(bool acceptTextClick);
205 
206 
211  bool isTextClickable() const;
212 
213 
217  virtual bool mouseOnWidget(sf::Vector2f pos) const override;
218 
222  virtual void leftMouseReleased(sf::Vector2f pos) override;
223 
227  virtual void keyPressed(const sf::Event::KeyEvent& event) override;
228 
232  virtual void widgetFocused() override;
233 
234 
242  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
243 
244 
246  protected:
247 
254  virtual void rendererChanged(const std::string& property) override;
255 
256 
258  // This function is called when the mouse enters the widget. If requested, a callback will be send.
260  virtual void mouseEnteredWidget() override;
261 
262 
264  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
266  virtual void mouseLeftWidget() override;
267 
268 
270  // Returns the size without the borders
272  sf::Vector2f getInnerSize() const;
273 
274 
276  // Returns the check color that is being used in the current state
278  sf::Color getCurrentCheckColor() const;
279 
280 
282  // Returns the background color that is being used in the current state
284  sf::Color getCurrentBackgroundColor() const;
285 
286 
288  // Returns the border color that is being used in the current state
290  sf::Color getCurrentBorderColor() const;
291 
292 
294  // Resets the sizes of the textures if they are used
296  virtual void updateTextureSizes();
297 
298 
300  // Updates the text color of the label depending on the current state
302  void updateTextColor();
303 
304 
306  // Makes a copy of the widget
308  virtual Widget::Ptr clone() const override
309  {
310  return std::make_shared<RadioButton>(*this);
311  }
312 
313 
315  protected:
316 
317  // This is the checked flag. When the radio button is checked then this variable will be true.
318  bool m_checked = false;
319 
320  // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
321  bool m_allowTextClick = true;
322 
323  // This will contain the text that is written next to radio button.
324  Text m_text;
325 
326  // This will store the size of the text ( 0 to auto size )
327  unsigned int m_textSize = 0;
328 
329  Sprite m_spriteUnchecked;
330  Sprite m_spriteChecked;
331  Sprite m_spriteUncheckedHover;
332  Sprite m_spriteCheckedHover;
333  Sprite m_spriteUncheckedDisabled;
334  Sprite m_spriteCheckedDisabled;
335  Sprite m_spriteFocused;
336 
337  // Cached renderer properties
338  Borders m_bordersCached;
339  TextStyle m_textStyleCached;
340  TextStyle m_textStyleCheckedCached;
341  Color m_checkColorCached;
342  Color m_checkColorHoverCached;
343  Color m_checkColorDisabledCached;
344  Color m_borderColorCached;
345  Color m_borderColorHoverCached;
346  Color m_borderColorDisabledCached;
347  Color m_borderColorCheckedCached;
348  Color m_borderColorCheckedHoverCached;
349  Color m_borderColorCheckedDisabledCached;
350  Color m_backgroundColorCached;
351  Color m_backgroundColorHoverCached;
352  Color m_backgroundColorDisabledCached;
353  Color m_backgroundColorCheckedCached;
354  Color m_backgroundColorCheckedHoverCached;
355  Color m_backgroundColorCheckedDisabledCached;
356  float m_textDistanceRatioCached = 0;
357  };
358 
360 }
361 
363 
364 #endif // TGUI_RADIO_BUTTON_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Wrapper for text styles.
Definition: TextStyle.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:263
Radio button widget.
Definition: RadioButton.hpp:53
RadioButtonRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: RadioButton.hpp:93
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: Text.hpp:41
Clickable widget.
Definition: ClickableWidget.hpp:55
Definition: RadioButtonRenderer.hpp:36
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Definition: Outline.hpp:37
std::shared_ptr< RadioButton > Ptr
Shared widget pointer.
Definition: RadioButton.hpp:57
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: RadioButton.hpp:308
bool isChecked() const
Returns whether the radio button is checked or not.
Definition: RadioButton.hpp:155
std::shared_ptr< const RadioButton > ConstPtr
Shared constant widget pointer.
Definition: RadioButton.hpp:58