TGUI  0.8-alpha
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  void 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 
366  protected:
367 
368 
375  void rendererChanged(const std::string& property) override;
376 
377 
381  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
382 
383 
387  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
388 
389 
391  // This function is called every frame with the time passed since the last frame.
393  void update(sf::Time elapsedTime) override;
394 
395 
397  // The function returns true when the event is consumed and false when the event was ignored by all widgets.
399  bool handleEvent(sf::Event& event);
400 
401 
403  // Checks above which widget the mouse is standing.
404  // If there is no widget below the mouse then this function will return a null pointer.
406  Widget::Ptr mouseOnWhichWidget(Vector2f mousePos);
407 
408 
410  // This function will call the draw function from all the widgets.
412  virtual void drawWidgetContainer(sf::RenderTarget* target, const sf::RenderStates& states = sf::RenderStates::Default) const;
413 
414 
416  // Find out what the index of the focused widget is. Returns 0 when no widget is focused and index+1 otherwise.
418  std::size_t getFocusedWidgetIndex() const;
419 
420 
422  // Try to focus the given child widget
424  bool tryFocusWidget(const tgui::Widget::Ptr &widget, bool reverseWidgetOrder);
425 
426 
428  protected:
429 
430  std::vector<Widget::Ptr> m_widgets;
431  std::vector<sf::String> m_widgetNames;
432 
433  Widget::Ptr m_widgetBelowMouse;
434  Widget::Ptr m_focusedWidget;
435 
436  // Did we enter handleEvent directly or because we got a MouseReleased event?
437  bool m_handingMouseReleased = false;
438 
439  // Does focusing the next widget always keep a widget from this container focused (e.g. in a ChildWindow)?
440  bool m_isolatedFocus = false;
441 
442 
444  };
445 
446 
450  class TGUI_API GuiContainer : public Container
451  {
452  public:
453 
454  typedef std::shared_ptr<GuiContainer> Ptr;
455  typedef std::shared_ptr<const GuiContainer> ConstPtr;
456 
457 
462  GuiContainer();
463 
464 
473  void setSize(const Layout2d& size) override;
474  using Widget::setSize;
475 
476 
483  void setFocused(bool focused) override;
484 
485 
494  bool mouseOnWidget(Vector2f pos) const override;
495 
496 
498  private:
499 
500 
502  // This function does nothing.
504  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
505 
506 
508  // Returns a nullptr.
510  Widget::Ptr clone() const override
511  {
512  return nullptr;
513  }
514 
515 
517  protected:
518 
519  friend class Gui; // Required to let Gui access protected members from container and Widget
520 
522  };
523 
524 
526 }
527 
529 
530 #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