TGUI  0.10-beta
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 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
518 void moveWithAnimation(Layout2d position, Duration duration);
519
520
528
529
537 virtual void setVisible(bool visible);
538
539
548 bool isVisible() const
549 {
550 return m_visible;
551 }
552
553
561 virtual void setEnabled(bool enabled);
562
563
572 bool isEnabled() const
573 {
574 return m_enabled;
575 }
576
577
586 virtual void setFocused(bool focused);
587
588
594 bool isFocused() const
595 {
596 return m_focused;
597 }
598
599
605 const String& getWidgetType() const;
606
607
614 {
615 return m_parent;
616 }
617
618
625 {
626 return m_parentGui;
627 }
628
629
635 bool isAnimationPlaying() const;
636
637
644
645
652
653
664 void setUserData(Any userData)
665 {
666 m_userData = std::move(userData);
667 }
668
674 template <typename T>
675 T getUserData() const
676 {
677 return AnyCast<T>(m_userData);
678 }
679
680
689 void setInheritedFont(const Font& font);
690
691
697 const Font& getInheritedFont() const;
698
699
708 void setInheritedOpacity(float opacity);
709
710
716 float getInheritedOpacity() const;
717
718
726 void setTextSize(unsigned int size);
727
728
737 unsigned int getTextSize() const;
738
739
745 void setToolTip(Widget::Ptr toolTip);
746
747
754
755
763 void setWidgetName(const String& name);
764
765
772
773
782
783
790
791
799 void setFocusable(bool focusable);
800
801
809 bool isFocusable() const;
810
811
816
817
824 virtual bool canGainFocus() const;
825
826
831 bool isContainer() const;
832
833
838 bool isDraggableWidget() const;
839
840
845 bool isMouseDown() const;
846
847
857 virtual Signal& getSignal(String signalName);
858
859
865 virtual void setParent(Container* parent);
866
867
872 virtual bool updateTime(Duration elapsedTime);
873
874
879 virtual bool isMouseOnWidget(Vector2f pos) const = 0;
880
884 virtual void leftMousePressed(Vector2f pos);
885
889 virtual void leftMouseReleased(Vector2f pos);
890
894 virtual void rightMousePressed(Vector2f pos);
895
899 virtual void rightMouseReleased(Vector2f pos);
900
904 virtual void mousePressed(Event::MouseButton button, Vector2f pos);
905
909 virtual void mouseReleased(Event::MouseButton button, Vector2f pos);
910
914 virtual void mouseMoved(Vector2f pos);
915
919 virtual void keyPressed(const Event::KeyEvent& event);
920
924 virtual void textEntered(char32_t key);
925
930 virtual bool mouseWheelScrolled(float delta, Vector2f pos);
931
935 virtual void mouseNoLongerOnWidget();
936
940 virtual void leftMouseButtonNoLongerDown();
941
945 virtual void rightMouseButtonNoLongerDown();
946
947
950 // Show the tool tip when the widget is located below the mouse.
951 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
952 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
954 virtual Widget::Ptr askToolTip(Vector2f mousePos);
955
956
961 const Layout2d& getPositionLayout() const
962 {
963 return m_position;
964 }
965
966
971 const Layout2d& getSizeLayout() const
972 {
973 return m_size;
974 }
975
976
981 void bindPositionLayout(Layout* layout);
982
983
988 void unbindPositionLayout(Layout* layout);
989
990
995 void bindSizeLayout(Layout* layout);
996
997
1002 void unbindSizeLayout(Layout* layout);
1003
1004
1013 virtual void draw(BackendRenderTarget& target, RenderStates states) const = 0;
1014
1015
1019 template <typename WidgetType>
1020 std::shared_ptr<const WidgetType> cast() const
1021 {
1022 return std::dynamic_pointer_cast<const WidgetType>(shared_from_this());
1023 }
1024
1025
1029 template <typename WidgetType>
1030 std::shared_ptr<WidgetType> cast()
1031 {
1032 return std::dynamic_pointer_cast<WidgetType>(shared_from_this());
1033 }
1034
1035
1044 virtual Widget::Ptr clone() const = 0;
1045
1046
1048 protected:
1049
1050 using SavingRenderersMap = std::map<const Widget*, std::pair<std::unique_ptr<DataIO::Node>, String>>;
1051 using LoadingRenderersMap = std::map<String, std::shared_ptr<RendererData>>;
1052
1053
1059 virtual void rendererChanged(const String& property);
1060
1061
1065 virtual std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const;
1066
1067
1071 virtual void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers);
1072
1073
1077 virtual void updateTextSize();
1078
1079
1083 virtual void mouseEnteredWidget();
1084
1085
1089 virtual void mouseLeftWidget();
1090
1091
1093 private:
1094
1096 // Callback function which is called on a renderer change and which calls the virtual rendererChanged function
1098 void rendererChangedCallback(const String& property);
1099
1100
1102 public:
1103
1104 SignalVector2f onPositionChange = {"PositionChanged"};
1105 SignalVector2f onSizeChange = {"SizeChanged"};
1106 Signal onFocus = {"Focused"};
1107 Signal onUnfocus = {"Unfocused"};
1108 Signal onMouseEnter = {"MouseEntered"};
1109 Signal onMouseLeave = {"MouseLeft"};
1110 SignalShowEffect onShowEffectFinish = {"ShowEffectFinished"};
1111
1118 SignalAnimationType onAnimationFinish = {"AnimationFinished"};
1119
1121 protected:
1122
1123 String m_type;
1124 String m_name;
1125
1126 Layout2d m_position;
1127 Layout2d m_size;
1128 unsigned int m_textSize = 0; // This may be overwritten by the renderer, m_textSizeCached contains the actual text size
1129
1130 Vector2f m_origin;
1131 Optional<Vector2f> m_rotationOrigin;
1132 Optional<Vector2f> m_scaleOrigin;
1133 Vector2f m_scaleFactors = {1, 1};
1134 float m_rotationDeg = 0;
1135
1136 // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
1137 // changed and there would be no way for the widget to detect whether the values changed or not.
1138 Vector2f m_prevPosition;
1139 Vector2f m_prevSize;
1140
1141 // Layouts that need to recalculate their value when the position or size of this widget changes
1142 std::unordered_set<Layout*> m_boundPositionLayouts;
1143 std::unordered_set<Layout*> m_boundSizeLayouts;
1144
1145 // When a widget is disabled, it will no longer receive events
1146 bool m_enabled = true;
1147
1148 // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
1149 bool m_visible = true;
1150
1151 // This will point to our parent widget. If there is no parent then this will be nullptr.
1152 Container* m_parent = nullptr;
1153 BackendGui* m_parentGui = nullptr;
1154
1155 // Is the mouse on top of the widget? Did the mouse go down on the widget?
1156 bool m_mouseHover = false;
1157 bool m_mouseDown = false;
1158
1159 // Is the widget focused?
1160 bool m_focused = false;
1161
1162 // Can the widget be focused?
1163 bool m_focusable = true;
1164
1165 // Keep track of the elapsed time.
1166 Duration m_animationTimeElapsed;
1167
1168 // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
1169 bool m_draggableWidget = false;
1170
1171 // This is set to true for widgets that store other widgets inside them
1172 bool m_containerWidget = false;
1173
1174 // The tool tip connected to the widget
1175 Widget::Ptr m_toolTip = nullptr;
1176
1177 // Renderer of the widget
1178 aurora::CopiedPtr<WidgetRenderer> m_renderer = nullptr;
1179
1180 // Show animations
1181 std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
1182
1183 // Renderer properties that can be passed from containers to their children
1184 Font m_inheritedFont;
1185 float m_inheritedOpacity = 1;
1186
1187 // Cached renderer properties
1188 Font m_fontCached = Font::getGlobalFont();
1189 float m_opacityCached = 1;
1190 bool m_transparentTextureCached = false;
1191 unsigned int m_textSizeCached = 0;
1192
1193 Any m_userData;
1194 Cursor::Type m_mouseCursor = Cursor::Type::Arrow;
1195
1196 std::function<void(const String& property)> m_rendererChangedCallback = [this](const String& property){ rendererChangedCallback(property); };
1197
1198
1200
1201 friend class Container; // Container accesses save and load functions
1202 };
1203
1205}
1206
1208
1209#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
@ Arrow
Arrow cursor (default)
Wrapper for durations.
Definition: Duration.hpp:52
Definition: Font.hpp:57
static Font getGlobalFont()
Returns the global font that is used for all new widgets.
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:779
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:62
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 setTextSize(unsigned int size)
Changes the character size of text in this widget if it uses text.
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:1030
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 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: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
virtual void updateTextSize()
Called when the text size is changed (either by setTextSize or via the renderer)
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
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.
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:613
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:548
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.
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 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:572
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:675
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
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...
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.
BackendGui * getParentGui() const
Returns a pointer to the gui to which this widget belongs.
Definition: Widget.hpp:624
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: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:664
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 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 Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:74
std::shared_ptr< const WidgetType > cast() const
Downcast const widget.
Definition: Widget.hpp:1020
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:594
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