TinyMaterialDesign
Loading...
Searching...
No Matches
AnchorLayout.h
Go to the documentation of this file.
1#pragma once
2#include "TinyMaterialDesign/Core/Bounds.h"
3
4namespace tinymd {
5
6enum class Anchor {
11 Center,
16};
17
18/**
19 * @brief Positions a single rect relative to a corner/edge of a container.
20 *
21 * Not a widget - see GridLayout.h for the rationale. Solves the "place a FAB
22 * in the bottom-right" / "badge in the top-right corner of an avatar" cases
23 * that would otherwise be hand-computed arithmetic in every sketch:
24 *
25 * fab.bounds = AnchorLayout(screenBounds, 16).rect(Anchor::BottomRight, 56, 56);
26 * badge.bounds = AnchorLayout(avatar.bounds, 0).rect(Anchor::TopRight, 16, 16);
27 */
29 public:
30 explicit AnchorLayout(Bounds container, int32_t margin = 0) : container_(container), margin_(margin) {}
31
32 /// Bounds for a `width` x `height` rect anchored at `anchor`, inset by margin.
33 Bounds rect(Anchor anchor, int32_t width, int32_t height) const {
34 int32_t x;
35 int32_t y;
36 switch (anchor) {
37 case Anchor::TopLeft:
38 x = container_.x + margin_;
39 y = container_.y + margin_;
40 break;
42 x = container_.centerX() - width / 2;
43 y = container_.y + margin_;
44 break;
46 x = container_.right() - margin_ - width;
47 y = container_.y + margin_;
48 break;
50 x = container_.x + margin_;
51 y = container_.centerY() - height / 2;
52 break;
53 case Anchor::Center:
54 x = container_.centerX() - width / 2;
55 y = container_.centerY() - height / 2;
56 break;
58 x = container_.right() - margin_ - width;
59 y = container_.centerY() - height / 2;
60 break;
62 x = container_.x + margin_;
63 y = container_.bottom() - margin_ - height;
64 break;
66 x = container_.centerX() - width / 2;
67 y = container_.bottom() - margin_ - height;
68 break;
70 default:
71 x = container_.right() - margin_ - width;
72 y = container_.bottom() - margin_ - height;
73 break;
74 }
75 return Bounds(x, y, width, height);
76 }
77
78 private:
79 Bounds container_;
80 int32_t margin_;
81};
82
83} // namespace tinymd
Positions a single rect relative to a corner/edge of a container.
Definition: AnchorLayout.h:28
AnchorLayout(Bounds container, int32_t margin=0)
Definition: AnchorLayout.h:30
Bounds rect(Anchor anchor, int32_t width, int32_t height) const
Bounds for a width x height rect anchored at anchor, inset by margin.
Definition: AnchorLayout.h:33
Definition: AnchorLayout.h:4
Anchor
Definition: AnchorLayout.h:6
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t right() const
Definition: Bounds.h:30
int32_t bottom() const
Definition: Bounds.h:31
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