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 
29 
30 #include <TGUI/Signal.hpp>
31 #include <TGUI/Sprite.hpp>
32 #include <TGUI/Transformable.hpp>
33 #include <TGUI/Renderers/WidgetRenderer.hpp>
34 #include <TGUI/Aurora/SmartPtr/CopiedPtr.hpp>
35 #include <TGUI/Aurora/Tools/Downcast.hpp>
36 
37 #include <SFML/System/Time.hpp>
38 #include <SFML/Window/Event.hpp>
39 #include <SFML/Graphics/RenderTarget.hpp>
40 
42 
43 namespace tgui
44 {
45  class Container;
46 
47  enum class ShowAnimationType;
48  namespace priv
49  {
50  class Animation;
51  }
52 
53 
72  class TGUI_API Widget : public Transformable, 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 
84  Widget();
85 
89  Widget(const Widget&);
90 
94  Widget(Widget&&);
95 
99  ~Widget();
100 
104  Widget& operator=(const Widget&);
105 
109  Widget& operator=(Widget&&);
110 
111 
118  void setRenderer(std::shared_ptr<RendererData> rendererData);
119 
120 
127  WidgetRenderer* getRenderer() const;
128 
129 
154  virtual void setPosition(const Layout2d& position) override;
156 
157 
176  virtual void setSize(const Layout2d& size) override;
178 
179 
186  virtual sf::Vector2f getAbsolutePosition() const;
187 
188 
197  virtual sf::Vector2f getWidgetOffset() const;
198 
199 
208  virtual void show();
209 
210 
225  virtual void showWithEffect(ShowAnimationType type, sf::Time duration);
226 
227 
235  virtual void hide();
236 
237 
252  virtual void hideWithEffect(ShowAnimationType type, sf::Time duration);
253 
254 
264  bool isVisible() const
265  {
266  return m_visible;
267  }
268 
269 
277  virtual void enable();
278 
279 
287  virtual void disable();
288 
289 
299  bool isEnabled() const
300  {
301  return m_enabled;
302  }
303 
304 
314  virtual void focus();
315 
316 
324  virtual void unfocus();
325 
326 
333  bool isFocused() const
334  {
335  return m_focused;
336  }
337 
338 
345  const std::string& getWidgetType() const;
346 
347 
355  {
356  return m_parent;
357  }
358 
359 
364  virtual void moveToFront();
365 
366 
371  virtual void moveToBack();
372 
373 
380  void setToolTip(Widget::Ptr toolTip);
381 
382 
389  Widget::Ptr getToolTip();
390 
391 
397  virtual void setParent(Container* parent);
398 
399 
404  virtual void update(sf::Time elapsedTime);
405 
406 
410  virtual bool mouseOnWidget(sf::Vector2f pos) const = 0;
411 
415  virtual void leftMousePressed(sf::Vector2f pos);
416 
420  virtual void leftMouseReleased(sf::Vector2f pos);
421 
425  virtual void mouseMoved(sf::Vector2f pos);
426 
430  virtual void keyPressed(const sf::Event::KeyEvent& event);
431 
435  virtual void textEntered(sf::Uint32 key);
436 
440  virtual void mouseWheelScrolled(float delta, int x, int y);
441 
445  virtual void widgetFocused();
446 
450  virtual void widgetUnfocused();
451 
455  virtual void mouseNoLongerOnWidget();
456 
460  virtual void mouseNoLongerDown();
461 
462 
465  // Show the tool tip when the widget is located below the mouse.
466  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
467  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
469  virtual Widget::Ptr askToolTip(sf::Vector2f mousePos);
470 
471 
481  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
482 
483 
493  virtual Widget::Ptr clone() const = 0;
494 
495 
497  protected:
498 
505  virtual void rendererChanged(const std::string& property);
506 
507 
512  virtual void mouseEnteredWidget();
513 
514 
519  virtual void mouseLeftWidget();
520 
521 
523  // Draw a rectangle shape with the right opacity
525  void drawRectangleShape(sf::RenderTarget& target,
526  const sf::RenderStates& states,
527  sf::Vector2f size,
528  sf::Color color) const;
529 
530 
532  // Draw the borders around the widget
534  void drawBorders(sf::RenderTarget& target,
535  const sf::RenderStates& states,
536  const Borders& borders,
537  sf::Vector2f size,
538  sf::Color color) const;
539 
540 
542  private:
543 
545  // Callback function which is called on a renderer change and which calls the virtual rendererChanged function
547  void rendererChangedCallback(const std::string& property);
548 
549 
551  protected:
552 
553  std::string m_type;
554 
555  // When a widget is disabled, it will no longer receive events
556  bool m_enabled = true;
557 
558  // Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
559  bool m_visible = true;
560 
561  // This will point to our parent widget. If there is no parent then this will be nullptr.
562  Container* m_parent = nullptr;
563 
564  // Is the mouse on top of the widget? Did the mouse go down on the widget?
565  bool m_mouseHover = false;
566  bool m_mouseDown = false;
567 
568  // Are you focused on the widget?
569  bool m_focused = false;
570 
571  // Can the widget be focused?
572  bool m_allowFocus = false;
573 
574  // Keep track of the elapsed time.
575  sf::Time m_animationTimeElapsed;
576 
577  // This is set to true for widgets that have something to be dragged around (e.g. sliders and scrollbars)
578  bool m_draggableWidget = false;
579 
580  // This is set to true for widgets that store other widgets inside them
581  bool m_containerWidget = false;
582 
583  // The tool tip connected to the widget
584  Widget::Ptr m_toolTip = nullptr;
585 
586  // Renderer of the widget
587  aurora::CopiedPtr<WidgetRenderer> m_renderer = aurora::makeCopied<WidgetRenderer>();
588 
589  // Show animations
590  std::vector<std::shared_ptr<priv::Animation>> m_showAnimations;
591 
592  // Cached renderer properties
593  Font m_fontCached;
594  float m_opacityCached;
595 
596 
598 
599  friend class Container; // Container changes widget properties of its child widgets
600  };
601 
603 }
604 
606 
607 #endif // TGUI_WIDGET_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Transformable.hpp:35
Definition: Font.hpp:41
Class to store the position or size of a widget.
Definition: Layout.hpp:263
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:77
Base class for widgets to enable signal handling.
Definition: Signal.hpp:301
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 void setPosition(const Layout2d &position)
Sets the position of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
bool isEnabled() const
Returns true when the widget is enabled.
Definition: Widget.hpp:299
Definition: Outline.hpp:37
bool isFocused() const
Returns true when the widget is focused and false otherwise.
Definition: Widget.hpp:333
bool isVisible() const
Returns true when the widget is visible.
Definition: Widget.hpp:264
Container * getParent() const
Returns a pointer to the parent widget.
Definition: Widget.hpp:354
Base class for all renderer classes.
Definition: WidgetRenderer.hpp:59