TGUI 1.11
Loading...
Searching...
No Matches
Grid.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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#ifndef TGUI_GRID_HPP
26#define TGUI_GRID_HPP
27
28#include <TGUI/Container.hpp>
29
30#include <unordered_map>
31
33
34namespace tgui
35{
37
38 class TGUI_API Grid : public Container
39 {
40 public:
41
42 using Ptr = std::shared_ptr<Grid>;
43 using ConstPtr = std::shared_ptr<const Grid>;
44
45 static constexpr const char StaticWidgetType[] = "Grid";
46
65
73 Grid(const char* typeName = StaticWidgetType, bool initRenderer = true);
74
80 TGUI_NODISCARD static Grid::Ptr create();
81
85 Grid(const Grid& copy);
86
90 Grid(Grid&& copy) noexcept;
91
95 Grid& operator= (const Grid& right);
96
100 Grid& operator= (Grid&& right) noexcept;
101
109 TGUI_NODISCARD static Grid::Ptr copy(const Grid::ConstPtr& grid);
110
121 void setSize(const Layout2d& size) override;
122 using Widget::setSize;
123
134 void setAutoSize(bool autoSize);
135
143 TGUI_NODISCARD bool getAutoSize() const;
144
150 bool remove(const Widget::Ptr& widget) override;
151
155 void removeAllWidgets() override;
156
166 void addWidget(const Widget::Ptr& widget,
167 std::size_t row,
168 std::size_t column,
169 Alignment alignment = Alignment::Center,
170 const Padding& padding = Padding{0});
171
183 bool setWidgetCell(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
197 TGUI_NODISCARD Widget::Ptr getWidget(std::size_t row, std::size_t column) const;
198
205 TGUI_NODISCARD const std::unordered_map<Widget::Ptr, std::pair<std::size_t, std::size_t>>& getWidgetLocations() const;
206
213 void setWidgetPadding(const Widget::Ptr& widget, const Padding& padding = {});
214
222 void setWidgetPadding(std::size_t row, std::size_t column, const Padding& padding = {});
223
231 TGUI_NODISCARD Padding getWidgetPadding(const Widget::Ptr& widget) const;
232
241 TGUI_NODISCARD Padding getWidgetPadding(std::size_t row, std::size_t column) const;
242
250
258 void setWidgetAlignment(std::size_t row, std::size_t column, Alignment alignment = Alignment::Center);
259
267 TGUI_NODISCARD Alignment getWidgetAlignment(const Widget::Ptr& widget) const;
268
277 TGUI_NODISCARD Alignment getWidgetAlignment(std::size_t row, std::size_t column) const;
278
284 TGUI_NODISCARD const std::vector<std::vector<Widget::Ptr>>& getGridWidgets() const;
285
291 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
292
294 protected:
295
299 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
300
304 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
305
307 // Returns the minimum size required by the grid to display correctly all widgets.
309 TGUI_NODISCARD Vector2f getMinimumSize() const;
310
312 // Reposition all the widgets.
314 void updatePositionsOfAllWidgets();
315
317 // Updates the position and size of the widget
319 void updateWidgets();
320
322 // Updates the grid to the correct size when auto-sizing. This function should only be called if m_autoSize is true.
324 void recalculateAutoSize();
325
327 // Makes a copy of the widget
329 TGUI_NODISCARD Widget::Ptr clone() const override;
330
332 protected:
333
334 bool m_autoSize = true;
335
336 std::vector<std::vector<Widget::Ptr>> m_gridWidgets;
337 std::vector<std::vector<Padding>> m_objPadding;
338 std::vector<std::vector<Alignment>> m_objAlignment;
339
340 std::vector<float> m_rowHeight;
341 std::vector<float> m_columnWidth;
342
343 std::unordered_map<Widget::Ptr, std::pair<std::size_t, std::size_t>> m_widgetCells;
344
345 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
346
348 };
349
351}
352
354
355#endif // TGUI_GRID_HPP
Definition Grid.hpp:39
bool setWidgetCell(const Widget::Ptr &widget, std::size_t row, std::size_t column, Alignment alignment=Alignment::Center, const Padding &padding=Padding{0})
Chooses the row and column in which a widget should be placed.
const std::unordered_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 removeAllWidgets() override
Removes all widgets that were added to the container.
Grid(const Grid &copy)
Copy constructor.
std::shared_ptr< const Grid > ConstPtr
Shared constant widget pointer.
Definition Grid.hpp:43
Widget::Ptr getWidget(std::size_t row, std::size_t column) const
Returns the widget in a specific cell of the 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.
static constexpr const char StaticWidgetType[]
Type name of the widget.
Definition Grid.hpp:45
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
std::shared_ptr< Grid > Ptr
Shared widget pointer.
Definition Grid.hpp:42
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.
void setWidgetPadding(const Widget::Ptr &widget, const Padding &padding={})
Changes padding of a given widget.
Alignment
The alignment of the widget in its cell.
Definition Grid.hpp:54
@ Up
Draw the widget at the upper side of the cell (horizontally centered)
Definition Grid.hpp:57
@ Bottom
Draw the widget at the bottom of the cell (horizontally centered)
Definition Grid.hpp:61
@ Center
Center the widget in the cell.
Definition Grid.hpp:55
@ UpperLeft
Draw the widget in the upper left corner of the cell.
Definition Grid.hpp:56
@ UpperRight
Draw the widget in the upper right corner of the cell.
Definition Grid.hpp:58
@ BottomRight
Draw the widget in the bottom right corner of the cell.
Definition Grid.hpp:60
@ Right
Draw the widget at the right side of the cell (vertically centered)
Definition Grid.hpp:59
@ Left
Draw the widget at the left side of the cell (vertically centered)
Definition Grid.hpp:63
@ BottomLeft
Draw the widget in the bottom left corner of the cell.
Definition Grid.hpp:62
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.
Grid(Grid &&copy) noexcept
Move constructor.
static Grid::Ptr create()
Creates a new grid widget.
void setWidgetPadding(std::size_t row, std::size_t column, const Padding &padding={})
Changes padding of a widget in a certain cell.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
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.
const std::vector< std::vector< Widget::Ptr > > & getGridWidgets() const
Returns the widgets and their positions in the grid.
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.
static Grid::Ptr copy(const Grid::ConstPtr &grid)
Makes a copy of another grid.
Class to store the position or size of a widget.
Definition Layout.hpp:321
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36