use crate::geometry::Rect;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct BufferRegion {
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
}
impl BufferRegion {
#[inline]
pub const fn new(x: u32, y: u32, width: u32, height: u32) -> Self {
Self {
x,
y,
width,
height,
}
}
pub const ZERO: Self = Self {
x: 0,
y: 0,
width: 0,
height: 0,
};
#[inline]
pub const fn full(width: u32, height: u32) -> Self {
Self {
x: 0,
y: 0,
width,
height,
}
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.width == 0 || self.height == 0
}
#[inline]
pub const fn area(&self) -> u64 {
self.width as u64 * self.height as u64
}
#[inline]
pub const fn to_rect(&self) -> Rect {
Rect::new(self.x as i32, self.y as i32, self.width, self.height)
}
#[inline]
pub fn from_rect(rect: Rect) -> Self {
Self {
x: rect.x.max(0) as u32,
y: rect.y.max(0) as u32,
width: rect.width,
height: rect.height,
}
}
#[inline]
pub const fn contains(&self, x: u32, y: u32) -> bool {
x >= self.x && x < self.x + self.width && y >= self.y && y < self.y + self.height
}
}
impl From<Rect> for BufferRegion {
#[inline]
fn from(r: Rect) -> Self {
Self::from_rect(r)
}
}
impl From<BufferRegion> for Rect {
#[inline]
fn from(r: BufferRegion) -> Self {
r.to_rect()
}
}