TGUI  0.10-beta
Grid.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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#include <unordered_map>
32
34
35namespace tgui
36{
38
39 class TGUI_API Grid : public Container
40 {
41 public:
42
43 typedef std::shared_ptr<Grid> Ptr;
44 typedef std::shared_ptr<const Grid> ConstPtr;
45
46
54 enum class Alignment
55 {
56 Center,
57 UpperLeft,
58 Up,
59 UpperRight,
60 Right,
61 BottomRight,
62 Bottom,
63 BottomLeft,
64 Left
65 };
66
67
75 Grid(const char* typeName = "Grid", bool initRenderer = true);
76
77
84 static Grid::Ptr create();
85
86
90 Grid(const Grid& copy);
91
92
96 Grid(Grid&& copy);
97
98
102 Grid& operator= (const Grid& right);
103
104
108 Grid& operator= (Grid&& right);
109
110
120
121
132 void setSize(const Layout2d& size) override;
133 using Widget::setSize;
134
135
146 void setAutoSize(bool autoSize);
147
148
156 bool getAutoSize() const;
157
158
164 bool remove(const Widget::Ptr& widget) override;
165
166
171 void removeAllWidgets() override;
172
173
184 void addWidget(const Widget::Ptr& widget,
185 std::size_t row,
186 std::size_t column,
187 Alignment alignment = Alignment::Center,
188 const Padding& padding = Padding{0});
189
190
200 Widget::Ptr getWidget(std::size_t row, std::size_t column) const;
201
202
210 std::map<Widget::Ptr, std::pair<std::size_t, std::size_t>> getWidgetLocations() const;
211
212
220 void setWidgetPadding(const Widget::Ptr& widget, const Padding& padding = Padding(0, 0, 0, 0));
221
222
231 void setWidgetPadding(std::size_t row, std::size_t column, const Padding& padding = Padding(0, 0, 0, 0));
232
233
243
244
254 Padding getWidgetPadding(std::size_t row, std::size_t column) const;
255
256
264 void setWidgetAlignment(const Widget::Ptr& widget, Alignment alignment = Alignment::Center);
265
266
275 void setWidgetAlignment(std::size_t row, std::size_t column, Alignment alignment = Alignment::Center);
276
277
287
288
298 Alignment getWidgetAlignment(std::size_t row, std::size_t column) const;
299
300
307 const std::vector<std::vector<Widget::Ptr>>& getGridWidgets() const;
308
309
316 bool isMouseOnWidget(Vector2f pos) const override;
317
318
320 protected:
321
325 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
326
327
331 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
332
333
335 // Returns the minimum size required by the grid to display correctly all widgets.
337 Vector2f getMinimumSize() const;
338
339
341 // Reposition all the widgets.
343 void updatePositionsOfAllWidgets();
344
345
347 // Updates the position and size of the widget
349 void updateWidgets();
350
351
353 // Makes a copy of the widget
355 Widget::Ptr clone() const override;
356
357
359 protected:
360
361 bool m_autoSize = true;
362
363 std::vector<std::vector<Widget::Ptr>> m_gridWidgets;
364 std::vector<std::vector<Padding>> m_objPadding;
365 std::vector<std::vector<Alignment>> m_objAlignment;
366
367 std::vector<float> m_rowHeight;
368 std::vector<float> m_columnWidth;
369
370 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
371
373 };
374
376}
377
379
380#endif // TGUI_GRID_HPP
Container widget.
Definition: Container.hpp:47
Definition: Grid.hpp:40
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.
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:55
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:43
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:44
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:284
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