TGUI  1.0-beta
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/Backend/Renderer/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 BackendGui;
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 using Ptr = std::shared_ptr<Widget>;
74 using ConstPtr = std::shared_ptr<const Widget>;
75
76
84 Widget(const char* typeName, bool initRenderer);
85
86
90 Widget(const Widget&);
91
95 Widget(Widget&&) noexcept;
96
100 virtual ~Widget();
101
105 Widget& operator=(const Widget&);
106
110 Widget& operator=(Widget&&) noexcept;
111
112
120 void setRenderer(std::shared_ptr<RendererData> rendererData);
121
122
127 WidgetRenderer* getSharedRenderer();
128 const WidgetRenderer* getSharedRenderer() const;
129
135 WidgetRenderer* getRenderer();
136
137
161 virtual void setPosition(const Layout2d& position);
162
163
176 void setPosition(Layout x, Layout y)
177 {
178 setPosition({std::move(x), std::move(y)});
179 }
180
181
188 {
189 return m_position.getValue();
190 }
191
192
210 virtual void setSize(const Layout2d& size);
211
212
221 void setSize(Layout width, Layout height)
222 {
223 setSize({std::move(width), std::move(height)});
224 }
225
226
235 void setWidth(Layout width)
236 {
237 setSize({std::move(width), m_size.y});
238 }
239
240
249 void setHeight(Layout height)
250 {
251 setSize({m_size.x, std::move(height)});
252 }
253
254
261 {
262 return m_size.getValue();
263 }
264
265
274 virtual Vector2f getFullSize() const;
275
276
283
284
292 virtual Vector2f getWidgetOffset() const;
293
294
305 void setOrigin(float x, float y)
306 {
307 setOrigin({x, y});
308 }
309
310
320 void setOrigin(Vector2f origin);
321
322
329 {
330 return m_origin;
331 }
332
333
347 void setScale(Vector2f scaleFactors);
348
349
363 void setScale(Vector2f scaleFactors, Vector2f origin);
364
365
366
380 void setScale(float scaleFactor)
381 {
382 setScale({scaleFactor, scaleFactor});
383 }
384
385
399 void setScale(float scaleFactor, Vector2f origin)
400 {
401 setScale({scaleFactor, scaleFactor}, origin);
402 }
403
404
411 {
412 return m_scaleFactors;
413 }
414
415
422
423
434 void setRotation(float angle);
435
436
447 void setRotation(float angle, Vector2f origin);
448
449
455 float getRotation() const
456 {
457 return m_rotationDeg;
458 }
459
460
467
468
488
489
509
510
517 void moveWithAnimation(Layout2d position, Duration duration);
518
519
527
528
536 virtual void setVisible(bool visible);
537
538
547 bool isVisible() const
548 {
549 return m_visible;
550 }
551
552
560 virtual void setEnabled(bool enabled);
561
562
571 bool isEnabled() const
572 {
573 return m_enabled;
574 }
575
576
585 virtual void setFocused(bool focused);
586
587
593 bool isFocused() const
594 {
595 return m_focused;
596 }
597
598
604 const String& getWidgetType() const;
605
606
613 {
614 return m_parent;
615 }
616
617
624 {
625 return m_parentGui;
626 }
627
628
634 bool isAnimationPlaying() const;
635
636
643
644
651
652
663 void setUserData(Any userData)
664 {
665 m_userData = std::move(userData);
666 }
667
673 template <typename T>
674 T getUserData() const
675 {
676 return AnyCast<T>(m_userData);
677 }
678
679
684 bool hasUserData() const
685 {
686 return m_userData.has_value();
687 }
688
689
698 void setInheritedFont(const Font& font);
699
700
706 const Font& getInheritedFont() const;
707
708
717 void setInheritedOpacity(float opacity);
718
719
725 float getInheritedOpacity() const;
726
727
735 void setTextSize(unsigned int size);
736
737
746 unsigned int getTextSize() const;
747
748
754 void setToolTip(Widget::Ptr toolTip);
755
756
763
764
772 void setWidgetName(const String& name);
773
774
781
782
791
792
799
800
808 void setFocusable(bool focusable);
809
810
818 bool isFocusable() const;
819
820
825
826
833 virtual bool canGainFocus() const;
834
835
840 bool isContainer() const;
841
842
847 bool isDraggableWidget() const;
848
849
854 bool isMouseDown() const;
855
856
866 virtual Signal& getSignal(String signalName);
867
868
874 virtual void setParent(Container* parent);
875
876
881 virtual bool updateTime(Duration elapsedTime);
882
883
888 virtual bool isMouseOnWidget(Vector2f pos) const = 0;
889
893 virtual void leftMousePressed(Vector2f pos);
894
898 virtual void leftMouseReleased(Vector2f pos);
899
903 virtual void rightMousePressed(Vector2f pos);
904
908 virtual void rightMouseReleased(Vector2f pos);
909
913 virtual void mousePressed(Event::MouseButton button, Vector2f pos);
914
918 virtual void mouseReleased(Event::MouseButton button, Vector2f pos);
919
923 virtual void mouseMoved(Vector2f pos);
924
928 virtual void keyPressed(const Event::KeyEvent& event);
929
933 virtual void textEntered(char32_t key);
934
939 virtual bool mouseWheelScrolled(float delta, Vector2f pos);
940
944 virtual void mouseNoLongerOnWidget();
945
949 virtual void leftMouseButtonNoLongerDown();
950
954 virtual void rightMouseButtonNoLongerDown();
955
956
959 // Show the tool tip when the widget is located below the mouse.
960 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
961 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
963 virtual Widget::Ptr askToolTip(Vector2f mousePos);
964
965
970 const Layout2d& getPositionLayout() const
971 {
972 return m_position;
973 }
974
975
980 const Layout2d& getSizeLayout() const
981 {
982 return m_size;
983 }
984
985
990 void bindPositionLayout(Layout* layout);
991
992
997 void unbindPositionLayout(Layout* layout);
998
999
1004 void bindSizeLayout(Layout* layout);
1005
1006
1011 void unbindSizeLayout(Layout* layout);
1012
1013
1022 virtual void draw(BackendRenderTarget& target, RenderStates states) const = 0;
1023
1024
1028 template <typename WidgetType>
1029 std::shared_ptr<const WidgetType> cast() const
1030 {
1031 return std::dynamic_pointer_cast<const WidgetType>(shared_from_this());
1032 }
1033
1034
1038 template <typename WidgetType>
1039 std::shared_ptr<WidgetType> cast()
1040 {
1041 return std::dynamic_pointer_cast<WidgetType>(shared_from_this());
1042 }
1043
1044
1053 virtual Widget::Ptr clone() const = 0;
1054
1055
1060 void rendererChangedCallback(const String& property);
1061
1062
1067 virtual void updateTextSize();
1068
1069
1071 protected:
1072
1073 using SavingRenderersMap = std::map<const Widget*, std::pair<std::unique_ptr<DataIO::Node>, String>>;
1074 using LoadingRenderersMap = std::map<String, std::shared_ptr<RendererData>>;
1075
1076
1082 virtual void rendererChanged(const String& property);
1083
1084
1088 virtual std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const;
1089
1090
1094 virtual void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers);
1095
1096
1100 virtual void mouseEnteredWidget();
1101
1102
1106 virtual void mouseLeftWidget();
1107
1108
1110 public:
1111
1112 SignalVector2f onPositionChange = {"PositionChanged"};
1113 SignalVector2f onSizeChange = {"SizeChanged"};
1114 Signal onFocus = {"Focused"};
1115 Signal onUnfocus = {"Unfocused"};
1116 Signal onMouseEnter = {"MouseEntered"};
1117 Signal onMouseLeave = {"MouseLeft"};
1118 SignalShowEffect onShowEffectFinish = {"ShowEffectFinished"};
1119
1126 SignalAnimationType onAnimationFinish = {"AnimationFinished"};
1127
1129 protected:
1130
1131 String m_type;
1132 String m_name;
1133
1134
1139
1144 unsigned int m_textSize = 0; // This may be overwritten by the renderer, m_textSizeCached contains the actual text size
1145
1146 Vector2f m_origin;
1147 Optional<Vector2f> m_rotationOrigin;
1148 Optional<Vector2f> m_scaleOrigin;
1149 Vector2f m_scaleFactors = {1, 1};
1150 float m_rotationDeg = 0;
1151
1152 // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
1153 // changed and there would be no way for the widget to detect whether the values changed or not.
1154 Vector2f m_prevPosition;
1155 Vector2f m_prevSize;
1156
1157 // Layouts that need to recalculate their value when the position or size of this widget changes
1158 std::unordered_set<Layout*> m_boundPositionLayouts;
1159 std::unordered_set<Layout*> m_boundSizeLayouts;
1160
1161
1167 bool m_enabled = true;
1168
1174 bool m_visible = true;
1175
1176 // This will point to our parent widget. If there is no parent then this will be nullptr.
1177 Container* m_parent = nullptr;
1178 BackendGui* m_parentGui = nullptr;
1179
1180 // Is the mouse on top of the widget? Did the mouse go down on the widget?
1181 bool m_mouseHover = false;
1182 bool m_mouseDown = false;
1183
1184 // Is the widget focused?
1185 bool m_focused = false;
1186
1187 // Can the widget be focused?
1188 bool m_focusable = true;
1189
1190 // Keep track of the elapsed time.
1191 Duration m_animationTimeElapsed;
1192
1193 // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
1194 bool m_draggableWidget = false;
1195
1196 // This is set to true for widgets that store other widgets inside them
1197 bool m_containerWidget = false;
1198
1199 // The tool tip connected to the widget
1200 Widget::Ptr m_toolTip = nullptr;
1201
1202 // Renderer of the widget
1203 aurora::CopiedPtr<WidgetRenderer> m_renderer = nullptr;
1204
1205 // Show animations
1206 std::vector<std::unique_ptr<priv::Animation>> m_showAnimations;
1207
1208 // Renderer properties that can be passed from containers to their children
1209 Font m_inheritedFont;
1210 float m_inheritedOpacity = 1;
1211
1212 // Cached renderer properties
1213 Font m_fontCached = Font::getGlobalFont();
1214 float m_opacityCached = 1;
1215 bool m_transparentTextureCached = false;
1216 unsigned int m_textSizeCached = 0;
1217
1218 Any m_userData;
1219 Cursor::Type m_mouseCursor = Cursor::Type::Arrow;
1220
1222
1223 friend class Container; // Container accesses save and load functions
1224 };
1225
1227}
1228
1230
1231#endif // TGUI_WIDGET_HPP
Base class for the Gui.
Definition: BackendGui.hpp:45
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:57
Class to store the position or size of a widget.
Definition: Layout.hpp:284
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:880
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:770
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:69
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:221
void moveToFront()
Places the widget before all other widgets.
void setTextSize(unsigned int size)
Changes the character size of text in this widget if it uses text.
Layout2d m_size
Stores the size of this widget.
Definition: Widget.hpp:1143
void setHeight(Layout height)
Changes the height of the widget.
Definition: Widget.hpp:249
const String & getWidgetType() const
Returns the type of the widget.
Layout2d m_position
Stores the position of this widget.
Definition: Widget.hpp:1138
std::shared_ptr< WidgetType > cast()
Downcast widget.
Definition: Widget.hpp:1039
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)
void resizeWithAnimation(Layout2d size, Duration duration)
Resizes the widget from its current size to the given size, over a given duration.
void setScale(float scaleFactor, Vector2f origin)
Sets the scaling to be applied to the widget.
Definition: Widget.hpp:399
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:410
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:305
virtual void draw(BackendRenderTarget &target, RenderStates states) const =0
Draw the widget to a render target.
unsigned int getTextSize() const
Returns the character size of text in this widget.
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:612
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:547
bool isFocusable() const
Returns whether a widget could be focused.
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:455
Vector2f getScaleOrigin() const
Returns the origin used for scaling.
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:235
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 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(Widget &&) noexcept
Move constructor.
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.
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:74
virtual Signal & getSignal(String signalName)
Retrieves a signal based on its name.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
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)
bool isEnabled() const
Returns true when the widget is enabled.
Definition: Widget.hpp:571
Vector2f getOrigin() const
Returns the relative origin point on which the position, scale and rotation is based.
Definition: Widget.hpp:328
T getUserData() const
Returns data stored in the widget.
Definition: Widget.hpp:674
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Vector2f getSize() const
Returns the size of the widget.
Definition: Widget.hpp:260
void finishAllAnimations()
Makes all animations of the widget finish immediately.
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...
Widget::Ptr getToolTip() const
Returns the tool tip that is displayed when hovering over the widget.
BackendGui * getParentGui() const
Returns a pointer to the gui to which this widget belongs.
Definition: Widget.hpp:623
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 setVisible(bool visible)
Shows or hides a widget.
void setScale(float scaleFactor)
Sets the scaling to be applied to the widget.
Definition: Widget.hpp:380
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.
bool hasUserData() const
Returns whether data stored in the widget.
Definition: Widget.hpp:684
void setUserData(Any userData)
Stores some data into the widget.
Definition: Widget.hpp:663
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 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 moveWithAnimation(Layout2d position, Duration duration)
Moves the widget from its current position to the given position, over a given duration.
void setOrigin(Vector2f origin)
Sets the origin point on which the position, scale and rotation is based.
std::shared_ptr< const WidgetType > cast() const
Downcast const widget.
Definition: Widget.hpp:1029
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:187
void hideWithEffect(ShowEffectType type, Duration duration)
Hides the widget by making it leave with an animation.
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:593
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
ShowEffectType
Type of effect 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
Shared data used in renderer classes.
Definition: WidgetRenderer.hpp:47