TinyMaterialDesign
Loading...
Searching...
No Matches
GridLayout.h
Go to the documentation of this file.
1#pragma once
2#include "TinyMaterialDesign/Core/Bounds.h"
3
4namespace tinymd {
5
6/**
7 * @brief Computes wrapped grid cell positions for a row of equal-size cards.
8 *
9 * Not a widget - TinyMaterialDesign deliberately has no auto-layout
10 * container (widgets always get an explicit Bounds), so this is a small
11 * calculator you use before constructing/positioning widgets, the same way
12 * Dialog::actionRect()/Drawer::itemRect() suggest single-widget spots:
13 *
14 * GridLayout grid(Bounds(16, 56, 308, 400), 90, 110);
15 * for (size_t i = 0; i < cards.size(); ++i) cards[i].bounds = grid.cellRect(i);
16 *
17 * As many columns as fit `container`'s width are used; further items wrap
18 * to additional rows. Rows are not clipped to the container's height - use
19 * totalHeight() to size a scroll area or decide how many items actually fit.
20 */
22 public:
23 GridLayout(Bounds container, int32_t cellWidth, int32_t cellHeight, int32_t spacing = 8)
24 : container_(container), cellWidth_(cellWidth), cellHeight_(cellHeight), spacing_(spacing) {
25 const int32_t step = cellWidth_ + spacing_;
26 columns_ = step > 0 ? (container_.w + spacing_) / step : 1;
27 if (columns_ < 1) columns_ = 1;
28 }
29
30 /// Bounds for the `index`-th cell (0-based, row-major).
31 Bounds cellRect(int index) const {
32 const int32_t col = index % columns_;
33 const int32_t row = index / columns_;
34 const int32_t x = container_.x + col * (cellWidth_ + spacing_);
35 const int32_t y = container_.y + row * (cellHeight_ + spacing_);
36 return Bounds(x, y, cellWidth_, cellHeight_);
37 }
38
39 int columns() const { return columns_; }
40
41 /// Total height needed to lay out `count` items.
42 int32_t totalHeight(int count) const {
43 if (count <= 0) return 0;
44 const int32_t rows = (count + columns_ - 1) / columns_;
45 return rows * cellHeight_ + (rows - 1) * spacing_;
46 }
47
48 private:
49 Bounds container_;
50 int32_t cellWidth_;
51 int32_t cellHeight_;
52 int32_t spacing_;
53 int32_t columns_;
54};
55
56} // namespace tinymd
Computes wrapped grid cell positions for a row of equal-size cards.
Definition: GridLayout.h:21
int columns() const
Definition: GridLayout.h:39
GridLayout(Bounds container, int32_t cellWidth, int32_t cellHeight, int32_t spacing=8)
Definition: GridLayout.h:23
int32_t totalHeight(int count) const
Total height needed to lay out count items.
Definition: GridLayout.h:42
Bounds cellRect(int index) const
Bounds for the index-th cell (0-based, row-major).
Definition: GridLayout.h:31
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t w
Definition: Bounds.h:24
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22