TinyMaterialDesign
Loading...
Searching...
No Matches
StackLayout.h
Go to the documentation of this file.
1#pragma once
2#include "TinyMaterialDesign/Core/Bounds.h"
3
4namespace tinymd {
5
6/**
7 * @brief Two unrelated single-rect placement helpers bundled under one name -
8 * not "arrange these into a row/column" (that's LinearLayout), but "place
9 * one rect relative to another rect it sits on top of or overlaps":
10 *
11 * - centered(w, h): centers a rect inside the container - e.g. an icon
12 * centered inside a button.
13 * - offset(index, w, h): places the `index`-th of a series of same-size
14 * rects, each shifted `overlapStep` pixels from the previous one,
15 * starting at the container's (x, y) origin - e.g. a row of overlapping
16 * avatar chips. Only that origin is used - the container's width/height
17 * are never read, so pass whatever Bounds is convenient (even 0x0) as
18 * long as its x/y is where the series should start.
19 *
20 * Not a widget - see GridLayout.h for the rationale. A single StackLayout
21 * is normally used for just one of the two, not both.
22 *
23 * StackLayout stack(button.bounds);
24 * icon.bounds = stack.centered(20, 20);
25 *
26 * StackLayout avatars(Bounds(16, 16, 0, 0), 20); // 20px overlap step
27 * for (int i = 0; i < 3; ++i) avatar[i].bounds = avatars.offset(i, 32, 32);
28 */
30 public:
31 explicit StackLayout(Bounds container, int32_t overlapStep = 0)
32 : container_(container), overlapStep_(overlapStep) {}
33
34 /// A `width` x `height` rect centered within the container.
35 Bounds centered(int32_t width, int32_t height) const {
36 return Bounds(container_.centerX() - width / 2, container_.centerY() - height / 2, width, height);
37 }
38
39 /// The `index`-th rect, offset horizontally by `index * overlapStep` from
40 /// the container's origin - e.g. a row of overlapping avatars.
41 Bounds offset(int index, int32_t width, int32_t height) const {
42 return Bounds(container_.x + index * overlapStep_, container_.y, width, height);
43 }
44
45 private:
46 Bounds container_;
47 int32_t overlapStep_;
48};
49
50} // namespace tinymd
Two unrelated single-rect placement helpers bundled under one name - not "arrange these into a row/co...
Definition: StackLayout.h:29
StackLayout(Bounds container, int32_t overlapStep=0)
Definition: StackLayout.h:31
Bounds centered(int32_t width, int32_t height) const
A width x height rect centered within the container.
Definition: StackLayout.h:35
Bounds offset(int index, int32_t width, int32_t height) const
Definition: StackLayout.h:41
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t centerY() const
Definition: Bounds.h:33
int32_t centerX() const
Definition: Bounds.h:32
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22