TGUI  0.9.1
Widget.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_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 
54 namespace tgui
55 {
56  class GuiBase;
57  class Container;
58 
59  enum class ShowAnimationType;
60  namespace priv
61  {
62  class Animation;
63  }
64 
65 
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 
283  virtual Vector2f getAbsolutePosition() const;
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 
625  void moveToFront();
626 
627 
633  void moveToBack();
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  SignalAnimation onAnimationFinish = {"AnimationFinished"};
1076 
1077 
1079  protected:
1080 
1081  String m_type;
1082  String m_name;
1083 
1084  Layout2d m_position;
1085  Layout2d m_size;
1086  unsigned int m_textSize = 0;
1087 
1088  Vector2f m_origin;
1089  Optional<Vector2f> m_rotationOrigin;
1090  Optional<Vector2f> m_scaleOrigin;
1091  Vector2f m_scaleFactors = {1, 1};
1092  float m_rotationDeg = 0;
1093 
1094  // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
1095  // changed and there would be no way for the widget to detect whether the values changed or not.
1096  Vector2f m_prevPosition;
1097  Vector2f m_prevSize;
1098 
1099  // Layouts that need to recalculate their value when the position or size of this widget changes
1100  std::unordered_set<Layout*> m_boundPositionLayouts;
1101  std::unordered_set<Layout*> m_boundSizeLayouts;
1102 
1103  // When a widget is disabled, it will no longer receive events
1104  bool m_enabled = true;
1105 
1106  // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
1107  bool m_visible = true;
1108 
1109  // This will point to our parent widget. If there is no parent then this will be nullptr.
1110  Container* m_parent = nullptr;
1111  GuiBase* m_parentGui = nullptr;
1112 
1113  // Is the mouse on top of the widget? Did the mouse go down on the widget?
1114  bool m_mouseHover = false;
1115  bool m_mouseDown = false;
1116 
1117  // Is the widget focused?
1118  bool m_focused = false;
1119 
1120  // Can the widget be focused?
1121  bool m_focusable = true;
1122 
1123  // Keep track of the elapsed time.
1124  Duration m_animationTimeElapsed;
1125 
1126  // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
1127  bool m_draggableWidget = false;
1128 
1129  // This is set to true for widgets that store other widgets inside them
1130  bool m_containerWidget = false;
1131 
1132  // The tool tip connected to the widget
1133  Widget::Ptr m_toolTip = nullptr;
1134 
1135  // Renderer of the widget
1136  aurora::CopiedPtr<WidgetRenderer> m_renderer = nullptr;
1137 
1138  // Show animations
1139  std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
1140 
1141  // Renderer properties that can be passed from containers to their children
1142  Font m_inheritedFont;
1143  float m_inheritedOpacity = 1;
1144 
1145  // Cached renderer properties
1146  Font m_fontCached = Font::getGlobalFont();
1147  float m_opacityCached = 1;
1148  bool m_transparentTextureCached = false;
1149 
1150  Any m_userData;
1151  Cursor::Type m_mouseCursor = Cursor::Type::Arrow;
1152 
1153  std::function<void(const String& property)> m_rendererChangedCallback = [this](const String& property){ rendererChangedCallback(property); };
1154 
1155 
1157 
1158  friend class Container; // Container accesses save and load functions
1159  };
1160 
1162 }
1163 
1165 
1166 #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
@ Arrow
Arrow cursor (default)
Wrapper for durations.
Definition: Duration.hpp:52
Definition: Font.hpp:56
static Font getGlobalFont()
Returns the global font that is used for all new widgets.
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:74
Base class for all renderer classes.
Definition: WidgetRenderer.hpp:81
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< const WidgetType > cast() const
Downcast const widget.
Definition: Widget.hpp:991
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 showWithEffect(ShowAnimationType type, Duration duration)
Shows the widget by introducing it with an animation.
void setHeight(Layout height)
Changes the height of the widget.
Definition: Widget.hpp:250
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=(Widget &&)
Move assignment.
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
GuiBase * getParentGui() const
Returns a pointer to the gui to which this widget belongs.
Definition: Widget.hpp:606
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual bool canGainFocus() const
Returns whether the widget can currently gain focus.
const Font & getInheritedFont() const
Returns the font of the widget that is used when no font is set in the renderer.
String getWidgetName() const
Returns the name of a widget.
void hideWithEffect(ShowAnimationType type, Duration duration)
Hides the widget by making it leave with an animation.
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.
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.
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.
const String & getWidgetType() const
Returns the type of the widget.
virtual void mouseEnteredWidget()
This function is called when the mouse enters the widget.
WidgetRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
Widget & operator=(const Widget &)
Overload of copy assignment operator.
virtual Signal & getSignal(String signalName)
Retrieves a signal based on its name.
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.
Container * getParent() const
Returns a pointer to the parent widget.
Definition: Widget.hpp:595
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...
Widget::Ptr getToolTip() const
Returns the tool tip that is displayed when hovering over the widget.
std::shared_ptr< WidgetType > cast()
Downcast widget.
Definition: Widget.hpp:1001
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 * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
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
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
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
ShowAnimationType
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