TGUI  0.9-dev
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 
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 
120  virtual void add(const Widget::Ptr& widgetPtr, const String& widgetName = "");
121 
122 
136  Widget::Ptr get(const String& widgetName) const;
137 
138 
153  template <class T>
154  typename T::Ptr get(const String& widgetName) const
155  {
156  return std::dynamic_pointer_cast<T>(get(widgetName));
157  }
158 
159 
168  virtual bool remove(const Widget::Ptr& widget);
169 
170 
175  virtual void removeAllWidgets();
176 
177 
182  void uncheckRadioButtons();
183 
184 
189  virtual Vector2f getInnerSize() const;
190 
191 
200  {
201  return Vector2f{0, 0};
202  }
203 
204 
210  void setTextSize(unsigned int size) override;
211 
212 
221  void loadWidgetsFromFile(const String& filename, bool replaceExisting = true);
222 
223 
229  void saveWidgetsToFile(const String& filename);
230 
231 
238  void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
239 
240 
247  void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
248 
249 
255  void saveWidgetsToStream(std::stringstream& stream) const;
256 
257 
264  void moveWidgetToFront(const Widget::Ptr& widget);
265 
266 
273  void moveWidgetToBack(const Widget::Ptr& widget);
274 
275 
280  bool focusNextWidget();
281 
282 
287  bool focusPreviousWidget();
288 
289 
298  void setFocused(bool focused) override;
299 
300 
305  void childWidgetFocused(const Widget::Ptr& child);
306 
307 
311  void leftMousePressed(Vector2f pos) override;
312 
316  void leftMouseReleased(Vector2f pos) override;
317 
321  void rightMousePressed(Vector2f pos) override;
322 
326  void rightMouseReleased(Vector2f pos) override;
327 
331  void mouseMoved(Vector2f pos) override;
332 
336  void keyPressed(const sf::Event::KeyEvent& event) override;
337 
341  void textEntered(char32_t key) override;
342 
346  bool mouseWheelScrolled(float delta, Vector2f pos) override;
347 
351  void mouseNoLongerOnWidget() override;
352 
356  void leftMouseButtonNoLongerDown() override;
357 
361  void rightMouseButtonNoLongerDown() override;
362 
363 
366  // Shows the tool tip when the widget is located below the mouse.
367  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
368  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
370  Widget::Ptr askToolTip(Vector2f mousePos) override;
371 
372 
375  // This function is called every frame with the time passed since the last frame.
377  void update(Duration elapsedTime) override;
378 
379 
382  // The function returns true when the event is consumed and false when the event was ignored by all widgets.
384  bool handleEvent(sf::Event& event);
385 
386 
388  protected:
389 
390 
397  void rendererChanged(const String& property) override;
398 
399 
406  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
407 
408 
412  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
413 
414 
418  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
419 
420 
422  // Checks above which widget the mouse is standing.
423  // If there is no widget below the mouse then this function will return a null pointer.
425  Widget::Ptr mouseOnWhichWidget(Vector2f mousePos);
426 
427 
429  // Find out what the index of the focused widget is. Returns 0 when no widget is focused and index+1 otherwise.
431  std::size_t getFocusedWidgetIndex() const;
432 
433 
435  // Try to focus the given child widget
437  bool tryFocusWidget(const tgui::Widget::Ptr &widget, bool reverseWidgetOrder);
438 
439 
441  protected:
442 
443  std::vector<Widget::Ptr> m_widgets;
444 
445  Widget::Ptr m_widgetBelowMouse;
446  Widget::Ptr m_focusedWidget;
447 
448  Vector2f m_prevInnerSize;
449 
450  // Did we enter handleEvent directly or because we got a MouseReleased event?
451  bool m_handingMouseReleased = false;
452 
453  // Does focusing the next widget always keep a widget from this container focused (e.g. in a ChildWindow)?
454  bool m_isolatedFocus = false;
455 
456 
457  friend class SubwidgetContainer; // Needs access to save and load functions
458 
460  };
461 
462 
466  class TGUI_API GuiContainer : public Container
467  {
468  public:
469 
470  typedef std::shared_ptr<GuiContainer> Ptr;
471  typedef std::shared_ptr<const GuiContainer> ConstPtr;
472 
473 
478  GuiContainer();
479 
480 
489  void setSize(const Layout2d& size) override;
490  using Widget::setSize;
491 
492 
499  void setFocused(bool focused) override;
500 
501 
510  bool mouseOnWidget(Vector2f pos) const override;
511 
512 
514  private:
515 
517  // Returns a nullptr.
519  Widget::Ptr clone() const override
520  {
521  return nullptr;
522  }
523 
524 
526  protected:
527 
528  friend class Gui; // Required to let Gui access protected members from container and Widget
529 
531  };
532 
533 
535 }
536 
538 
539 #endif // TGUI_CONTAINER_HPP
Container widget.
Definition: Container.hpp:47
T::Ptr get(const String &widgetName) const
Returns a pointer to a widget that was added earlier.
Definition: Container.hpp:154
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
Base class for widgets that consist of subwidgets that act together as if they are a single widget.
Definition: SubwidgetContainer.hpp:42
Class to store the position or size of a widget.
Definition: Layout.hpp:258
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:199
Wrapper for durations.
Definition: Duration.hpp:48
Wrapper class to store strings.
Definition: String.hpp:70
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets in this container.
Definition: Container.hpp:105
The parent class for every widget.
Definition: Widget.hpp:68
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Container > Ptr
Shared widget pointer.
Definition: Container.hpp:51
std::shared_ptr< const Container > ConstPtr
Shared constant widget pointer.
Definition: Container.hpp:52