TGUI  v0.7.5
Widget.hpp
1 //
3 // TGUI - Texus's 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_WIDGETS_HPP
27 #define TGUI_WIDGETS_HPP
28 
29 
30 #include <TGUI/Global.hpp>
31 #include <TGUI/Signal.hpp>
32 #include <TGUI/Transformable.hpp>
33 #include <TGUI/Texture.hpp>
34 #include <TGUI/Color.hpp>
35 #include <TGUI/Font.hpp>
36 #include <TGUI/Loading/Deserializer.hpp>
37 
39 
40 namespace tgui
41 {
42  class BaseTheme;
43  class Container;
44  class WidgetRenderer;
45 
46  enum class ShowAnimationType;
47  namespace priv
48  {
49  class Animation;
50  }
51 
52 
71  class TGUI_API Widget : public sf::Drawable, public Transformable, public SignalWidgetBase, public std::enable_shared_from_this<Widget>
72  {
73  public:
74 
75  typedef std::shared_ptr<Widget> Ptr;
76  typedef std::shared_ptr<const Widget> ConstPtr;
77 
78 
82  Widget();
83 
84 
91  Widget(const Widget& copy);
92 
93 
97  ~Widget();
98 
99 
108  Widget& operator= (const Widget& right);
109 
110 
117  std::shared_ptr<WidgetRenderer> getRenderer() const
118  {
119  return m_renderer;
120  }
121 
122 
147  virtual void setPosition(const Layout2d& position) override;
149 
150 
169  virtual void setSize(const Layout2d& size) override;
171 
172 
179  virtual sf::Vector2f getAbsolutePosition() const;
180 
181 
190  virtual void show();
191 
192 
207  virtual void showWithEffect(ShowAnimationType type, sf::Time duration);
208 
209 
217  virtual void hide();
218 
219 
232  virtual void hideWithEffect(ShowAnimationType type, sf::Time duration);
233 
234 
244  bool isVisible() const
245  {
246  return m_visible;
247  }
248 
249 
257  virtual void enable();
258 
259 
269  virtual void disable(bool blockMouseEvents = true);
270 
271 
281  bool isEnabled() const
282  {
283  return m_enabled;
284  }
285 
286 
296  virtual void focus();
297 
298 
306  virtual void unfocus();
307 
308 
315  bool isFocused() const
316  {
317  return m_focused;
318  }
319 
320 
327  const std::string& getWidgetType() const
328  {
329  return m_callback.widgetType;
330  }
331 
332 
340  {
341  return m_parent;
342  }
343 
344 
351  virtual void setOpacity(float opacity);
352 
353 
360  float getOpacity() const
361  {
362  return m_opacity;
363  }
364 
365 
370  virtual void moveToFront();
371 
372 
377  virtual void moveToBack();
378 
379 
386  void setToolTip(Widget::Ptr toolTip);
387 
388 
395  Widget::Ptr getToolTip();
396 
397 
408  virtual void setFont(const Font& font);
409 
410 
417  std::shared_ptr<sf::Font> getFont() const;
418 
419 
426  void detachTheme();
427 
428 
435  std::shared_ptr<BaseTheme> getTheme() const
436  {
437  return m_theme;
438  }
439 
440 
447  std::string getPrimaryLoadingParameter() const
448  {
449  return m_primaryLoadingParameter;
450  }
451 
452 
459  std::string getSecondaryLoadingParameter() const
460  {
461  return m_secondaryLoadingParameter;
462  }
463 
464 
473  virtual sf::Vector2f getWidgetOffset() const
474  {
475  return sf::Vector2f{0, 0};
476  }
477 
478 
484  virtual void setParent(Container* parent);
485 
486 
491  virtual void update(sf::Time elapsedTime);
492 
493 
497  virtual bool mouseOnWidget(float x, float y) const = 0;
498 
502  virtual void leftMousePressed(float x, float y);
503 
507  virtual void leftMouseReleased(float x, float y);
508 
512  virtual void mouseMoved(float x, float y);
513 
517  virtual void keyPressed(const sf::Event::KeyEvent& event);
518 
522  virtual void textEntered(sf::Uint32 key);
523 
527  virtual void mouseWheelMoved(int delta, int x, int y);
528 
532  virtual void widgetFocused();
533 
537  virtual void widgetUnfocused();
538 
542  virtual void mouseNoLongerOnWidget();
543 
547  virtual void mouseNoLongerDown();
548 
549 
552  // Show the tool tip when the widget is located below the mouse.
553  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
554  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
556  virtual Widget::Ptr askToolTip(sf::Vector2f mousePos);
557 
558 
568  virtual Widget::Ptr clone() const = 0;
569 
570 
572  protected:
573 
582  void attachTheme(std::shared_ptr<BaseTheme> theme);
583 
584 
597  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false);
598 
599 
601  // This function is called when the mouse enters the widget. If requested, a callback will be send.
603  virtual void mouseEnteredWidget();
604 
605 
607  // This function is called when the mouse leaves the widget. If requested, a callback will be send.
609  virtual void mouseLeftWidget();
610 
611 
613  // Check if the widget blocks mouse events when disabled
615  bool isDisabledBlockingMouseEvents() const;
616 
617 
619  protected:
620 
621  // Block mouse events when the widget is disabled or let them pass to the widget behind it?
622  bool m_disabledBlockingMouseEvents = true;
623 
624  // When a widget is disabled, it will no longer receive events
625  bool m_enabled = true;
626 
627  // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
628  bool m_visible = true;
629 
630  // This will point to our parent widget. If there is no parent then this will be nullptr.
631  Container* m_parent = nullptr;
632 
633  // How transparent is the widget
634  float m_opacity = 1;
635 
636  // Is the mouse on top of the widget? Did the mouse go down on the widget?
637  bool m_mouseHover = false;
638  bool m_mouseDown = false;
639 
640  // Are you focused on the widget?
641  bool m_focused = false;
642 
643  // Can the widget be focused?
644  bool m_allowFocus = false;
645 
646  // Keep track of the elapsed time.
647  sf::Time m_animationTimeElapsed;
648 
649  // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
650  bool m_draggableWidget = false;
651 
652  // This is set to true for widgets that store other widgets inside them
653  bool m_containerWidget = false;
654 
655  // The tool tip connected to the widget
656  Widget::Ptr m_toolTip = nullptr;
657 
658  // The font that the widget can use
659  std::shared_ptr<sf::Font> m_font = nullptr;
660 
661  // Renderer of the widget
662  std::shared_ptr<WidgetRenderer> m_renderer = nullptr;
663 
664  // Theme object that loaded this widget (if any)
665  std::shared_ptr<BaseTheme> m_theme = nullptr;
666  std::string m_primaryLoadingParameter;
667  std::string m_secondaryLoadingParameter;
668 
669  // Show animations
670  std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
671 
673 
674  friend class Container;
675  friend class BaseTheme;
676  };
677 
678 
682  class TGUI_API WidgetRenderer
683  {
684  public:
685 
690  virtual ~WidgetRenderer() {};
691 
692 
702  virtual void setProperty(std::string property, const std::string& value);
703 
704 
715  virtual void setProperty(std::string property, ObjectConverter&& value);
716 
717 
727  virtual ObjectConverter getProperty(std::string property) const;
728 
729 
736  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const;
737 
738 
740  private:
741 
743  // Makes a copy of the renderer
745  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) = 0;
746 
747 
749 
750  friend class Widget;
751  };
752 
753 
755 }
756 
758 
759 #endif // TGUI_WIDGETS_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Transformable.hpp:36
std::string getPrimaryLoadingParameter() const
Returns the primary parameter that was passed to the loader to load this widget.
Definition: Widget.hpp:447
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:262
The parent class for every widget.
Definition: Widget.hpp:71
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:76
float getOpacity() const
Returns the opacity of the widget.
Definition: Widget.hpp:360
Base class for widgets to enable signal handling.
Definition: Signal.hpp:312
std::string getSecondaryLoadingParameter() const
Returns the secondary parameter that was passed to the loader to load this widget.
Definition: Widget.hpp:459
ShowAnimationType
Type of animation to show/hide widget.
Definition: Animation.hpp:38
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual sf::Vector2f getWidgetOffset() const
Returns the distance between the position where the widget is drawn and where the widget is placed...
Definition: Widget.hpp:473
virtual void setPosition(const Layout2d &position)
set the position of the widget
std::shared_ptr< WidgetRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: Widget.hpp:117
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Container widget.
Definition: Container.hpp:47
const std::string & getWidgetType() const
Returns the type of the widget.
Definition: Widget.hpp:327
bool isEnabled() const
Returns true when the widget is enabled.
Definition: Widget.hpp:281
virtual ~WidgetRenderer()
Virtual destructor.
Definition: Widget.hpp:690
Base class for the Theme classes.
Definition: Theme.hpp:42
std::shared_ptr< BaseTheme > getTheme() const
Returns the theme to which the widget is currently connected.
Definition: Widget.hpp:435
bool isFocused() const
Returns true when the widget is focused and false otherwise.
Definition: Widget.hpp:315
bool isVisible() const
Returns true when the widget is visible.
Definition: Widget.hpp:244
Container * getParent() const
Returns a pointer to the parent widget.
Definition: Widget.hpp:339
Base class for all renderer classes.
Definition: Widget.hpp:682