emath/rect_align.rs
1use crate::{Align2, Pos2, Rect, Vec2};
2
3/// Position a child [`Rect`] relative to a parent [`Rect`].
4///
5/// The corner from [`RectAlign::child`] on the new rect will be aligned to
6/// the corner from [`RectAlign::parent`] on the original rect.
7///
8/// There are helper constants for the 12 common menu positions:
9/// ```text
10/// ┌───────────┐ ┌────────┐ ┌─────────┐
11/// │ TOP_START │ │ TOP │ │ TOP_END │
12/// └───────────┘ └────────┘ └─────────┘
13/// ┌──────────┐ ┌────────────────────────────────────┐ ┌───────────┐
14/// │LEFT_START│ │ │ │RIGHT_START│
15/// └──────────┘ │ │ └───────────┘
16/// ┌──────────┐ │ │ ┌───────────┐
17/// │ LEFT │ │ some_rect │ │ RIGHT │
18/// └──────────┘ │ │ └───────────┘
19/// ┌──────────┐ │ │ ┌───────────┐
20/// │ LEFT_END │ │ │ │ RIGHT_END │
21/// └──────────┘ └────────────────────────────────────┘ └───────────┘
22/// ┌────────────┐ ┌──────┐ ┌──────────┐
23/// │BOTTOM_START│ │BOTTOM│ │BOTTOM_END│
24/// └────────────┘ └──────┘ └──────────┘
25/// ```
26// There is no `new` function on purpose, since writing out `parent` and `child` is more
27// reasonable.
28#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
29#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
30pub struct RectAlign {
31 /// The alignment in the parent (original) rect.
32 pub parent: Align2,
33
34 /// The alignment in the child (new) rect.
35 pub child: Align2,
36}
37
38impl Default for RectAlign {
39 fn default() -> Self {
40 Self::BOTTOM_START
41 }
42}
43
44impl RectAlign {
45 /// Along the top edge, leftmost.
46 pub const TOP_START: Self = Self {
47 parent: Align2::LEFT_TOP,
48 child: Align2::LEFT_BOTTOM,
49 };
50
51 /// Along the top edge, centered.
52 pub const TOP: Self = Self {
53 parent: Align2::CENTER_TOP,
54 child: Align2::CENTER_BOTTOM,
55 };
56
57 /// Along the top edge, rightmost.
58 pub const TOP_END: Self = Self {
59 parent: Align2::RIGHT_TOP,
60 child: Align2::RIGHT_BOTTOM,
61 };
62
63 /// Along the right edge, topmost.
64 pub const RIGHT_START: Self = Self {
65 parent: Align2::RIGHT_TOP,
66 child: Align2::LEFT_TOP,
67 };
68
69 /// Along the right edge, centered.
70 pub const RIGHT: Self = Self {
71 parent: Align2::RIGHT_CENTER,
72 child: Align2::LEFT_CENTER,
73 };
74
75 /// Along the right edge, bottommost.
76 pub const RIGHT_END: Self = Self {
77 parent: Align2::RIGHT_BOTTOM,
78 child: Align2::LEFT_BOTTOM,
79 };
80
81 /// Along the bottom edge, rightmost.
82 pub const BOTTOM_END: Self = Self {
83 parent: Align2::RIGHT_BOTTOM,
84 child: Align2::RIGHT_TOP,
85 };
86
87 /// Along the bottom edge, centered.
88 pub const BOTTOM: Self = Self {
89 parent: Align2::CENTER_BOTTOM,
90 child: Align2::CENTER_TOP,
91 };
92
93 /// Along the bottom edge, leftmost.
94 pub const BOTTOM_START: Self = Self {
95 parent: Align2::LEFT_BOTTOM,
96 child: Align2::LEFT_TOP,
97 };
98
99 /// Along the left edge, bottommost.
100 pub const LEFT_END: Self = Self {
101 parent: Align2::LEFT_BOTTOM,
102 child: Align2::RIGHT_BOTTOM,
103 };
104
105 /// Along the left edge, centered.
106 pub const LEFT: Self = Self {
107 parent: Align2::LEFT_CENTER,
108 child: Align2::RIGHT_CENTER,
109 };
110
111 /// Along the left edge, topmost.
112 pub const LEFT_START: Self = Self {
113 parent: Align2::LEFT_TOP,
114 child: Align2::RIGHT_TOP,
115 };
116
117 /// The 12 most common menu positions as an array, for use with [`RectAlign::find_best_align`].
118 pub const MENU_ALIGNS: [Self; 12] = [
119 Self::BOTTOM_START,
120 Self::BOTTOM_END,
121 Self::TOP_START,
122 Self::TOP_END,
123 Self::RIGHT_END,
124 Self::RIGHT_START,
125 Self::LEFT_END,
126 Self::LEFT_START,
127 // These come last on purpose, we prefer the corner ones
128 Self::TOP,
129 Self::RIGHT,
130 Self::BOTTOM,
131 Self::LEFT,
132 ];
133
134 /// Align in the parent rect.
135 pub fn parent(&self) -> Align2 {
136 self.parent
137 }
138
139 /// Align in the child rect.
140 pub fn child(&self) -> Align2 {
141 self.child
142 }
143
144 /// Convert an [`Align2`] to an [`RectAlign`], positioning the child rect inside the parent.
145 pub fn from_align2(align: Align2) -> Self {
146 Self {
147 parent: align,
148 child: align,
149 }
150 }
151
152 /// The center of the child rect will be aligned to a corner of the parent rect.
153 pub fn over_corner(align: Align2) -> Self {
154 Self {
155 parent: align,
156 child: Align2::CENTER_CENTER,
157 }
158 }
159
160 /// Position the child rect outside the parent rect.
161 pub fn outside(align: Align2) -> Self {
162 Self {
163 parent: align,
164 child: align.flip(),
165 }
166 }
167
168 /// Calculate the child rect based on a size and some optional gap.
169 pub fn align_rect(&self, parent_rect: &Rect, size: Vec2, gap: f32) -> Rect {
170 let (pivot, anchor) = self.pivot_pos(parent_rect, gap);
171 pivot.anchor_size(anchor, size)
172 }
173
174 /// Returns a [`Align2`] and a [`Pos2`] that you can e.g. use with `Area::fixed_pos`
175 /// and `Area::pivot` to align an `Area` to some rect.
176 pub fn pivot_pos(&self, parent_rect: &Rect, gap: f32) -> (Align2, Pos2) {
177 (self.child(), self.anchor(parent_rect, gap))
178 }
179
180 /// Returns a sign vector (-1, 0 or 1 in each direction) that can be used as an offset to the
181 /// child rect, creating a gap between the rects while keeping the edges aligned.
182 pub fn gap_vector(&self) -> Vec2 {
183 let mut gap = -self.child.to_sign();
184
185 // Align the edges in these cases
186 match *self {
187 Self::TOP_START | Self::TOP_END | Self::BOTTOM_START | Self::BOTTOM_END => {
188 gap.x = 0.0;
189 }
190 Self::LEFT_START | Self::LEFT_END | Self::RIGHT_START | Self::RIGHT_END => {
191 gap.y = 0.0;
192 }
193 _ => {}
194 }
195
196 gap
197 }
198
199 /// Calculator the anchor point for the child rect, based on the parent rect and an optional gap.
200 pub fn anchor(&self, parent_rect: &Rect, gap: f32) -> Pos2 {
201 let pos = self.parent.pos_in_rect(parent_rect);
202
203 let offset = self.gap_vector() * gap;
204
205 pos + offset
206 }
207
208 /// Flip the alignment on the x-axis.
209 pub fn flip_x(self) -> Self {
210 Self {
211 parent: self.parent.flip_x(),
212 child: self.child.flip_x(),
213 }
214 }
215
216 /// Flip the alignment on the y-axis.
217 pub fn flip_y(self) -> Self {
218 Self {
219 parent: self.parent.flip_y(),
220 child: self.child.flip_y(),
221 }
222 }
223
224 /// Flip the alignment on both axes.
225 pub fn flip(self) -> Self {
226 Self {
227 parent: self.parent.flip(),
228 child: self.child.flip(),
229 }
230 }
231
232 /// Returns the 3 alternative [`RectAlign`]s that are flipped in various ways, for use
233 /// with [`RectAlign::find_best_align`].
234 pub fn symmetries(self) -> [Self; 3] {
235 [self.flip_x(), self.flip_y(), self.flip()]
236 }
237
238 /// Look for the first alternative [`RectAlign`] that allows the child rect to fit
239 /// inside the `screen_rect`.
240 ///
241 /// If no alternative fits, the first is returned.
242 /// If no alternatives are given, `None` is returned.
243 ///
244 /// See also:
245 /// - [`RectAlign::symmetries`] to calculate alternatives
246 /// - [`RectAlign::MENU_ALIGNS`] for the 12 common menu positions
247 pub fn find_best_align(
248 values_to_try: impl Iterator<Item = Self>,
249 screen_rect: Rect,
250 parent_rect: Rect,
251 gap: f32,
252 expected_size: Vec2,
253 ) -> Option<Self> {
254 let mut first_choice = None;
255
256 for align in values_to_try {
257 first_choice = first_choice.or(Some(align)); // Remember the first alternative
258
259 let suggested_popup_rect = align.align_rect(&parent_rect, expected_size, gap);
260
261 if screen_rect.contains_rect(suggested_popup_rect) {
262 return Some(align);
263 }
264 }
265
266 first_choice
267 }
268}