graphene/
quad.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, Point, Quad, Rect};
8
9impl Quad {
10    /// Initializes a [`Quad`][crate::Quad] with the given points.
11    /// ## `p1`
12    /// the first point of the quadrilateral
13    /// ## `p2`
14    /// the second point of the quadrilateral
15    /// ## `p3`
16    /// the third point of the quadrilateral
17    /// ## `p4`
18    /// the fourth point of the quadrilateral
19    ///
20    /// # Returns
21    ///
22    /// the initialized [`Quad`][crate::Quad]
23    #[doc(alias = "graphene_quad_init")]
24    pub fn new(p1: &Point, p2: &Point, p3: &Point, p4: &Point) -> Self {
25        assert_initialized_main_thread!();
26        unsafe {
27            let mut quad = Self::uninitialized();
28            ffi::graphene_quad_init(
29                quad.to_glib_none_mut().0,
30                p1.to_glib_none().0,
31                p2.to_glib_none().0,
32                p3.to_glib_none().0,
33                p4.to_glib_none().0,
34            );
35            quad
36        }
37    }
38
39    /// Initializes a [`Quad`][crate::Quad] using the four corners of the
40    /// given [`Rect`][crate::Rect].
41    /// ## `r`
42    /// a [`Rect`][crate::Rect]
43    ///
44    /// # Returns
45    ///
46    /// the initialized [`Quad`][crate::Quad]
47    #[doc(alias = "graphene_quad_init_from_rect")]
48    #[doc(alias = "init_from_rect")]
49    pub fn from_rect(r: &Rect) -> Self {
50        assert_initialized_main_thread!();
51        unsafe {
52            let mut quad = Self::uninitialized();
53            ffi::graphene_quad_init_from_rect(quad.to_glib_none_mut().0, r.to_glib_none().0);
54            quad
55        }
56    }
57
58    /// Initializes a [`Quad`][crate::Quad] using an array of points.
59    /// ## `points`
60    /// an array of 4 [`Point`][crate::Point]
61    ///
62    /// # Returns
63    ///
64    /// the initialized [`Quad`][crate::Quad]
65    #[doc(alias = "graphene_quad_init_from_points")]
66    #[doc(alias = "init_from_points")]
67    pub fn from_points(points: &[Point; 4]) -> Self {
68        assert_initialized_main_thread!();
69        unsafe {
70            let points = [
71                *points[0].to_glib_none().0,
72                *points[1].to_glib_none().0,
73                *points[2].to_glib_none().0,
74                *points[3].to_glib_none().0,
75            ];
76            let mut quad = Self::uninitialized();
77            ffi::graphene_quad_init_from_points(
78                quad.to_glib_none_mut().0,
79                points.as_ptr() as *const _,
80            );
81            quad
82        }
83    }
84
85    /// Retrieves the point of a [`Quad`][crate::Quad] at the given index.
86    /// ## `index_`
87    /// the index of the point to retrieve
88    ///
89    /// # Returns
90    ///
91    /// a [`Point`][crate::Point]
92    #[doc(alias = "graphene_quad_get_point")]
93    #[doc(alias = "get_point")]
94    pub fn point(&self, index_: u32) -> Point {
95        assert!(index_ < 4);
96        unsafe { from_glib_none(ffi::graphene_quad_get_point(self.to_glib_none().0, index_)) }
97    }
98
99    #[inline]
100    pub fn points(&self) -> &[Point; 4] {
101        unsafe { &*(&self.inner.points as *const [ffi::graphene_point_t; 4] as *const [Point; 4]) }
102    }
103}
104
105impl fmt::Debug for Quad {
106    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107        f.debug_struct("Quad")
108            .field("points", &self.points())
109            .finish()
110    }
111}