TGUI  1.3-dev
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
26#ifndef TGUI_GRID_HPP
27#define TGUI_GRID_HPP
28
29
30#include <TGUI/Container.hpp>
31
32#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <unordered_map>
34#endif
35
37
38TGUI_MODULE_EXPORT namespace tgui
39{
41
42 class TGUI_API Grid : public Container
43 {
44 public:
45
46 using Ptr = std::shared_ptr<Grid>;
47 using ConstPtr = std::shared_ptr<const Grid>;
48
49 static constexpr const char StaticWidgetType[] = "Grid";
50
51
59 enum class Alignment
60 {
61 Center,
62 UpperLeft,
63 Up,
64 UpperRight,
65 Right,
66 BottomRight,
67 Bottom,
68 BottomLeft,
69 Left
70 };
71
72
80 Grid(const char* typeName = StaticWidgetType, bool initRenderer = true);
81
82
89 TGUI_NODISCARD static Grid::Ptr create();
90
91
95 Grid(const Grid& copy);
96
97
101 Grid(Grid&& copy) noexcept;
102
103
107 Grid& operator= (const Grid& right);
108
109
113 Grid& operator= (Grid&& right) noexcept;
114
115
124 TGUI_NODISCARD static Grid::Ptr copy(const Grid::ConstPtr& grid);
125
126
137 void setSize(const Layout2d& size) override;
138 using Widget::setSize;
139
140
151 void setAutoSize(bool autoSize);
152
153
161 TGUI_NODISCARD bool getAutoSize() const;
162
163
169 bool remove(const Widget::Ptr& widget) override;
170
171
176 void removeAllWidgets() override;
177
178
188 void addWidget(const Widget::Ptr& widget,
189 std::size_t row,
190 std::size_t column,
191 Alignment alignment = Alignment::Center,
192 const Padding& padding = Padding{0});
193
194
206 bool setWidgetCell(const Widget::Ptr& widget,
207 std::size_t row,
208 std::size_t column,
209 Alignment alignment = Alignment::Center,
210 const Padding& padding = Padding{0});
211
212
222 TGUI_NODISCARD Widget::Ptr getWidget(std::size_t row, std::size_t column) const;
223
224
232 TGUI_NODISCARD const std::unordered_map<Widget::Ptr, std::pair<std::size_t, std::size_t>>& getWidgetLocations() const;
233
234
242 void setWidgetPadding(const Widget::Ptr& widget, const Padding& padding = Padding(0, 0, 0, 0));
243
244
253 void setWidgetPadding(std::size_t row, std::size_t column, const Padding& padding = Padding(0, 0, 0, 0));
254
255
264 TGUI_NODISCARD Padding getWidgetPadding(const Widget::Ptr& widget) const;
265
266
276 TGUI_NODISCARD Padding getWidgetPadding(std::size_t row, std::size_t column) const;
277
278
286 void setWidgetAlignment(const Widget::Ptr& widget, Alignment alignment = Alignment::Center);
287
288
297 void setWidgetAlignment(std::size_t row, std::size_t column, Alignment alignment = Alignment::Center);
298
299
308 TGUI_NODISCARD Alignment getWidgetAlignment(const Widget::Ptr& widget) const;
309
310
320 TGUI_NODISCARD Alignment getWidgetAlignment(std::size_t row, std::size_t column) const;
321
322
329 TGUI_NODISCARD const std::vector<std::vector<Widget::Ptr>>& getGridWidgets() const;
330
331
338 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
339
340
342 protected:
343
347 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
348
349
353 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
354
355
357 // Returns the minimum size required by the grid to display correctly all widgets.
359 TGUI_NODISCARD Vector2f getMinimumSize() const;
360
361
363 // Reposition all the widgets.
365 void updatePositionsOfAllWidgets();
366
367
369 // Updates the position and size of the widget
371 void updateWidgets();
372
373
375 // Makes a copy of the widget
377 TGUI_NODISCARD Widget::Ptr clone() const override;
378
379
381 protected:
382
383 bool m_autoSize = true;
384
385 std::vector<std::vector<Widget::Ptr>> m_gridWidgets;
386 std::vector<std::vector<Padding>> m_objPadding;
387 std::vector<std::vector<Alignment>> m_objAlignment;
388
389 std::vector<float> m_rowHeight;
390 std::vector<float> m_columnWidth;
391
392 std::unordered_map<Widget::Ptr, std::pair<std::size_t, std::size_t>> m_widgetCells;
393
394 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
395
397 };
398
400}
401
403
404#endif // TGUI_GRID_HPP
Container widget.
Definition Container.hpp:49
Definition Grid.hpp:43
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.
void removeAllWidgets() override
Removes all widgets that were added to the container.
TGUI_NODISCARD Padding getWidgetPadding(const Widget::Ptr &widget) const
Returns the padding around a widget.
Grid(const Grid &copy)
Copy constructor.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
TGUI_NODISCARD Alignment getWidgetAlignment(std::size_t row, std::size_t column) const
Returns the alignment of a given widget in its cell.
std::shared_ptr< const Grid > ConstPtr
Shared constant widget pointer.
Definition Grid.hpp:47
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.
std::shared_ptr< Grid > Ptr
Shared widget pointer.
Definition Grid.hpp:46
TGUI_NODISCARD 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.
TGUI_NODISCARD Padding getWidgetPadding(std::size_t row, std::size_t column) const
Returns the padding around a widget in a specific cell of the grid.
static TGUI_NODISCARD Grid::Ptr copy(const Grid::ConstPtr &grid)
Makes a copy of another grid.
Alignment
The alignment of the widget in its cell.
Definition Grid.hpp:60
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
static TGUI_NODISCARD Grid::Ptr create()
Creates a new grid widget.
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.
TGUI_NODISCARD bool getAutoSize() const
Returns whether the grid is auto-sized or not.
void setWidgetPadding(const Widget::Ptr &widget, const Padding &padding=Padding(0, 0, 0, 0))
Changes padding of a given 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.
TGUI_NODISCARD Widget::Ptr getWidget(std::size_t row, std::size_t column) const
Returns the widget in a specific cell of the grid.
TGUI_NODISCARD const std::vector< std::vector< Widget::Ptr > > & getGridWidgets() const
Returns the widgets and their positions in the grid.
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.
TGUI_NODISCARD Alignment getWidgetAlignment(const Widget::Ptr &widget) const
Returns the alignment of a given widget.
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.
TGUI_NODISCARD 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.
Class to store the position or size of a widget.
Definition Layout.hpp:305
Definition Outline.hpp:39
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39