TGUI  0.8.0
RadioButton.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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  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);
170 
171 
178  unsigned int getTextSize() const;
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 
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  // This will store the size of the text ( 0 to auto size )
339  unsigned int m_textSize = 0;
340 
341  Sprite m_spriteUnchecked;
342  Sprite m_spriteChecked;
343  Sprite m_spriteUncheckedHover;
344  Sprite m_spriteCheckedHover;
345  Sprite m_spriteUncheckedDisabled;
346  Sprite m_spriteCheckedDisabled;
347  Sprite m_spriteUncheckedFocused;
348  Sprite m_spriteCheckedFocused;
349 
350  // Cached renderer properties
351  Borders m_bordersCached;
352  TextStyle m_textStyleCached;
353  TextStyle m_textStyleCheckedCached;
354  Color m_checkColorCached;
355  Color m_checkColorHoverCached;
356  Color m_checkColorDisabledCached;
357  Color m_borderColorCached;
358  Color m_borderColorHoverCached;
359  Color m_borderColorDisabledCached;
360  Color m_borderColorFocusedCached;
361  Color m_borderColorCheckedCached;
362  Color m_borderColorCheckedHoverCached;
363  Color m_borderColorCheckedDisabledCached;
364  Color m_borderColorCheckedFocusedCached;
365  Color m_backgroundColorCached;
366  Color m_backgroundColorHoverCached;
367  Color m_backgroundColorDisabledCached;
368  Color m_backgroundColorCheckedCached;
369  Color m_backgroundColorCheckedHoverCached;
370  Color m_backgroundColorCheckedDisabledCached;
371  float m_textDistanceRatioCached = 0.2f;
372  };
373 
375 }
376 
378 
379 #endif // TGUI_RADIO_BUTTON_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:43
Wrapper for colors.
Definition: Color.hpp:47
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
Definition: Vector2f.hpp:37
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: RadioButton.hpp:313
Definition: Text.hpp:42
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:76
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:138
std::shared_ptr< const RadioButton > ConstPtr
Shared constant widget pointer.
Definition: RadioButton.hpp:46