TGUI  0.8-alpha
Button.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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 setEnabled(bool enabled) override;
111 
112 
119  virtual void setText(const sf::String& text);
120 
121 
128  const sf::String& getText() const;
129 
130 
138  void setTextSize(unsigned int size);
139 
140 
147  unsigned int getTextSize() const;
148 
149 
154  bool mouseOnWidget(Vector2f pos) const override;
155 
156 
160  void leftMousePressed(Vector2f pos) override;
161 
165  void leftMouseReleased(Vector2f pos) override;
166 
170  void keyPressed(const sf::Event::KeyEvent& event) override;
171 
172 
180  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
181 
182 
184  protected:
185 
187  // This function is called when the mouse enters the widget. If requested, a callback will be send.
189  void mouseEnteredWidget() override;
190 
191 
193  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
195  void mouseLeftWidget() override;
196 
197 
207  Signal& getSignal(std::string signalName) override;
208 
209 
216  void rendererChanged(const std::string& property) override;
217 
218 
222  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
223 
224 
228  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
229 
230 
232  // Returns the size without the borders
234  Vector2f getInnerSize() const;
235 
236 
238  // Returns the background color that is being used in the current state
240  const Color& getCurrentBackgroundColor() const;
241 
242 
244  // Returns the border color that is being used in the current state
246  const Color& getCurrentBorderColor() const;
247 
248 
250  // Sets the color and style of m_text depending on the current state
252  void updateTextColorAndStyle();
253 
254 
256  // Called when size of button is updated
258  virtual void updateSize();
259 
260 
262  // Makes a copy of the widget
264  Widget::Ptr clone() const override
265  {
266  return std::make_shared<Button>(*this);
267  }
268 
269 
271  public:
272 
273  SignalString onPress = {"Pressed"};
274 
275 
277  protected:
278 
279  sf::String m_string;
280  Text m_text;
281 
282  unsigned int m_textSize = 0;
283  bool m_autoSize = true;
284 
285  Sprite m_sprite;
286  Sprite m_spriteHover;
287  Sprite m_spriteDown;
288  Sprite m_spriteDisabled;
289  Sprite m_spriteFocused;
290 
291  // Cached renderer properties
292  Borders m_bordersCached;
293  Color m_borderColorCached;
294  Color m_borderColorHoverCached;
295  Color m_borderColorDownCached;
296  Color m_borderColorDisabledCached;
297  Color m_borderColorFocusedCached;
298  Color m_backgroundColorCached;
299  Color m_backgroundColorHoverCached;
300  Color m_backgroundColorDownCached;
301  Color m_backgroundColorDisabledCached;
302  Color m_backgroundColorFocusedCached;
303  };
304 
306 }
307 
309 
310 #endif // TGUI_BUTTON_HPP
311 
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:264
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:76
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