use crate::geometry::Rect;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ClipRect {
pub rect: Rect,
pub enabled: bool,
}
impl ClipRect {
#[inline]
pub const fn new(rect: Rect) -> Self {
Self {
rect,
enabled: true,
}
}
pub const NONE: Self = Self {
rect: Rect::ZERO,
enabled: false,
};
#[inline]
pub fn contains(&self, x: i32, y: i32) -> bool {
if !self.enabled {
return true;
}
self.rect.contains_point(crate::geometry::Point::new(x, y))
}
#[inline]
pub fn intersect(&self, other: &ClipRect) -> ClipRect {
if !self.enabled {
return *other;
}
if !other.enabled {
return *self;
}
match self.rect.intersection(&other.rect) {
Some(r) => ClipRect::new(r),
None => ClipRect {
rect: Rect::ZERO,
enabled: true, },
}
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.enabled && self.rect.is_empty()
}
}
impl From<Rect> for ClipRect {
#[inline]
fn from(rect: Rect) -> Self {
Self::new(rect)
}
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
pub enum ClipOp {
#[default]
Replace = 0,
Intersect = 1,
Union = 2,
Subtract = 3,
}
impl ClipOp {
#[inline]
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Replace),
1 => Some(Self::Intersect),
2 => Some(Self::Union),
3 => Some(Self::Subtract),
_ => None,
}
}
}