//! Contains types which represent the sides and corners of a rectangular shape. use glam::Vec2; #[derive(Clone, Copy, Debug, PartialEq, Default)] pub struct Rect { pub position: Vec2, pub size: Vec2, } impl Rect { pub fn contains_point(&self, point: Vec2) -> bool { point.cmpge(self.position).all() && point.cmple(self.position + self.size).all() } } /// Represents 4 sides of a rectangular shape. #[derive(Default, Clone, Copy, PartialEq, Eq, Debug)] pub struct Sides { pub top: T, pub bottom: T, pub left: T, pub right: T, } impl Sides { #[inline] pub fn all(value: T) -> Self { Self { top: value.clone(), bottom: value.clone(), left: value.clone(), right: value, } } #[inline] pub fn horizontal_vertical(horizontal: T, vertical: T) -> Self { Self { top: vertical.clone(), bottom: vertical, left: horizontal.clone(), right: horizontal, } } } impl From for Sides { fn from(value: T) -> Self { Self::all(value) } } impl From<(T, T)> for Sides { fn from((horizontal, vertical): (T, T)) -> Self { Self::horizontal_vertical(horizontal, vertical) } } impl From<(T, T, T, T)> for Sides { fn from((top, bottom, left, right): (T, T, T, T)) -> Self { Self { top, bottom, left, right } } } #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub struct Corners { pub top_left: T, pub top_right: T, pub bottom_left: T, pub bottom_right: T, } impl Corners { #[inline] pub fn all(value: T) -> Self { Self { top_left: value.clone(), top_right: value.clone(), bottom_left: value.clone(), bottom_right: value, } } #[inline] pub fn top_bottom(top: T, bottom: T) -> Self { Self { top_left: top.clone(), top_right: top, bottom_left: bottom.clone(), bottom_right: bottom, } } #[inline] pub fn left_right(left: T, right: T) -> Self { Self { top_left: left.clone(), top_right: right.clone(), bottom_left: left, bottom_right: right, } } } impl Corners { pub fn max(&self) -> T { self.top_left.clone() .max(self.top_right.clone()) .max(self.bottom_left.clone()) .max(self.bottom_right.clone()) .clone() } } /// Represents 4 corners of a rectangular shape. impl Corners { pub fn max_f32(&self) -> f32 { self.top_left .max(self.top_right) .max(self.bottom_left) .max(self.bottom_right) } } impl Corners { pub fn max_f64(&self) -> f64 { self.top_left .max(self.top_right) .max(self.bottom_left) .max(self.bottom_right) } } impl From for Corners { fn from(value: T) -> Self { Self::all(value) } } impl From<(T, T, T, T)> for Corners { fn from((top_left, top_right, bottom_left, bottom_right): (T, T, T, T)) -> Self { Self { top_left, top_right, bottom_left, bottom_right, } } }