TGUI  0.8.0
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 
97  const std::vector<Widget::Ptr>& getWidgets() const
98  {
99  return m_widgets;
100  }
101 
102 
109  const std::vector<sf::String>& getWidgetNames() const
110  {
111  return m_widgetNames;
112  }
113 
114 
124  virtual void add(const Widget::Ptr& widgetPtr, const sf::String& widgetName = "");
125 
126 
140  Widget::Ptr get(const sf::String& widgetName) const;
141 
142 
157  template <class T>
158  typename T::Ptr get(const sf::String& widgetName) const
159  {
160  return std::dynamic_pointer_cast<T>(get(widgetName));
161  }
162 
163 
172  virtual bool remove(const Widget::Ptr& widget);
173 
174 
179  virtual void removeAllWidgets();
180 
181 
191  bool setWidgetName(const Widget::Ptr& widget, const std::string& name);
192 
193 
202  std::string getWidgetName(const Widget::ConstPtr& widget) const;
203 
204 
209  void uncheckRadioButtons();
210 
211 
220  {
221  return Vector2f{0, 0};
222  }
223 
224 
230  void loadWidgetsFromFile(const std::string& filename);
231 
232 
238  void saveWidgetsToFile(const std::string& filename);
239 
240 
246  void loadWidgetsFromStream(std::stringstream& stream);
247 
248 
254  void loadWidgetsFromStream(std::stringstream&& stream);
255 
256 
262  void saveWidgetsToStream(std::stringstream& stream) const;
263 
264 
271  void moveWidgetToFront(const Widget::Ptr& widget);
272 
273 
280  void moveWidgetToBack(const Widget::Ptr& widget);
281 
282 
287  bool focusNextWidget();
288 
289 
294  bool focusPreviousWidget();
295 
296 
305  void setFocused(bool focused) override;
306 
307 
312  void childWidgetFocused(const Widget::Ptr& child);
313 
314 
318  void leftMousePressed(Vector2f pos) override;
319 
323  void leftMouseReleased(Vector2f pos) override;
324 
328  void mouseMoved(Vector2f pos) override;
329 
333  void keyPressed(const sf::Event::KeyEvent& event) override;
334 
338  void textEntered(std::uint32_t key) override;
339 
343  bool mouseWheelScrolled(float delta, Vector2f pos) override;
344 
348  void mouseNoLongerOnWidget() override;
349 
353  void mouseNoLongerDown() override;
354 
355 
358  // Shows the tool tip when the widget is located below the mouse.
359  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
360  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
362  Widget::Ptr askToolTip(Vector2f mousePos) override;
363 
364 
367  // This function is called every frame with the time passed since the last frame.
369  void update(sf::Time elapsedTime) override;
370 
371 
374  // The function returns true when the event is consumed and false when the event was ignored by all widgets.
376  bool handleEvent(sf::Event& event);
377 
378 
380  protected:
381 
382 
389  void rendererChanged(const std::string& property) override;
390 
391 
395  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
396 
397 
401  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
402 
403 
405  // Checks above which widget the mouse is standing.
406  // If there is no widget below the mouse then this function will return a null pointer.
408  Widget::Ptr mouseOnWhichWidget(Vector2f mousePos);
409 
410 
412  // This function will call the draw function from all the widgets.
414  virtual void drawWidgetContainer(sf::RenderTarget* target, const sf::RenderStates& states = sf::RenderStates::Default) const;
415 
416 
418  // Find out what the index of the focused widget is. Returns 0 when no widget is focused and index+1 otherwise.
420  std::size_t getFocusedWidgetIndex() const;
421 
422 
424  // Try to focus the given child widget
426  bool tryFocusWidget(const tgui::Widget::Ptr &widget, bool reverseWidgetOrder);
427 
428 
430  protected:
431 
432  std::vector<Widget::Ptr> m_widgets;
433  std::vector<sf::String> m_widgetNames;
434 
435  Widget::Ptr m_widgetBelowMouse;
436  Widget::Ptr m_focusedWidget;
437 
438  // Did we enter handleEvent directly or because we got a MouseReleased event?
439  bool m_handingMouseReleased = false;
440 
441  // Does focusing the next widget always keep a widget from this container focused (e.g. in a ChildWindow)?
442  bool m_isolatedFocus = false;
443 
444 
446  };
447 
448 
452  class TGUI_API GuiContainer : public Container
453  {
454  public:
455 
456  typedef std::shared_ptr<GuiContainer> Ptr;
457  typedef std::shared_ptr<const GuiContainer> ConstPtr;
458 
459 
464  GuiContainer();
465 
466 
475  void setSize(const Layout2d& size) override;
476  using Widget::setSize;
477 
478 
485  void setFocused(bool focused) override;
486 
487 
496  bool mouseOnWidget(Vector2f pos) const override;
497 
498 
500  private:
501 
502 
504  // This function does nothing.
506  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
507 
508 
510  // Returns a nullptr.
512  Widget::Ptr clone() const override
513  {
514  return nullptr;
515  }
516 
517 
519  protected:
520 
521  friend class Gui; // Required to let Gui access protected members from container and Widget
522 
524  };
525 
526 
528 }
529 
531 
532 #endif // TGUI_CONTAINER_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
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.
Definition: Container.hpp:109
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:219
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.
Definition: Container.hpp:97
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