TGUI  0.8-dev
Container.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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 
67  Container(const Container& copy);
68 
69 
73  ~Container();
74 
75 
84  Container& operator= (const Container& right);
85 
86 
93  const std::vector<Widget::Ptr>& getWidgets() const
94  {
95  return m_widgets;
96  }
97 
98 
105  const std::vector<sf::String>& getWidgetNames() const
106  {
107  return m_widgetNames;
108  }
109 
110 
120  virtual void add(const Widget::Ptr& widgetPtr, const sf::String& widgetName = "");
121 
122 
134  Widget::Ptr get(const sf::String& widgetName, bool recursive = false) const;
135 
136 
149  template <class T>
150  typename T::Ptr get(const sf::String& widgetName, bool recursive = false) const
151  {
152  return std::dynamic_pointer_cast<T>(get(widgetName, recursive));
153  }
154 
155 
164  virtual bool remove(const Widget::Ptr& widget);
165 
166 
171  virtual void removeAllWidgets();
172 
173 
183  bool setWidgetName(const Widget::Ptr& widget, const std::string& name);
184 
185 
194  std::string getWidgetName(const Widget::Ptr& widget) const;
195 
196 
205  void focusWidget(const Widget::Ptr& widget);
206 
207 
215  void focusNextWidget();
216 
217 
225  void focusPreviousWidget();
226 
227 
232  void unfocusWidgets();
233 
234 
239  void uncheckRadioButtons();
240 
241 
249  virtual sf::Vector2f getChildWidgetsOffset() const
250  {
251  return sf::Vector2f{0, 0};
252  }
253 
254 
261  void loadWidgetsFromFile(const std::string& filename);
262 
263 
270  void saveWidgetsToFile(const std::string& filename);
271 
272 
279  void loadWidgetsFromStream(std::stringstream& stream);
280 
281 
288  void saveWidgetsToStream(std::stringstream& stream);
289 
290 
297  void moveWidgetToFront(const Widget::Ptr& widget);
298 
299 
306  void moveWidgetToBack(const Widget::Ptr& widget);
307 
308 
312  virtual void leftMousePressed(sf::Vector2f pos) override;
313 
317  virtual void leftMouseReleased(sf::Vector2f pos) override;
318 
322  virtual void mouseMoved(sf::Vector2f pos) override;
323 
327  virtual void keyPressed(const sf::Event::KeyEvent& event) override;
328 
332  virtual void textEntered(sf::Uint32 key) override;
333 
337  virtual void mouseWheelScrolled(float delta, int x, int y) override;
338 
342  virtual void mouseNoLongerOnWidget() override;
343 
347  virtual void mouseNoLongerDown() override;
348 
352  virtual void widgetUnfocused() override;
353 
354 
357  // Shows the tool tip when the widget is located below the mouse.
358  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
359  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
361  virtual Widget::Ptr askToolTip(sf::Vector2f mousePos) override;
362 
363 
365  protected:
366 
373  virtual void rendererChanged(const std::string& property) override;
374 
375 
377  // This function is called every frame with the time passed since the last frame.
379  virtual void update(sf::Time elapsedTime) override;
380 
381 
383  // When this function is called then all the widgets receive the event (if there are widgets).
384  // The function returns true when the event is consumed and false when the event was ignored by all widgets.
386  bool handleEvent(sf::Event& event);
387 
388 
390  // Focuses the next widget in the container. If the last widget was focused then all widgets will be unfocused and
391  // this function will return false.
393  bool focusNextWidgetInContainer();
394 
395 
397  // When the tab key is pressed then this function is called. The focus will move to the next widget (if there is one).
398  // This function will only work when tabKeyUsageEnabled is true.
399  // The function will return true when another widget was focused.
401  bool tabKeyPressed();
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(sf::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  protected:
419 
420  std::vector<Widget::Ptr> m_widgets;
421  std::vector<sf::String> m_widgetNames;
422 
423  Widget::Ptr m_widgetBelowMouse;
424 
425  // The id of the focused widget
426  std::size_t m_focusedWidget = 0;
427 
428  // Did we enter handleEvent directly or because we got a MouseReleased event?
429  bool m_handingMouseReleased = false;
430 
431 
433  };
434 
435 
439  class TGUI_API GuiContainer : public Container
440  {
441  public:
442 
443  typedef std::shared_ptr<GuiContainer> Ptr;
444  typedef std::shared_ptr<const GuiContainer> ConstPtr;
445 
446 
451  GuiContainer();
452 
453 
462  virtual void setSize(const Layout2d& size) override;
464 
465 
470  virtual bool mouseOnWidget(sf::Vector2f pos) const override;
471 
472 
474  private:
475 
476 
478  // This function does nothing.
480  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
481 
482 
484  // Returns a nullptr.
486  virtual Widget::Ptr clone() const override
487  {
488  return nullptr;
489  }
490 
491 
493  protected:
494 
495  sf::RenderTarget* m_window = nullptr;
496 
497  friend class Gui; // Required to let Gui access protected members from container and Widget
498 
500  };
501 
502 
504 }
505 
507 
508 #endif // TGUI_CONTAINER_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Class to store the position or size of a widget.
Definition: Layout.hpp:263
The parent class for every widget.
Definition: Widget.hpp:72
Gui class.
Definition: Gui.hpp:42
const std::vector< sf::String > & getWidgetNames() const
Returns a list of the names of all the widgets.
Definition: Container.hpp:105
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
virtual sf::Vector2f getChildWidgetsOffset() const
Returns the distance between the position of the container and a widget that would be drawn inside th...
Definition: Container.hpp:249
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets.
Definition: Container.hpp:93
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