TGUI  0.8.7
Button.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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) override;
139 
140 
147  unsigned int getTextSize() const override;
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  bool m_autoSize = true;
283 
284  Sprite m_sprite;
285  Sprite m_spriteHover;
286  Sprite m_spriteDown;
287  Sprite m_spriteDisabled;
288  Sprite m_spriteFocused;
289 
290  // Cached renderer properties
291  Borders m_bordersCached;
292  Color m_borderColorCached;
293  Color m_borderColorHoverCached;
294  Color m_borderColorDownCached;
295  Color m_borderColorDisabledCached;
296  Color m_borderColorFocusedCached;
297  Color m_backgroundColorCached;
298  Color m_backgroundColorHoverCached;
299  Color m_backgroundColorDownCached;
300  Color m_backgroundColorDisabledCached;
301  Color m_backgroundColorFocusedCached;
302  };
303 
305 }
306 
308 
309 #endif // TGUI_BUTTON_HPP
310 
Definition: ButtonRenderer.hpp:36
Button widget.
Definition: Button.hpp:41
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:220
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Text.hpp:42
std::shared_ptr< const Button > ConstPtr
Shared constant widget pointer.
Definition: Button.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:48
Definition: Sprite.hpp:45
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition: Button.hpp:45
Definition: Vector2f.hpp:38
Definition: Outline.hpp:38
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Button.hpp:264
Clickable widget.
Definition: ClickableWidget.hpp:39