graphene/
rect.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5use glib::translate::*;
6
7use crate::{ffi, Rect, Vec2};
8
9impl Rect {
10    /// Computes the four vertices of a [`Rect`][crate::Rect].
11    ///
12    /// # Returns
13    ///
14    ///
15    /// ## `vertices`
16    /// return location for an array
17    ///  of 4 [`Vec2`][crate::Vec2]
18    #[doc(alias = "graphene_rect_get_vertices")]
19    #[doc(alias = "get_vertices")]
20    pub fn vertices(&self) -> &[Vec2; 4] {
21        unsafe {
22            let mut out: [ffi::graphene_vec2_t; 4] = std::mem::zeroed();
23            ffi::graphene_rect_get_vertices(self.to_glib_none().0, &mut out as *mut _);
24            &*(&out as *const [ffi::graphene_vec2_t; 4] as *const [Vec2; 4])
25        }
26    }
27
28    /// Initializes the given [`Rect`][crate::Rect] with the given values.
29    ///
30    /// This function will implicitly normalize the [`Rect`][crate::Rect]
31    /// before returning.
32    /// ## `x`
33    /// the X coordinate of the [`Rect`][crate::Rect]
34    /// ## `y`
35    /// the Y coordinate of the [`Rect`][crate::Rect]
36    /// ## `width`
37    /// the width of the [`Rect`][crate::Rect]
38    /// ## `height`
39    /// the height of the [`Rect`][crate::Rect]
40    ///
41    /// # Returns
42    ///
43    /// the initialized rectangle
44    #[doc(alias = "graphene_rect_init")]
45    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
46        assert_initialized_main_thread!();
47        unsafe {
48            let mut rect = Self::uninitialized();
49            ffi::graphene_rect_init(rect.to_glib_none_mut().0, x, y, width, height);
50            rect
51        }
52    }
53}
54
55impl fmt::Debug for Rect {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        f.debug_struct("Rect")
58            .field("x", &self.x())
59            .field("y", &self.y())
60            .field("width", &self.width())
61            .field("height", &self.height())
62            .finish()
63    }
64}
65
66impl Default for Rect {
67    fn default() -> Self {
68        Self::zero()
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75    use crate::Point;
76
77    #[test]
78    fn contains_point() {
79        let rect = Rect::new(100., 100., 100., 100.);
80
81        let right = Point::new(250., 150.);
82        let below = Point::new(150., 50.);
83        let left = Point::new(50., 150.);
84        let above = Point::new(150., 250.);
85
86        assert!(!rect.contains_point(&right));
87        assert!(!rect.contains_point(&below));
88        assert!(!rect.contains_point(&left));
89        assert!(!rect.contains_point(&above));
90    }
91}