1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Take a look at the license at the top of the repository in the LICENSE file.

use std::fmt;

use glib::translate::*;

use crate::{Point, Quad, Rect};

impl Quad {
    /// Initializes a [`Quad`][crate::Quad] with the given points.
    /// ## `p1`
    /// the first point of the quadrilateral
    /// ## `p2`
    /// the second point of the quadrilateral
    /// ## `p3`
    /// the third point of the quadrilateral
    /// ## `p4`
    /// the fourth point of the quadrilateral
    ///
    /// # Returns
    ///
    /// the initialized [`Quad`][crate::Quad]
    #[doc(alias = "graphene_quad_init")]
    pub fn new(p1: &Point, p2: &Point, p3: &Point, p4: &Point) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            let mut quad = Self::uninitialized();
            ffi::graphene_quad_init(
                quad.to_glib_none_mut().0,
                p1.to_glib_none().0,
                p2.to_glib_none().0,
                p3.to_glib_none().0,
                p4.to_glib_none().0,
            );
            quad
        }
    }

    /// Initializes a [`Quad`][crate::Quad] using the four corners of the
    /// given [`Rect`][crate::Rect].
    /// ## `r`
    /// a [`Rect`][crate::Rect]
    ///
    /// # Returns
    ///
    /// the initialized [`Quad`][crate::Quad]
    #[doc(alias = "graphene_quad_init_from_rect")]
    #[doc(alias = "init_from_rect")]
    pub fn from_rect(r: &Rect) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            let mut quad = Self::uninitialized();
            ffi::graphene_quad_init_from_rect(quad.to_glib_none_mut().0, r.to_glib_none().0);
            quad
        }
    }

    /// Initializes a [`Quad`][crate::Quad] using an array of points.
    /// ## `points`
    /// an array of 4 [`Point`][crate::Point]
    ///
    /// # Returns
    ///
    /// the initialized [`Quad`][crate::Quad]
    #[doc(alias = "graphene_quad_init_from_points")]
    #[doc(alias = "init_from_points")]
    pub fn from_points(points: &[Point; 4]) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            let points = [
                *points[0].to_glib_none().0,
                *points[1].to_glib_none().0,
                *points[2].to_glib_none().0,
                *points[3].to_glib_none().0,
            ];
            let mut quad = Self::uninitialized();
            ffi::graphene_quad_init_from_points(
                quad.to_glib_none_mut().0,
                points.as_ptr() as *const _,
            );
            quad
        }
    }

    /// Retrieves the point of a [`Quad`][crate::Quad] at the given index.
    /// ## `index_`
    /// the index of the point to retrieve
    ///
    /// # Returns
    ///
    /// a [`Point`][crate::Point]
    #[doc(alias = "graphene_quad_get_point")]
    #[doc(alias = "get_point")]
    pub fn point(&self, index_: u32) -> Point {
        assert!(index_ < 4);
        unsafe { from_glib_none(ffi::graphene_quad_get_point(self.to_glib_none().0, index_)) }
    }

    #[inline]
    pub fn points(&self) -> &[Point; 4] {
        unsafe { &*(&self.inner.points as *const [ffi::graphene_point_t; 4] as *const [Point; 4]) }
    }
}

impl fmt::Debug for Quad {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Quad")
            .field("points", &self.points())
            .finish()
    }
}