roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
alignment.h
Go to the documentation of this file.
1#pragma once
2
3#include <inttypes.h>
4
6
7namespace roo_display {
8
9/// Anchor point used for alignment.
10enum Anchor {
11 ANCHOR_ORIGIN = 0, // Point with the zero coordinate.
12 ANCHOR_MIN = 1, // Left or top.
13 ANCHOR_MID = 2, // Center or middle.
14 ANCHOR_MAX = 3 // Right or bottom.
15};
16
17namespace internal {
18
19template <typename Dim>
21 switch (anchor) {
22 case ANCHOR_MIN:
23 return first;
24 case ANCHOR_MID:
25 return (first + last) / 2;
26 case ANCHOR_MAX:
27 return last;
28 case ANCHOR_ORIGIN:
29 default:
30 return 0;
31 }
32}
33
34class AlignBase {
35 public:
36 explicit constexpr AlignBase() : AlignBase(ANCHOR_ORIGIN, ANCHOR_ORIGIN, 0) {}
37
38 explicit constexpr AlignBase(Anchor dst, Anchor src, int16_t shift)
39 : rep_((uint16_t)(shift + (1 << 11)) << 4 | src << 2 | dst) {}
40
41 constexpr Anchor src() const { return (Anchor)((rep_ >> 2) & 3); }
42 constexpr Anchor dst() const { return (Anchor)(rep_ & 3); }
43
44 constexpr int16_t shift() const { return (int16_t)((rep_ >> 4) - (1 << 11)); }
45
46 template <typename Dim>
52
53 bool operator==(AlignBase other) const { return rep_ == other.rep_; }
54
55 bool operator!=(AlignBase other) const { return rep_ != other.rep_; }
56
57 protected:
59};
60
61} // namespace internal
62
63/// Horizontal alignment.
64///
65/// Consists of:
66/// 1) source anchor (left/center/right/origin)
67/// 2) destination anchor (left/center/right/origin)
68/// 3) absolute offset (padding)
69///
70/// Prefer using the constants and modifying them, e.g.
71/// `kLeft.toCenter().shiftBy(5)` aligns the left boundary of the source
72/// to be 5 pixels to the right of the destination center.
74 public:
75 using AlignBase::AlignBase;
76
77 // Applies an extra horizontal shift.
78 constexpr HAlign shiftBy(int16_t shift_by) const {
79 return HAlign(dst(), src(), shift() + shift_by);
80 }
81
82 // Sets the anchor relative to the left of the destination.
83 constexpr HAlign toLeft() const { return HAlign(ANCHOR_MIN, src(), shift()); }
84
85 // Sets the anchor relative to the center of the destination.
86 constexpr HAlign toCenter() const {
87 return HAlign(ANCHOR_MID, src(), shift());
88 }
89
90 // Sets the anchor relative to the right of the destination.
91 constexpr HAlign toRight() const {
92 return HAlign(ANCHOR_MAX, src(), shift());
93 }
94
95 // Sets the anchor relative to the origin (point zero) of the destination.
96 constexpr HAlign toOrigin() const {
97 return HAlign(ANCHOR_ORIGIN, src(), shift());
98 }
99};
100
101/// Left-to-left with no shift.
102static constexpr HAlign kLeft = HAlign(ANCHOR_MIN, ANCHOR_MIN, 0);
103
104/// Center-to-center with no shift.
105static constexpr HAlign kCenter = HAlign(ANCHOR_MID, ANCHOR_MID, 0);
106
107/// Right-to-right with no shift.
108static constexpr HAlign kRight = HAlign(ANCHOR_MAX, ANCHOR_MAX, 0);
109
110/// Origin-to-origin with no shift.
112
113/// Vertical alignment.
114///
115/// Consists of:
116/// 1) source anchor (top/middle/bottom/baseline)
117/// 2) destination anchor (top/middle/bottom/baseline)
118/// 3) absolute offset (padding)
119///
120/// Prefer using the constants and modifying them, e.g.
121/// `kTop.toMiddle().shiftBy(5)` aligns the top boundary of the source
122/// to be 5 pixels below the destination middle.
124 public:
125 using AlignBase::AlignBase;
126
127 constexpr VAlign shiftBy(int16_t shift_by) const {
128 return VAlign(dst(), src(), shift() + shift_by);
129 }
130
131 // Sets the anchor relative to the top of the destination.
132 constexpr VAlign toTop() const { return VAlign(ANCHOR_MIN, src(), shift()); }
133
134 // Sets the anchor relative to the middle of the destination.
135 constexpr VAlign toMiddle() const {
136 return VAlign(ANCHOR_MID, src(), shift());
137 }
138
139 // Sets the anchor relative to the bottom of the destination.
140 constexpr VAlign toBottom() const {
141 return VAlign(ANCHOR_MAX, src(), shift());
142 }
143
144 // Sets the anchor relative to the baseline (zero coordinate) of the
145 // destination.
146 constexpr VAlign toBaseline() const {
147 return VAlign(ANCHOR_ORIGIN, src(), shift());
148 }
149};
150
151/// Top-to-top with no shift.
152static constexpr VAlign kTop = VAlign(ANCHOR_MIN, ANCHOR_MIN, 0);
153
154/// Middle-to-middle with no shift.
155static constexpr VAlign kMiddle = VAlign(ANCHOR_MID, ANCHOR_MID, 0);
156
157/// Bottom-to-bottom with no shift.
158static constexpr VAlign kBottom = VAlign(ANCHOR_MAX, ANCHOR_MAX, 0);
159
160/// Baseline-to-baseline with no shift.
162
167
168/// Combines horizontal and vertical alignment.
169///
170/// Lightweight and pass-by-value. Use `|` to compose, e.g.
171/// `kTop.shiftBy(5) | kMiddle`.
173 public:
174 constexpr Alignment() : h_(), v_() {}
175
176 constexpr Alignment(HAlign h) : h_(h), v_() {}
177
178 constexpr Alignment(VAlign v) : h_(), v_(v) {}
179
180 constexpr Alignment(HAlign h, VAlign v) : h_(h), v_(v) {}
181
182 constexpr HAlign h() const { return h_; }
183
184 constexpr VAlign v() const { return v_; }
185
186 Offset resolveOffset(const Box& outer, const Box& inner) const {
187 return Offset{.dx = h().resolveOffset(outer.xMin(), outer.xMax(),
188 inner.xMin(), inner.xMax()),
189 .dy = v().resolveOffset(outer.yMin(), outer.yMax(),
190 inner.yMin(), inner.yMax())};
191 }
192
194 return Alignment(h_.shiftBy(dx), v_.shiftBy(dy));
195 }
196
198 return h_ == other.h_ && v_ == other.v_;
199 }
200
202 return h_ != other.h_ || v_ != other.v_;
203 }
204
205 private:
206 HAlign h_;
207 VAlign v_;
208};
209
210/// Absolute alignment (no repositioning).
211static constexpr Alignment kNoAlign = Alignment();
212
213/// Combine horizontal and vertical alignments.
214inline constexpr Alignment operator|(HAlign h, VAlign v) {
215 return Alignment(h, v);
216}
217
218/// Combine vertical and horizontal alignments.
219inline constexpr Alignment operator|(VAlign v, HAlign h) {
220 return Alignment(h, v);
221}
222
223} // namespace roo_display
Combines horizontal and vertical alignment.
Definition alignment.h:172
constexpr Alignment(HAlign h, VAlign v)
Definition alignment.h:180
constexpr HAlign h() const
Definition alignment.h:182
Offset resolveOffset(const Box &outer, const Box &inner) const
Definition alignment.h:186
constexpr Alignment(VAlign v)
Definition alignment.h:178
constexpr Alignment()
Definition alignment.h:174
bool operator!=(Alignment other) const
Definition alignment.h:201
Alignment shiftBy(int16_t dx, int16_t dy)
Definition alignment.h:193
constexpr VAlign v() const
Definition alignment.h:184
constexpr Alignment(HAlign h)
Definition alignment.h:176
bool operator==(Alignment other) const
Definition alignment.h:197
Axis-aligned integer rectangle.
Definition box.h:12
Horizontal alignment.
Definition alignment.h:73
constexpr HAlign toCenter() const
Definition alignment.h:86
constexpr HAlign toLeft() const
Definition alignment.h:83
constexpr HAlign shiftBy(int16_t shift_by) const
Definition alignment.h:78
constexpr HAlign toRight() const
Definition alignment.h:91
constexpr HAlign toOrigin() const
Definition alignment.h:96
Vertical alignment.
Definition alignment.h:123
constexpr VAlign shiftBy(int16_t shift_by) const
Definition alignment.h:127
constexpr VAlign toBottom() const
Definition alignment.h:140
constexpr VAlign toMiddle() const
Definition alignment.h:135
constexpr VAlign toBaseline() const
Definition alignment.h:146
constexpr VAlign toTop() const
Definition alignment.h:132
Dim resolveOffset(Dim first_outer, Dim last_outer, Dim first_inner, Dim last_inner) const
Definition alignment.h:47
bool operator!=(AlignBase other) const
Definition alignment.h:55
constexpr int16_t shift() const
Definition alignment.h:44
constexpr AlignBase(Anchor dst, Anchor src, int16_t shift)
Definition alignment.h:38
constexpr Anchor dst() const
Definition alignment.h:42
constexpr Anchor src() const
Definition alignment.h:41
bool operator==(AlignBase other) const
Definition alignment.h:53
Dim resolveAnchor(Anchor anchor, Dim first, Dim last)
Definition alignment.h:20
Defines 140 opaque HTML named colors.
static constexpr HAlign kCenter
Center-to-center with no shift.
Definition alignment.h:105
static constexpr HAlign kLeft
Left-to-left with no shift.
Definition alignment.h:102
static constexpr Alignment kNoAlign
Absolute alignment (no repositioning).
Definition alignment.h:211
static constexpr HAlign kRight
Right-to-right with no shift.
Definition alignment.h:108
static constexpr VAlign kMiddle
Middle-to-middle with no shift.
Definition alignment.h:155
Anchor
Anchor point used for alignment.
Definition alignment.h:10
static constexpr VAlign kBaseline
Baseline-to-baseline with no shift.
Definition alignment.h:161
constexpr Alignment operator|(HAlign h, VAlign v)
Combine horizontal and vertical alignments.
Definition alignment.h:214
static constexpr VAlign kBottom
Bottom-to-bottom with no shift.
Definition alignment.h:158
static constexpr VAlign kTop
Top-to-top with no shift.
Definition alignment.h:152
static constexpr HAlign kOrigin
Origin-to-origin with no shift.
Definition alignment.h:111