TGUI  0.8.0
Widget.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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/Vector2f.hpp>
36 #include <TGUI/Loading/Theme.hpp>
37 #include <TGUI/Loading/DataIO.hpp>
38 #include <TGUI/Loading/Serializer.hpp>
39 #include <TGUI/Loading/Deserializer.hpp>
40 #include <TGUI/Renderers/WidgetRenderer.hpp>
41 #include <TGUI/Aurora/SmartPtr/CopiedPtr.hpp>
42 #include <TGUI/Aurora/Tools/Downcast.hpp>
43 
44 #include <SFML/System/Time.hpp>
45 #include <SFML/Window/Event.hpp>
46 #include <SFML/Graphics/RenderTarget.hpp>
47 
48 #include <unordered_set>
49 
50 #ifdef TGUI_USE_CPP17
51  #include <any>
52 #else
53  #include <TGUI/Any.hpp>
54 #endif
55 
57 
58 namespace tgui
59 {
60  class Container;
61 
62  enum class ShowAnimationType;
63  namespace priv
64  {
65  class Animation;
66  }
67 
68 
72  class TGUI_API Widget : public SignalWidgetBase, public std::enable_shared_from_this<Widget>
73  {
74  public:
75 
76  typedef std::shared_ptr<Widget> Ptr;
77  typedef std::shared_ptr<const Widget> ConstPtr;
78 
79 
83  Widget();
84 
88  Widget(const Widget&);
89 
93  Widget(Widget&&);
94 
98  ~Widget();
99 
103  Widget& operator=(const Widget&);
104 
108  Widget& operator=(Widget&&);
109 
110 
118  void setRenderer(std::shared_ptr<RendererData> rendererData);
119 
120 
125  WidgetRenderer* getSharedRenderer();
126  const WidgetRenderer* getSharedRenderer() const;
127 
133  WidgetRenderer* getRenderer();
134  const WidgetRenderer* getRenderer() const;
135 
136 
160  virtual void setPosition(const Layout2d& position);
161 
162 
176  {
177  setPosition({std::move(x), std::move(y)});
178  }
179 
180 
187  {
188  return m_position.getValue();
189  }
190 
191 
209  virtual void setSize(const Layout2d& size);
210 
211 
220  void setSize(Layout width, Layout height)
221  {
222  setSize({std::move(width), std::move(height)});
223  }
224 
225 
232  {
233  return m_size.getValue();
234  }
235 
236 
245  virtual Vector2f getFullSize() const;
246 
247 
253  virtual Vector2f getAbsolutePosition() const;
254 
255 
263  virtual Vector2f getWidgetOffset() const;
264 
265 
279  virtual void showWithEffect(ShowAnimationType type, sf::Time duration);
280 
281 
295  virtual void hideWithEffect(ShowAnimationType type, sf::Time duration);
296 
297 
305  virtual void setVisible(bool visible);
306 
307 
316  bool isVisible() const
317  {
318  return m_visible;
319  }
320 
321 
329  virtual void setEnabled(bool enabled);
330 
331 
340  bool isEnabled() const
341  {
342  return m_enabled;
343  }
344 
345 
354  virtual void setFocused(bool focused);
355 
356 
362  bool isFocused() const
363  {
364  return m_focused;
365  }
366 
367 
373  const std::string& getWidgetType() const;
374 
375 
382  {
383  return m_parent;
384  }
385 
386 
392  virtual void moveToFront();
393 
394 
400  virtual void moveToBack();
401 
402 
407  #ifdef TGUI_USE_CPP17
408  void setUserData(std::any userData)
409  #else
410  void setUserData(tgui::Any userData)
411  #endif
412  {
413  m_userData = std::move(userData);
414  }
415 
421  template <typename T>
422  T getUserData() const
423  {
424  #ifdef TGUI_USE_CPP17
425  return std::any_cast<T>(m_userData);
426  #else
427  return m_userData.as<T>();
428  #endif
429  }
430 
431 
437  void setInheritedFont(const Font& font);
438 
439 
445  const Font& getInheritedFont() const;
446 
447 
453  void setInheritedOpacity(float opacity);
454 
455 
461  float getInheritedOpacity() const;
462 
463 
469  void setToolTip(Widget::Ptr toolTip);
470 
471 
477  Widget::Ptr getToolTip() const;
478 
479 
484  virtual bool canGainFocus() const;
485 
486 
491  bool isContainer() const;
492 
493 
499  virtual void setParent(Container* parent);
500 
501 
506  virtual void update(sf::Time elapsedTime);
507 
508 
513  virtual bool mouseOnWidget(Vector2f pos) const = 0;
514 
518  virtual void leftMousePressed(Vector2f pos);
519 
523  virtual void leftMouseReleased(Vector2f pos);
524 
528  virtual void rightMousePressed(Vector2f pos);
529 
533  virtual void rightMouseReleased(Vector2f pos);
534 
538  virtual void mouseMoved(Vector2f pos);
539 
543  virtual void keyPressed(const sf::Event::KeyEvent& event);
544 
548  virtual void textEntered(std::uint32_t key);
549 
554  virtual bool mouseWheelScrolled(float delta, Vector2f pos);
555 
559  virtual void mouseNoLongerOnWidget();
560 
564  virtual void mouseNoLongerDown();
565 
566 
569  // Show the tool tip when the widget is located below the mouse.
570  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
571  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
573  virtual Widget::Ptr askToolTip(Vector2f mousePos);
574 
575 
580  const Layout2d& getPositionLayout() const
581  {
582  return m_position;
583  }
584 
585 
590  const Layout2d& getSizeLayout() const
591  {
592  return m_size;
593  }
594 
595 
600  void bindPositionLayout(Layout* layout);
601 
602 
607  void unbindPositionLayout(Layout* layout);
608 
609 
614  void bindSizeLayout(Layout* layout);
615 
616 
621  void unbindSizeLayout(Layout* layout);
622 
623 
632  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
633 
634 
638  template <typename WidgetType>
639  std::shared_ptr<const WidgetType> cast() const
640  {
641  return std::dynamic_pointer_cast<const WidgetType>(shared_from_this());
642  }
643 
644 
648  template <typename WidgetType>
649  std::shared_ptr<WidgetType> cast()
650  {
651  return std::dynamic_pointer_cast<WidgetType>(shared_from_this());
652  }
653 
654 
663  virtual Widget::Ptr clone() const = 0;
664 
665 
667  protected:
668 
669  using SavingRenderersMap = std::map<const Widget*, std::pair<std::unique_ptr<DataIO::Node>, std::string>>;
670  using LoadingRenderersMap = std::map<std::string, std::shared_ptr<RendererData>>;
671 
672 
682  Signal& getSignal(std::string signalName) override;
683 
684 
690  virtual void rendererChanged(const std::string& property);
691 
692 
696  virtual std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const;
697 
698 
702  virtual void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers);
703 
704 
708  virtual void mouseEnteredWidget();
709 
710 
714  virtual void mouseLeftWidget();
715 
716 
718  // Draw a rectangle shape with the right opacity
720  void drawRectangleShape(sf::RenderTarget& target,
721  const sf::RenderStates& states,
722  Vector2f size,
723  Color color) const;
724 
725 
727  // Draw the borders around the widget
729  void drawBorders(sf::RenderTarget& target,
730  const sf::RenderStates& states,
731  const Borders& borders,
732  Vector2f size,
733  Color color) const;
734 
735 
737  private:
738 
740  // Callback function which is called on a renderer change and which calls the virtual rendererChanged function
742  void rendererChangedCallback(const std::string& property);
743 
744 
746  public:
747 
748  SignalVector2f onPositionChange = {"PositionChanged"};
749  SignalVector2f onSizeChange = {"SizeChanged"};
750  Signal onFocus = {"Focused"};
751  Signal onUnfocus = {"Unfocused"};
752  Signal onMouseEnter = {"MouseEntered"};
753  Signal onMouseLeave = {"MouseLeft"};
754  SignalAnimation onAnimationFinished = {"AnimationFinished"};
755 
756 
758  protected:
759 
760  std::string m_type;
761 
762  Layout2d m_position;
763  Layout2d m_size;
764 
765  // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
766  // changed and there would be no way for the widget to detect whether the values changed or not.
767  Vector2f m_prevPosition;
768  Vector2f m_prevSize;
769 
770  // Layouts that need to recalculate their value when the position or size of this widget changes
771  std::unordered_set<Layout*> m_boundPositionLayouts;
772  std::unordered_set<Layout*> m_boundSizeLayouts;
773 
774  // When a widget is disabled, it will no longer receive events
775  bool m_enabled = true;
776 
777  // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
778  bool m_visible = true;
779 
780  // This will point to our parent widget. If there is no parent then this will be nullptr.
781  Container* m_parent = nullptr;
782 
783  // Is the mouse on top of the widget? Did the mouse go down on the widget?
784  bool m_mouseHover = false;
785  bool m_mouseDown = false;
786 
787  // Is the widget focused?
788  bool m_focused = false;
789 
790  // Keep track of the elapsed time.
791  sf::Time m_animationTimeElapsed;
792 
793  // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
794  bool m_draggableWidget = false;
795 
796  // This is set to true for widgets that store other widgets inside them
797  bool m_containerWidget = false;
798 
799  // The tool tip connected to the widget
800  Widget::Ptr m_toolTip = nullptr;
801 
802  // Renderer of the widget
803  aurora::CopiedPtr<WidgetRenderer> m_renderer = aurora::makeCopied<WidgetRenderer>();
804 
805  // Show animations
806  std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
807 
808  // Renderer properties that can be passed from containers to their children
809  Font m_inheritedFont;
810  float m_inheritedOpacity = 1;
811 
812  // Cached renderer properties
813  Font m_fontCached = getGlobalFont();
814  float m_opacityCached = 1;
815  bool m_transparentTextureCached = false;
816 
817  #ifdef TGUI_USE_CPP17
818  std::any m_userData;
819  #else
820  tgui::Any m_userData;
821  #endif
822 
823  std::function<void(const std::string& property)> m_rendererChangedCallback = [this](const std::string& property){ rendererChangedCallback(property); };
824 
825 
827 
828  friend class Container; // Container accesses m_mouseDown, m_draggableWidget, save and load
829  };
830 
832 }
833 
835 
836 #endif // TGUI_WIDGET_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
std::shared_ptr< const WidgetType > cast() const
Downcast const widget.
Definition: Widget.hpp:639
Wrapper for colors.
Definition: Color.hpp:47
Definition: Font.hpp:42
Class to store the position or size of a widget.
Definition: Layout.hpp:243
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:77
Definition: Vector2f.hpp:37
std::shared_ptr< WidgetType > cast()
Downcast widget.
Definition: Widget.hpp:649
Base class for Widget which provides functionality to connect signals based on their name...
Definition: Signal.hpp:625
Class to store the left, top, width or height of a widget.
Definition: Layout.hpp:48
ShowAnimationType
Type of animation to show/hide widget.
Definition: Animation.hpp:43
Definition: Any.hpp:36
Vector2f getPosition() const
Gets the position of the widget.
Definition: Widget.hpp:186
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
bool isEnabled() const
Returns true when the widget is enabled.
Definition: Widget.hpp:340
void setSize(Layout width, Layout height)
Changes the size of the widget.
Definition: Widget.hpp:220
T getUserData() const
Returns data stored in the widget.
Definition: Widget.hpp:422
Vector2f getSize() const
Returns the size of the widget.
Definition: Widget.hpp:231
Definition: Outline.hpp:38
bool isFocused() const
Returns true when the widget is focused and false otherwise.
Definition: Widget.hpp:362
void setUserData(tgui::Any userData)
Stores some data into the widget.
Definition: Widget.hpp:410
bool isVisible() const
Returns true when the widget is visible.
Definition: Widget.hpp:316
TGUI_API Font getGlobalFont()
Retrieves the default font used for all new widgets.
void setPosition(Layout x, Layout y)
Sets the position of the widget.
Definition: Widget.hpp:175
Container * getParent() const
Returns a pointer to the parent widget.
Definition: Widget.hpp:381
Base class for all renderer classes.
Definition: WidgetRenderer.hpp:80
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:492