TGUI  1.0-alpha
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
72 ButtonBase& operator=(const ButtonBase&);
73
77 ButtonBase& operator=(ButtonBase&&) noexcept;
78
82 virtual ~ButtonBase();
83
84
89 ButtonRenderer* getSharedRenderer();
90 const ButtonRenderer* getSharedRenderer() const;
91
97 ButtonRenderer* getRenderer();
98
99
105 void setSize(const Layout2d& size) override;
106 using Widget::setSize;
107
108
116 void setEnabled(bool enabled) override;
117
118
124 virtual void setText(const String& text);
125
126
132 const String& getText() const;
133
134
152 void setTextPosition(Vector2<AbsoluteOrRelativeValue> position, Vector2f origin);
153
154
163 void setFocused(bool focused) override;
164
165
170 bool isMouseOnWidget(Vector2f pos) const override;
171
172
176 void leftMousePressed(Vector2f pos) override;
177
181 void leftMouseReleased(Vector2f pos) override;
182
186 void leftMouseButtonNoLongerDown() override;
187
188
196 void draw(BackendRenderTarget& target, RenderStates states) const override;
197
198
200 protected:
201
203 // This function is called when the mouse enters the widget. If requested, a callback will be send.
205 void mouseEnteredWidget() override;
206
207
209 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
211 void mouseLeftWidget() override;
212
213
220 void rendererChanged(const String& property) override;
221
222
226 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
227
228
232 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
233
234
238 void updateTextSize() override;
239
240
242 // Updates the state of the button
244 void updateState();
245
246
248 // Called when size of button is updated
250 virtual void updateSize();
251
252
254 // Called whenever the text position might need to be updated
256 virtual void updateTextPosition();
257
258
262 virtual void initComponents();
263
264
268 void addComponent(const std::shared_ptr<priv::dev::Component>& component);
269
270
274 std::shared_ptr<priv::dev::Component> getComponent(const String& name);
275
276
278 protected:
279
280 String m_string;
281
282 bool m_down = false;
283 priv::dev::ComponentState m_state = priv::dev::ComponentState::Normal;
284
285 bool m_autoSize = true;
286 bool m_updatingTextSize = false; // Internal variable so that updateSize knows that it is called from updateTextSize
287
288 Vector2<AbsoluteOrRelativeValue> m_textPosition;
289 Vector2f m_textOrigin;
290
291 priv::dev::StylePropertyBackground background;
292 priv::dev::StylePropertyText text;
293
294 std::uint64_t m_textStyleChangedCallbackId = 0;
295
296 // These maps must be declared AFTER the style properties
297 std::map<String, priv::dev::StylePropertyBase*> m_stylePropertiesNames;
298 std::map<String, std::vector<priv::dev::StylePropertyBase*>> m_stylePropertiesGlobalNames;
299 std::map<String, std::shared_ptr<priv::dev::Component>> m_namedComponents;
300
301 // These components must be declared AFTER the style properties
302 std::shared_ptr<priv::dev::BackgroundComponent> m_backgroundComponent;
303 std::shared_ptr<priv::dev::TextComponent> m_textComponent;
304
305 std::vector<std::shared_ptr<priv::dev::Component>> m_components;
306 };
307
309}
310
312
313#endif // TGUI_BUTTON_BASE_HPP
Class to store the a value that is either a constant or a ratio.
Definition: AbsoluteOrRelativeValue.hpp:43
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Base class for button widgets.
Definition: ButtonBase.hpp:42
ButtonBase(ButtonBase &&) noexcept
Move constructor.
std::shared_ptr< ButtonBase > Ptr
Shared widget pointer.
Definition: ButtonBase.hpp:45
std::shared_ptr< const ButtonBase > ConstPtr
Shared constant widget pointer.
Definition: ButtonBase.hpp:46
ButtonBase(const ButtonBase &)
Copy constructor.
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
Definition: Vector2.hpp:42
The parent class for every widget.
Definition: Widget.hpp:70
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39