TGUI  0.8.9
Grid.hpp
1
2//
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_GRID_HPP
27#define TGUI_GRID_HPP
28
29
30#include <TGUI/Container.hpp>
31
33
34namespace tgui
35{
37
38 class TGUI_API Grid : public Container
39 {
40 public:
41
42 typedef std::shared_ptr<Grid> Ptr;
43 typedef std::shared_ptr<const Grid> ConstPtr;
44
45
53 enum class Alignment
54 {
55 Center,
56 UpperLeft,
57 Up,
58 UpperRight,
59 Right,
60 BottomRight,
61 Bottom,
62 BottomLeft,
63 Left
64 };
65
66
68 // Default constructor
70 Grid();
71
72
79 static Grid::Ptr create();
80
81
85 Grid(const Grid& copy);
86
87
91 Grid(Grid&& copy);
92
93
97 Grid& operator= (const Grid& right);
98
99
103 Grid& operator= (Grid&& right);
104
105
115
116
127 void setSize(const Layout2d& size) override;
128 using Widget::setSize;
129
130
141 void setAutoSize(bool autoSize);
142
143
151 bool getAutoSize() const;
152
153
162 bool remove(const Widget::Ptr& widget) override;
163
164
169 void removeAllWidgets() override;
170
171
182 void addWidget(const Widget::Ptr& widget,
183 std::size_t row,
184 std::size_t column,
185 const Padding& padding = Padding{0},
186 Alignment alignment = Alignment::Center);
187
188
198 Widget::Ptr getWidget(std::size_t row, std::size_t column) const;
199
200
208 std::map<Widget::Ptr, std::pair<std::size_t, std::size_t>> getWidgetLocations() const;
209
210
218 void setWidgetPadding(const Widget::Ptr& widget, const Padding& padding = Padding(0, 0, 0, 0));
219
220
229 void setWidgetPadding(std::size_t row, std::size_t column, const Padding& padding = Padding(0, 0, 0, 0));
230
231
241
242
252 Padding getWidgetPadding(std::size_t row, std::size_t column) const;
253
254
262 void setWidgetAlignment(const Widget::Ptr& widget, Alignment alignment = Alignment::Center);
263
264
273 void setWidgetAlignment(std::size_t row, std::size_t column, Alignment alignment = Alignment::Center);
274
275
285
286
296 Alignment getWidgetAlignment(std::size_t row, std::size_t column) const;
297
298
305 const std::vector<std::vector<Widget::Ptr>>& getGridWidgets() const;
306
307
314 bool mouseOnWidget(Vector2f pos) const override;
315
316
324 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
325
326
328 protected:
329
333 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
334
335
339 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
340
341
343 // Returns the minimum size required by the grid to display correctly all widgets.
345 Vector2f getMinimumSize() const;
346
347
349 // Reposition all the widgets.
351 void updatePositionsOfAllWidgets();
352
353
355 // Updates the position and size of the widget
357 void updateWidgets();
358
359
361 // Makes a copy of the widget
363 Widget::Ptr clone() const override
364 {
365 return std::make_shared<Grid>(*this);
366 }
367
368
370 protected:
371
372 bool m_autoSize = true;
373
374 std::vector<std::vector<Widget::Ptr>> m_gridWidgets;
375 std::vector<std::vector<Padding>> m_objPadding;
376 std::vector<std::vector<Alignment>> m_objAlignment;
377
378 std::vector<float> m_rowHeight;
379 std::vector<float> m_columnWidth;
380
381 std::map<Widget::Ptr, unsigned int> m_connectedCallbacks;
382
384 };
385
387}
388
390
391#endif // TGUI_GRID_HPP
Container widget.
Definition: Container.hpp:48
Definition: Grid.hpp:39
void removeAllWidgets() override
Removes all widgets that were added to the container.
Grid(const Grid &copy)
Copy constructor.
Widget::Ptr getWidget(std::size_t row, std::size_t column) const
Returns the widget in a specific cell of the grid.
static Grid::Ptr copy(Grid::ConstPtr grid)
Makes a copy of another grid.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
void setSize(const Layout2d &size) override
Changes the size of the grid.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Grid.hpp:363
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
void addWidget(const Widget::Ptr &widget, std::size_t row, std::size_t column, const Padding &padding=Padding{0}, Alignment alignment=Alignment::Center)
Adds a widget to the grid.
Alignment getWidgetAlignment(const Widget::Ptr &widget) const
Returns the alignment of a given widget.
Alignment getWidgetAlignment(std::size_t row, std::size_t column) const
Returns the alignment of a given widget in its cell.
Padding getWidgetPadding(std::size_t row, std::size_t column) const
Returns the padding around a widget in a specific cell of the grid.
Alignment
The alignment of the widget in its cell.
Definition: Grid.hpp:54
Padding getWidgetPadding(const Widget::Ptr &widget) const
Returns the padding around a widget.
bool getAutoSize() const
Returns whether the grid is auto-sized or not.
static Grid::Ptr create()
Creates a new grid widget.
std::map< Widget::Ptr, std::pair< std::size_t, std::size_t > > getWidgetLocations() const
Returns a list of widgets and the cell they are in.
void setWidgetPadding(const Widget::Ptr &widget, const Padding &padding=Padding(0, 0, 0, 0))
Changes padding of a given widget.
std::shared_ptr< Grid > Ptr
Shared widget pointer.
Definition: Grid.hpp:42
bool remove(const Widget::Ptr &widget) override
Removes a single widget that was added to the container.
void setWidgetAlignment(std::size_t row, std::size_t column, Alignment alignment=Alignment::Center)
Changes the alignment of a given widget in its cell.
Grid(Grid &&copy)
Move constructor.
const std::vector< std::vector< Widget::Ptr > > & getGridWidgets() const
Returns the widgets and their positions in the grid.
std::shared_ptr< const Grid > ConstPtr
Shared constant widget pointer.
Definition: Grid.hpp:43
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void setWidgetPadding(std::size_t row, std::size_t column, const Padding &padding=Padding(0, 0, 0, 0))
Changes padding of a widget in a certain cell.
void setWidgetAlignment(const Widget::Ptr &widget, Alignment alignment=Alignment::Center)
Changes the alignment of a given widget in its cell.
void setAutoSize(bool autoSize)
Changes whether the grid is auto-sized or not.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Definition: Outline.hpp:39
Definition: Vector2f.hpp:39
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37