1use blit::{Axis, Constraints, Context, PlaceCx, Point, Rect, Scalar, Size};
2
3use super::percent_share;
4
5pub fn place<T: Scalar>() -> Place<T> {
6 Place::new()
7}
8
9blit::builder! {
10 #[derive(Clone, Copy, Debug, PartialEq)]
12 pub struct Place<T: Scalar> {
13 new(),
14 target_anchor: Anchor = Anchor::TopLeft,
15 child_anchor: Anchor = Anchor::TopLeft,
16 x: T = T::ZERO,
17 y: T = T::ZERO,
18 #[into]
19 width: Sizing<T> = Sizing::fit(),
20 #[into]
21 height: Sizing<T> = Sizing::fit(),
22 flip: bool = false,
24 clamp: bool = false,
26 }
27}
28
29#[derive(Clone, Copy, Debug, PartialEq)]
30pub enum Sizing<T> {
31 Fit { min: T, max: T },
32 Fixed(T),
33 Percent(f32),
34}
35
36impl<T: Scalar> Sizing<T> {
37 pub const fn fit() -> Self {
38 Self::fit_range(T::ZERO, T::UNBOUNDED)
39 }
40
41 pub const fn fit_range(min: T, max: T) -> Self {
42 Self::Fit { min, max }
43 }
44
45 pub const fn fixed(size: T) -> Self {
46 Self::Fixed(size)
47 }
48
49 pub const fn percent(fraction: f32) -> Self {
50 Self::Percent(fraction)
51 }
52
53 pub const fn full() -> Self {
54 Self::percent(1.0)
55 }
56}
57
58impl<T: Scalar> From<T> for Sizing<T> {
59 fn from(size: T) -> Self {
60 Self::Fixed(size)
61 }
62}
63
64#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
65pub enum Anchor {
66 #[default]
67 TopLeft,
68 Top,
69 TopRight,
70 Left,
71 Center,
72 Right,
73 BottomLeft,
74 Bottom,
75 BottomRight,
76}
77
78impl<C: Context<Scalar = T>, T: Scalar> blit::Place<C> for Place<T> {
79 fn place(&self, cx: &mut PlaceCx<'_, C>, target: Rect<T>) -> Point<T> {
80 let range = |sizing: Sizing<T>, parent: T, target: T| match sizing {
82 Sizing::Fit { min, max } => {
83 let min = min.max(T::ZERO);
84 (min, max.max(min).min(parent).max(min))
85 }
86 Sizing::Fixed(size) => {
87 let size = size.max(T::ZERO);
88 (size, size)
89 }
90 Sizing::Percent(fraction) => {
91 let size = percent_share(target, fraction, &mut 0.0);
92 (size, size)
93 }
94 };
95 let parent = cx.parent();
96 let width = range(self.width, parent.width, target.width);
97 let height = range(self.height, parent.height, target.height);
98 let size = cx.resolve(Constraints {
99 min: Size::new(width.0, height.0),
100 max: Size::new(width.1, height.1),
101 });
102 let anchor = |anchor| match anchor {
103 Anchor::TopLeft => Point::new(0.0, 0.0),
104 Anchor::Top => Point::new(0.5, 0.0),
105 Anchor::TopRight => Point::new(1.0, 0.0),
106 Anchor::Left => Point::new(0.0, 0.5),
107 Anchor::Center => Point::new(0.5, 0.5),
108 Anchor::Right => Point::new(1.0, 0.5),
109 Anchor::BottomLeft => Point::new(0.0, 1.0),
110 Anchor::Bottom => Point::new(0.5, 1.0),
111 Anchor::BottomRight => Point::new(1.0, 1.0),
112 };
113 let target_anchor = anchor(self.target_anchor);
114 let child_anchor = anchor(self.child_anchor);
115 let screen = cx.screen();
116 let axis = |axis: Axis, offset: T, target_anchor: f32, child_anchor: f32| {
117 let (start, screen_start) = match axis {
118 Axis::Horizontal => (target.x, screen.x),
119 Axis::Vertical => (target.y, screen.y),
120 };
121 let extent = axis.extent(target.size());
122 let screen_end = screen_start.endpoint(axis.extent(screen.size()));
123 let position = |size: T, target_anchor: f32, child_anchor: f32, offset: T| {
124 let from = if target_anchor == 1.0 { extent } else { T::ZERO }
125 - if child_anchor == 1.0 { size } else { T::ZERO };
126 let to = if target_anchor == 0.0 { T::ZERO } else { extent }
127 - if child_anchor == 0.0 { T::ZERO } else { size };
128 start + from.lerp(to, 0.5) + offset
129 };
130 let size = axis.extent(size);
131 let overflows = |target_anchor, child_anchor, offset| {
132 let position = position(size, target_anchor, child_anchor, offset);
133 position < screen_start || position.endpoint(size) > screen_end
134 };
135 let mut result = position(size, target_anchor, child_anchor, offset);
136 if self.flip
137 && overflows(target_anchor, child_anchor, offset)
138 && !overflows(1.0 - target_anchor, 1.0 - child_anchor, T::ZERO - offset)
139 {
140 result = position(size, 1.0 - target_anchor, 1.0 - child_anchor, T::ZERO - offset);
141 }
142 if self.clamp {
143 result = result.min(screen_end - size).max(screen_start);
144 }
145 result
146 };
147 Point::new(
148 axis(Axis::Horizontal, self.x, target_anchor.x, child_anchor.x),
149 axis(Axis::Vertical, self.y, target_anchor.y, child_anchor.y),
150 )
151 }
152}