TGUI  0.9-dev
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/Signal.hpp>
32 #include <TGUI/Sprite.hpp>
33 #include <TGUI/Layout.hpp>
34 #include <TGUI/String.hpp>
35 #include <TGUI/Vector2.hpp>
36 #include <TGUI/Duration.hpp>
37 #include <TGUI/Any.hpp>
38 #include <TGUI/Loading/Theme.hpp>
39 #include <TGUI/Loading/DataIO.hpp>
40 #include <TGUI/Loading/Serializer.hpp>
41 #include <TGUI/Loading/Deserializer.hpp>
42 #include <TGUI/Renderers/WidgetRenderer.hpp>
43 #include <TGUI/Aurora/SmartPtr/CopiedPtr.hpp>
44 #include <TGUI/Aurora/Tools/Downcast.hpp>
45 
46 #include <SFML/System/Time.hpp>
47 #include <SFML/Window/Event.hpp>
48 #include <SFML/Graphics/RenderTarget.hpp>
49 
50 #include <unordered_set>
51 
53 
54 namespace tgui
55 {
56  class Container;
57 
58  enum class ShowAnimationType;
59  namespace priv
60  {
61  class Animation;
62  }
63 
64 
68  class TGUI_API Widget : public std::enable_shared_from_this<Widget>
69  {
70  public:
71 
72  typedef std::shared_ptr<Widget> Ptr;
73  typedef std::shared_ptr<const Widget> ConstPtr;
74 
75 
79  Widget();
80 
84  Widget(const Widget&);
85 
89  Widget(Widget&&);
90 
94  virtual ~Widget();
95 
99  Widget& operator=(const Widget&);
100 
104  Widget& operator=(Widget&&);
105 
106 
114  void setRenderer(std::shared_ptr<RendererData> rendererData);
115 
116 
121  WidgetRenderer* getSharedRenderer();
122  const WidgetRenderer* getSharedRenderer() const;
123 
129  WidgetRenderer* getRenderer();
130  const WidgetRenderer* getRenderer() const;
131 
132 
156  virtual void setPosition(const Layout2d& position);
157 
158 
172  {
173  setPosition({std::move(x), std::move(y)});
174  }
175 
176 
183  {
184  return m_position.getValue();
185  }
186 
187 
205  virtual void setSize(const Layout2d& size);
206 
207 
216  void setSize(Layout width, Layout height)
217  {
218  setSize({std::move(width), std::move(height)});
219  }
220 
221 
228  {
229  return m_size.getValue();
230  }
231 
232 
241  virtual Vector2f getFullSize() const;
242 
243 
249  virtual Vector2f getAbsolutePosition() const;
250 
251 
259  virtual Vector2f getWidgetOffset() const;
260 
261 
280  void showWithEffect(ShowAnimationType type, Duration duration);
281 
282 
301  void hideWithEffect(ShowAnimationType type, Duration duration);
302 
303 
311  virtual void setVisible(bool visible);
312 
313 
322  bool isVisible() const
323  {
324  return m_visible;
325  }
326 
327 
335  virtual void setEnabled(bool enabled);
336 
337 
346  bool isEnabled() const
347  {
348  return m_enabled;
349  }
350 
351 
360  virtual void setFocused(bool focused);
361 
362 
368  bool isFocused() const
369  {
370  return m_focused;
371  }
372 
373 
379  const String& getWidgetType() const;
380 
381 
388  {
389  return m_parent;
390  }
391 
392 
398  bool isAnimationPlaying() const;
399 
400 
406  void moveToFront();
407 
408 
414  void moveToBack();
415 
416 
427  void setUserData(Any userData)
428  {
429  m_userData = std::move(userData);
430  }
431 
437  template <typename T>
438  T getUserData() const
439  {
440  return AnyCast<T>(m_userData);
441  }
442 
443 
449  void setInheritedFont(const Font& font);
450 
451 
457  const Font& getInheritedFont() const;
458 
459 
465  void setInheritedOpacity(float opacity);
466 
467 
473  float getInheritedOpacity() const;
474 
475 
481  virtual void setTextSize(unsigned int size);
482 
483 
489  virtual unsigned int getTextSize() const;
490 
491 
497  void setToolTip(Widget::Ptr toolTip);
498 
499 
505  Widget::Ptr getToolTip() const;
506 
507 
515  void setWidgetName(const String& name);
516 
517 
523  String getWidgetName() const;
524 
525 
533  void setFocusable(bool focusable);
534 
535 
543  bool isFocusable() const;
544 
545 
552  virtual bool canGainFocus() const;
553 
554 
559  bool isContainer() const;
560 
561 
571  virtual Signal& getSignal(String signalName);
572 
573 
579  virtual void setParent(Container* parent);
580 
581 
586  virtual void update(Duration elapsedTime);
587 
588 
593  virtual bool mouseOnWidget(Vector2f pos) const = 0;
594 
598  virtual void leftMousePressed(Vector2f pos);
599 
603  virtual void leftMouseReleased(Vector2f pos);
604 
608  virtual void rightMousePressed(Vector2f pos);
609 
613  virtual void rightMouseReleased(Vector2f pos);
614 
618  virtual void mousePressed(sf::Mouse::Button button, Vector2f pos);
619 
623  virtual void mouseReleased(sf::Mouse::Button button, Vector2f pos);
624 
628  virtual void mouseMoved(Vector2f pos);
629 
633  virtual void keyPressed(const sf::Event::KeyEvent& event);
634 
638  virtual void textEntered(char32_t key);
639 
644  virtual bool mouseWheelScrolled(float delta, Vector2f pos);
645 
649  virtual void mouseNoLongerOnWidget();
650 
654  virtual void leftMouseButtonNoLongerDown();
655 
659  virtual void rightMouseButtonNoLongerDown();
660 
661 
664  // Show the tool tip when the widget is located below the mouse.
665  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
666  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
668  virtual Widget::Ptr askToolTip(Vector2f mousePos);
669 
670 
675  const Layout2d& getPositionLayout() const
676  {
677  return m_position;
678  }
679 
680 
685  const Layout2d& getSizeLayout() const
686  {
687  return m_size;
688  }
689 
690 
695  void bindPositionLayout(Layout* layout);
696 
697 
702  void unbindPositionLayout(Layout* layout);
703 
704 
709  void bindSizeLayout(Layout* layout);
710 
711 
716  void unbindSizeLayout(Layout* layout);
717 
718 
727  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
728 
729 
733  template <typename WidgetType>
734  std::shared_ptr<const WidgetType> cast() const
735  {
736  return std::dynamic_pointer_cast<const WidgetType>(shared_from_this());
737  }
738 
739 
743  template <typename WidgetType>
744  std::shared_ptr<WidgetType> cast()
745  {
746  return std::dynamic_pointer_cast<WidgetType>(shared_from_this());
747  }
748 
749 
758  virtual Widget::Ptr clone() const = 0;
759 
760 
762  protected:
763 
764  using SavingRenderersMap = std::map<const Widget*, std::pair<std::unique_ptr<DataIO::Node>, String>>;
765  using LoadingRenderersMap = std::map<String, std::shared_ptr<RendererData>>;
766 
767 
773  virtual void rendererChanged(const String& property);
774 
775 
779  virtual std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const;
780 
781 
785  virtual void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers);
786 
787 
791  virtual void mouseEnteredWidget();
792 
793 
797  virtual void mouseLeftWidget();
798 
799 
801  // Draw a rectangle shape with the right opacity
803  void drawRectangleShape(sf::RenderTarget& target,
804  const sf::RenderStates& states,
805  Vector2f size,
806  Color color) const;
807 
808 
810  // Draw the borders around the widget
812  void drawBorders(sf::RenderTarget& target,
813  const sf::RenderStates& states,
814  const Borders& borders,
815  Vector2f size,
816  Color color) const;
817 
818 
820  private:
821 
823  // Callback function which is called on a renderer change and which calls the virtual rendererChanged function
825  void rendererChangedCallback(const String& property);
826 
827 
829  public:
830 
831  SignalVector2f onPositionChange = {"PositionChanged"};
832  SignalVector2f onSizeChange = {"SizeChanged"};
833  Signal onFocus = {"Focused"};
834  Signal onUnfocus = {"Unfocused"};
835  Signal onMouseEnter = {"MouseEntered"};
836  Signal onMouseLeave = {"MouseLeft"};
837  SignalAnimation onAnimationFinish = {"AnimationFinished"};
838 
839 
841  protected:
842 
843  String m_type;
844  String m_name;
845 
846  Layout2d m_position;
847  Layout2d m_size;
848  unsigned int m_textSize = 0;
849 
850  // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
851  // changed and there would be no way for the widget to detect whether the values changed or not.
852  Vector2f m_prevPosition;
853  Vector2f m_prevSize;
854 
855  // Layouts that need to recalculate their value when the position or size of this widget changes
856  std::unordered_set<Layout*> m_boundPositionLayouts;
857  std::unordered_set<Layout*> m_boundSizeLayouts;
858 
859  // When a widget is disabled, it will no longer receive events
860  bool m_enabled = true;
861 
862  // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
863  bool m_visible = true;
864 
865  // This will point to our parent widget. If there is no parent then this will be nullptr.
866  Container* m_parent = nullptr;
867 
868  // Is the mouse on top of the widget? Did the mouse go down on the widget?
869  bool m_mouseHover = false;
870  bool m_mouseDown = false;
871 
872  // Is the widget focused?
873  bool m_focused = false;
874 
875  // Can the widget be focused?
876  bool m_focusable = true;
877 
878  // Keep track of the elapsed time.
879  Duration m_animationTimeElapsed;
880 
881  // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
882  bool m_draggableWidget = false;
883 
884  // This is set to true for widgets that store other widgets inside them
885  bool m_containerWidget = false;
886 
887  // The tool tip connected to the widget
888  Widget::Ptr m_toolTip = nullptr;
889 
890  // Renderer of the widget
891  aurora::CopiedPtr<WidgetRenderer> m_renderer = aurora::makeCopied<WidgetRenderer>();
892 
893  // Show animations
894  std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
895 
896  // Renderer properties that can be passed from containers to their children
897  Font m_inheritedFont;
898  float m_inheritedOpacity = 1;
899 
900  // Cached renderer properties
901  Font m_fontCached = getGlobalFont();
902  float m_opacityCached = 1;
903  bool m_transparentTextureCached = false;
904 
905  Any m_userData;
906 
907  std::function<void(const String& property)> m_rendererChangedCallback = [this](const String& property){ rendererChangedCallback(property); };
908 
909 
911 
912  friend class Container; // Container accesses m_mouseDown, m_draggableWidget, save and load
913  };
914 
916 }
917 
919 
920 #endif // TGUI_WIDGET_HPP
Vector2f getSize() const
Returns the size of the widget.
Definition: Widget.hpp:227
Definition: Font.hpp:42
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:216
Container widget.
Definition: Container.hpp:47
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:823
TGUI_API Font getGlobalFont()
Retrieves the default font used for all new widgets.
ShowAnimationType
Type of animation to show/hide widget.
Definition: Animation.hpp:43
T getUserData() const
Returns data stored in the widget.
Definition: Widget.hpp:438
Base class for all renderer classes.
Definition: WidgetRenderer.hpp:80
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:47
Vector2f getPosition() const
Gets the position of the widget.
Definition: Widget.hpp:182
void setUserData(Any userData)
Stores some data into the widget.
Definition: Widget.hpp:427
bool isEnabled() const
Returns true when the widget is enabled.
Definition: Widget.hpp:346
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:73
Wrapper for durations.
Definition: Duration.hpp:48
Wrapper class to store strings.
Definition: String.hpp:70
void setPosition(Layout x, Layout y)
Sets the position of the widget.
Definition: Widget.hpp:171
Definition: Outline.hpp:38
The parent class for every widget.
Definition: Widget.hpp:68
std::shared_ptr< WidgetType > cast()
Downcast widget.
Definition: Widget.hpp:744
bool isVisible() const
Returns true when the widget is visible.
Definition: Widget.hpp:322
bool isFocused() const
Returns true when the widget is focused and false otherwise.
Definition: Widget.hpp:368
Class to store the left, top, width or height of a widget.
Definition: Layout.hpp:48
Container * getParent() const
Returns a pointer to the parent widget.
Definition: Widget.hpp:387
std::shared_ptr< const WidgetType > cast() const
Downcast const widget.
Definition: Widget.hpp:734
void setSize(Layout width, Layout height)
Changes the size of the widget.
Definition: Widget.hpp:216