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 {
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 
61  static RadioButton::Ptr create();
62 
63 
72  static RadioButton::Ptr copy(RadioButton::ConstPtr radioButton);
73 
74 
79  RadioButtonRenderer* getSharedRenderer();
80  const RadioButtonRenderer* getSharedRenderer() const;
81 
87  RadioButtonRenderer* getRenderer();
88  const RadioButtonRenderer* getRenderer() const;
89 
90 
97  void setSize(const Layout2d& size) override;
98  using Widget::setSize;
99 
100 
109  sf::Vector2f getFullSize() const override;
110 
111 
122  sf::Vector2f getWidgetOffset() const override;
123 
124 
131  virtual void check();
132 
133 
138  virtual void uncheck();
139 
140 
147  bool isChecked() const
148  {
149  return m_checked;
150  }
151 
152 
159  void setText(const sf::String& text);
160 
161 
168  const sf::String& getText() const;
169 
170 
178  void setTextSize(unsigned int size);
179 
180 
187  unsigned int getTextSize() const;
188 
189 
196  void setTextClickable(bool acceptTextClick);
197 
198 
203  bool isTextClickable() const;
204 
205 
212  bool mouseOnWidget(sf::Vector2f pos) const override;
213 
217  void leftMouseReleased(sf::Vector2f pos) override;
218 
222  void keyPressed(const sf::Event::KeyEvent& event) override;
223 
227  void widgetFocused() override;
228 
229 
237  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
238 
239 
241  protected:
242 
252  Signal& getSignal(std::string signalName) override;
253 
254 
261  void rendererChanged(const std::string& property) 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  sf::Vector2f getInnerSize() const;
280 
281 
283  // Returns the check color that is being used in the current state
285  sf::Color getCurrentCheckColor() const;
286 
287 
289  // Returns the background color that is being used in the current state
291  sf::Color getCurrentBackgroundColor() const;
292 
293 
295  // Returns the border color that is being used in the current state
297  sf::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  return std::make_shared<RadioButton>(*this);
318  }
319 
320 
322  public:
323 
324  SignalBool onCheck = {"Checked"};
325  SignalBool onUncheck = {"Unchecked"};
326 
327 
329  protected:
330 
331  // This is the checked flag. When the radio button is checked then this variable will be true.
332  bool m_checked = false;
333 
334  // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
335  bool m_allowTextClick = true;
336 
337  // This will contain the text that is written next to radio button.
338  Text m_text;
339 
340  // This will store the size of the text ( 0 to auto size )
341  unsigned int m_textSize = 0;
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_spriteFocused;
350 
351  // Cached renderer properties
352  Borders m_bordersCached;
353  TextStyle m_textStyleCached;
354  TextStyle m_textStyleCheckedCached;
355  Color m_checkColorCached;
356  Color m_checkColorHoverCached;
357  Color m_checkColorDisabledCached;
358  Color m_borderColorCached;
359  Color m_borderColorHoverCached;
360  Color m_borderColorDisabledCached;
361  Color m_borderColorCheckedCached;
362  Color m_borderColorCheckedHoverCached;
363  Color m_borderColorCheckedDisabledCached;
364  Color m_backgroundColorCached;
365  Color m_backgroundColorHoverCached;
366  Color m_backgroundColorDisabledCached;
367  Color m_backgroundColorCheckedCached;
368  Color m_backgroundColorCheckedHoverCached;
369  Color m_backgroundColorCheckedDisabledCached;
370  float m_textDistanceRatioCached = 0;
371  };
372 
374 }
375 
377 
378 #endif // TGUI_RADIO_BUTTON_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
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:243
Radio button widget.
Definition: RadioButton.hpp:41
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: RadioButton.hpp:315
Definition: Text.hpp:41
Clickable widget.
Definition: ClickableWidget.hpp:39
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: RadioButtonRenderer.hpp:36
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:66
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
Definition: Outline.hpp:38
std::shared_ptr< RadioButton > Ptr
Shared widget pointer.
Definition: RadioButton.hpp:45
bool isChecked() const
Returns whether the radio button is checked or not.
Definition: RadioButton.hpp:147
std::shared_ptr< const RadioButton > ConstPtr
Shared constant widget pointer.
Definition: RadioButton.hpp:46