TGUI  0.9.1
ButtonBase.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 
36 namespace 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 
137  void setTextSize(unsigned int size) override;
138 
139 
145  unsigned int getTextSize() const override;
146 
147 
156  void setFocused(bool focused) override;
157 
158 
163  bool isMouseOnWidget(Vector2f pos) const override;
164 
165 
169  void leftMousePressed(Vector2f pos) override;
170 
174  void leftMouseReleased(Vector2f pos) override;
175 
179  void leftMouseButtonNoLongerDown() override;
180 
181 
189  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
190 
191 
193  protected:
194 
196  // This function is called when the mouse enters the widget. If requested, a callback will be send.
198  void mouseEnteredWidget() override;
199 
200 
202  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
204  void mouseLeftWidget() override;
205 
206 
213  void rendererChanged(const String& property) override;
214 
215 
219  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
220 
221 
225  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
226 
227 
229  // Updates the state of the button
231  void updateState();
232 
233 
235  // Called when size of button is updated
237  virtual void updateSize();
238 
239 
243  virtual void initComponents();
244 
245 
249  void addComponent(const std::shared_ptr<priv::dev::Component>& component);
250 
251 
255  std::shared_ptr<priv::dev::Component> getComponent(const String& name);
256 
257 
259  protected:
260 
261  String m_string;
262 
263  bool m_down = false;
264  priv::dev::ComponentState m_state = priv::dev::ComponentState::Normal;
265 
266  bool m_autoSize = true;
267  bool m_updatingSizeWhileSettingText = false; // Internal variable so that updateSize knows that it is called from setText
268 
269  priv::dev::StylePropertyBackground background;
270  priv::dev::StylePropertyText text;
271 
272  // These maps must be declared AFTER the style properties
273  std::map<String, priv::dev::StylePropertyBase*> m_stylePropertiesNames;
274  std::map<String, std::vector<priv::dev::StylePropertyBase*>> m_stylePropertiesGlobalNames;
275  std::map<String, std::shared_ptr<priv::dev::Component>> m_namedComponents;
276 
277  // These components must be declared AFTER the style properties
278  std::shared_ptr<priv::dev::BackgroundComponent> m_backgroundComponent;
279  std::shared_ptr<priv::dev::TextComponent> m_textComponent;
280 
281  std::vector<std::shared_ptr<priv::dev::Component>> m_components;
282  };
283 
285 }
286 
288 
289 #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.
ButtonBase & operator=(const ButtonBase &)
Overload of copy assignment operator.
void setFocused(bool focused) override
Focus or unfocus the widget.
void setSize(const Layout2d &size) override
Changes the size of the button.
unsigned int getTextSize() const override
Returns the character size of the text.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
ButtonRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
ButtonRenderer * getSharedRenderer()
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.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
const String & getText() const
Returns the caption of the button.
ButtonBase & operator=(ButtonBase &&)
Move assignment.
void setTextSize(unsigned int size) override
Changes the character size of the text.
std::shared_ptr< const ButtonBase > ConstPtr
Shared constant widget pointer.
Definition: ButtonBase.hpp:46
ButtonBase(const ButtonBase &)
Copy constructor.
void draw(BackendRenderTargetBase &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.
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.
void mouseLeftWidget() override
This function is called when the mouse leaves the widget.
ButtonBase(ButtonBase &&)
Move constructor.
Definition: ButtonRenderer.hpp:37
Clickable widget.
Definition: ClickableWidget.hpp:40
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Wrapper class to store strings.
Definition: String.hpp:74
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