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  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(sf::Vector2f pos) override;
164 
168  void leftMouseReleased(sf::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 
228  // Returns the size without the borders
230  sf::Vector2f getInnerSize() const;
231 
232 
234  // Returns the background color that is being used in the current state
236  sf::Color getCurrentBackgroundColor() const;
237 
238 
240  // Returns the border color that is being used in the current state
242  sf::Color getCurrentBorderColor() const;
243 
244 
246  // Sets the color and style of m_text depending on the current state
248  void updateTextColorAndStyle();
249 
250 
252  // Makes a copy of the widget
254  Widget::Ptr clone() const override
255  {
256  return std::make_shared<Button>(*this);
257  }
258 
259 
261  public:
262 
263  SignalString onPress = {"Pressed"};
264 
265 
267  private:
268 
269  sf::String m_string;
270  Text m_text;
271 
272  // This will store the size of the text (0 to auto-size)
273  unsigned int m_textSize = 0;
274 
275  Sprite m_sprite;
276  Sprite m_spriteHover;
277  Sprite m_spriteDown;
278  Sprite m_spriteDisabled;
279  Sprite m_spriteFocused;
280 
281  // Cached renderer properties
282  Borders m_bordersCached;
283  Color m_borderColorCached;
284  Color m_borderColorHoverCached;
285  Color m_borderColorDownCached;
286  Color m_borderColorDisabledCached;
287  Color m_backgroundColorCached;
288  Color m_backgroundColorHoverCached;
289  Color m_backgroundColorDownCached;
290  Color m_backgroundColorDisabledCached;
291  };
292 
294 }
295 
297 
298 #endif // TGUI_BUTTON_HPP
299 
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:254
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:243
Definition: Text.hpp:41
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:66
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
Button widget.
Definition: Button.hpp:41
Definition: Outline.hpp:38
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition: Button.hpp:45