TGUI  0.8-alpha
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 
553  virtual void mouseWheelScrolled(float delta, Vector2f pos);
554 
558  virtual void mouseNoLongerOnWidget();
559 
563  virtual void mouseNoLongerDown();
564 
565 
568  // Show the tool tip when the widget is located below the mouse.
569  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
570  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
572  virtual Widget::Ptr askToolTip(Vector2f mousePos);
573 
574 
579  const Layout2d& getPositionLayout() const
580  {
581  return m_position;
582  }
583 
584 
589  const Layout2d& getSizeLayout() const
590  {
591  return m_size;
592  }
593 
594 
599  void bindPositionLayout(Layout* layout);
600 
601 
606  void unbindPositionLayout(Layout* layout);
607 
608 
613  void bindSizeLayout(Layout* layout);
614 
615 
620  void unbindSizeLayout(Layout* layout);
621 
622 
631  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
632 
633 
637  template <typename WidgetType>
638  std::shared_ptr<const WidgetType> cast() const
639  {
640  return std::dynamic_pointer_cast<const WidgetType>(shared_from_this());
641  }
642 
643 
647  template <typename WidgetType>
648  std::shared_ptr<WidgetType> cast()
649  {
650  return std::dynamic_pointer_cast<WidgetType>(shared_from_this());
651  }
652 
653 
662  virtual Widget::Ptr clone() const = 0;
663 
664 
666  protected:
667 
668  using SavingRenderersMap = std::map<const Widget*, std::pair<std::unique_ptr<DataIO::Node>, std::string>>;
669  using LoadingRenderersMap = std::map<std::string, std::shared_ptr<RendererData>>;
670 
671 
681  Signal& getSignal(std::string signalName) override;
682 
683 
689  virtual void rendererChanged(const std::string& property);
690 
691 
695  virtual std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const;
696 
697 
701  virtual void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers);
702 
703 
707  virtual void mouseEnteredWidget();
708 
709 
713  virtual void mouseLeftWidget();
714 
715 
717  // Draw a rectangle shape with the right opacity
719  void drawRectangleShape(sf::RenderTarget& target,
720  const sf::RenderStates& states,
721  Vector2f size,
722  Color color) const;
723 
724 
726  // Draw the borders around the widget
728  void drawBorders(sf::RenderTarget& target,
729  const sf::RenderStates& states,
730  const Borders& borders,
731  Vector2f size,
732  Color color) const;
733 
734 
736  private:
737 
739  // Callback function which is called on a renderer change and which calls the virtual rendererChanged function
741  void rendererChangedCallback(const std::string& property);
742 
743 
745  public:
746 
747  SignalVector2f onPositionChange = {"PositionChanged"};
748  SignalVector2f onSizeChange = {"SizeChanged"};
749  Signal onFocus = {"Focused"};
750  Signal onUnfocus = {"Unfocused"};
751  Signal onMouseEnter = {"MouseEntered"};
752  Signal onMouseLeave = {"MouseLeft"};
753 
754 
756  protected:
757 
758  std::string m_type;
759 
760  Layout2d m_position;
761  Layout2d m_size;
762 
763  // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
764  // changed and there would be no way for the widget to detect whether the values changed or not.
765  Vector2f m_prevPosition;
766  Vector2f m_prevSize;
767 
768  // Layouts that need to recalculate their value when the position or size of this widget changes
769  std::unordered_set<Layout*> m_boundPositionLayouts;
770  std::unordered_set<Layout*> m_boundSizeLayouts;
771 
772  // When a widget is disabled, it will no longer receive events
773  bool m_enabled = true;
774 
775  // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
776  bool m_visible = true;
777 
778  // This will point to our parent widget. If there is no parent then this will be nullptr.
779  Container* m_parent = nullptr;
780 
781  // Is the mouse on top of the widget? Did the mouse go down on the widget?
782  bool m_mouseHover = false;
783  bool m_mouseDown = false;
784 
785  // Is the widget focused?
786  bool m_focused = false;
787 
788  // Keep track of the elapsed time.
789  sf::Time m_animationTimeElapsed;
790 
791  // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
792  bool m_draggableWidget = false;
793 
794  // This is set to true for widgets that store other widgets inside them
795  bool m_containerWidget = false;
796 
797  // The tool tip connected to the widget
798  Widget::Ptr m_toolTip = nullptr;
799 
800  // Renderer of the widget
801  aurora::CopiedPtr<WidgetRenderer> m_renderer = aurora::makeCopied<WidgetRenderer>();
802 
803  // Show animations
804  std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
805 
806  // Renderer properties that can be passed from containers to their children
807  Font m_inheritedFont;
808  float m_inheritedOpacity = 1;
809 
810  // Cached renderer properties
811  Font m_fontCached = getGlobalFont();
812  float m_opacityCached = 1;
813  bool m_transparentTextureCached = false;
814 
815  #ifdef TGUI_USE_CPP17
816  std::any m_userData;
817  #else
818  tgui::Any m_userData;
819  #endif
820 
821  std::function<void(const std::string& property)> m_rendererChangedCallback = [this](const std::string& property){ rendererChangedCallback(property); };
822 
823 
825 
826  friend class Container; // Container accesses m_mouseDown, m_draggableWidget, save and load
827  };
828 
830 }
831 
833 
834 #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:638
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:648
Base class for Widget which provides functionality to connect signals based on their name...
Definition: Signal.hpp:577
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:38
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:58
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