TGUI  0.9.3
Grid.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2021 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
74 Grid(const char* typeName = "Grid", bool initRenderer = true);
75
76
83 static Grid::Ptr create();
84
85
89 Grid(const Grid& copy);
90
91
95 Grid(Grid&& copy);
96
97
101 Grid& operator= (const Grid& right);
102
103
107 Grid& operator= (Grid&& right);
108
109
119
120
131 void setSize(const Layout2d& size) override;
132 using Widget::setSize;
133
134
145 void setAutoSize(bool autoSize);
146
147
155 bool getAutoSize() const;
156
157
163 bool remove(const Widget::Ptr& widget) override;
164
165
170 void removeAllWidgets() override;
171
172
183 void addWidget(const Widget::Ptr& widget,
184 std::size_t row,
185 std::size_t column,
186 Alignment alignment = Alignment::Center,
187 const Padding& padding = Padding{0});
188
189
199 Widget::Ptr getWidget(std::size_t row, std::size_t column) const;
200
201
209 std::map<Widget::Ptr, std::pair<std::size_t, std::size_t>> getWidgetLocations() const;
210
211
219 void setWidgetPadding(const Widget::Ptr& widget, const Padding& padding = Padding(0, 0, 0, 0));
220
221
230 void setWidgetPadding(std::size_t row, std::size_t column, const Padding& padding = Padding(0, 0, 0, 0));
231
232
242
243
253 Padding getWidgetPadding(std::size_t row, std::size_t column) const;
254
255
263 void setWidgetAlignment(const Widget::Ptr& widget, Alignment alignment = Alignment::Center);
264
265
274 void setWidgetAlignment(std::size_t row, std::size_t column, Alignment alignment = Alignment::Center);
275
276
286
287
297 Alignment getWidgetAlignment(std::size_t row, std::size_t column) const;
298
299
306 const std::vector<std::vector<Widget::Ptr>>& getGridWidgets() const;
307
308
315 bool isMouseOnWidget(Vector2f pos) const override;
316
317
319 protected:
320
324 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
325
326
330 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
331
332
334 // Returns the minimum size required by the grid to display correctly all widgets.
336 Vector2f getMinimumSize() const;
337
338
340 // Reposition all the widgets.
342 void updatePositionsOfAllWidgets();
343
344
346 // Updates the position and size of the widget
348 void updateWidgets();
349
350
352 // Makes a copy of the widget
354 Widget::Ptr clone() const override
355 {
356 return std::make_shared<Grid>(*this);
357 }
358
359
361 protected:
362
363 bool m_autoSize = true;
364
365 std::vector<std::vector<Widget::Ptr>> m_gridWidgets;
366 std::vector<std::vector<Padding>> m_objPadding;
367 std::vector<std::vector<Alignment>> m_objAlignment;
368
369 std::vector<float> m_rowHeight;
370 std::vector<float> m_columnWidth;
371
372 std::map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
373
375 };
376
378}
379
381
382#endif // TGUI_GRID_HPP
Container widget.
Definition: Container.hpp:47
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:354
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.
void addWidget(const Widget::Ptr &widget, std::size_t row, std::size_t column, Alignment alignment=Alignment::Center, const Padding &padding=Padding{0})
Adds a widget to the grid.
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.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the 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
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:262
Definition: Outline.hpp:39
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36