sim_logic/rendering/screen/
clip.rs1use crate::input::PointerSample;
4use sim_engine::{LogicalScreenPosition, LogicalScreenVector, SceneError, ScreenClipRect};
5
6#[derive(Debug, Default, Clone, Copy, PartialEq)]
12pub enum ScreenClip {
13 #[default]
15 Unclipped,
16 Empty,
18 Rectangle(ScreenClipRect),
20}
21
22impl ScreenClip {
23 pub fn new(
26 position: LogicalScreenPosition,
27 size: LogicalScreenVector,
28 ) -> Result<Self, SceneError> {
29 Ok(Self::Rectangle(ScreenClipRect::from_min_size(
30 position, size,
31 )?))
32 }
33
34 pub fn intersection(self, other: Self) -> Self {
36 match (self, other) {
37 (Self::Empty, _) | (_, Self::Empty) => Self::Empty,
38 (Self::Unclipped, clip) | (clip, Self::Unclipped) => clip,
39 (Self::Rectangle(a), Self::Rectangle(b)) => {
40 a.intersection(b).map_or(Self::Empty, Self::Rectangle)
41 }
42 }
43 }
44
45 pub fn contains_pointer(self, sample: PointerSample) -> bool {
47 sample.is_inside_viewport() && self.contains(sample.position())
48 }
49
50 pub(super) fn contains(self, position: LogicalScreenPosition) -> bool {
51 let p = position.to_vec2();
52 match self {
53 Self::Unclipped => position.is_finite(),
54 Self::Empty => false,
55 Self::Rectangle(rect) => {
56 let min = rect.min().to_vec2();
57 let max = rect.max().to_vec2();
58 p.x() >= min.x() && p.y() >= min.y() && p.x() < max.x() && p.y() < max.y()
59 }
60 }
61 }
62}