TGUI  1.6.1
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
154 void setIgnoreKeyEvents(bool ignore);
155
166 TGUI_NODISCARD bool getIgnoreKeyEvents() const;
167
176 void setFocused(bool focused) override;
177
182 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
183
187 bool leftMousePressed(Vector2f pos) override;
188
192 void leftMouseReleased(Vector2f pos) override;
193
197 void leftMouseButtonNoLongerDown() override;
198
205 void draw(BackendRenderTarget& target, RenderStates states) const override;
206
208 protected:
209
211 // This function is called when the mouse enters the widget. If requested, a callback will be send.
213 void mouseEnteredWidget() override;
214
216 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
218 void mouseLeftWidget() override;
219
225 void rendererChanged(const String& property) override;
226
230 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
231
235 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
236
240 void updateTextSize() override;
241
243 // Updates the state of the button
245 void updateState();
246
248 // Called when size of button is updated
250 virtual void updateSize();
251
253 // Called whenever the text position might need to be updated
255 virtual void updateTextPosition();
256
260 virtual void initComponents();
261
265 void addComponent(const std::shared_ptr<priv::dev::Component>& component);
266
270 TGUI_NODISCARD std::shared_ptr<priv::dev::Component> getComponent(const String& name);
271
273 protected:
274
275 String m_string;
276
277 bool m_down = false;
278 priv::dev::ComponentState m_state = priv::dev::ComponentState::Normal;
279
280 bool m_autoSize = true;
281 bool m_updatingTextSize = false; // Internal variable so that updateSize knows that it is called from updateTextSize
282 bool m_ignoreKeyEvents = false; // Whether we ignore space and return key presses or not when the button is focused
283
284 Vector2<AbsoluteOrRelativeValue> m_textPosition;
285 Vector2f m_textOrigin;
286
287 priv::dev::StylePropertyBackground background;
288 priv::dev::StylePropertyText text;
289
290 std::uint64_t m_textStyleChangedCallbackId = 0;
291
292 // These maps must be declared AFTER the style properties
293 std::map<String, priv::dev::StylePropertyBase*> m_stylePropertiesNames;
294 std::map<String, std::vector<priv::dev::StylePropertyBase*>> m_stylePropertiesGlobalNames;
295 std::map<String, std::shared_ptr<priv::dev::Component>> m_namedComponents;
296
297 // These components must be declared AFTER the style properties
298 std::shared_ptr<priv::dev::BackgroundComponent> m_backgroundComponent;
299 std::shared_ptr<priv::dev::TextComponent> m_textComponent;
300
301 std::vector<std::shared_ptr<priv::dev::Component>> m_components;
302 };
303
305}
306
308
309#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:323
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