TGUI  0.9.5
Loading...
Searching...
No Matches
Widget.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_WIDGET_HPP
27#define TGUI_WIDGET_HPP
28
30
31#include <TGUI/Signal.hpp>
32#include <TGUI/Font.hpp>
33#include <TGUI/Sprite.hpp>
34#include <TGUI/Layout.hpp>
35#include <TGUI/String.hpp>
36#include <TGUI/Vector2.hpp>
37#include <TGUI/Duration.hpp>
38#include <TGUI/Cursor.hpp>
39#include <TGUI/Event.hpp>
40#include <TGUI/Any.hpp>
41#include <TGUI/BackendRenderTarget.hpp>
42#include <TGUI/Loading/Theme.hpp>
43#include <TGUI/Loading/DataIO.hpp>
44#include <TGUI/Loading/Serializer.hpp>
45#include <TGUI/Loading/Deserializer.hpp>
46#include <TGUI/Renderers/WidgetRenderer.hpp>
47#include <TGUI/extlibs/Aurora/SmartPtr/CopiedPtr.hpp>
48#include <TGUI/extlibs/Aurora/Tools/Downcast.hpp>
49
50#include <unordered_set>
51
53
54namespace tgui
55{
56 class GuiBase;
57 class Container;
58
59 enum class ShowEffectType;
60 namespace priv
61 {
62 class Animation;
63 }
64
69 class TGUI_API Widget : public std::enable_shared_from_this<Widget>
70 {
71 public:
72
73 typedef std::shared_ptr<Widget> Ptr;
74 typedef std::shared_ptr<const Widget> ConstPtr;
75
76
84 Widget(const char* typeName, bool initRenderer);
85
86
90 Widget(const Widget&);
91
96
100 virtual ~Widget();
101
106
111
112
120 void setRenderer(std::shared_ptr<RendererData> rendererData);
121
122
128 const WidgetRenderer* getSharedRenderer() const;
129
136 const WidgetRenderer* getRenderer() const;
137
138
162 virtual void setPosition(const Layout2d& position);
163
164
178 {
179 setPosition({std::move(x), std::move(y)});
180 }
181
182
189 {
190 return m_position.getValue();
191 }
192
193
211 virtual void setSize(const Layout2d& size);
212
213
222 void setSize(Layout width, Layout height)
223 {
224 setSize({std::move(width), std::move(height)});
225 }
226
227
236 void setWidth(Layout width)
237 {
238 setSize({std::move(width), m_size.y});
239 }
240
241
250 void setHeight(Layout height)
251 {
252 setSize({m_size.x, std::move(height)});
253 }
254
255
262 {
263 return m_size.getValue();
264 }
265
266
275 virtual Vector2f getFullSize() const;
276
277
284
285
293 virtual Vector2f getWidgetOffset() const;
294
295
306 void setOrigin(float x, float y)
307 {
308 setOrigin({x, y});
309 }
310
311
321 void setOrigin(Vector2f origin);
322
323
330 {
331 return m_origin;
332 }
333
334
348 void setScale(Vector2f scaleFactors);
349
350
364 void setScale(Vector2f scaleFactors, Vector2f origin);
365
366
367
381 void setScale(float scaleFactor)
382 {
383 setScale({scaleFactor, scaleFactor});
384 }
385
386
400 void setScale(float scaleFactor, Vector2f origin)
401 {
402 setScale({scaleFactor, scaleFactor}, origin);
403 }
404
405
412 {
413 return m_scaleFactors;
414 }
415
416
423
424
435 void setRotation(float angle);
436
437
448 void setRotation(float angle, Vector2f origin);
449
450
456 float getRotation() const
457 {
458 return m_rotationDeg;
459 }
460
461
468
469
489
490
510
511
519 virtual void setVisible(bool visible);
520
521
530 bool isVisible() const
531 {
532 return m_visible;
533 }
534
535
543 virtual void setEnabled(bool enabled);
544
545
554 bool isEnabled() const
555 {
556 return m_enabled;
557 }
558
559
568 virtual void setFocused(bool focused);
569
570
576 bool isFocused() const
577 {
578 return m_focused;
579 }
580
581
587 const String& getWidgetType() const;
588
589
596 {
597 return m_parent;
598 }
599
600
607 {
608 return m_parentGui;
609 }
610
611
617 bool isAnimationPlaying() const;
618
619
626
627
634
635
646 void setUserData(Any userData)
647 {
648 m_userData = std::move(userData);
649 }
650
656 template <typename T>
657 T getUserData() const
658 {
659 return AnyCast<T>(m_userData);
660 }
661
662
671 void setInheritedFont(const Font& font);
672
673
679 const Font& getInheritedFont() const;
680
681
690 void setInheritedOpacity(float opacity);
691
692
698 float getInheritedOpacity() const;
699
700
706 virtual void setTextSize(unsigned int size);
707
708
714 virtual unsigned int getTextSize() const;
715
716
722 void setToolTip(Widget::Ptr toolTip);
723
724
731
732
740 void setWidgetName(const String& name);
741
742
749
750
759
760
767
768
776 void setFocusable(bool focusable);
777
778
786 bool isFocusable() const;
787
788
795 virtual bool canGainFocus() const;
796
797
802 bool isContainer() const;
803
804
809 bool isDraggableWidget() const;
810
811
816 bool isMouseDown() const;
817
818
828 virtual Signal& getSignal(String signalName);
829
830
836 virtual void setParent(Container* parent);
837
838
843 virtual bool updateTime(Duration elapsedTime);
844
845
850 virtual bool isMouseOnWidget(Vector2f pos) const = 0;
851
855 virtual void leftMousePressed(Vector2f pos);
856
860 virtual void leftMouseReleased(Vector2f pos);
861
865 virtual void rightMousePressed(Vector2f pos);
866
870 virtual void rightMouseReleased(Vector2f pos);
871
875 virtual void mousePressed(Event::MouseButton button, Vector2f pos);
876
880 virtual void mouseReleased(Event::MouseButton button, Vector2f pos);
881
885 virtual void mouseMoved(Vector2f pos);
886
890 virtual void keyPressed(const Event::KeyEvent& event);
891
895 virtual void textEntered(char32_t key);
896
901 virtual bool mouseWheelScrolled(float delta, Vector2f pos);
902
906 virtual void mouseNoLongerOnWidget();
907
911 virtual void leftMouseButtonNoLongerDown();
912
916 virtual void rightMouseButtonNoLongerDown();
917
918
921 // Show the tool tip when the widget is located below the mouse.
922 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
923 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
925 virtual Widget::Ptr askToolTip(Vector2f mousePos);
926
927
932 const Layout2d& getPositionLayout() const
933 {
934 return m_position;
935 }
936
937
942 const Layout2d& getSizeLayout() const
943 {
944 return m_size;
945 }
946
947
952 void bindPositionLayout(Layout* layout);
953
954
959 void unbindPositionLayout(Layout* layout);
960
961
966 void bindSizeLayout(Layout* layout);
967
968
973 void unbindSizeLayout(Layout* layout);
974
975
984 virtual void draw(BackendRenderTargetBase& target, RenderStates states) const = 0;
985
986
990 template <typename WidgetType>
991 std::shared_ptr<const WidgetType> cast() const
992 {
993 return std::dynamic_pointer_cast<const WidgetType>(shared_from_this());
994 }
995
996
1000 template <typename WidgetType>
1001 std::shared_ptr<WidgetType> cast()
1002 {
1003 return std::dynamic_pointer_cast<WidgetType>(shared_from_this());
1004 }
1005
1006
1015 virtual Widget::Ptr clone() const = 0;
1016
1017
1019 protected:
1020
1021 using SavingRenderersMap = std::map<const Widget*, std::pair<std::unique_ptr<DataIO::Node>, String>>;
1022 using LoadingRenderersMap = std::map<String, std::shared_ptr<RendererData>>;
1023
1024
1030 virtual void rendererChanged(const String& property);
1031
1032
1036 virtual std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const;
1037
1038
1042 virtual void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers);
1043
1044
1048 virtual void mouseEnteredWidget();
1049
1050
1054 virtual void mouseLeftWidget();
1055
1056
1058 private:
1059
1061 // Callback function which is called on a renderer change and which calls the virtual rendererChanged function
1063 void rendererChangedCallback(const String& property);
1064
1065
1067 public:
1068
1069 SignalVector2f onPositionChange = {"PositionChanged"};
1070 SignalVector2f onSizeChange = {"SizeChanged"};
1071 Signal onFocus = {"Focused"};
1072 Signal onUnfocus = {"Unfocused"};
1073 Signal onMouseEnter = {"MouseEntered"};
1074 Signal onMouseLeave = {"MouseLeft"};
1075 TGUI_DEPRECATED("onAnimationFinish was renamed to onShowEffectFinish") SignalShowEffect onAnimationFinish = {"AnimationFinished"};
1076 SignalShowEffect onShowEffectFinish = {"ShowEffectFinished"};
1077
1078
1080 protected:
1081
1082 String m_type;
1083 String m_name;
1084
1085 Layout2d m_position;
1086 Layout2d m_size;
1087 unsigned int m_textSize = 0;
1088
1089 Vector2f m_origin;
1090 Optional<Vector2f> m_rotationOrigin;
1091 Optional<Vector2f> m_scaleOrigin;
1092 Vector2f m_scaleFactors = {1, 1};
1093 float m_rotationDeg = 0;
1094
1095 // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
1096 // changed and there would be no way for the widget to detect whether the values changed or not.
1097 Vector2f m_prevPosition;
1098 Vector2f m_prevSize;
1099
1100 // Layouts that need to recalculate their value when the position or size of this widget changes
1101 std::unordered_set<Layout*> m_boundPositionLayouts;
1102 std::unordered_set<Layout*> m_boundSizeLayouts;
1103
1104 // When a widget is disabled, it will no longer receive events
1105 bool m_enabled = true;
1106
1107 // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
1108 bool m_visible = true;
1109
1110 // This will point to our parent widget. If there is no parent then this will be nullptr.
1111 Container* m_parent = nullptr;
1112 GuiBase* m_parentGui = nullptr;
1113
1114 // Is the mouse on top of the widget? Did the mouse go down on the widget?
1115 bool m_mouseHover = false;
1116 bool m_mouseDown = false;
1117
1118 // Is the widget focused?
1119 bool m_focused = false;
1120
1121 // Can the widget be focused?
1122 bool m_focusable = true;
1123
1124 // Keep track of the elapsed time.
1125 Duration m_animationTimeElapsed;
1126
1127 // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
1128 bool m_draggableWidget = false;
1129
1130 // This is set to true for widgets that store other widgets inside them
1131 bool m_containerWidget = false;
1132
1133 // The tool tip connected to the widget
1134 Widget::Ptr m_toolTip = nullptr;
1135
1136 // Renderer of the widget
1137 aurora::CopiedPtr<WidgetRenderer> m_renderer = nullptr;
1138
1139 // Show animations
1140 std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
1141
1142 // Renderer properties that can be passed from containers to their children
1143 Font m_inheritedFont;
1144 float m_inheritedOpacity = 1;
1145
1146 // Cached renderer properties
1147 Font m_fontCached = Font::getGlobalFont();
1148 float m_opacityCached = 1;
1149 bool m_transparentTextureCached = false;
1150
1151 Any m_userData;
1152 Cursor::Type m_mouseCursor = Cursor::Type::Arrow;
1153
1154 std::function<void(const String& property)> m_rendererChangedCallback = [this](const String& property){ rendererChangedCallback(property); };
1155
1156
1158
1159 friend class Container; // Container accesses save and load functions
1160 };
1161
1163}
1164
1166
1167#endif // TGUI_WIDGET_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Container widget.
Definition Container.hpp:47
Type
List of available cursors.
Definition Cursor.hpp:48
Wrapper for durations.
Definition Duration.hpp:52
Definition Font.hpp:56
Base class for the Gui.
Definition GuiBase.hpp:45
Class to store the position or size of a widget.
Definition Layout.hpp:262
Class to store the left, top, width or height of a widget.
Definition Layout.hpp:50
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:669
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:58
Wrapper class to store strings.
Definition String.hpp:79
Base class for all renderer classes.
Definition WidgetRenderer.hpp:81
The parent class for every widget.
Definition Widget.hpp:70
void showWithEffect(ShowEffectType type, Duration duration)
Shows the widget by introducing it with an animation.
void setSize(Layout width, Layout height)
Changes the size of the widget.
Definition Widget.hpp:222
Widget(Widget &&)
Move constructor.
void moveToFront()
Places the widget before all other widgets.
void setHeight(Layout height)
Changes the height of the widget.
Definition Widget.hpp:250
const String & getWidgetType() const
Returns the type of the widget.
std::shared_ptr< WidgetType > cast()
Downcast widget.
Definition Widget.hpp:1001
virtual Widget::Ptr clone() const =0
Makes a copy of the widget if you don't know its exact type.
bool isDraggableWidget() const
Returns whether the widget has something to drag (e.g. slider or scrollbar thumbs)
Widget & operator=(const Widget &)
Overload of copy assignment operator.
void setScale(float scaleFactor, Vector2f origin)
Sets the scaling to be applied to the widget.
Definition Widget.hpp:400
void setScale(Vector2f scaleFactors, Vector2f origin)
Sets the scaling to be applied to the widget.
void setScale(Vector2f scaleFactors)
Sets the scaling to be applied to the widget.
Vector2f getScale() const
Returns the scaling to be applied to the widget.
Definition Widget.hpp:411
void setFocusable(bool focusable)
Changes whether a widget could be focused.
void setOrigin(float x, float y)
Sets the origin point on which the position, scale and rotation is based.
Definition Widget.hpp:306
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:73
virtual bool canGainFocus() const
Returns whether the widget can currently gain focus.
String getWidgetName() const
Returns the name of a widget.
Container * getParent() const
Returns a pointer to the parent widget.
Definition Widget.hpp:595
Cursor::Type getMouseCursor() const
Returns which mouse cursor is shown when hovering over the widget.
bool isVisible() const
Returns true when the widget is visible.
Definition Widget.hpp:530
bool isFocusable() const
Returns whether a widget could be focused.
Widget & operator=(Widget &&)
Move assignment.
void setRotation(float angle, Vector2f origin)
Sets the rotation to be applied to the widget.
float getRotation() const
Returns the rotation to be applied to the widget.
Definition Widget.hpp:456
void setPosition(Layout x, Layout y)
Sets the position of the widget.
Definition Widget.hpp:177
Vector2f getScaleOrigin() const
Returns the origin used for scaling.
GuiBase * getParentGui() const
Returns a pointer to the gui to which this widget belongs.
Definition Widget.hpp:606
virtual unsigned int getTextSize() const
Returns the character size of text in this widget.
bool isContainer() const
Returns whether the widget is a container widget or not.
virtual void mouseEnteredWidget()
This function is called when the mouse enters the widget.
const Font & getInheritedFont() const
Returns the font of the widget that is used when no font is set in the renderer.
void setWidth(Layout width)
Changes the width of the widget.
Definition Widget.hpp:236
virtual void mouseLeftWidget()
This function is called when the mouse leaves the widget.
virtual void rendererChanged(const String &property)
Function called when one of the properties of the renderer is changed.
void moveToBack()
Places the widget behind all other widgets.
virtual void setTextSize(unsigned int size)
Changes the character size of text in this widget if it uses text.
virtual std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const
Saves the widget as a tree node in order to save it to a file.
Widget(const Widget &)
Copy constructor.
virtual void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers)
Loads the widget from a tree of nodes.
virtual Signal & getSignal(String signalName)
Retrieves a signal based on its name.
bool isMouseDown() const
Returns whether the left mouse button has been pressed on top of the widget.
void setWidgetName(const String &name)
Changes the name of a widget.
bool isAnimationPlaying() const
Returns whether there is an active animation (started with showWithEffect or hideWithEffect)
virtual ~Widget()
Destructor.
bool isEnabled() const
Returns true when the widget is enabled.
Definition Widget.hpp:554
Vector2f getOrigin() const
Returns the relative origin point on which the position, scale and rotation is based.
Definition Widget.hpp:329
T getUserData() const
Returns data stored in the widget.
Definition Widget.hpp:657
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Vector2f getSize() const
Returns the size of the widget.
Definition Widget.hpp:261
virtual bool isMouseOnWidget(Vector2f pos) const =0
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
WidgetRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Widget::Ptr getToolTip() const
Returns the tool tip that is displayed when hovering over the widget.
void setMouseCursor(Cursor::Type cursor)
Changes which mouse cursor is shown when hovering over the widget.
virtual void setFocused(bool focused)
Focus or unfocus the widget.
virtual Vector2f getFullSize() const
Returns the entire size that the widget is using.
virtual void draw(BackendRenderTargetBase &target, RenderStates states) const =0
Draw the widget to a render target.
virtual void setVisible(bool visible)
Shows or hides a widget.
void setScale(float scaleFactor)
Sets the scaling to be applied to the widget.
Definition Widget.hpp:381
void setInheritedOpacity(float opacity)
Sets the opacity of the widget that will be multiplied with the opacity set in the renderer.
void setRotation(float angle)
Sets the rotation to be applied to the widget.
WidgetRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setUserData(Any userData)
Stores some data into the widget.
Definition Widget.hpp:646
virtual Vector2f getAbsolutePosition() const
Get the absolute position of the top-left point of the widget instead of the relative position to its...
void setToolTip(Widget::Ptr toolTip)
Sets the tool tip that should be displayed when hovering over the widget.
void setRenderer(std::shared_ptr< RendererData > rendererData)
Sets a new renderer for the widget. The renderer determines how the widget looks.
void setInheritedFont(const Font &font)
Sets the font of the widget that is used when no font is set in the renderer.
virtual Vector2f getWidgetOffset() const
Returns the distance between the position where the widget is drawn and where the widget is placed.
void setOrigin(Vector2f origin)
Sets the origin point on which the position, scale and rotation is based.
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition Widget.hpp:74
std::shared_ptr< const WidgetType > cast() const
Downcast const widget.
Definition Widget.hpp:991
virtual void setEnabled(bool enabled)
Enables or disables the widget.
float getInheritedOpacity() const
Returns the opacity of the widget that is multiplied with the opacity set in the renderer.
Vector2f getPosition() const
Gets the position of the widget.
Definition Widget.hpp:188
void hideWithEffect(ShowEffectType type, Duration duration)
Hides the widget by making it leave with an animation.
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Vector2f getRotationOrigin() const
Returns the origin used for rotations.
bool isFocused() const
Returns true when the widget is focused and false otherwise.
Definition Widget.hpp:576
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
ShowEffectType
Type of animation to show/hide widget.
Definition Animation.hpp:44
KeyPressed event parameters.
Definition Event.hpp:167
MouseButton
Mouse buttons.
Definition Event.hpp:148
States used for drawing.
Definition RenderStates.hpp:39