TGUI  0.8.2
Container.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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 
70  Container(Container&& copy);
71 
72 
76  ~Container();
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 
117  const std::vector<sf::String>& getWidgetNames() const
118  {
119  return m_widgetNames;
120  }
121 
122 
132  virtual void add(const Widget::Ptr& widgetPtr, const sf::String& widgetName = "");
133 
134 
148  Widget::Ptr get(const sf::String& widgetName) const;
149 
150 
165  template <class T>
166  typename T::Ptr get(const sf::String& widgetName) const
167  {
168  return std::dynamic_pointer_cast<T>(get(widgetName));
169  }
170 
171 
180  virtual bool remove(const Widget::Ptr& widget);
181 
182 
187  virtual void removeAllWidgets();
188 
189 
199  bool setWidgetName(const Widget::Ptr& widget, const std::string& name);
200 
201 
210  std::string getWidgetName(const Widget::ConstPtr& widget) const;
211 
212 
217  void uncheckRadioButtons();
218 
219 
224  virtual Vector2f getInnerSize() const;
225 
226 
235  {
236  return Vector2f{0, 0};
237  }
238 
239 
245  void loadWidgetsFromFile(const std::string& filename);
246 
247 
253  void saveWidgetsToFile(const std::string& filename);
254 
255 
261  void loadWidgetsFromStream(std::stringstream& stream);
262 
263 
269  void loadWidgetsFromStream(std::stringstream&& stream);
270 
271 
277  void saveWidgetsToStream(std::stringstream& stream) const;
278 
279 
286  void moveWidgetToFront(const Widget::Ptr& widget);
287 
288 
295  void moveWidgetToBack(const Widget::Ptr& widget);
296 
297 
302  bool focusNextWidget();
303 
304 
309  bool focusPreviousWidget();
310 
311 
320  void setFocused(bool focused) override;
321 
322 
327  void childWidgetFocused(const Widget::Ptr& child);
328 
329 
333  void leftMousePressed(Vector2f pos) override;
334 
338  void leftMouseReleased(Vector2f pos) override;
339 
343  void mouseMoved(Vector2f pos) override;
344 
348  void keyPressed(const sf::Event::KeyEvent& event) override;
349 
353  void textEntered(std::uint32_t key) override;
354 
358  bool mouseWheelScrolled(float delta, Vector2f pos) override;
359 
363  void mouseNoLongerOnWidget() override;
364 
368  void mouseNoLongerDown() override;
369 
370 
373  // Shows the tool tip when the widget is located below the mouse.
374  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
375  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
377  Widget::Ptr askToolTip(Vector2f mousePos) override;
378 
379 
382  // This function is called every frame with the time passed since the last frame.
384  void update(sf::Time elapsedTime) override;
385 
386 
389  // The function returns true when the event is consumed and false when the event was ignored by all widgets.
391  bool handleEvent(sf::Event& event);
392 
393 
395  protected:
396 
397 
404  void rendererChanged(const std::string& property) override;
405 
406 
410  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
411 
412 
416  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
417 
418 
420  // Checks above which widget the mouse is standing.
421  // If there is no widget below the mouse then this function will return a null pointer.
423  Widget::Ptr mouseOnWhichWidget(Vector2f mousePos);
424 
425 
427  // This function will call the draw function from all the widgets.
429  virtual void drawWidgetContainer(sf::RenderTarget* target, const sf::RenderStates& states = sf::RenderStates::Default) const;
430 
431 
433  // Find out what the index of the focused widget is. Returns 0 when no widget is focused and index+1 otherwise.
435  std::size_t getFocusedWidgetIndex() const;
436 
437 
439  // Try to focus the given child widget
441  bool tryFocusWidget(const tgui::Widget::Ptr &widget, bool reverseWidgetOrder);
442 
443 
445  protected:
446 
447  std::vector<Widget::Ptr> m_widgets;
448  std::vector<sf::String> m_widgetNames;
449 
450  Widget::Ptr m_widgetBelowMouse;
451  Widget::Ptr m_focusedWidget;
452 
453  Vector2f m_prevInnerSize;
454 
455  // Did we enter handleEvent directly or because we got a MouseReleased event?
456  bool m_handingMouseReleased = false;
457 
458  // Does focusing the next widget always keep a widget from this container focused (e.g. in a ChildWindow)?
459  bool m_isolatedFocus = false;
460 
461 
463  };
464 
465 
469  class TGUI_API GuiContainer : public Container
470  {
471  public:
472 
473  typedef std::shared_ptr<GuiContainer> Ptr;
474  typedef std::shared_ptr<const GuiContainer> ConstPtr;
475 
476 
481  GuiContainer();
482 
483 
492  void setSize(const Layout2d& size) override;
493  using Widget::setSize;
494 
495 
502  void setFocused(bool focused) override;
503 
504 
513  bool mouseOnWidget(Vector2f pos) const override;
514 
515 
517  private:
518 
519 
521  // This function does nothing.
523  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
524 
525 
527  // Returns a nullptr.
529  Widget::Ptr clone() const override
530  {
531  return nullptr;
532  }
533 
534 
536  protected:
537 
538  friend class Gui; // Required to let Gui access protected members from container and Widget
539 
541  };
542 
543 
545 }
546 
548 
549 #endif // TGUI_CONTAINER_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Class to store the position or size of a widget.
Definition: Layout.hpp:245
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< const Widget > ConstPtr
Shared constant widget pointer.
Definition: Widget.hpp:77
Definition: Vector2f.hpp:37
const std::vector< sf::String > & getWidgetNames() const
Returns a list of the names of all the widgets in this container.
Definition: Container.hpp:117
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
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:234
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets in this container.
Definition: Container.hpp:105
std::shared_ptr< const Container > ConstPtr
Shared constant widget pointer.
Definition: Container.hpp:52
std::shared_ptr< Container > Ptr
Shared widget pointer.
Definition: Container.hpp:51