TinyMaterialDesign
Loading...
Searching...
No Matches
LinearLayout.h
Go to the documentation of this file.
1#pragma once
2#include "TinyMaterialDesign/Core/Bounds.h"
3
4namespace tinymd {
5
7
8/**
9 * @brief Computes equal- or weighted-split rects along one axis of a container.
10 *
11 * Not a widget - see GridLayout.h for the rationale. Use it before
12 * constructing/positioning widgets:
13 *
14 * LinearLayout row(Bounds(16, 56, 308, 48), LayoutAxis::Horizontal);
15 * button1.bounds = row.itemRect(0, 3);
16 * button2.bounds = row.itemRect(1, 3);
17 * button3.bounds = row.itemRect(2, 3);
18 *
19 * By default all `count` items split the container equally (minus spacing
20 * gutters). Pass per-item weights via itemRect(index, weights, count) to give
21 * some items more of the remaining space than others (weights are relative,
22 * not required to sum to 1).
23 */
25 public:
26 LinearLayout(Bounds container, LayoutAxis axis, int32_t spacing = 8)
27 : container_(container), axis_(axis), spacing_(spacing) {}
28
29 /// Bounds for the `index`-th of `count` equally-sized items (0-based).
30 Bounds itemRect(int index, int count) const {
31 if (count <= 0) count = 1;
32 const int32_t total = axis_ == LayoutAxis::Horizontal ? container_.w : container_.h;
33 const int32_t extent = (total - (count - 1) * spacing_) / count;
34 return rectAt(index * (extent + spacing_), extent);
35 }
36
37 /// Bounds for the `index`-th of `count` items, sized proportionally to
38 /// `weights[index]` relative to the sum of `weights[0..count)`.
39 Bounds itemRect(int index, const float* weights, int count) const {
40 if (count <= 0) return Bounds(container_.x, container_.y, 0, 0);
41 float sum = 0;
42 for (int i = 0; i < count; ++i) sum += weights[i];
43 if (sum <= 0) return itemRect(index, count);
44
45 const int32_t total = axis_ == LayoutAxis::Horizontal ? container_.w : container_.h;
46 const int32_t available = total - (count - 1) * spacing_;
47
48 int32_t offset = 0;
49 for (int i = 0; i < index; ++i) offset += static_cast<int32_t>(available * (weights[i] / sum)) + spacing_;
50 const int32_t extent = static_cast<int32_t>(available * (weights[index] / sum));
51 return rectAt(offset, extent);
52 }
53
54 private:
55 Bounds rectAt(int32_t offset, int32_t extent) const {
56 if (axis_ == LayoutAxis::Horizontal) {
57 return Bounds(container_.x + offset, container_.y, extent, container_.h);
58 }
59 return Bounds(container_.x, container_.y + offset, container_.w, extent);
60 }
61
62 Bounds container_;
63 LayoutAxis axis_;
64 int32_t spacing_;
65};
66
67} // namespace tinymd
Computes equal- or weighted-split rects along one axis of a container.
Definition: LinearLayout.h:24
Bounds itemRect(int index, const float *weights, int count) const
Definition: LinearLayout.h:39
Bounds itemRect(int index, int count) const
Bounds for the index-th of count equally-sized items (0-based).
Definition: LinearLayout.h:30
LinearLayout(Bounds container, LayoutAxis axis, int32_t spacing=8)
Definition: LinearLayout.h:26
Definition: AnchorLayout.h:4
LayoutAxis
Definition: LinearLayout.h:6
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 h
Definition: Bounds.h:25
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22