use crate::input::PointerSample;
use sim_engine::{LogicalScreenPosition, LogicalScreenVector, SceneError, ScreenClipRect};
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub enum ScreenClip {
#[default]
Unclipped,
Empty,
Rectangle(ScreenClipRect),
}
impl ScreenClip {
pub fn new(
position: LogicalScreenPosition,
size: LogicalScreenVector,
) -> Result<Self, SceneError> {
Ok(Self::Rectangle(ScreenClipRect::from_min_size(
position, size,
)?))
}
pub fn intersection(self, other: Self) -> Self {
match (self, other) {
(Self::Empty, _) | (_, Self::Empty) => Self::Empty,
(Self::Unclipped, clip) | (clip, Self::Unclipped) => clip,
(Self::Rectangle(a), Self::Rectangle(b)) => {
a.intersection(b).map_or(Self::Empty, Self::Rectangle)
}
}
}
pub fn contains_pointer(self, sample: PointerSample) -> bool {
sample.is_inside_viewport() && self.contains(sample.position())
}
pub(super) fn contains(self, position: LogicalScreenPosition) -> bool {
let p = position.to_vec2();
match self {
Self::Unclipped => position.is_finite(),
Self::Empty => false,
Self::Rectangle(rect) => {
let min = rect.min().to_vec2();
let max = rect.max().to_vec2();
p.x() >= min.x() && p.y() >= min.y() && p.x() < max.x() && p.y() < max.y()
}
}
}
}