TGUI  0.10-beta
ButtonBase.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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_BASE_HPP
27#define TGUI_BUTTON_BASE_HPP
28
29
30#include <TGUI/Renderers/ButtonRenderer.hpp>
31#include <TGUI/Widgets/ClickableWidget.hpp>
32#include <TGUI/Components.hpp>
33
35
36namespace tgui
37{
41 class TGUI_API ButtonBase : public ClickableWidget
42 {
43 public:
44
45 typedef std::shared_ptr<ButtonBase> Ptr;
46 typedef std::shared_ptr<const ButtonBase> ConstPtr;
47
48
56 ButtonBase(const char* typeName, bool initRenderer);
57
58
63
68
73
78
79
85 const ButtonRenderer* getSharedRenderer() const;
86
93 const ButtonRenderer* getRenderer() const;
94
95
101 void setSize(const Layout2d& size) override;
102 using Widget::setSize;
103
104
112 void setEnabled(bool enabled) override;
113
114
120 virtual void setText(const String& text);
121
122
128 const String& getText() const;
129
130
139 void setFocused(bool focused) override;
140
141
146 bool isMouseOnWidget(Vector2f pos) const override;
147
148
152 void leftMousePressed(Vector2f pos) override;
153
157 void leftMouseReleased(Vector2f pos) override;
158
162 void leftMouseButtonNoLongerDown() override;
163
164
172 void draw(BackendRenderTarget& target, RenderStates states) const override;
173
174
176 protected:
177
179 // This function is called when the mouse enters the widget. If requested, a callback will be send.
181 void mouseEnteredWidget() override;
182
183
185 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
187 void mouseLeftWidget() override;
188
189
196 void rendererChanged(const String& property) override;
197
198
202 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
203
204
208 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
209
210
214 void updateTextSize() override;
215
216
218 // Updates the state of the button
220 void updateState();
221
222
224 // Called when size of button is updated
226 virtual void updateSize();
227
228
232 virtual void initComponents();
233
234
238 void addComponent(const std::shared_ptr<priv::dev::Component>& component);
239
240
244 std::shared_ptr<priv::dev::Component> getComponent(const String& name);
245
246
248 protected:
249
250 String m_string;
251
252 bool m_down = false;
253 priv::dev::ComponentState m_state = priv::dev::ComponentState::Normal;
254
255 bool m_autoSize = true;
256 bool m_updatingTextSize = false; // Internal variable so that updateSize knows that it is called from updateTextSize
257
258 priv::dev::StylePropertyBackground background;
259 priv::dev::StylePropertyText text;
260
261 // These maps must be declared AFTER the style properties
262 std::map<String, priv::dev::StylePropertyBase*> m_stylePropertiesNames;
263 std::map<String, std::vector<priv::dev::StylePropertyBase*>> m_stylePropertiesGlobalNames;
264 std::map<String, std::shared_ptr<priv::dev::Component>> m_namedComponents;
265
266 // These components must be declared AFTER the style properties
267 std::shared_ptr<priv::dev::BackgroundComponent> m_backgroundComponent;
268 std::shared_ptr<priv::dev::TextComponent> m_textComponent;
269
270 std::vector<std::shared_ptr<priv::dev::Component>> m_components;
271 };
272
274}
275
277
278#endif // TGUI_BUTTON_BASE_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Base class for button widgets.
Definition: ButtonBase.hpp:42
void setEnabled(bool enabled) override
Enables or disables the widget.
std::shared_ptr< ButtonBase > Ptr
Shared widget pointer.
Definition: ButtonBase.hpp:45
virtual void setText(const String &text)
Changes the caption of the button.
void setFocused(bool focused) override
Focus or unfocus the widget.
ButtonRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setSize(const Layout2d &size) override
Changes the size of the button.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
const String & getText() const
Returns the caption of the button.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
ButtonBase & operator=(const ButtonBase &)
Overload of copy assignment operator.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
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.
std::shared_ptr< const ButtonBase > ConstPtr
Shared constant widget pointer.
Definition: ButtonBase.hpp:46
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
ButtonBase(const ButtonBase &)
Copy constructor.
ButtonBase & operator=(ButtonBase &&)
Move assignment.
void mouseEnteredWidget() override
This function is called when the mouse enters the widget.
void mouseLeftWidget() override
This function is called when the mouse leaves the widget.
ButtonBase(ButtonBase &&)
Move constructor.
ButtonRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Definition: ButtonRenderer.hpp:37
Clickable widget.
Definition: ClickableWidget.hpp:40
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Wrapper class to store strings.
Definition: String.hpp:79
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39