TGUI  1.6.1
Loading...
Searching...
No Matches
Grid.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
31 #include <unordered_map>
32#endif
33
35
36TGUI_MODULE_EXPORT namespace tgui
37{
39
40 class TGUI_API Grid : public Container
41 {
42 public:
43
44 using Ptr = std::shared_ptr<Grid>;
45 using ConstPtr = std::shared_ptr<const Grid>;
46
47 static constexpr const char StaticWidgetType[] = "Grid";
48
55 enum class Alignment
56 {
57 Center,
58 UpperLeft,
59 Up,
60 UpperRight,
61 Right,
62 BottomRight,
63 Bottom,
64 BottomLeft,
65 Left
66 };
67
75 Grid(const char* typeName = StaticWidgetType, bool initRenderer = true);
76
82 TGUI_NODISCARD static Grid::Ptr create();
83
87 Grid(const Grid& copy);
88
92 Grid(Grid&& copy) noexcept;
93
97 Grid& operator= (const Grid& right);
98
102 Grid& operator= (Grid&& right) noexcept;
103
111 TGUI_NODISCARD static Grid::Ptr copy(const Grid::ConstPtr& grid);
112
123 void setSize(const Layout2d& size) override;
124 using Widget::setSize;
125
136 void setAutoSize(bool autoSize);
137
145 TGUI_NODISCARD bool getAutoSize() const;
146
152 bool remove(const Widget::Ptr& widget) override;
153
157 void removeAllWidgets() override;
158
168 void addWidget(const Widget::Ptr& widget,
169 std::size_t row,
170 std::size_t column,
171 Alignment alignment = Alignment::Center,
172 const Padding& padding = Padding{0});
173
185 bool setWidgetCell(const Widget::Ptr& widget,
186 std::size_t row,
187 std::size_t column,
188 Alignment alignment = Alignment::Center,
189 const Padding& padding = Padding{0});
190
199 TGUI_NODISCARD Widget::Ptr getWidget(std::size_t row, std::size_t column) const;
200
207 TGUI_NODISCARD const std::unordered_map<Widget::Ptr, std::pair<std::size_t, std::size_t>>& getWidgetLocations() const;
208
215 void setWidgetPadding(const Widget::Ptr& widget, const Padding& padding = {});
216
224 void setWidgetPadding(std::size_t row, std::size_t column, const Padding& padding = {});
225
233 TGUI_NODISCARD Padding getWidgetPadding(const Widget::Ptr& widget) const;
234
243 TGUI_NODISCARD Padding getWidgetPadding(std::size_t row, std::size_t column) const;
244
251 void setWidgetAlignment(const Widget::Ptr& widget, Alignment alignment = Alignment::Center);
252
260 void setWidgetAlignment(std::size_t row, std::size_t column, Alignment alignment = Alignment::Center);
261
269 TGUI_NODISCARD Alignment getWidgetAlignment(const Widget::Ptr& widget) const;
270
279 TGUI_NODISCARD Alignment getWidgetAlignment(std::size_t row, std::size_t column) const;
280
286 TGUI_NODISCARD const std::vector<std::vector<Widget::Ptr>>& getGridWidgets() const;
287
293 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
294
296 protected:
297
301 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
302
306 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
307
309 // Returns the minimum size required by the grid to display correctly all widgets.
311 TGUI_NODISCARD Vector2f getMinimumSize() const;
312
314 // Reposition all the widgets.
316 void updatePositionsOfAllWidgets();
317
319 // Updates the position and size of the widget
321 void updateWidgets();
322
324 // Updates the grid to the correct size when auto-sizing. This function should only be called if m_autoSize is true.
326 void recalculateAutoSize();
327
329 // Makes a copy of the widget
331 TGUI_NODISCARD Widget::Ptr clone() const override;
332
334 protected:
335
336 bool m_autoSize = true;
337
338 std::vector<std::vector<Widget::Ptr>> m_gridWidgets;
339 std::vector<std::vector<Padding>> m_objPadding;
340 std::vector<std::vector<Alignment>> m_objAlignment;
341
342 std::vector<float> m_rowHeight;
343 std::vector<float> m_columnWidth;
344
345 std::unordered_map<Widget::Ptr, std::pair<std::size_t, std::size_t>> m_widgetCells;
346
347 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
348
350 };
351
353}
354
356
357#endif // TGUI_GRID_HPP
Container widget.
Definition Container.hpp:48
Definition Grid.hpp:41
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:45
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.
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:44
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:56
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:323
Definition Outline.hpp:38
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38