TGUI  0.8.8
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 
110 #ifndef TGUI_REMOVE_DEPRECATED_CODE
111  TGUI_DEPRECATED("Use getWidgets() and Widget::getWidgetName instead") const std::vector<sf::String> getWidgetNames() const
118  {
119  std::vector<sf::String> m_widgetNames;
120  m_widgetNames.reserve(m_widgets.size());
121  for (std::size_t i = 0; i < m_widgets.size(); ++i)
122  m_widgetNames.emplace_back(m_widgets[i]->getWidgetName());
123 
124  return m_widgetNames;
125  }
126 #endif
127 
137  virtual void add(const Widget::Ptr& widgetPtr, const sf::String& widgetName = "");
138 
139 
153  Widget::Ptr get(const sf::String& widgetName) const;
154 
155 
170  template <class T>
171  typename T::Ptr get(const sf::String& widgetName) const
172  {
173  return std::dynamic_pointer_cast<T>(get(widgetName));
174  }
175 
176 
185  virtual bool remove(const Widget::Ptr& widget);
186 
187 
192  virtual void removeAllWidgets();
193 
194 #ifndef TGUI_REMOVE_DEPRECATED_CODE
195  TGUI_DEPRECATED("Use Widget::setWidgetName instead") bool setWidgetName(const Widget::Ptr& widget, const std::string& name);
205  using Widget::setWidgetName;
206 
207 
216  TGUI_DEPRECATED("Use Widget::getWidgetName instead") std::string getWidgetName(const Widget::ConstPtr& widget) const;
217 #endif
218 
224 
225 
230  virtual Vector2f getInnerSize() const;
231 
232 
241  {
242  return Vector2f{0, 0};
243  }
244 
245 
251  void setTextSize(unsigned int size) override;
252 
253 
262  void loadWidgetsFromFile(const std::string& filename, bool replaceExisting = true);
263 
264 
270  void saveWidgetsToFile(const std::string& filename);
271 
272 
279  void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
280 
281 
288  void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
289 
290 
296  void saveWidgetsToStream(std::stringstream& stream) const;
297 
298 
305  void moveWidgetToFront(const Widget::Ptr& widget);
306 
307 
314  void moveWidgetToBack(const Widget::Ptr& widget);
315 
316 
326 
327 
337 
338 
344 
345 
351 
352 
361  void setFocused(bool focused) override;
362 
363 
368  void childWidgetFocused(const Widget::Ptr& child);
369 
370 
374  void leftMousePressed(Vector2f pos) override;
375 
379  void leftMouseReleased(Vector2f pos) override;
380 
384  void rightMousePressed(Vector2f pos) override;
385 
389  void rightMouseReleased(Vector2f pos) override;
390 
394  void mouseMoved(Vector2f pos) override;
395 
399  void keyPressed(const sf::Event::KeyEvent& event) override;
400 
404  void textEntered(std::uint32_t key) override;
405 
409  bool mouseWheelScrolled(float delta, Vector2f pos) override;
410 
414  void mouseNoLongerOnWidget() override;
415 
419  void leftMouseButtonNoLongerDown() override;
420 
424  void rightMouseButtonNoLongerDown() override;
425 
426 
429  // Shows the tool tip when the widget is located below the mouse.
430  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
431  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
433  Widget::Ptr askToolTip(Vector2f mousePos) override;
434 
435 
438  // This function is called every frame with the time passed since the last frame.
440  bool update(sf::Time elapsedTime) override;
441 
442 
445  // The function returns true when the event is consumed and false when the event was ignored by all widgets.
447  bool handleEvent(sf::Event& event);
448 
449 
451  protected:
452 
453 
460  void rendererChanged(const std::string& property) override;
461 
462 
466  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
467 
468 
472  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
473 
474 
476  // Checks above which widget the mouse is standing.
477  // If there is no widget below the mouse then this function will return a null pointer.
479  Widget::Ptr mouseOnWhichWidget(Vector2f mousePos);
480 
481 
483  // This function will call the draw function from all the widgets.
485  virtual void drawWidgetContainer(sf::RenderTarget* target, const sf::RenderStates& states = sf::RenderStates::Default) const;
486 
487 
489  // Find out what the index of the focused widget is. Returns 0 when no widget is focused and index+1 otherwise.
491  std::size_t getFocusedWidgetIndex() const;
492 
493 
495  // Try to focus the given child widget
497  bool tryFocusWidget(const tgui::Widget::Ptr &widget, bool reverseWidgetOrder);
498 
499 
501  protected:
502 
503  std::vector<Widget::Ptr> m_widgets;
504 
505  Widget::Ptr m_widgetBelowMouse;
506  Widget::Ptr m_focusedWidget;
507 
508  Vector2f m_prevInnerSize;
509 
510  // Did we enter handleEvent directly or because we got a MouseReleased event?
511  bool m_handingMouseReleased = false;
512 
513  // Does focusing the next widget always keep a widget from this container focused (e.g. in a ChildWindow)?
514  bool m_isolatedFocus = false;
515 
516 
517  friend class SubwidgetContainer; // Needs access to save and load functions
518 
520  };
521 
522 
526  class TGUI_API GuiContainer : public Container
527  {
528  public:
529 
530  typedef std::shared_ptr<GuiContainer> Ptr;
531  typedef std::shared_ptr<const GuiContainer> ConstPtr;
532 
533 
538  GuiContainer();
539 
540 
549  void setSize(const Layout2d& size) override;
550  using Widget::setSize;
551 
552 
559  void setFocused(bool focused) override;
560 
561 
570  bool mouseOnWidget(Vector2f pos) const override;
571 
572 
574  private:
575 
576 
578  // This function does nothing.
580  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
581 
582 
584  // Returns a nullptr.
586  Widget::Ptr clone() const override
587  {
588  return nullptr;
589  }
590 
591 
593  protected:
594 
595  friend class Gui; // Required to let Gui access protected members from container and Widget
596 
598  };
599 
600 
602 }
603 
605 
606 #endif // TGUI_CONTAINER_HPP
bool focusNextWidget()
Focuses the next widget in this container.
Container widget.
Definition: Container.hpp:48
Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
void saveWidgetsToStream(std::stringstream &stream) const
Saves the child widgets to a text file.
void setTextSize(unsigned int size) override
Changes the character size of all existing and future child widgets.
void loadWidgetsFromStream(std::stringstream &stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
Base class for widgets that consist of subwidgets that act together as if they are a single widget.
Definition: SubwidgetContainer.hpp:43
bool focusPreviousWidget()
Focuses the previous widget in this container.
virtual Vector2f getInnerSize() const
Returns the space available for widgets inside the container.
void uncheckRadioButtons()
Unchecks all the radio buttons.
Container(const Container &copy)
Copy constructor.
void loadWidgetsFromStream(std::stringstream &&stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
~Container()
Destructor.
void setSize(const Layout2d &size) override
Changes the size of the container.
void moveWidgetToBack(const Widget::Ptr &widget)
Places a widget behind all other widgets.
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:240
void setFocused(bool focused) override
Focus or unfocus the widget.
virtual bool remove(const Widget::Ptr &widget)
Removes a single widget that was added to the container.
void moveWidgetToFront(const Widget::Ptr &widget)
Places a widget before all other widgets.
void loadWidgetsFromFile(const std::string &filename, bool replaceExisting=true)
Loads the child widgets from a text file.
Wrapper class to store strings.
Definition: String.hpp:119
Widget::Ptr get(const sf::String &widgetName) const
Returns a pointer to a widget that was added earlier.
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets in this container.
Definition: Container.hpp:105
T::Ptr get(const sf::String &widgetName) const
Returns a pointer to a widget that was added earlier.
Definition: Container.hpp:171
Definition: Vector2f.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Container(Container &&copy)
Move constructor.
Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
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::shared_ptr< Container > Ptr
Shared widget pointer.
Definition: Container.hpp:51
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
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.
virtual void removeAllWidgets()
Removes all widgets that were added to the container.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
std::shared_ptr< const Container > ConstPtr
Shared constant widget pointer.
Definition: Container.hpp:52