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 
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 
141  Widget::Ptr get(const sf::String& widgetName) const;
142 
143 
159  template <class T>
160  typename T::Ptr get(const sf::String& widgetName) const
161  {
162  return std::dynamic_pointer_cast<T>(get(widgetName));
163  }
164 
165 
174  virtual bool remove(const Widget::Ptr& widget);
175 
176 
181  virtual void removeAllWidgets();
182 
183 
193  bool setWidgetName(const Widget::Ptr& widget, const std::string& name);
194 
195 
204  std::string getWidgetName(const Widget::Ptr& widget) const;
205 
206 
215  void focusWidget(const Widget::Ptr& widget);
216 
217 
225  void focusNextWidget();
226 
227 
235  void focusPreviousWidget();
236 
237 
242  void unfocusWidgets();
243 
244 
249  void uncheckRadioButtons();
250 
251 
259  virtual sf::Vector2f getChildWidgetsOffset() const
260  {
261  return sf::Vector2f{0, 0};
262  }
263 
264 
271  void loadWidgetsFromFile(const std::string& filename);
272 
273 
280  void saveWidgetsToFile(const std::string& filename);
281 
282 
289  void loadWidgetsFromStream(std::stringstream& stream);
290 
291 
298  void loadWidgetsFromStream(std::stringstream&& stream);
299 
300 
307  void saveWidgetsToStream(std::stringstream& stream) const;
308 
309 
316  void moveWidgetToFront(const Widget::Ptr& widget);
317 
318 
325  void moveWidgetToBack(const Widget::Ptr& widget);
326 
327 
331  void leftMousePressed(sf::Vector2f pos) override;
332 
336  void leftMouseReleased(sf::Vector2f pos) override;
337 
341  void mouseMoved(sf::Vector2f pos) override;
342 
346  void keyPressed(const sf::Event::KeyEvent& event) override;
347 
351  void textEntered(sf::Uint32 key) override;
352 
356  void mouseWheelScrolled(float delta, sf::Vector2f pos) override;
357 
361  void mouseNoLongerOnWidget() override;
362 
366  void mouseNoLongerDown() override;
367 
371  void widgetUnfocused() override;
372 
373 
376  // Shows the tool tip when the widget is located below the mouse.
377  // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
378  // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
380  Widget::Ptr askToolTip(sf::Vector2f mousePos) override;
381 
382 
384  protected:
385 
392  void rendererChanged(const std::string& property) override;
393 
394 
396  // This function is called every frame with the time passed since the last frame.
398  void update(sf::Time elapsedTime) override;
399 
400 
402  // When this function is called then all the widgets receive the event (if there are widgets).
403  // The function returns true when the event is consumed and false when the event was ignored by all widgets.
405  bool handleEvent(sf::Event& event);
406 
407 
409  // Focuses the next widget in the container. If the last widget was focused then all widgets will be unfocused and
410  // this function will return false.
412  bool focusNextWidgetInContainer();
413 
414 
416  // When the tab key is pressed then this function is called. The focus will move to the next widget (if there is one).
417  // This function will only work when tabKeyUsageEnabled is true.
418  // The function will return true when another widget was focused.
420  bool tabKeyPressed();
421 
422 
424  // Checks above which widget the mouse is standing.
425  // If there is no widget below the mouse then this function will return a null pointer.
427  Widget::Ptr mouseOnWhichWidget(sf::Vector2f mousePos);
428 
429 
431  // This function will call the draw function from all the widgets.
433  virtual void drawWidgetContainer(sf::RenderTarget* target, const sf::RenderStates& states = sf::RenderStates::Default) const;
434 
435 
437  protected:
438 
439  std::vector<Widget::Ptr> m_widgets;
440  std::vector<sf::String> m_widgetNames;
441 
442  Widget::Ptr m_widgetBelowMouse;
443 
444  // The id of the focused widget
445  std::size_t m_focusedWidget = 0;
446 
447  // Did we enter handleEvent directly or because we got a MouseReleased event?
448  bool m_handingMouseReleased = false;
449 
450 
452  };
453 
454 
458  class TGUI_API GuiContainer : public Container
459  {
460  public:
461 
462  typedef std::shared_ptr<GuiContainer> Ptr;
463  typedef std::shared_ptr<const GuiContainer> ConstPtr;
464 
465 
470  GuiContainer();
471 
472 
481  void setSize(const Layout2d& size) override;
482  using Widget::setSize;
483 
484 
493  bool mouseOnWidget(sf::Vector2f pos) const override;
494 
495 
497  private:
498 
499 
501  // This function does nothing.
503  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
504 
505 
507  // Returns a nullptr.
509  Widget::Ptr clone() const override
510  {
511  return nullptr;
512  }
513 
514 
516  protected:
517 
518  friend class Gui; // Required to let Gui access protected members from container and Widget
519 
521  };
522 
523 
525 }
526 
528 
529 #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:243
The parent class for every widget.
Definition: Widget.hpp:62
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:109
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:66
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:259
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