TGUI  0.7.4
RadioButton.hpp
1 //
3 // TGUI - Texus's 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/Widgets/Label.hpp>
31 
33 
34 namespace tgui
35 {
36  class RadioButtonRenderer;
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 
93  std::shared_ptr<RadioButtonRenderer> getRenderer() const
94  {
95  return std::static_pointer_cast<RadioButtonRenderer>(m_renderer);
96  }
97 
98 
111  virtual void setPosition(const Layout2d& position) override;
113 
114 
121  virtual void setSize(const Layout2d& size) override;
123 
124 
133  virtual sf::Vector2f getFullSize() const override;
134 
135 
144  virtual void setFont(const Font& font) override;
145 
146 
153  virtual void check();
154 
155 
160  virtual void uncheck();
161 
162 
169  bool isChecked() const
170  {
171  return m_checked;
172  }
173 
174 
181  void setText(const sf::String& text);
182 
183 
190  sf::String getText() const
191  {
192  return m_text.getText();
193  }
194 
195 
203  void setTextSize(unsigned int size);
204 
205 
212  unsigned int getTextSize() const
213  {
214  return m_text.getTextSize();
215  }
216 
217 
224  void allowTextClick(bool acceptTextClick = true);
225 
226 
233  virtual void setOpacity(float opacity) override;
234 
235 
239  virtual bool mouseOnWidget(float x, float y) const override;
240 
244  virtual void leftMouseReleased(float x, float y) override;
245 
249  virtual void keyPressed(const sf::Event::KeyEvent& event) override;
250 
254  virtual void widgetFocused() override;
255 
256 
258  protected:
259 
272  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
273 
274 
276  // Makes a copy of the widget
278  virtual Widget::Ptr clone() const override
279  {
280  return std::make_shared<RadioButton>(*this);
281  }
282 
283 
285  // This function is called when the mouse enters the widget. If requested, a callback will be send.
287  virtual void mouseEnteredWidget() override;
288 
289 
291  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
293  virtual void mouseLeftWidget() override;
294 
295 
297  // Draws the widget on the render target.
299  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
300 
301 
303  protected:
304 
305  // This is the checked flag. When the radio button is checked then this variable will be true.
306  bool m_checked = false;
307 
308  // When this boolean is true (default) then the radio button will also be checked by clicking on the text.
309  bool m_allowTextClick = true;
310 
311  // This will contain the text that is written next to radio button.
312  Label m_text;
313 
314  // This will store the size of the text ( 0 to auto size )
315  unsigned int m_textSize = 0;
316 
317  friend class RadioButtonRenderer;
318  friend class CheckBoxRenderer;
319 
321  };
322 
323 
325 
326  class TGUI_API RadioButtonRenderer : public WidgetRenderer, public WidgetPadding
327  {
328  public:
329 
336  RadioButtonRenderer(RadioButton* radioButton) : m_radioButton{radioButton} {}
337 
338 
348  virtual void setProperty(std::string property, const std::string& value) override;
349 
350 
361  virtual void setProperty(std::string property, ObjectConverter&& value) override;
362 
363 
373  virtual ObjectConverter getProperty(std::string property) const override;
374 
375 
382  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
383 
384 
396  void setTextColor(const Color& color);
397 
398 
405  void setTextColorNormal(const Color& color);
406 
407 
414  void setTextColorHover(const Color& color);
415 
416 
432  void setBackgroundColor(const Color& color);
433 
434 
445  void setBackgroundColorNormal(const Color& color);
446 
447 
458  void setBackgroundColorHover(const Color& color);
459 
460 
477  void setForegroundColor(const Color& color);
478 
479 
493  void setForegroundColorNormal(const Color& color);
494 
495 
507  void setForegroundColorHover(const Color& color);
508 
509 
523  void setCheckColor(const Color& color);
524 
525 
534  void setCheckColorNormal(const Color& color);
535 
536 
545  void setCheckColorHover(const Color& color);
546 
547 
557  void setUncheckedTexture(const Texture& texture);
558 
559 
569  void setCheckedTexture(const Texture& texture);
570 
571 
581  void setUncheckedHoverTexture(const Texture& texture);
582 
583 
593  void setCheckedHoverTexture(const Texture& texture);
594 
595 
605  void setFocusedTexture(const Texture& texture);
606 
607 
609  // Draws the widget on the render target.
611  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
612 
613 
615  protected:
616 
618  // Makes a copy of the renderer
620  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
621 
622 
624  protected:
625 
626  RadioButton* m_radioButton;
627 
628  Texture m_textureUnchecked;
629  Texture m_textureChecked;
630  Texture m_textureUncheckedHover;
631  Texture m_textureCheckedHover;
632  Texture m_textureFocused;
633 
634  sf::Color m_textColorNormal;
635  sf::Color m_textColorHover;
636 
637  sf::Color m_backgroundColorNormal;
638  sf::Color m_backgroundColorHover;
639 
640  sf::Color m_foregroundColorNormal;
641  sf::Color m_foregroundColorHover;
642 
643  sf::Color m_checkColorNormal;
644  sf::Color m_checkColorHover;
645 
646  friend class RadioButton;
647 
649  };
650 
652 }
653 
655 
656 #endif // TGUI_RADIO_BUTTON_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Implicit converter for colors.
Definition: Color.hpp:39
sf::String getText() const
Returns the text of the radio button.
Definition: RadioButton.hpp:190
unsigned int getTextSize() const
Returns the character size of the text.
Definition: RadioButton.hpp:212
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Label widget.
Definition: Label.hpp:49
The parent class for every widget.
Definition: Widget.hpp:71
Radio button widget.
Definition: RadioButton.hpp:53
std::shared_ptr< RadioButtonRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: RadioButton.hpp:93
RadioButtonRenderer(RadioButton *radioButton)
Constructor.
Definition: RadioButton.hpp:336
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Parent class for every widget that has padding.
Definition: Borders.hpp:210
virtual void setPosition(const Layout2d &position)
set the position of the widget
Clickable widget.
Definition: ClickableWidget.hpp:55
Definition: CheckBox.hpp:136
Definition: RadioButton.hpp:326
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Definition: Texture.hpp:44
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:278
bool isChecked() const
Returns whether the radio button is checked or not.
Definition: RadioButton.hpp:169
std::shared_ptr< const RadioButton > ConstPtr
Shared constant widget pointer.
Definition: RadioButton.hpp:58
Base class for all renderer classes.
Definition: Widget.hpp:682