TGUI 1.13
Loading...
Searching...
No Matches
ButtonBase.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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/Components.hpp>
29#include <TGUI/Renderers/ButtonRenderer.hpp>
30#include <TGUI/Widgets/ClickableWidget.hpp>
31
33
34namespace tgui
35{
39 class TGUI_API ButtonBase : public ClickableWidget
40 {
41 public:
42 using Ptr = std::shared_ptr<ButtonBase>;
43 using ConstPtr = std::shared_ptr<const ButtonBase>;
44
52 ButtonBase(const char* typeName, bool initRenderer);
53
57 ButtonBase(const ButtonBase& other);
58
62 ButtonBase(ButtonBase&& other) noexcept;
63
67 ButtonBase& operator=(const ButtonBase& other);
68
72 ButtonBase& operator=(ButtonBase&& other) noexcept;
73
77 ~ButtonBase() override;
78
83 [[nodiscard]] ButtonRenderer* getSharedRenderer() override;
84 [[nodiscard]] const ButtonRenderer* getSharedRenderer() const override;
85
91 [[nodiscard]] ButtonRenderer* getRenderer() override;
92
98 void setSize(const Layout2d& size) override;
99 using Widget::setSize;
100
108 void setEnabled(bool enabled) override;
109
115 virtual void setText(const String& caption);
116
122 [[nodiscard]] const String& getText() const;
123
141 void setTextPosition(Vector2<AbsoluteOrRelativeValue> position, Vector2f origin);
142
153 void setIgnoreKeyEvents(bool ignore);
154
165 [[nodiscard]] bool getIgnoreKeyEvents() const;
166
175 void setFocused(bool focused) override;
176
181 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
182
186 bool leftMousePressed(Vector2f pos) override;
187
191 void leftMouseReleased(Vector2f pos) override;
192
196 void leftMouseButtonNoLongerDown() override;
197
204 void draw(BackendRenderTarget& target, RenderStates states) const override;
205
207
208 protected:
210 // This function is called when the mouse enters the widget. If requested, a callback will be send.
212 void mouseEnteredWidget() override;
213
215 // This function is called when the mouse leaves the widget. If requested, a callback will be send.
217 void mouseLeftWidget() override;
218
224 void rendererChanged(const String& property) override;
225
229 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
230
234 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
235
239 void updateTextSize() override;
240
242 // Updates the state of the button
244 void updateState();
245
247 // Called when size of button is updated
249 virtual void updateSize();
250
252 // Called whenever the text position might need to be updated
254 virtual void updateTextPosition();
255
259 virtual void initComponents();
260
264 void addComponent(const std::shared_ptr<priv::dev::Component>& component);
265
269 [[nodiscard]] std::shared_ptr<priv::dev::Component> getComponent(const String& name);
270
272
273 protected:
274 String m_string;
275
276 bool m_down = false;
277 priv::dev::ComponentState m_state = priv::dev::ComponentState::Normal;
278
279 bool m_autoSize = true;
280 bool m_updatingTextSize = false; // Internal variable so that updateSize knows that it is called from updateTextSize
281 bool m_ignoreKeyEvents = false; // Whether we ignore space and return key presses or not when the button is focused
282
284 Vector2f m_textOrigin;
285
286 priv::dev::StylePropertyBackground background;
287 priv::dev::StylePropertyText text;
288
289 std::uint64_t m_textStyleChangedCallbackId = 0;
290
291 // These maps must be declared AFTER the style properties
292 std::map<String, priv::dev::StylePropertyBase*> m_stylePropertiesNames;
293 std::map<String, std::vector<priv::dev::StylePropertyBase*>> m_stylePropertiesGlobalNames;
294 std::map<String, std::shared_ptr<priv::dev::Component>> m_namedComponents;
295
296 // These components must be declared AFTER the style properties
297 std::shared_ptr<priv::dev::BackgroundComponent> m_backgroundComponent;
298 std::shared_ptr<priv::dev::TextComponent> m_textComponent;
299
300 std::vector<std::shared_ptr<priv::dev::Component>> m_components;
301 };
302
304} // namespace tgui
305
307
308#endif // TGUI_BUTTON_BASE_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
~ButtonBase() override
Destructor.
void setEnabled(bool enabled) override
Enables or disables the widget.
ButtonBase(ButtonBase &&other) noexcept
Move constructor.
void setIgnoreKeyEvents(bool ignore)
Sets whether the button should ignore space and return key pressed and only react to mouse clicks.
void setFocused(bool focused) override
Focus or unfocus the widget.
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.
ButtonRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
ButtonRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
std::shared_ptr< const ButtonBase > ConstPtr
Shared constant widget pointer.
Definition ButtonBase.hpp:43
virtual void setText(const String &caption)
Changes 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...
std::shared_ptr< ButtonBase > Ptr
Shared widget pointer.
Definition ButtonBase.hpp:42
ButtonBase & operator=(const ButtonBase &other)
Overload of copy assignment operator.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
bool getIgnoreKeyEvents() const
Returns whether the button ignores space and return key presses and only react to mouse clicks.
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 leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
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(const ButtonBase &other)
Copy constructor.
void setTextPosition(Vector2< AbsoluteOrRelativeValue > position, Vector2f origin)
Changes the position of the caption text.
ButtonBase & operator=(ButtonBase &&other) noexcept
Move assignment.
Definition ButtonRenderer.hpp:35
Class to store the position or size of a widget.
Definition Layout.hpp:320
Wrapper class to store strings.
Definition String.hpp:94
Definition Vector2.hpp:42
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
States used for drawing.
Definition RenderStates.hpp:38