three_d/core/
scissor_box.rs

1///
2/// Defines the part of the screen or render target that is rendered to.
3/// All pixels outside of the scissor box will not be modified.
4/// All values should be given in physical pixels.
5///
6#[derive(Debug, Copy, Clone, PartialEq)]
7pub struct ScissorBox {
8    /// The distance in pixels from the left edge of the target.
9    pub x: i32,
10    /// The distance in pixels from the bottom edge of the target.
11    pub y: i32,
12    /// The width of the box.
13    pub width: u32,
14    /// The height of the box.
15    pub height: u32,
16}
17
18impl ScissorBox {
19    ///
20    /// Creates a new scissor box which starts at origo (x and y are both zero).
21    ///
22    pub fn new_at_origo(width: u32, height: u32) -> Self {
23        Self {
24            x: 0,
25            y: 0,
26            width,
27            height,
28        }
29    }
30
31    ///
32    /// Returns the intersection between this and the other ScissorBox.
33    ///
34    pub fn intersection(&self, other: impl Into<Self>) -> Self {
35        let other = other.into();
36        let x = self.x.max(other.x);
37        let y = self.y.max(other.y);
38        let width = (self.x + self.width as i32 - x)
39            .min(other.x + other.width as i32 - x)
40            .max(0) as u32;
41        let height = (self.y + self.height as i32 - y)
42            .min(other.y + other.height as i32 - y)
43            .max(0) as u32;
44        Self {
45            x,
46            y,
47            width,
48            height,
49        }
50    }
51}
52
53impl From<crate::core::Viewport> for ScissorBox {
54    fn from(viewport: crate::core::Viewport) -> Self {
55        Self {
56            x: viewport.x,
57            y: viewport.y,
58            width: viewport.width,
59            height: viewport.height,
60        }
61    }
62}
63
64impl From<crate::core::ScissorBox> for crate::core::Viewport {
65    fn from(viewport: crate::core::ScissorBox) -> Self {
66        Self {
67            x: viewport.x,
68            y: viewport.y,
69            width: viewport.width,
70            height: viewport.height,
71        }
72    }
73}