--- --- TGUI: include/TGUI/Widgets/ButtonBase.hpp Source File
TGUI  1.x-dev
Loading...
Searching...
No Matches
ButtonBase.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#ifndef TGUI_BUTTON_BASE_HPP
26#define TGUI_BUTTON_BASE_HPP
27
28#include <TGUI/Renderers/ButtonRenderer.hpp>
29#include <TGUI/Widgets/ClickableWidget.hpp>
30#include <TGUI/Components.hpp>
31
33
34TGUI_MODULE_EXPORT namespace tgui
35{
39 class TGUI_API ButtonBase : public ClickableWidget
40 {
41 public:
42
43 using Ptr = std::shared_ptr<ButtonBase>;
44 using ConstPtr = std::shared_ptr<const ButtonBase>;
45
53 ButtonBase(const char* typeName, bool initRenderer);
54
59
64
68 ButtonBase& operator=(const ButtonBase&);
69
73 ButtonBase& operator=(ButtonBase&&) noexcept;
74
78 ~ButtonBase() override;
79
84 TGUI_NODISCARD ButtonRenderer* getSharedRenderer() override;
85 TGUI_NODISCARD const ButtonRenderer* getSharedRenderer() const override;
86
92 TGUI_NODISCARD ButtonRenderer* getRenderer() override;
93
99 void setSize(const Layout2d& size) override;
100 using Widget::setSize;
101
109 void setEnabled(bool enabled) override;
110
116 virtual void setText(const String& text);
117
123 TGUI_NODISCARD const String& getText() const;
124
142 void setTextPosition(Vector2<AbsoluteOrRelativeValue> position, Vector2f origin);
143
152 void setFocused(bool focused) override;
153
158 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
159
163 bool leftMousePressed(Vector2f pos) override;
164
168 void leftMouseReleased(Vector2f pos) override;
169
173 void leftMouseButtonNoLongerDown() override;
174
181 void draw(BackendRenderTarget& target, RenderStates states) const override;
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
192 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
194 void mouseLeftWidget() override;
195
201 void rendererChanged(const String& property) override;
202
206 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
207
211 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
212
216 void updateTextSize() override;
217
219 // Updates the state of the button
221 void updateState();
222
224 // Called when size of button is updated
226 virtual void updateSize();
227
229 // Called whenever the text position might need to be updated
231 virtual void updateTextPosition();
232
236 virtual void initComponents();
237
241 void addComponent(const std::shared_ptr<priv::dev::Component>& component);
242
246 TGUI_NODISCARD std::shared_ptr<priv::dev::Component> getComponent(const String& name);
247
249 protected:
250
251 String m_string;
252
253 bool m_down = false;
254 priv::dev::ComponentState m_state = priv::dev::ComponentState::Normal;
255
256 bool m_autoSize = true;
257 bool m_updatingTextSize = false; // Internal variable so that updateSize knows that it is called from updateTextSize
258
259 Vector2<AbsoluteOrRelativeValue> m_textPosition;
260 Vector2f m_textOrigin;
261
262 priv::dev::StylePropertyBackground background;
263 priv::dev::StylePropertyText text;
264
265 std::uint64_t m_textStyleChangedCallbackId = 0;
266
267 // These maps must be declared AFTER the style properties
268 std::map<String, priv::dev::StylePropertyBase*> m_stylePropertiesNames;
269 std::map<String, std::vector<priv::dev::StylePropertyBase*>> m_stylePropertiesGlobalNames;
270 std::map<String, std::shared_ptr<priv::dev::Component>> m_namedComponents;
271
272 // These components must be declared AFTER the style properties
273 std::shared_ptr<priv::dev::BackgroundComponent> m_backgroundComponent;
274 std::shared_ptr<priv::dev::TextComponent> m_textComponent;
275
276 std::vector<std::shared_ptr<priv::dev::Component>> m_components;
277 };
278
280}
281
283
284#endif // TGUI_BUTTON_BASE_HPP
Class to store the a value that is either a constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:45
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Base class for button widgets.
Definition ButtonBase.hpp:40
ButtonBase(ButtonBase &&) noexcept
Move constructor.
std::shared_ptr< const ButtonBase > ConstPtr
Shared constant widget pointer.
Definition ButtonBase.hpp:44
std::shared_ptr< ButtonBase > Ptr
Shared widget pointer.
Definition ButtonBase.hpp:43
ButtonBase(const ButtonBase &)
Copy constructor.
Definition ButtonRenderer.hpp:35
Clickable widget.
Definition ClickableWidget.hpp:38
Class to store the position or size of a widget.
Definition Layout.hpp:313
Wrapper class to store strings.
Definition String.hpp:96
Definition Vector2.hpp:41
The parent class for every widget.
Definition Widget.hpp:83
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
States used for drawing.
Definition RenderStates.hpp:38