Add helper functions to Rect

This commit is contained in:
Emil Ernerfeldt 2021-03-14 16:42:06 +01:00
parent 0c9b4858f0
commit 4e852727c0

View file

@ -106,6 +106,34 @@ impl Rect {
}
}
/// A `Rect` that contains every point to the right of the given X coordinate.
pub fn everything_right_of(left_x: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_left(left_x);
rect
}
/// A `Rect` that contains every point to the left of the given X coordinate.
pub fn everything_left_of(right_x: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_right(right_x);
rect
}
/// A `Rect` that contains every point below a certain y coordinate
pub fn everything_below(top_y: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_top(top_y);
rect
}
/// A `Rect` that contains every point above a certain y coordinate
pub fn everything_above(bottom_y: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_bottom(bottom_y);
rect
}
/// Expand by this much in each direction, keeping the center
#[must_use]
pub fn expand(self, amnt: f32) -> Self {
@ -265,20 +293,62 @@ impl Rect {
pub fn is_finite(&self) -> bool {
self.min.is_finite() && self.max.is_finite()
}
}
// Convenience functions (assumes origin is towards left top):
/// ## Convenience functions (assumes origin is towards left top):
impl Rect {
/// `min.x`
pub fn left(&self) -> f32 {
self.min.x
}
/// `min.x`
pub fn left_mut(&mut self) -> &mut f32 {
&mut self.min.x
}
/// `min.x`
pub fn set_left(&mut self, x: f32) {
self.min.x = x;
}
/// `max.x`
pub fn right(&self) -> f32 {
self.max.x
}
/// `max.x`
pub fn right_mut(&mut self) -> &mut f32 {
&mut self.max.x
}
/// `max.x`
pub fn set_right(&mut self, x: f32) {
self.max.x = x;
}
/// `min.y`
pub fn top(&self) -> f32 {
self.min.y
}
/// `min.y`
pub fn top_mut(&mut self) -> &mut f32 {
&mut self.min.y
}
/// `min.y`
pub fn set_top(&mut self, y: f32) {
self.min.y = y;
}
/// `max.y`
pub fn bottom(&self) -> f32 {
self.max.y
}
/// `max.y`
pub fn bottom_mut(&mut self) -> &mut f32 {
&mut self.max.y
}
/// `max.y`
pub fn set_bottom(&mut self, y: f32) {
self.max.y = y;
}
pub fn left_top(&self) -> Pos2 {
pos2(self.left(), self.top())
}