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 {
41  class TGUI_API Button : public ClickableWidget
42  {
43  public:
44 
45  typedef std::shared_ptr<Button> Ptr;
46  typedef std::shared_ptr<const Button> ConstPtr;
47 
48 
50  // Default constructor
52  Button();
53 
54 
63  static Button::Ptr create(const sf::String& text = "");
64 
65 
74  static Button::Ptr copy(Button::ConstPtr button);
75 
76 
81  ButtonRenderer* getSharedRenderer();
82  const ButtonRenderer* getSharedRenderer() const;
83 
89  ButtonRenderer* getRenderer();
90  const ButtonRenderer* getRenderer() const;
91 
92 
99  void setSize(const Layout2d& size) override;
100  using Widget::setSize;
101 
102 
110  void enable() override;
111 
112 
120  void disable() override;
121 
122 
129  virtual void setText(const sf::String& text);
130 
131 
138  const sf::String& getText() const;
139 
140 
148  void setTextSize(unsigned int size);
149 
150 
157  unsigned int getTextSize() const;
158 
159 
163  void leftMousePressed(Vector2f pos) override;
164 
168  void leftMouseReleased(Vector2f pos) override;
169 
173  void keyPressed(const sf::Event::KeyEvent& event) override;
174 
178  void widgetFocused() override;
179 
180 
188  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
189 
190 
192  protected:
193 
195  // This function is called when the mouse enters the widget. If requested, a callback will be send.
197  void mouseEnteredWidget() override;
198 
199 
201  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
203  void mouseLeftWidget() override;
204 
205 
215  Signal& getSignal(std::string signalName) override;
216 
217 
224  void rendererChanged(const std::string& property) override;
225 
226 
230  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
231 
232 
236  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
237 
238 
240  // Returns the size without the borders
242  Vector2f getInnerSize() const;
243 
244 
246  // Returns the background color that is being used in the current state
248  const Color& getCurrentBackgroundColor() const;
249 
250 
252  // Returns the border color that is being used in the current state
254  const Color& getCurrentBorderColor() const;
255 
256 
258  // Sets the color and style of m_text depending on the current state
260  void updateTextColorAndStyle();
261 
262 
264  // Called when size of button is updated
266  virtual void updateSize();
267 
268 
270  // Makes a copy of the widget
272  Widget::Ptr clone() const override
273  {
274  return std::make_shared<Button>(*this);
275  }
276 
277 
279  public:
280 
281  SignalString onPress = {"Pressed"};
282 
283 
285  protected:
286 
287  sf::String m_string;
288  Text m_text;
289 
290  unsigned int m_textSize = 0;
291  bool m_autoSize = true;
292 
293  Sprite m_sprite;
294  Sprite m_spriteHover;
295  Sprite m_spriteDown;
296  Sprite m_spriteDisabled;
297  Sprite m_spriteFocused;
298 
299  // Cached renderer properties
300  Borders m_bordersCached;
301  Color m_borderColorCached;
302  Color m_borderColorHoverCached;
303  Color m_borderColorDownCached;
304  Color m_borderColorDisabledCached;
305  Color m_backgroundColorCached;
306  Color m_backgroundColorHoverCached;
307  Color m_backgroundColorDownCached;
308  Color m_backgroundColorDisabledCached;
309  };
310 
312 }
313 
315 
316 #endif // TGUI_BUTTON_HPP
317 
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Button.hpp:272
Definition: Sprite.hpp:43
Wrapper for colors.
Definition: Color.hpp:47
Class to store the position or size of a widget.
Definition: Layout.hpp:243
Definition: Vector2f.hpp:37
Definition: Text.hpp:42
Definition: ButtonRenderer.hpp:36
std::shared_ptr< const Button > ConstPtr
Shared constant widget pointer.
Definition: Button.hpp:46
Clickable widget.
Definition: ClickableWidget.hpp:39
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:70
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Button widget.
Definition: Button.hpp:41
Definition: Outline.hpp:38
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition: Button.hpp:45