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 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 29 | pub fn center_x(&self) -> f64 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 30 | self.x + self.w / 2.0 |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 31 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 32 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 33 | pub fn center_y(&self) -> f64 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 34 | self.y + self.h / 2.0 |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 35 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 36 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 37 | pub fn lerp(&self, other: &Rect, s: f64) -> Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 38 | Rect::new( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 39 | self.x + (other.x - self.x) * s, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 40 | self.y + (other.y - self.y) * s, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 41 | self.w + (other.w - self.w) * s, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 42 | self.h + (other.h - self.h) * s, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 43 | ) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 44 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 45 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 46 | pub(crate) fn same_x(&self, o: &Rect) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 47 | (self.x - o.x).abs() < TOL && (self.w - o.w).abs() < TOL |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 48 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 49 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 50 | pub(crate) fn same_y(&self, o: &Rect) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 51 | (self.y - o.y).abs() < TOL && (self.h - o.h).abs() < TOL |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 52 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 53 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 54 | /// Takes the horizontal extent of `x_from` and the vertical extent of `y_from`. |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 55 | pub(crate) fn mixed(x_from: &Rect, y_from: &Rect) -> Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 56 | Rect::new(x_from.x, y_from.y, x_from.w, y_from.h) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 57 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 58 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 59 | pub(crate) fn bounding<'a>(rects: impl IntoIterator<Item = &'a Rect>) -> Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 60 | let (mut x0, mut y0, mut x1, mut y1) = ( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 61 | f64::INFINITY, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 62 | f64::INFINITY, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 63 | f64::NEG_INFINITY, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 64 | f64::NEG_INFINITY, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 65 | ); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 66 | for r in rects { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 67 | x0 = x0.min(r.x); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 68 | y0 = y0.min(r.y); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 69 | x1 = x1.max(r.right()); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 70 | y1 = y1.max(r.bottom()); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 71 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 72 | Rect::new(x0, y0, x1 - x0, y1 - y0) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 73 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 74 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 75 | pub(crate) fn expanded(&self, by: f64) -> Rect { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 76 | Rect::new( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 77 | self.x - by, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 78 | self.y - by, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 79 | self.w + 2.0 * by, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 80 | self.h + 2.0 * by, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 81 | ) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 82 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 83 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 84 | pub(crate) fn intersects(&self, o: &Rect) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 85 | self.x < o.right() && o.x < self.right() && self.y < o.bottom() && o.y < self.bottom() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 86 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 87 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 88 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 89 | /// How a window changes between two keyframes. |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 90 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 91 | pub(crate) struct Change { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 92 | pub x: bool, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 93 | pub y: bool, |
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 | impl Change { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 97 | pub fn between(a: &Rect, b: &Rect) -> Self { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 98 | Self { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 99 | x: !a.same_x(b), |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 100 | y: !a.same_y(b), |
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 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 104 | pub fn moves(&self) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 105 | self.x || self.y |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 106 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 107 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 108 | pub fn both(&self) -> bool { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 109 | self.x && self.y |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 110 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 111 | } |