TGUI  0.8-dev
Widget.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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/Loading/Theme.hpp>
36 #include <TGUI/Renderers/WidgetRenderer.hpp>
37 #include <TGUI/Aurora/SmartPtr/CopiedPtr.hpp>
38 #include <TGUI/Aurora/Tools/Downcast.hpp>
39 
40 #include <SFML/System/Time.hpp>
41 #include <SFML/Window/Event.hpp>
42 #include <SFML/Graphics/RenderTarget.hpp>
43 
44 #include <unordered_set>
45 
47 
48 namespace tgui
49 {
50  class Container;
51 
52  enum class ShowAnimationType;
53  namespace priv
54  {
55  class Animation;
56  }
57 
58 
62  class TGUI_API Widget : public SignalWidgetBase, public std::enable_shared_from_this<Widget>
63  {
64  public:
65 
66  typedef std::shared_ptr<Widget> Ptr;
67  typedef std::shared_ptr<const Widget> ConstPtr;
68 
69 
73  Widget();
74 
78  Widget(const Widget&);
79 
83  Widget(Widget&&);
84 
88  ~Widget();
89 
93  Widget& operator=(const Widget&);
94 
98  Widget& operator=(Widget&&);
99 
100 
108  void setRenderer(std::shared_ptr<RendererData> rendererData);
109 
110 
115  WidgetRenderer* getSharedRenderer();
116  const WidgetRenderer* getSharedRenderer() const;
117 
123  WidgetRenderer* getRenderer();
124  const WidgetRenderer* getRenderer() const;
125 
126 
150  virtual void setPosition(const Layout2d& position);
151 
152 
166  {
167  setPosition({std::move(x), std::move(y)});
168  }
169 
170 
176  sf::Vector2f getPosition() const
177  {
178  return m_position.getValue();
179  }
180 
181 
199  virtual void setSize(const Layout2d& size);
200 
201 
210  void setSize(Layout width, Layout height)
211  {
212  setSize({std::move(width), std::move(height)});
213  }
214 
215 
221  sf::Vector2f getSize() const
222  {
223  return m_size.getValue();
224  }
225 
226 
235  virtual sf::Vector2f getFullSize() const;
236 
237 
243  virtual sf::Vector2f getAbsolutePosition() const;
244 
245 
253  virtual sf::Vector2f getWidgetOffset() const;
254 
255 
263  virtual void show();
264 
265 
279  virtual void showWithEffect(ShowAnimationType type, sf::Time duration);
280 
281 
288  virtual void hide();
289 
290 
304  virtual void hideWithEffect(ShowAnimationType type, sf::Time duration);
305 
306 
315  bool isVisible() const
316  {
317  return m_visible;
318  }
319 
320 
327  virtual void enable();
328 
329 
336  virtual void disable();
337 
338 
347  bool isEnabled() const
348  {
349  return m_enabled;
350  }
351 
352 
360  virtual void focus();
361 
362 
368  virtual void unfocus();
369 
370 
376  bool isFocused() const
377  {
378  return m_focused;
379  }
380 
381 
387  const std::string& getWidgetType() const;
388 
389 
396  {
397  return m_parent;
398  }
399 
400 
406  virtual void moveToFront();
407 
408 
414  virtual void moveToBack();
415 
416 
422  void setInheritedFont(const Font& font);
423 
424 
430  const Font& getInheritedFont() const;
431 
432 
438  void setInheritedOpacity(float opacity);
439 
440 
446  float getInheritedOpacity() const;
447 
448 
454  void setToolTip(Widget::Ptr toolTip);
455 
456 
462  Widget::Ptr getToolTip();
463 
464 
470  virtual void setParent(Container* parent);
471 
472 
477  virtual void update(sf::Time elapsedTime);
478 
479 
485  virtual bool mouseOnWidget(sf::Vector2f pos) const = 0;
486 
490  virtual void leftMousePressed(sf::Vector2f pos);
491 
495  virtual void leftMouseReleased(sf::Vector2f pos);
496 
500  virtual void rightMousePressed(sf::Vector2f pos);
501 
505  virtual void rightMouseReleased(sf::Vector2f pos);
506 
510  virtual void mouseMoved(sf::Vector2f pos);
511 
515  virtual void keyPressed(const sf::Event::KeyEvent& event);
516 
520  virtual void textEntered(sf::Uint32 key);
521 
525  virtual void mouseWheelScrolled(float delta, sf::Vector2f pos);
526 
530  virtual void widgetFocused();
531 
535  virtual void widgetUnfocused();
536 
540  virtual void mouseNoLongerOnWidget();
541 
545  virtual void mouseNoLongerDown();
546 
547 
550  // Show the tool tip when the widget is located below the mouse.
551  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
552  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
554  virtual Widget::Ptr askToolTip(sf::Vector2f mousePos);
555 
556 
561  const Layout2d& getPositionLayout() const
562  {
563  return m_position;
564  }
565 
566 
571  const Layout2d& getSizeLayout() const
572  {
573  return m_size;
574  }
575 
576 
581  void bindPositionLayout(Layout* layout);
582 
583 
588  void unbindPositionLayout(Layout* layout);
589 
590 
595  void bindSizeLayout(Layout* layout);
596 
597 
602  void unbindSizeLayout(Layout* layout);
603 
604 
613  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
614 
615 
624  virtual Widget::Ptr clone() const = 0;
625 
626 
628  protected:
629 
639  Signal& getSignal(std::string signalName) override;
640 
641 
647  virtual void rendererChanged(const std::string& property);
648 
649 
653  virtual void mouseEnteredWidget();
654 
655 
659  virtual void mouseLeftWidget();
660 
661 
663  // Draw a rectangle shape with the right opacity
665  void drawRectangleShape(sf::RenderTarget& target,
666  const sf::RenderStates& states,
667  sf::Vector2f size,
668  sf::Color color) const;
669 
670 
672  // Draw the borders around the widget
674  void drawBorders(sf::RenderTarget& target,
675  const sf::RenderStates& states,
676  const Borders& borders,
677  sf::Vector2f size,
678  sf::Color color) const;
679 
680 
682  private:
683 
685  // Callback function which is called on a renderer change and which calls the virtual rendererChanged function
687  void rendererChangedCallback(const std::string& property);
688 
689 
691  public:
692 
693  SignalVector2f onPositionChange = {"PositionChanged"};
694  SignalVector2f onSizeChange = {"SizeChanged"};
695  Signal onFocus = {"Focused"};
696  Signal onUnfocus = {"Unfocused"};
697  Signal onMouseEnter = {"MouseEntered"};
698  Signal onMouseLeave = {"MouseLeft"};
699 
700 
702  protected:
703 
704  std::string m_type;
705 
706  Layout2d m_position;
707  Layout2d m_size;
708 
709  // The previous position and size have to be stored because when setPosition/setSize is called, the layout may already be
710  // changed and there would be no way for the widget to detect whether the values changed or not.
711  sf::Vector2f m_prevPosition;
712  sf::Vector2f m_prevSize;
713 
714  // Layouts that need to recalculate their value when the position or size of this widget changes
715  std::unordered_set<Layout*> m_boundPositionLayouts;
716  std::unordered_set<Layout*> m_boundSizeLayouts;
717 
718  // When a widget is disabled, it will no longer receive events
719  bool m_enabled = true;
720 
721  // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
722  bool m_visible = true;
723 
724  // This will point to our parent widget. If there is no parent then this will be nullptr.
725  Container* m_parent = nullptr;
726 
727  // Is the mouse on top of the widget? Did the mouse go down on the widget?
728  bool m_mouseHover = false;
729  bool m_mouseDown = false;
730 
731  // Are you focused on the widget?
732  bool m_focused = false;
733 
734  // Can the widget be focused?
735  bool m_allowFocus = false;
736 
737  // Keep track of the elapsed time.
738  sf::Time m_animationTimeElapsed;
739 
740  // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
741  bool m_draggableWidget = false;
742 
743  // This is set to true for widgets that store other widgets inside them
744  bool m_containerWidget = false;
745 
746  // The tool tip connected to the widget
747  Widget::Ptr m_toolTip = nullptr;
748 
749  // Renderer of the widget
750  aurora::CopiedPtr<WidgetRenderer> m_renderer = aurora::makeCopied<WidgetRenderer>();
751 
752  // Show animations
753  std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
754 
755  // Renderer properties that can be passed from containers to their children
756  Font m_inheritedFont;
757  float m_inheritedOpacity = 1;
758 
759  // Cached renderer properties
760  Font m_fontCached;
761  float m_opacityCached = 1;
762 
763  std::function<void(const std::string& property)> m_rendererChangedCallback = [this](const std::string& property){ rendererChangedCallback(property); };
764 
765 
767 
768  friend class Container; // Container changes widget properties of its child widgets
769  };
770 
772 }
773 
775 
776 #endif // TGUI_WIDGET_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Font.hpp:41
Class to store the position or size of a widget.
Definition: Layout.hpp:243
The parent class for every widget.
Definition: Widget.hpp:62
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:67
sf::Vector2f getPosition() const
Gets the position of the widget.
Definition: Widget.hpp:176
Base class for Widget which provides functionality to connect signals based on their name...
Definition: Signal.hpp:850
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
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:66
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:210
Definition: Outline.hpp:38
bool isFocused() const
Returns true when the widget is focused and false otherwise.
Definition: Widget.hpp:376
sf::Vector2f getSize() const
Returns the size of the widget.
Definition: Widget.hpp:221
bool isVisible() const
Returns true when the widget is visible.
Definition: Widget.hpp:315
void setPosition(Layout x, Layout y)
Sets the position of the widget.
Definition: Widget.hpp:165
Container * getParent() const
Returns a pointer to the parent widget.
Definition: Widget.hpp:395
Base class for all renderer classes.
Definition: WidgetRenderer.hpp:60