TGUI  v0.7.5
Button.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_BUTTON_HPP
27 #define TGUI_BUTTON_HPP
28 
29 
30 #include <TGUI/Widgets/Label.hpp>
31 
33 
34 namespace tgui
35 {
36  class Container;
37  class ButtonRenderer;
38 
50  class TGUI_API Button : public ClickableWidget
51  {
52  public:
53 
54  typedef std::shared_ptr<Button> Ptr;
55  typedef std::shared_ptr<const Button> ConstPtr;
56 
57 
59  // Default constructor
61  Button();
62 
63 
72  static Button::Ptr create(sf::String text = "");
73 
74 
83  static Button::Ptr copy(Button::ConstPtr button);
84 
85 
92  std::shared_ptr<ButtonRenderer> getRenderer() const
93  {
94  return std::static_pointer_cast<ButtonRenderer>(m_renderer);
95  }
96 
97 
110  virtual void setPosition(const Layout2d& position) override;
112 
113 
120  virtual void setSize(const Layout2d& size) override;
122 
123 
132  virtual sf::Vector2f getFullSize() const override;
133 
134 
143  virtual void setFont(const Font& font) override;
144 
145 
152  void setText(const sf::String& text);
153 
154 
161  const sf::String& getText() const
162  {
163  return m_string;
164  }
165 
166 
174  void setTextSize(unsigned int size);
175 
176 
183  unsigned int getTextSize() const
184  {
185  return m_text.getTextSize();
186  }
187 
188 
195  virtual void setOpacity(float opacity) override;
196 
197 
206  virtual sf::Vector2f getWidgetOffset() const override;
207 
208 
212  virtual void leftMousePressed(float x, float y) override;
213 
217  virtual void leftMouseReleased(float x, float y) override;
218 
222  virtual void keyPressed(const sf::Event::KeyEvent& event) override;
223 
227  virtual void widgetFocused() override;
228 
229 
231  protected:
232 
245  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
246 
247 
249  // Makes a copy of the widget
251  virtual Widget::Ptr clone() const override
252  {
253  return std::make_shared<Button>(*this);
254  }
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  // Draws the widget on the render target.
272  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
273 
274 
276  protected:
277 
278  sf::String m_string;
279  Label m_text;
280 
281  // This will store the size of the text (0 to auto-size)
282  unsigned int m_textSize = 0;
283 
284  friend class ButtonRenderer;
285  friend class ChildWindow;
286  };
287 
289 
290  class TGUI_API ButtonRenderer : public WidgetRenderer, public WidgetBorders
291  {
292  public:
293 
300  ButtonRenderer(Button* button) : m_button{button} {}
301 
302 
312  virtual void setProperty(std::string property, const std::string& value) override;
313 
314 
325  virtual void setProperty(std::string property, ObjectConverter&& value) override;
326 
327 
337  virtual ObjectConverter getProperty(std::string property) const override;
338 
339 
346  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
347 
348 
361  void setTextColor(const Color& color);
362 
363 
370  void setTextColorNormal(const Color& color);
371 
372 
379  void setTextColorHover(const Color& color);
380 
381 
388  void setTextColorDown(const Color& color);
389 
390 
405  void setBackgroundColor(const Color& color);
406 
407 
416  void setBackgroundColorNormal(const Color& color);
417 
418 
427  void setBackgroundColorHover(const Color& color);
428 
429 
438  void setBackgroundColorDown(const Color& color);
439 
440 
447  void setBorderColor(const Color& color);
448 
449 
459  void setNormalTexture(const Texture& texture);
460 
461 
470  void setHoverTexture(const Texture& texture);
471 
472 
481  void setDownTexture(const Texture& texture);
482 
483 
492  void setFocusTexture(const Texture& texture);
493 
494 
496  // Draws the widget on the render target.
498  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
499 
500 
502  private:
503 
505  // Makes a copy of the renderer
507  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
508 
509 
511  protected:
512 
513  Button* m_button;
514 
515  sf::Color m_textColorNormal;
516  sf::Color m_textColorHover;
517  sf::Color m_textColorDown;
518 
519  sf::Color m_backgroundColorNormal;
520  sf::Color m_backgroundColorHover;
521  sf::Color m_backgroundColorDown;
522 
523  sf::Color m_borderColor;
524 
525  Texture m_textureNormal;
526  Texture m_textureHover;
527  Texture m_textureDown;
528  Texture m_textureFocused;
529 
530  friend class Button;
531  friend class ChildWindow;
532  friend class ChildWindowRenderer;
533  friend class MessageBox;
534 
536  };
537 
539 }
540 
542 
543 #endif // TGUI_BUTTON_HPP
544 
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Implicit converter for colors.
Definition: Color.hpp:39
Child window widget.
Definition: ChildWindow.hpp:60
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
const sf::String & getText() const
Returns the caption of the button.
Definition: Button.hpp:161
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Button.hpp:251
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: Button.hpp:290
std::shared_ptr< const Button > ConstPtr
Shared constant widget pointer.
Definition: Button.hpp:55
virtual void setPosition(const Layout2d &position)
set the position of the widget
Clickable widget.
Definition: ClickableWidget.hpp:55
Parent class for every widget that has borders.
Definition: Borders.hpp:136
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
std::shared_ptr< ButtonRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: Button.hpp:92
Button widget.
Definition: Button.hpp:50
Definition: Texture.hpp:44
ButtonRenderer(Button *button)
Constructor.
Definition: Button.hpp:300
Definition: ChildWindow.hpp:586
unsigned int getTextSize() const
Returns the character size of the text.
Definition: Button.hpp:183
Message box widget.
Definition: MessageBox.hpp:51
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition: Button.hpp:54
Base class for all renderer classes.
Definition: Widget.hpp:682