Skip to main content

eternal_ui/
geometry.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Points, sizes and rectangles, in pixels.
6
7use eternal_styler::Sides;
8
9#[derive(Clone, Copy, Debug, PartialEq, Default)]
10pub struct Point {
11    pub x: f32,
12    pub y: f32,
13}
14
15impl Point {
16    pub const ZERO: Point = Point { x: 0.0, y: 0.0 };
17
18    pub const fn new(x: f32, y: f32) -> Self {
19        Self { x, y }
20    }
21}
22
23#[derive(Clone, Copy, Debug, PartialEq, Default)]
24pub struct Size {
25    pub width: f32,
26    pub height: f32,
27}
28
29impl Size {
30    pub const ZERO: Size = Size {
31        width: 0.0,
32        height: 0.0,
33    };
34
35    pub const fn new(width: f32, height: f32) -> Self {
36        Self { width, height }
37    }
38
39    /// The extent along `axis`.
40    pub fn main(self, axis: Axis) -> f32 {
41        match axis {
42            Axis::Horizontal => self.width,
43            Axis::Vertical => self.height,
44        }
45    }
46
47    /// The extent across `axis`.
48    pub fn cross(self, axis: Axis) -> f32 {
49        match axis {
50            Axis::Horizontal => self.height,
51            Axis::Vertical => self.width,
52        }
53    }
54
55    /// A size from its extents along and across `axis`.
56    pub fn from_axis(axis: Axis, main: f32, cross: f32) -> Self {
57        match axis {
58            Axis::Horizontal => Self::new(main, cross),
59            Axis::Vertical => Self::new(cross, main),
60        }
61    }
62
63    /// Both extents clamped to be at least zero.
64    pub fn non_negative(self) -> Self {
65        Self::new(self.width.max(0.0), self.height.max(0.0))
66    }
67}
68
69#[derive(Clone, Copy, Debug, PartialEq, Default)]
70pub struct Rect {
71    pub x: f32,
72    pub y: f32,
73    pub width: f32,
74    pub height: f32,
75}
76
77impl Rect {
78    pub const ZERO: Rect = Rect {
79        x: 0.0,
80        y: 0.0,
81        width: 0.0,
82        height: 0.0,
83    };
84
85    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
86        Self {
87            x,
88            y,
89            width,
90            height,
91        }
92    }
93
94    pub fn from_origin_size(origin: Point, size: Size) -> Self {
95        Self::new(origin.x, origin.y, size.width, size.height)
96    }
97
98    pub fn origin(self) -> Point {
99        Point::new(self.x, self.y)
100    }
101
102    pub fn size(self) -> Size {
103        Size::new(self.width, self.height)
104    }
105
106    pub fn right(self) -> f32 {
107        self.x + self.width
108    }
109
110    pub fn bottom(self) -> f32 {
111        self.y + self.height
112    }
113
114    pub fn center(self) -> Point {
115        Point::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
116    }
117
118    pub fn contains(self, point: Point) -> bool {
119        point.x >= self.x && point.x < self.right() && point.y >= self.y && point.y < self.bottom()
120    }
121
122    /// The rectangle shrunk by `edges` on each side, never below zero size.
123    pub fn inset(self, edges: Sides<f32>) -> Self {
124        Self::new(
125            self.x + edges.left,
126            self.y + edges.top,
127            (self.width - edges.left - edges.right).max(0.0),
128            (self.height - edges.top - edges.bottom).max(0.0),
129        )
130    }
131
132    pub fn intersection(self, other: Rect) -> Rect {
133        let x = self.x.max(other.x);
134        let y = self.y.max(other.y);
135        let right = self.right().min(other.right());
136        let bottom = self.bottom().min(other.bottom());
137        Rect::new(x, y, (right - x).max(0.0), (bottom - y).max(0.0))
138    }
139}
140
141/// The direction a container lays its children out in.
142#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
143pub enum Axis {
144    Horizontal,
145    Vertical,
146}
147
148impl Axis {
149    pub fn cross(self) -> Axis {
150        match self {
151            Self::Horizontal => Self::Vertical,
152            Self::Vertical => Self::Horizontal,
153        }
154    }
155}
156
157/// The total of two opposite sides, along an axis.
158pub(crate) fn sides_along(sides: Sides<f32>, axis: Axis) -> f32 {
159    match axis {
160        Axis::Horizontal => sides.left + sides.right,
161        Axis::Vertical => sides.top + sides.bottom,
162    }
163}
164
165/// The leading side (left or top) along an axis.
166pub(crate) fn side_start(sides: Sides<f32>, axis: Axis) -> f32 {
167    match axis {
168        Axis::Horizontal => sides.left,
169        Axis::Vertical => sides.top,
170    }
171}
172
173#[cfg(test)]
174mod tests {
175    use super::*;
176
177    #[test]
178    fn rect_helpers() {
179        let rect = Rect::new(10.0, 20.0, 30.0, 40.0);
180        assert!(rect.contains(Point::new(10.0, 20.0)));
181        assert!(!rect.contains(Point::new(40.0, 20.0)));
182        assert_eq!(rect.center(), Point::new(25.0, 40.0));
183        assert_eq!(
184            rect.inset(Sides::new(1.0, 2.0, 3.0, 4.0)),
185            Rect::new(14.0, 21.0, 24.0, 36.0)
186        );
187        assert_eq!(
188            rect.inset(Sides::all(100.0)),
189            Rect::new(110.0, 120.0, 0.0, 0.0)
190        );
191        assert_eq!(
192            rect.intersection(Rect::new(0.0, 0.0, 20.0, 30.0)),
193            Rect::new(10.0, 20.0, 10.0, 10.0)
194        );
195    }
196
197    #[test]
198    fn axis_helpers() {
199        let size = Size::new(3.0, 7.0);
200        assert_eq!(size.main(Axis::Horizontal), 3.0);
201        assert_eq!(size.cross(Axis::Horizontal), 7.0);
202        assert_eq!(Size::from_axis(Axis::Vertical, 7.0, 3.0), size);
203        assert_eq!(
204            sides_along(Sides::new(1.0, 2.0, 3.0, 4.0), Axis::Vertical),
205            4.0
206        );
207    }
208}