TGUI  0.8.9
Button.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2020 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
36namespace 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
75
76
82 const ButtonRenderer* getSharedRenderer() const;
83
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 bool m_updatingSizeWhileSettingText = false; // Internal variable so that updateSize knows that it is called from setText
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
Definition: ButtonRenderer.hpp:37
Button widget.
Definition: Button.hpp:42
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void mouseLeftWidget() override
This function is called when the mouse leaves the widget.
void setSize(const Layout2d &size) override
Changes the size of the button.
void setTextSize(unsigned int size) override
Changes the character size of the text.
ButtonRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition: Button.hpp:45
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
void setEnabled(bool enabled) override
Enables or disables the widget.
static Button::Ptr create(const sf::String &text="")
Creates a new button widget.
virtual void setText(const sf::String &text)
Changes the caption of the button.
const sf::String & getText() const
Returns the caption of the button.
ButtonRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void mouseEnteredWidget() override
This function is called when the mouse enters the widget.
unsigned int getTextSize() const override
Returns the character size of the text.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Button.hpp:264
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
std::shared_ptr< const Button > ConstPtr
Shared constant widget pointer.
Definition: Button.hpp:46
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
static Button::Ptr copy(Button::ConstPtr button)
Makes a copy of another button.
Clickable widget.
Definition: ClickableWidget.hpp:40
Wrapper for colors.
Definition: Color.hpp:49
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Definition: Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:231
Definition: Sprite.hpp:46
Definition: Text.hpp:43
Definition: Vector2f.hpp:39
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37