graphene/
box2_d.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, Box2D, Point, Rect, Vec2};
8
9impl Box2D {
10    #[doc(alias = "graphene_box2d_get_vertices")]
11    #[doc(alias = "get_vertices")]
12    pub fn vertices(&self) -> &[Vec2; 4] {
13        unsafe {
14            let mut out: [ffi::graphene_vec2_t; 4] = std::mem::zeroed();
15            ffi::graphene_box2d_get_vertices(self.to_glib_none().0, &mut out as *mut _);
16            &*(&out as *const [ffi::graphene_vec2_t; 4] as *const [Vec2; 4])
17        }
18    }
19
20    #[doc(alias = "graphene_box2d_to_float")]
21    pub fn to_float(&self) -> [f32; 4] {
22        unsafe {
23            let mut out = std::mem::MaybeUninit::uninit();
24            ffi::graphene_box2d_to_float(self.to_glib_none().0, out.as_mut_ptr());
25            out.assume_init()
26        }
27    }
28
29    #[doc(alias = "graphene_box2d_init")]
30    pub fn new(min: Option<&Point>, max: Option<&Point>) -> Self {
31        assert_initialized_main_thread!();
32        unsafe {
33            let mut b = Self::uninitialized();
34            ffi::graphene_box2d_init(
35                b.to_glib_none_mut().0,
36                min.to_glib_none().0,
37                max.to_glib_none().0,
38            );
39            b
40        }
41    }
42
43    #[doc(alias = "graphene_box2d_init_from_points")]
44    #[doc(alias = "init_from_points")]
45    pub fn from_points(points: &[Point]) -> Self {
46        assert_initialized_main_thread!();
47
48        let n = points.len() as u32;
49
50        unsafe {
51            let mut b = Self::uninitialized();
52            ffi::graphene_box2d_init_from_points(
53                b.to_glib_none_mut().0,
54                n,
55                points.to_glib_none().0,
56            );
57            b
58        }
59    }
60
61    #[doc(alias = "graphene_box2d_init_from_vec2")]
62    #[doc(alias = "init_from_vec2")]
63    pub fn from_vec2(min: Option<&Vec2>, max: Option<&Vec2>) -> Self {
64        assert_initialized_main_thread!();
65        unsafe {
66            let mut b = Self::uninitialized();
67            ffi::graphene_box2d_init_from_vec2(
68                b.to_glib_none_mut().0,
69                min.to_glib_none().0,
70                max.to_glib_none().0,
71            );
72            b
73        }
74    }
75
76    #[doc(alias = "graphene_box2d_init_from_vectors")]
77    #[doc(alias = "init_from_vectors")]
78    pub fn from_vectors(vectors: &[Vec2]) -> Self {
79        assert_initialized_main_thread!();
80
81        let n = vectors.len() as u32;
82
83        unsafe {
84            let mut b = Self::uninitialized();
85            ffi::graphene_box2d_init_from_vectors(
86                b.to_glib_none_mut().0,
87                n,
88                vectors.to_glib_none().0,
89            );
90            b
91        }
92    }
93
94    #[doc(alias = "graphene_box2d_init_from_rect")]
95    pub fn from_rect(src: &Rect) -> Self {
96        assert_initialized_main_thread!();
97        unsafe {
98            let mut b = Self::uninitialized();
99            ffi::graphene_box2d_init_from_rect(b.to_glib_none_mut().0, src.to_glib_none().0);
100            b
101        }
102    }
103}
104
105impl Default for Box2D {
106    fn default() -> Self {
107        Self::zero()
108    }
109}
110
111impl fmt::Debug for Box2D {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        f.debug_struct("Box2D")
114            .field("min", &self.min())
115            .field("max", &self.max())
116            .finish()
117    }
118}