TGUI  0.8.2
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 #ifdef TGUI_NEXT
280  void showWithEffect(ShowAnimationType type, sf::Time duration);
281 #else
282  virtual void showWithEffect(ShowAnimationType type, sf::Time duration);
283 #endif
284 
298 #ifdef TGUI_NEXT
299  void hideWithEffect(ShowAnimationType type, sf::Time duration);
300 #else
301  virtual void hideWithEffect(ShowAnimationType type, sf::Time duration);
302 #endif
303 
304 
312  virtual void setVisible(bool visible);
313 
314 
323  bool isVisible() const
324  {
325  return m_visible;
326  }
327 
328 
336  virtual void setEnabled(bool enabled);
337 
338 
347  bool isEnabled() const
348  {
349  return m_enabled;
350  }
351 
352 
361  virtual void setFocused(bool focused);
362 
363 
369  bool isFocused() const
370  {
371  return m_focused;
372  }
373 
374 
380  const std::string& getWidgetType() const;
381 
382 
389  {
390  return m_parent;
391  }
392 
393 
399 #ifdef TGUI_NEXT
400  void moveToFront();
401 #else
402  virtual void moveToFront();
403 #endif
404 
410 #ifdef TGUI_NEXT
411  void moveToBack();
412 #else
413  virtual void moveToBack();
414 #endif
415 
426  #ifdef TGUI_USE_CPP17
427  void setUserData(std::any userData)
428  #else
429  void setUserData(tgui::Any userData)
430  #endif
431  {
432  m_userData = std::move(userData);
433  }
434 
440  template <typename T>
441  T getUserData() const
442  {
443  #ifdef TGUI_USE_CPP17
444  return std::any_cast<T>(m_userData);
445  #else
446  return m_userData.as<T>();
447  #endif
448  }
449 
450 
456  void setInheritedFont(const Font& font);
457 
458 
464  const Font& getInheritedFont() const;
465 
466 
472  void setInheritedOpacity(float opacity);
473 
474 
480  float getInheritedOpacity() const;
481 
482 
488  void setToolTip(Widget::Ptr toolTip);
489 
490 
496  Widget::Ptr getToolTip() const;
497 
498 
503  virtual bool canGainFocus() const;
504 
505 
510  bool isContainer() const;
511 
512 
518  virtual void setParent(Container* parent);
519 
520 
525  virtual void update(sf::Time elapsedTime);
526 
527 
532  virtual bool mouseOnWidget(Vector2f pos) const = 0;
533 
537  virtual void leftMousePressed(Vector2f pos);
538 
542  virtual void leftMouseReleased(Vector2f pos);
543 
547  virtual void rightMousePressed(Vector2f pos);
548 
552  virtual void rightMouseReleased(Vector2f pos);
553 
557  virtual void mouseMoved(Vector2f pos);
558 
562  virtual void keyPressed(const sf::Event::KeyEvent& event);
563 
567  virtual void textEntered(std::uint32_t key);
568 
573  virtual bool mouseWheelScrolled(float delta, Vector2f pos);
574 
578  virtual void mouseNoLongerOnWidget();
579 
583  virtual void mouseNoLongerDown();
584 
585 
588  // Show the tool tip when the widget is located below the mouse.
589  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
590  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
592  virtual Widget::Ptr askToolTip(Vector2f mousePos);
593 
594 
599  const Layout2d& getPositionLayout() const
600  {
601  return m_position;
602  }
603 
604 
609  const Layout2d& getSizeLayout() const
610  {
611  return m_size;
612  }
613 
614 
619  void bindPositionLayout(Layout* layout);
620 
621 
626  void unbindPositionLayout(Layout* layout);
627 
628 
633  void bindSizeLayout(Layout* layout);
634 
635 
640  void unbindSizeLayout(Layout* layout);
641 
642 
651  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
652 
653 
657  template <typename WidgetType>
658  std::shared_ptr<const WidgetType> cast() const
659  {
660  return std::dynamic_pointer_cast<const WidgetType>(shared_from_this());
661  }
662 
663 
667  template <typename WidgetType>
668  std::shared_ptr<WidgetType> cast()
669  {
670  return std::dynamic_pointer_cast<WidgetType>(shared_from_this());
671  }
672 
673 
682  virtual Widget::Ptr clone() const = 0;
683 
684 
686  protected:
687 
688  using SavingRenderersMap = std::map<const Widget*, std::pair<std::unique_ptr<DataIO::Node>, std::string>>;
689  using LoadingRenderersMap = std::map<std::string, std::shared_ptr<RendererData>>;
690 
691 
701  Signal& getSignal(std::string signalName) override;
702 
703 
709  virtual void rendererChanged(const std::string& property);
710 
711 
715  virtual std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const;
716 
717 
721  virtual void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers);
722 
723 
727  virtual void mouseEnteredWidget();
728 
729 
733  virtual void mouseLeftWidget();
734 
735 
737  // Draw a rectangle shape with the right opacity
739  void drawRectangleShape(sf::RenderTarget& target,
740  const sf::RenderStates& states,
741  Vector2f size,
742  Color color) const;
743 
744 
746  // Draw the borders around the widget
748  void drawBorders(sf::RenderTarget& target,
749  const sf::RenderStates& states,
750  const Borders& borders,
751  Vector2f size,
752  Color color) const;
753 
754 
756  private:
757 
759  // Callback function which is called on a renderer change and which calls the virtual rendererChanged function
761  void rendererChangedCallback(const std::string& property);
762 
763 
765  public:
766 
767  SignalVector2f onPositionChange = {"PositionChanged"};
768  SignalVector2f onSizeChange = {"SizeChanged"};
769  Signal onFocus = {"Focused"};
770  Signal onUnfocus = {"Unfocused"};
771  Signal onMouseEnter = {"MouseEntered"};
772  Signal onMouseLeave = {"MouseLeft"};
773  SignalAnimation onAnimationFinished = {"AnimationFinished"};
774 
775 
777  protected:
778 
779  std::string m_type;
780 
781  Layout2d m_position;
782  Layout2d m_size;
783 
784  // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
785  // changed and there would be no way for the widget to detect whether the values changed or not.
786  Vector2f m_prevPosition;
787  Vector2f m_prevSize;
788 
789  // Layouts that need to recalculate their value when the position or size of this widget changes
790  std::unordered_set<Layout*> m_boundPositionLayouts;
791  std::unordered_set<Layout*> m_boundSizeLayouts;
792 
793  // When a widget is disabled, it will no longer receive events
794  bool m_enabled = true;
795 
796  // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
797  bool m_visible = true;
798 
799  // This will point to our parent widget. If there is no parent then this will be nullptr.
800  Container* m_parent = nullptr;
801 
802  // Is the mouse on top of the widget? Did the mouse go down on the widget?
803  bool m_mouseHover = false;
804  bool m_mouseDown = false;
805 
806  // Is the widget focused?
807  bool m_focused = false;
808 
809  // Keep track of the elapsed time.
810  sf::Time m_animationTimeElapsed;
811 
812  // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
813  bool m_draggableWidget = false;
814 
815  // This is set to true for widgets that store other widgets inside them
816  bool m_containerWidget = false;
817 
818  // The tool tip connected to the widget
819  Widget::Ptr m_toolTip = nullptr;
820 
821  // Renderer of the widget
822  aurora::CopiedPtr<WidgetRenderer> m_renderer = aurora::makeCopied<WidgetRenderer>();
823 
824  // Show animations
825  std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
826 
827  // Renderer properties that can be passed from containers to their children
828  Font m_inheritedFont;
829  float m_inheritedOpacity = 1;
830 
831  // Cached renderer properties
832  Font m_fontCached = getGlobalFont();
833  float m_opacityCached = 1;
834  bool m_transparentTextureCached = false;
835 
836  #ifdef TGUI_USE_CPP17
837  std::any m_userData;
838  #else
839  tgui::Any m_userData;
840  #endif
841 
842  std::function<void(const std::string& property)> m_rendererChangedCallback = [this](const std::string& property){ rendererChangedCallback(property); };
843 
844 
846 
847  friend class Container; // Container accesses m_mouseDown, m_draggableWidget, save and load
848  };
849 
851 }
852 
854 
855 #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:658
Wrapper for colors.
Definition: Color.hpp:47
Definition: Font.hpp:42
Class to store the position or size of a widget.
Definition: Layout.hpp:245
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:668
Base class for Widget which provides functionality to connect signals based on their name...
Definition: Signal.hpp:677
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:347
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:441
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:369
void setUserData(tgui::Any userData)
Stores some data into the widget.
Definition: Widget.hpp:429
bool isVisible() const
Returns true when the widget is visible.
Definition: Widget.hpp:323
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:388
Base class for all renderer classes.
Definition: WidgetRenderer.hpp:80
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:536