b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 1 | #[derive(Clone, Copy, Debug, PartialEq, Default)] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 2 | pub struct Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 3 | pub x: f64, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 4 | pub y: f64, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 5 | pub w: f64, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 6 | pub h: f64, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 7 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 8 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 9 | pub(crate) const TOL: f64 = 0.05; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 10 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 11 | impl Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 12 | pub const fn new(x: f64, y: f64, w: f64, h: f64) -> Self { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 13 | Self { x, y, w, h } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 14 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 15 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 16 | pub fn right(&self) -> f64 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 17 | self.x + self.w |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 18 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 19 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 20 | pub fn bottom(&self) -> f64 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 21 | self.y + self.h |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 22 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 23 | |
b22c0511d5af | atagen | 2026-10-17 03:48 UTC | 24 | /// Whether the point lies inside, counting the left and top edges only. |
b22c0511d5af | atagen | 2026-10-17 03:48 UTC | 25 | pub fn contains(&self, x: f64, y: f64) -> bool { |
b22c0511d5af | atagen | 2026-10-17 03:48 UTC | 26 | x >= self.x && y >= self.y && x < self.right() && y < self.bottom() |
b22c0511d5af | atagen | 2026-10-17 03:48 UTC | 27 | } |
b22c0511d5af | atagen | 2026-10-17 03:48 UTC | 28 | |
9e5c356ebe9e | atagen | 2026-10-17 05:10 UTC | 29 | pub fn union(&self, other: &Rect) -> Rect { |
9e5c356ebe9e | atagen | 2026-10-17 05:10 UTC | 30 | let (x, y) = (self.x.min(other.x), self.y.min(other.y)); |
9e5c356ebe9e | atagen | 2026-10-17 05:10 UTC | 31 | let r = self.right().max(other.right()); |
9e5c356ebe9e | atagen | 2026-10-17 05:10 UTC | 32 | let b = self.bottom().max(other.bottom()); |
9e5c356ebe9e | atagen | 2026-10-17 05:10 UTC | 33 | Rect::new(x, y, r - x, b - y) |
9e5c356ebe9e | atagen | 2026-10-17 05:10 UTC | 34 | } |
9e5c356ebe9e | atagen | 2026-10-17 05:10 UTC | 35 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 36 | pub fn center_x(&self) -> f64 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 37 | self.x + self.w / 2.0 |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 38 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 39 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 40 | pub fn center_y(&self) -> f64 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 41 | self.y + self.h / 2.0 |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 42 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 43 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 44 | pub fn lerp(&self, other: &Rect, s: f64) -> Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 45 | Rect::new( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 46 | self.x + (other.x - self.x) * s, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 47 | self.y + (other.y - self.y) * s, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 48 | self.w + (other.w - self.w) * s, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 49 | self.h + (other.h - self.h) * s, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 50 | ) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 51 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 52 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 53 | pub(crate) fn same_x(&self, o: &Rect) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 54 | (self.x - o.x).abs() < TOL && (self.w - o.w).abs() < TOL |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 55 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 56 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 57 | pub(crate) fn same_y(&self, o: &Rect) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 58 | (self.y - o.y).abs() < TOL && (self.h - o.h).abs() < TOL |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 59 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 60 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 61 | /// Takes the horizontal extent of `x_from` and the vertical extent of `y_from`. |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 62 | pub(crate) fn mixed(x_from: &Rect, y_from: &Rect) -> Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 63 | Rect::new(x_from.x, y_from.y, x_from.w, y_from.h) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 64 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 65 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 66 | pub(crate) fn bounding<'a>(rects: impl IntoIterator<Item = &'a Rect>) -> Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 67 | let (mut x0, mut y0, mut x1, mut y1) = ( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 68 | f64::INFINITY, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 69 | f64::INFINITY, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 70 | f64::NEG_INFINITY, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 71 | f64::NEG_INFINITY, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 72 | ); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 73 | for r in rects { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 74 | x0 = x0.min(r.x); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 75 | y0 = y0.min(r.y); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 76 | x1 = x1.max(r.right()); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 77 | y1 = y1.max(r.bottom()); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 78 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 79 | Rect::new(x0, y0, x1 - x0, y1 - y0) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 80 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 81 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 82 | pub(crate) fn expanded(&self, by: f64) -> Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 83 | Rect::new( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 84 | self.x - by, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 85 | self.y - by, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 86 | self.w + 2.0 * by, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 87 | self.h + 2.0 * by, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 88 | ) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 89 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 90 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 91 | pub(crate) fn intersects(&self, o: &Rect) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 92 | self.x < o.right() && o.x < self.right() && self.y < o.bottom() && o.y < self.bottom() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 93 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 94 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 95 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 96 | /// How a window changes between two keyframes. |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 97 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 98 | pub(crate) struct Change { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 99 | pub x: bool, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 100 | pub y: bool, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 101 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 102 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 103 | impl Change { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 104 | pub fn between(a: &Rect, b: &Rect) -> Self { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 105 | Self { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 106 | x: !a.same_x(b), |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 107 | y: !a.same_y(b), |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 108 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 109 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 110 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 111 | pub fn moves(&self) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 112 | self.x || self.y |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 113 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 114 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 115 | pub fn both(&self) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 116 | self.x && self.y |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 117 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 118 | } |