TGUI  0.8.8
Widget.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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/Global.hpp>
32 #include <TGUI/Signal.hpp>
33 #include <TGUI/Sprite.hpp>
34 #include <TGUI/Layout.hpp>
35 #include <TGUI/String.hpp>
36 #include <TGUI/Vector2f.hpp>
37 #include <TGUI/Loading/Theme.hpp>
38 #include <TGUI/Loading/DataIO.hpp>
39 #include <TGUI/Loading/Serializer.hpp>
40 #include <TGUI/Loading/Deserializer.hpp>
41 #include <TGUI/Renderers/WidgetRenderer.hpp>
42 #include <TGUI/Aurora/SmartPtr/CopiedPtr.hpp>
43 #include <TGUI/Aurora/Tools/Downcast.hpp>
44 
45 #include <SFML/System/Time.hpp>
46 #include <SFML/Window/Event.hpp>
47 #include <SFML/Graphics/RenderTarget.hpp>
48 
49 #include <unordered_set>
50 
51 #if TGUI_COMPILED_WITH_CPP_VER >= 17
52  #include <any>
53 #else
54  #include <TGUI/Any.hpp>
55 #endif
56 
58 
59 namespace tgui
60 {
61  class Container;
62 
63  enum class ShowAnimationType;
64  namespace priv
65  {
66  class Animation;
67  }
68 
69 
73  class TGUI_API Widget : public SignalWidgetBase, public std::enable_shared_from_this<Widget>
74  {
75  public:
76 
77  typedef std::shared_ptr<Widget> Ptr;
78  typedef std::shared_ptr<const Widget> ConstPtr;
79 
80 
84  Widget();
85 
89  Widget(const Widget&);
90 
95 
100 
105 
110 
111 
119  void setRenderer(std::shared_ptr<RendererData> rendererData);
120 
121 
127  const WidgetRenderer* getSharedRenderer() const;
128 
135  const WidgetRenderer* getRenderer() const;
136 
137 
161  virtual void setPosition(const Layout2d& position);
162 
163 
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 
233  {
234  return m_size.getValue();
235  }
236 
237 
246  virtual Vector2f getFullSize() const;
247 
248 
254  virtual Vector2f getAbsolutePosition() const;
255 
256 
264  virtual Vector2f getWidgetOffset() const;
265 
266 
280 #ifdef TGUI_NEXT
281  void showWithEffect(ShowAnimationType type, sf::Time duration);
282 #else
283  virtual void showWithEffect(ShowAnimationType type, sf::Time duration);
284 #endif
285 
299 #ifdef TGUI_NEXT
300  void hideWithEffect(ShowAnimationType type, sf::Time duration);
301 #else
302  virtual void hideWithEffect(ShowAnimationType type, sf::Time duration);
303 #endif
304 
305 
313  virtual void setVisible(bool visible);
314 
315 
324  bool isVisible() const
325  {
326  return m_visible;
327  }
328 
329 
337  virtual void setEnabled(bool enabled);
338 
339 
348  bool isEnabled() const
349  {
350  return m_enabled;
351  }
352 
353 
362  virtual void setFocused(bool focused);
363 
364 
370  bool isFocused() const
371  {
372  return m_focused;
373  }
374 
375 
381  const std::string& getWidgetType() const;
382 
383 
390  {
391  return m_parent;
392  }
393 
394 
400  bool isAnimationPlaying() const;
401 
402 
408 #ifdef TGUI_NEXT
409  void moveToFront();
410 #else
411  virtual void moveToFront();
412 #endif
413 
419 #ifdef TGUI_NEXT
420  void moveToBack();
421 #else
422  virtual void moveToBack();
423 #endif
424 
435 #if TGUI_COMPILED_WITH_CPP_VER >= 17
436  void setUserData(std::any userData)
437 #else
438  void setUserData(tgui::Any userData)
439 #endif
440  {
441  m_userData = std::move(userData);
442  }
443 
449  template <typename T>
450  T getUserData() const
451  {
452 #if TGUI_COMPILED_WITH_CPP_VER >= 17
453  return std::any_cast<T>(m_userData);
454 #else
455  return m_userData.as<T>();
456 #endif
457  }
458 
459 
465  void setInheritedFont(const Font& font);
466 
467 
473  const Font& getInheritedFont() const;
474 
475 
481  void setInheritedOpacity(float opacity);
482 
483 
489  float getInheritedOpacity() const;
490 
491 
497  virtual void setTextSize(unsigned int size);
498 
499 
505  virtual unsigned int getTextSize() const;
506 
507 
513  void setToolTip(Widget::Ptr toolTip);
514 
515 
522 
523 
531  void setWidgetName(const sf::String& name);
532 
533 
539  sf::String getWidgetName() const;
540 
541 
549  void setFocusable(bool focusable);
550 
551 
559  bool isFocusable() const;
560 
561 
568  virtual bool canGainFocus() const;
569 
570 
575  bool isContainer() const;
576 
577 
583  virtual void setParent(Container* parent);
584 
585 
590  virtual bool update(sf::Time elapsedTime);
591 
592 
597  virtual bool mouseOnWidget(Vector2f pos) const = 0;
598 
602  virtual void leftMousePressed(Vector2f pos);
603 
607  virtual void leftMouseReleased(Vector2f pos);
608 
612  virtual void rightMousePressed(Vector2f pos);
613 
617  virtual void rightMouseReleased(Vector2f pos);
618 
622  virtual void mousePressed(sf::Mouse::Button button, Vector2f pos);
623 
627  virtual void mouseReleased(sf::Mouse::Button button, Vector2f pos);
628 
632  virtual void mouseMoved(Vector2f pos);
633 
637  virtual void keyPressed(const sf::Event::KeyEvent& event);
638 
642  virtual void textEntered(std::uint32_t key);
643 
648  virtual bool mouseWheelScrolled(float delta, Vector2f pos);
649 
653  virtual void mouseNoLongerOnWidget();
654 
655 #ifndef TGUI_REMOVE_DEPRECATED_CODE
656  TGUI_DEPRECATED("Use leftMouseButtonNoLongerDown instead") virtual void mouseNoLongerDown();
660 #endif
661  virtual void leftMouseButtonNoLongerDown();
665 
669  virtual void rightMouseButtonNoLongerDown();
670 
671 
674  // Show the tool tip when the widget is located below the mouse.
675  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
676  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
678  virtual Widget::Ptr askToolTip(Vector2f mousePos);
679 
680 
685  const Layout2d& getPositionLayout() const
686  {
687  return m_position;
688  }
689 
690 
695  const Layout2d& getSizeLayout() const
696  {
697  return m_size;
698  }
699 
700 
705  void bindPositionLayout(Layout* layout);
706 
707 
712  void unbindPositionLayout(Layout* layout);
713 
714 
719  void bindSizeLayout(Layout* layout);
720 
721 
726  void unbindSizeLayout(Layout* layout);
727 
728 
738  Signal& getSignal(std::string signalName) override;
739 
740 
749  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
750 
751 
755  template <typename WidgetType>
756  std::shared_ptr<const WidgetType> cast() const
757  {
758  return std::dynamic_pointer_cast<const WidgetType>(shared_from_this());
759  }
760 
761 
765  template <typename WidgetType>
766  std::shared_ptr<WidgetType> cast()
767  {
768  return std::dynamic_pointer_cast<WidgetType>(shared_from_this());
769  }
770 
771 
780  virtual Widget::Ptr clone() const = 0;
781 
782 
784  protected:
785 
786  using SavingRenderersMap = std::map<const Widget*, std::pair<std::unique_ptr<DataIO::Node>, std::string>>;
787  using LoadingRenderersMap = std::map<std::string, std::shared_ptr<RendererData>>;
788 
789 
795  virtual void rendererChanged(const std::string& property);
796 
797 
801  virtual std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const;
802 
803 
807  virtual void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers);
808 
809 
813  virtual void mouseEnteredWidget();
814 
815 
819  virtual void mouseLeftWidget();
820 
821 
823  // Draw a rectangle shape with the right opacity
825  void drawRectangleShape(sf::RenderTarget& target,
826  const sf::RenderStates& states,
827  Vector2f size,
828  Color color) const;
829 
830 
832  // Draw the borders around the widget
834  void drawBorders(sf::RenderTarget& target,
835  const sf::RenderStates& states,
836  const Borders& borders,
837  Vector2f size,
838  Color color) const;
839 
840 
842  private:
843 
845  // Callback function which is called on a renderer change and which calls the virtual rendererChanged function
847  void rendererChangedCallback(const std::string& property);
848 
849 
851  public:
852 
853  SignalVector2f onPositionChange = {"PositionChanged"};
854  SignalVector2f onSizeChange = {"SizeChanged"};
855  Signal onFocus = {"Focused"};
856  Signal onUnfocus = {"Unfocused"};
857  Signal onMouseEnter = {"MouseEntered"};
858  Signal onMouseLeave = {"MouseLeft"};
859  SignalAnimation onAnimationFinished = {"AnimationFinished"};
860 
861 
863  protected:
864 
865  std::string m_type;
866  sf::String m_name;
867 
868  Layout2d m_position;
869  Layout2d m_size;
870  unsigned int m_textSize = 0;
871 
872  // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
873  // changed and there would be no way for the widget to detect whether the values changed or not.
874  Vector2f m_prevPosition;
875  Vector2f m_prevSize;
876 
877  // Layouts that need to recalculate their value when the position or size of this widget changes
878  std::unordered_set<Layout*> m_boundPositionLayouts;
879  std::unordered_set<Layout*> m_boundSizeLayouts;
880 
881  // When a widget is disabled, it will no longer receive events
882  bool m_enabled = true;
883 
884  // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
885  bool m_visible = true;
886 
887  // This will point to our parent widget. If there is no parent then this will be nullptr.
888  Container* m_parent = nullptr;
889 
890  // Is the mouse on top of the widget? Did the mouse go down on the widget?
891  bool m_mouseHover = false;
892  bool m_mouseDown = false;
893 
894  // Is the widget focused?
895  bool m_focused = false;
896 
897  // Can the widget be focused?
898  bool m_focusable = true;
899 
900  // Keep track of the elapsed time.
901  sf::Time m_animationTimeElapsed;
902 
903  // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
904  bool m_draggableWidget = false;
905 
906  // This is set to true for widgets that store other widgets inside them
907  bool m_containerWidget = false;
908 
909  // The tool tip connected to the widget
910  Widget::Ptr m_toolTip = nullptr;
911 
912  // Renderer of the widget
913  aurora::CopiedPtr<WidgetRenderer> m_renderer = aurora::makeCopied<WidgetRenderer>();
914 
915  // Show animations
916  std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
917 
918  // Renderer properties that can be passed from containers to their children
919  Font m_inheritedFont;
920  float m_inheritedOpacity = 1;
921 
922  // Cached renderer properties
923  Font m_fontCached = getGlobalFont();
924  float m_opacityCached = 1;
925  bool m_transparentTextureCached = false;
926 
927 #if TGUI_COMPILED_WITH_CPP_VER >= 17
928  std::any m_userData;
929 #else
930  tgui::Any m_userData;
931 #endif
932 
933  std::function<void(const std::string& property)> m_rendererChangedCallback = [this](const std::string& property){ rendererChangedCallback(property); };
934 
935 
937 
938  friend class Container; // Container accesses m_mouseDown, m_draggableWidget, save and load
939  };
940 
942 }
943 
945 
946 #endif // TGUI_WIDGET_HPP
void setUserData(tgui::Any userData)
Stores some data into the widget.
Definition: Widget.hpp:438
WidgetRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Vector2f getSize() const
Returns the size of the widget.
Definition: Widget.hpp:232
virtual void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers)
Loads the widget from a tree of nodes.
Definition: Font.hpp:43
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
Container widget.
Definition: Container.hpp:48
void setFocusable(bool focusable)
Changes whether a widget could be focused.
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.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:687
TGUI_API Font getGlobalFont()
Retrieves the default font used for all new widgets.
ShowAnimationType
Type of animation to show/hide widget.
Definition: Animation.hpp:44
T getUserData() const
Returns data stored in the widget.
Definition: Widget.hpp:450
Base class for all renderer classes.
Definition: WidgetRenderer.hpp:81
virtual void showWithEffect(ShowAnimationType type, sf::Time duration)
Shows the widget by introducing it with an animation.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
virtual bool mouseOnWidget(Vector2f pos) const =0
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
virtual void setEnabled(bool enabled)
Enables or disables the widget.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
virtual void setVisible(bool visible)
Shows or hides a widget.
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Widget()
Default constructor.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
virtual void setTextSize(unsigned int size)
Changes the character size of text in this widget if it uses text.
virtual void hideWithEffect(ShowAnimationType type, sf::Time duration)
Hides the widget by making it leave with an animation.
Wrapper for colors.
Definition: Color.hpp:49
Vector2f getPosition() const
Gets the position of the widget.
Definition: Widget.hpp:187
virtual void rendererChanged(const std::string &property)
Function called when one of the properties of the renderer is changed.
bool isContainer() const
Returns whether the widget is a container widget or not.
void setWidgetName(const sf::String &name)
Changes the name of a widget.
bool isEnabled() const
Returns true when the widget is enabled.
Definition: Widget.hpp:348
WidgetRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:78
~Widget()
Destructor.
virtual void mouseLeftWidget()
This function is called when the mouse leaves the widget.
Widget & operator=(Widget &&)
Move assignment.
virtual unsigned int getTextSize() const
Returns the character size of text in this widget.
Widget(const Widget &)
Copy constructor.
Widget(Widget &&)
Move constructor.
void setRenderer(std::shared_ptr< RendererData > rendererData)
Sets a new renderer for the widget. The renderer determines how the widget looks.
void setPosition(Layout x, Layout y)
Sets the position of the widget.
Definition: Widget.hpp:176
virtual Vector2f getAbsolutePosition() const
Get the absolute position of the widget instead of the relative position to its parent.
void setInheritedFont(const Font &font)
Sets the font of the widget that is used when no font is set in the renderer.
void setInheritedOpacity(float opacity)
Sets the opacity of the widget that will be multiplied with the opacity set in the renderer.
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const =0
Draw the widget to a render target.
virtual Vector2f getWidgetOffset() const
Returns the distance between the position where the widget is drawn and where the widget is placed.
virtual Widget::Ptr clone() const =0
Makes a copy of the widget if you don't know its exact type.
Definition: Vector2f.hpp:39
Definition: Any.hpp:37
Definition: Outline.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
virtual Vector2f getFullSize() const
Returns the entire size that the widget is using.
bool isAnimationPlaying() const
Returns whether there is an active animation (started with showWithEffect or hideWithEffect)
const Font & getInheritedFont() const
Returns the font of the widget that is used when no font is set in the renderer.
Widget & operator=(const Widget &)
Overload of copy assignment operator.
Widget::Ptr getToolTip() const
Returns the tool tip that is displayed when hovering over the widget.
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< WidgetType > cast()
Downcast widget.
Definition: Widget.hpp:766
virtual bool canGainFocus() const
Returns whether the widget can currently gain focus.
virtual void moveToBack()
Places the widget behind all other widgets.
bool isVisible() const
Returns true when the widget is visible.
Definition: Widget.hpp:324
bool isFocused() const
Returns true when the widget is focused and false otherwise.
Definition: Widget.hpp:370
const std::string & getWidgetType() const
Returns the type of the widget.
virtual void setFocused(bool focused)
Focus or unfocus the widget.
Class to store the left, top, width or height of a widget.
Definition: Layout.hpp:50
float getInheritedOpacity() const
Returns the opacity of the widget that is multiplied with the opacity set in the renderer.
Container * getParent() const
Returns a pointer to the parent widget.
Definition: Widget.hpp:389
Base class for Widget which provides functionality to connect signals based on their name.
Definition: Signal.hpp:827
void setToolTip(Widget::Ptr toolTip)
Sets the tool tip that should be displayed when hovering over the widget.
std::shared_ptr< const WidgetType > cast() const
Downcast const widget.
Definition: Widget.hpp:756
virtual void mouseEnteredWidget()
This function is called when the mouse enters the widget.
void setSize(Layout width, Layout height)
Changes the size of the widget.
Definition: Widget.hpp:221
sf::String getWidgetName() const
Returns the name of a widget.
virtual void moveToFront()
Places the widget before all other widgets.
bool isFocusable() const
Returns whether a widget could be focused.