TGUI  0.7.8
Container.hpp
1
2//
3// TGUI - Texus's 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
36namespace 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
78 Container& operator= (const Container& right);
79
80
89 virtual void setFont(const Font& font) override;
90
91
98 const std::vector<Widget::Ptr>& getWidgets()
99 {
100 return m_widgets;
101 }
102
103
110 const std::vector<sf::String>& getWidgetNames()
111 {
112 return m_objName;
113 }
114
115
125 virtual void add(const Widget::Ptr& widgetPtr, const sf::String& widgetName = "");
126
127
139 Widget::Ptr get(const sf::String& widgetName, bool recursive = false) const;
140
141
154 template <class T>
155 typename T::Ptr get(const sf::String& widgetName, bool recursive = false) const
156 {
157 return std::dynamic_pointer_cast<T>(get(widgetName, recursive));
158 }
159
160
169 virtual bool remove(const Widget::Ptr& widget);
170
171
176 virtual void removeAllWidgets();
177
178
188 bool setWidgetName(const Widget::Ptr& widget, const std::string& name);
189
190
199 std::string getWidgetName(const Widget::Ptr& widget) const;
200
201
210 void focusWidget(const Widget::Ptr& widget);
211
212
221 void focusWidget(Widget *const widget);
222
223
232
233
242
243
249
250
256
257
264 virtual void setOpacity(float opacity) override;
265
266
274 virtual sf::Vector2f getChildWidgetsOffset() const
275 {
276 return sf::Vector2f{0, 0};
277 }
278
279
286 void loadWidgetsFromFile(const std::string& filename);
287
288
295 void saveWidgetsToFile(const std::string& filename);
296
297
304 void loadWidgetsFromStream(std::stringstream& stream);
305
306
313 void saveWidgetsToStream(std::stringstream& stream);
314
315
319 virtual void leftMousePressed(float x, float y) override;
320
324 virtual void leftMouseReleased(float x, float y) override;
325
329 virtual void mouseMoved(float x, float y) override;
330
334 virtual void keyPressed(const sf::Event::KeyEvent& event) override;
335
339 virtual void textEntered(sf::Uint32 key) override;
340
344 virtual void mouseWheelMoved(int delta, int x, int y) override;
345
349 virtual void mouseNoLongerOnWidget() override;
350
354 virtual void mouseNoLongerDown() override;
355
359 virtual void widgetUnfocused() override;
360
361
364 // Show the tool tip when the widget is located below the mouse.
365 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
366 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
368 virtual Widget::Ptr askToolTip(sf::Vector2f mousePos) override;
369
370
372 protected:
373
380 void moveWidgetToFront(Widget *const widget);
381
382
389 void moveWidgetToBack(Widget *const widget);
390
391
393 // This function is called every frame with the time passed since the last frame.
395 virtual void update(sf::Time elapsedTime) override;
396
397
399 // When this function is called then all the widgets receive the event (if there are widgets).
400 // The function returns true when the event is consumed and false when the event was ignored by all widgets.
402 bool handleEvent(sf::Event& event);
403
404
406 // Focuses the next widget in the container. If the last widget was focused then all widgets will be unfocused and
407 // this function will return false.
409 bool focusNextWidgetInContainer();
410
411
413 // When the tab key is pressed then this function is called. The focus will move to the next widget (if there is one).
414 // This function will only work when tabKeyUsageEnabled is true.
415 // The function will return true when another widget was focused.
417 bool tabKeyPressed();
418
419
421 // Checks above which widget the mouse is standing.
422 // If there is no widget below the mouse then this function will return a null pointer.
424 Widget::Ptr mouseOnWhichWidget(float x, float y);
425
426
428 // This function will call the draw function from all the widgets.
430 virtual void drawWidgetContainer(sf::RenderTarget* target, const sf::RenderStates& states = sf::RenderStates::Default) const;
431
432
434 protected:
435
436 std::vector<Widget::Ptr> m_widgets;
437 std::vector<sf::String> m_objName;
438
439 Widget::Ptr m_widgetBelowMouse;
440
441 // The id of the focused widget
442 std::size_t m_focusedWidget = 0;
443
444 // Did we enter handleEvent directly or because we got a MouseReleased event?
445 bool m_handingMouseReleased = false;
446
447
448 friend class Widget;
449
451 };
452
453
457 class TGUI_API GuiContainer : public Container
458 {
459 public:
460
461 typedef std::shared_ptr<GuiContainer> Ptr;
462 typedef std::shared_ptr<const GuiContainer> ConstPtr;
463
464
469 GuiContainer();
470
471
480 virtual void setSize(const Layout2d& size) override;
482
483
488 virtual bool mouseOnWidget(float x, float y) const override;
489
490
492 private:
493
495 // Returns a nullptr.
497 virtual Widget::Ptr clone() const override
498 {
499 return nullptr;
500 }
501
502
504 // This function does nothing.
506 virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
507
508
510 protected:
511
512 sf::RenderTarget* m_window = nullptr;
513
514
515 friend class Gui;
516
517
519 };
520
521
523}
524
526
527#endif // TGUI_CONTAINER_HPP
Container widget.
Definition: Container.hpp:48
virtual bool remove(const Widget::Ptr &widget)
Removes a single widget that was added to the container.
Container(const Container &copy)
Copy constructor.
bool setWidgetName(const Widget::Ptr &widget, const std::string &name)
Changes the name of a widget.
std::shared_ptr< Container > Ptr
Shared widget pointer.
Definition: Container.hpp:51
virtual void removeAllWidgets()
Removes all widgets that were added to the container.
void focusWidget(const Widget::Ptr &widget)
Focuses a widget.
void moveWidgetToBack(Widget *const widget)
Places a widget behind all other widgets.
void saveWidgetsToStream(std::stringstream &stream)
Save the child widgets to a text file.
virtual void setFont(const Font &font) override
Changes the font of the text in the widget and its children.
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:274
void unfocusWidgets()
Unfocus all the widgets.
T::Ptr get(const sf::String &widgetName, bool recursive=false) const
Returns a pointer to an earlier created widget.
Definition: Container.hpp:155
void saveWidgetsToFile(const std::string &filename)
Save the child widgets to a text file.
virtual void add(const Widget::Ptr &widgetPtr, const sf::String &widgetName="")
Adds a widget to the container.
Widget::Ptr get(const sf::String &widgetName, bool recursive=false) const
Returns a pointer to an earlier created widget.
virtual void setOpacity(float opacity) override
Changes the opacity of the container and all its child widgets.
void moveWidgetToFront(Widget *const widget)
Places a widget before all other widgets.
void loadWidgetsFromStream(std::stringstream &stream)
Load the child widgets from a string stream.
void uncheckRadioButtons()
Uncheck all the radio buttons.
void focusWidget(Widget *const widget)
Focuses a widget.
void loadWidgetsFromFile(const std::string &filename)
Load the child widgets from a text file.
const std::vector< sf::String > & getWidgetNames()
Returns a list of the names of all the widgets.
Definition: Container.hpp:110
std::shared_ptr< const Container > ConstPtr
Shared constant widget pointer.
Definition: Container.hpp:52
void focusNextWidget()
Focuses the next widget.
std::string getWidgetName(const Widget::Ptr &widget) const
Returns the name of a widget.
const std::vector< Widget::Ptr > & getWidgets()
Returns a list of all the widgets.
Definition: Container.hpp:98
void focusPreviousWidget()
Focuses the previous widget.
Definition: Font.hpp:38
Class to store the position or size of a widget.
Definition: Layout.hpp:255
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:34