TGUI  0.8-dev
Button.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_BUTTON_HPP
27 #define TGUI_BUTTON_HPP
28 
29 
30 #include <TGUI/Renderers/ButtonRenderer.hpp>
31 #include <TGUI/Widgets/ClickableWidget.hpp>
32 #include <TGUI/Text.hpp>
33 
35 
36 namespace tgui
37 {
49  class TGUI_API Button : public ClickableWidget
50  {
51  public:
52 
53  typedef std::shared_ptr<Button> Ptr;
54  typedef std::shared_ptr<const Button> ConstPtr;
55 
56 
58  // Default constructor
60  Button();
61 
62 
71  static Button::Ptr create(sf::String text = "");
72 
73 
82  static Button::Ptr copy(Button::ConstPtr button);
83 
84 
92  {
93  return aurora::downcast<ButtonRenderer*>(m_renderer.get());
94  }
95 
96 
103  virtual void setSize(const Layout2d& size) override;
105 
106 
114  virtual void enable() override;
115 
116 
124  virtual void disable() override;
125 
126 
133  void setText(const sf::String& text);
134 
135 
142  const sf::String& getText() const;
143 
144 
152  void setTextSize(unsigned int size);
153 
154 
161  unsigned int getTextSize() const;
162 
163 
167  virtual void leftMousePressed(sf::Vector2f pos) override;
168 
172  virtual void leftMouseReleased(sf::Vector2f pos) override;
173 
177  virtual void keyPressed(const sf::Event::KeyEvent& event) override;
178 
182  virtual void widgetFocused() override;
183 
184 
192  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
193 
194 
196  protected:
197 
199  // This function is called when the mouse enters the widget. If requested, a callback will be send.
201  virtual void mouseEnteredWidget() override;
202 
203 
205  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
207  virtual void mouseLeftWidget() override;
208 
209 
216  virtual void rendererChanged(const std::string& property) override;
217 
218 
220  // Returns the size without the borders
222  sf::Vector2f getInnerSize() const;
223 
224 
226  // Returns the background color that is being used in the current state
228  sf::Color getCurrentBackgroundColor() const;
229 
230 
232  // Returns the border color that is being used in the current state
234  sf::Color getCurrentBorderColor() const;
235 
236 
238  // Sets the color and style of m_text depending on the current state
240  void updateTextColorAndStyle();
241 
242 
244  // Makes a copy of the widget
246  virtual Widget::Ptr clone() const override
247  {
248  return std::make_shared<Button>(*this);
249  }
250 
251 
253  private:
254 
255  sf::String m_string;
256  Text m_text;
257 
258  // This will store the size of the text (0 to auto-size)
259  unsigned int m_textSize = 0;
260 
261  Sprite m_sprite;
262  Sprite m_spriteHover;
263  Sprite m_spriteDown;
264  Sprite m_spriteDisabled;
265  Sprite m_spriteFocused;
266 
267  // Cached renderer properties
268  Borders m_bordersCached;
269  Color m_borderColorCached;
270  Color m_borderColorHoverCached;
271  Color m_borderColorDownCached;
272  Color m_borderColorDisabledCached;
273  Color m_backgroundColorCached;
274  Color m_backgroundColorHoverCached;
275  Color m_backgroundColorDownCached;
276  Color m_backgroundColorDisabledCached;
277  };
278 
280 }
281 
283 
284 #endif // TGUI_BUTTON_HPP
285 
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:263
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Button.hpp:246
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: Text.hpp:41
Definition: ButtonRenderer.hpp:36
std::shared_ptr< const Button > ConstPtr
Shared constant widget pointer.
Definition: Button.hpp:54
Clickable widget.
Definition: ClickableWidget.hpp:55
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
ButtonRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: Button.hpp:91
Button widget.
Definition: Button.hpp:49
Definition: Outline.hpp:37
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition: Button.hpp:53