TGUI  0.8.9
Container.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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_CONTAINER_HPP
27 #define TGUI_CONTAINER_HPP
28 
29 
30 #include <list>
31 
32 #include <TGUI/Widget.hpp>
33 
35 
36 namespace tgui
37 {
47  class TGUI_API Container : public Widget
48  {
49  public:
50 
51  typedef std::shared_ptr<Container> Ptr;
52  typedef std::shared_ptr<const Container> ConstPtr;
53 
54 
56  // Default constructor
58  Container();
59 
60 
64  Container(const Container& copy);
65 
66 
71 
72 
77 
78 
82  Container& operator= (const Container& right);
83 
84 
88  Container& operator= (Container&& right);
89 
90 
95  void setSize(const Layout2d& size) override;
96  using Widget::setSize;
97 
98 
105  const std::vector<Widget::Ptr>& getWidgets() const
106  {
107  return m_widgets;
108  }
109 
117  template<typename Function>
118  void sortWidgets(Function&& function)
119  {
120  std::sort(m_widgets.begin(), m_widgets.end(), std::forward<Function>(function));
121  }
122 
123 #ifndef TGUI_REMOVE_DEPRECATED_CODE
130  TGUI_DEPRECATED("Use getWidgets() and Widget::getWidgetName instead") const std::vector<sf::String> getWidgetNames() const
131  {
132  std::vector<sf::String> m_widgetNames;
133  m_widgetNames.reserve(m_widgets.size());
134  for (std::size_t i = 0; i < m_widgets.size(); ++i)
135  m_widgetNames.emplace_back(m_widgets[i]->getWidgetName());
136 
137  return m_widgetNames;
138  }
139 #endif
140 
150  virtual void add(const Widget::Ptr& widgetPtr, const sf::String& widgetName = "");
151 
152 
166  Widget::Ptr get(const sf::String& widgetName) const;
167 
168 
183  template <class T>
184  typename T::Ptr get(const sf::String& widgetName) const
185  {
186  return std::dynamic_pointer_cast<T>(get(widgetName));
187  }
188 
189 
198  virtual bool remove(const Widget::Ptr& widget);
199 
200 
205  virtual void removeAllWidgets();
206 
207 #ifndef TGUI_REMOVE_DEPRECATED_CODE
217  TGUI_DEPRECATED("Use Widget::setWidgetName instead") bool setWidgetName(const Widget::Ptr& widget, const std::string& name);
218  using Widget::setWidgetName;
219 
220 
229  TGUI_DEPRECATED("Use Widget::getWidgetName instead") std::string getWidgetName(const Widget::ConstPtr& widget) const;
230 #endif
231 
237 
238 
243  virtual Vector2f getInnerSize() const;
244 
245 
254  {
255  return Vector2f{0, 0};
256  }
257 
258 
264  void setTextSize(unsigned int size) override;
265 
266 
275  void loadWidgetsFromFile(const std::string& filename, bool replaceExisting = true);
276 
277 
283  void saveWidgetsToFile(const std::string& filename);
284 
285 
292  void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
293 
294 
301  void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
302 
303 
309  void saveWidgetsToStream(std::stringstream& stream) const;
310 
311 
319  void moveWidgetToFront(const Widget::Ptr& widget);
320 
321 
329  void moveWidgetToBack(const Widget::Ptr& widget);
330 
331 
342  std::size_t moveWidgetForward(const Widget::Ptr& widget);
343 
344 
355  std::size_t moveWidgetBackward(const Widget::Ptr& widget);
356 
357 
367 
368 
378 
379 
387  virtual Widget::Ptr getWidgetAtPosition(sf::Vector2f pos) const;
388 
389 
398  bool focusNextWidget(bool recursive = true);
399 
400 
409  bool focusPreviousWidget(bool recursive = true);
410 
411 
420  void setFocused(bool focused) override;
421 
422 
427  void childWidgetFocused(const Widget::Ptr& child);
428 
429 
433  void leftMousePressed(Vector2f pos) override;
434 
438  void leftMouseReleased(Vector2f pos) override;
439 
443  void rightMousePressed(Vector2f pos) override;
444 
448  void rightMouseReleased(Vector2f pos) override;
449 
453  void mouseMoved(Vector2f pos) override;
454 
458  void keyPressed(const sf::Event::KeyEvent& event) override;
459 
463  void textEntered(std::uint32_t key) override;
464 
468  bool mouseWheelScrolled(float delta, Vector2f pos) override;
469 
473  void mouseNoLongerOnWidget() override;
474 
478  void leftMouseButtonNoLongerDown() override;
479 
483  void rightMouseButtonNoLongerDown() override;
484 
485 
488  // Shows the tool tip when the widget is located below the mouse.
489  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
490  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
492  Widget::Ptr askToolTip(Vector2f mousePos) override;
493 
494 
497  // This function is called every frame with the time passed since the last frame.
499  bool update(sf::Time elapsedTime) override;
500 
501 
504  // The function returns true when the event is consumed and false when the event was ignored by all widgets.
506  bool handleEvent(sf::Event& event);
507 
508 
510  protected:
511 
512 
519  void rendererChanged(const std::string& property) override;
520 
521 
525  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
526 
527 
531  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
532 
533 
535  // Checks above which widget the mouse is standing.
536  // If there is no widget below the mouse then this function will return a null pointer.
538  Widget::Ptr mouseOnWhichWidget(Vector2f mousePos);
539 
540 
542  // This function will call the draw function from all the widgets.
544  virtual void drawWidgetContainer(sf::RenderTarget* target, const sf::RenderStates& states = sf::RenderStates::Default) const;
545 
546 
548  // Find out what the index of the focused widget is. Returns 0 when no widget is focused and index+1 otherwise.
550  std::size_t getFocusedWidgetIndex() const;
551 
552 
554  // Try to focus the given child widget
556  bool tryFocusWidget(const tgui::Widget::Ptr &widget, bool reverseWidgetOrder, bool recursive);
557 
558 
560  protected:
561 
562  std::vector<Widget::Ptr> m_widgets;
563 
564  Widget::Ptr m_widgetBelowMouse;
565  Widget::Ptr m_focusedWidget;
566 
567  Vector2f m_prevInnerSize;
568 
569  // Did we enter handleEvent directly or because we got a MouseReleased event?
570  bool m_handingMouseReleased = false;
571 
572  // Does focusing the next widget always keep a widget from this container focused (e.g. in a ChildWindow)?
573  bool m_isolatedFocus = false;
574 
575 
576  friend class SubwidgetContainer; // Needs access to save and load functions
577 
579  };
580 
581 
585  class TGUI_API GuiContainer : public Container
586  {
587  public:
588 
589  typedef std::shared_ptr<GuiContainer> Ptr;
590  typedef std::shared_ptr<const GuiContainer> ConstPtr;
591 
592 
597  GuiContainer();
598 
599 
608  void setSize(const Layout2d& size) override;
609  using Widget::setSize;
610 
611 
618  void setFocused(bool focused) override;
619 
620 
629  bool mouseOnWidget(Vector2f pos) const override;
630 
631 
633  private:
634 
635 
637  // This function does nothing.
639  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
640 
641 
643  // Returns a nullptr.
645  Widget::Ptr clone() const override
646  {
647  return nullptr;
648  }
649 
650 
652  protected:
653 
654  friend class Gui; // Required to let Gui access protected members from container and Widget
655 
657  };
658 
659 
661 }
662 
664 
665 #endif // TGUI_CONTAINER_HPP
Container widget.
Definition: Container.hpp:48
virtual bool remove(const Widget::Ptr &widget)
Removes a single widget that was added to the container.
Container(const Container &copy)
Copy constructor.
virtual Widget::Ptr getWidgetAtPosition(sf::Vector2f pos) const
Returns the leaf child widget that is located at the given position.
std::size_t moveWidgetBackward(const Widget::Ptr &widget)
Places a widget one step backward in the z-order.
bool focusPreviousWidget(bool recursive=true)
Focuses the previous widget in this container.
std::shared_ptr< Container > Ptr
Shared widget pointer.
Definition: Container.hpp:51
~Container()
Destructor.
virtual void removeAllWidgets()
Removes all widgets that were added to the container.
void loadWidgetsFromStream(std::stringstream &&stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
virtual Vector2f getInnerSize() const
Returns the space available for widgets inside the container.
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets in this container.
Definition: Container.hpp:105
bool focusNextWidget(bool recursive=true)
Focuses the next widget in this container.
void sortWidgets(Function &&function)
Sorts a list of all the widgets in this container.
Definition: Container.hpp:118
void setTextSize(unsigned int size) override
Changes the character size of all existing and future child widgets.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void moveWidgetToFront(const Widget::Ptr &widget)
Places a widget before all other widgets, to the front of the z-order.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
void moveWidgetToBack(const Widget::Ptr &widget)
Places a widget behind all other widgets, to the back of the z-order.
void saveWidgetsToFile(const std::string &filename)
Saves the child widgets to a text file.
virtual void add(const Widget::Ptr &widgetPtr, const sf::String &widgetName="")
Adds a widget to the container.
Widget::Ptr get(const sf::String &widgetName) const
Returns a pointer to a widget that was added earlier.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
std::size_t moveWidgetForward(const Widget::Ptr &widget)
Places a widget one step forward in the z-order.
virtual Vector2f getChildWidgetsOffset() const
Returns the distance between the position of the container and a widget that would be drawn inside th...
Definition: Container.hpp:253
void loadWidgetsFromFile(const std::string &filename, bool replaceExisting=true)
Loads the child widgets from a text file.
T::Ptr get(const sf::String &widgetName) const
Returns a pointer to a widget that was added earlier.
Definition: Container.hpp:184
Container(Container &&copy)
Move constructor.
void setFocused(bool focused) override
Focus or unfocus the widget.
void uncheckRadioButtons()
Unchecks all the radio buttons.
void setSize(const Layout2d &size) override
Changes the size of the container.
void saveWidgetsToStream(std::stringstream &stream) const
Saves the child widgets to a text file.
void loadWidgetsFromStream(std::stringstream &stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
std::shared_ptr< const Container > ConstPtr
Shared constant widget pointer.
Definition: Container.hpp:52
Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Wrapper class to store strings.
Definition: String.hpp:119
Base class for widgets that consist of subwidgets that act together as if they are a single widget.
Definition: SubwidgetContainer.hpp:43
Definition: Vector2f.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37