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 /// Computes the vertices of the given [`Box2D`][crate::Box2D].
11 ///
12 /// # Returns
13 ///
14 ///
15 /// ## `vertices`
16 /// return location for an array
17 /// of 4 [`Vec2`][crate::Vec2]
18 #[doc(alias = "graphene_box2d_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_box2d_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 /// Stores the minimum and maximum vertices of the given [`Box2D`][crate::Box2D]
29 /// into an array.
30 ///
31 /// The array layout is:
32 ///
33 /// - min_x
34 /// - min_y
35 /// - max_x
36 /// - max_y
37 ///
38 /// # Returns
39 ///
40 ///
41 /// ## `v`
42 /// return location
43 /// for an array of floating point values with at least 4 elements
44 #[doc(alias = "graphene_box2d_to_float")]
45 pub fn to_float(&self) -> [f32; 4] {
46 unsafe {
47 let mut out = std::mem::MaybeUninit::uninit();
48 ffi::graphene_box2d_to_float(self.to_glib_none().0, out.as_mut_ptr());
49 out.assume_init()
50 }
51 }
52
53 /// Initializes the given [`Box2D`][crate::Box2D] with two vertices.
54 /// ## `min`
55 /// the coordinates of the minimum vertex
56 /// ## `max`
57 /// the coordinates of the maximum vertex
58 ///
59 /// # Returns
60 ///
61 /// the initialized [`Box2D`][crate::Box2D]
62 #[doc(alias = "graphene_box2d_init")]
63 pub fn new(min: Option<&Point>, max: Option<&Point>) -> Self {
64 assert_initialized_main_thread!();
65 unsafe {
66 let mut b = Self::uninitialized();
67 ffi::graphene_box2d_init(
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 /// Initializes the given [`Box2D`][crate::Box2D] with the given array
77 /// of vertices.
78 ///
79 /// If `n_points` is 0, the returned box is initialized with
80 /// [`empty()`][Self::empty()].
81 /// ## `points`
82 /// an array of [`Point`][crate::Point]
83 ///
84 /// # Returns
85 ///
86 /// the initialized [`Box2D`][crate::Box2D]
87 #[doc(alias = "graphene_box2d_init_from_points")]
88 #[doc(alias = "init_from_points")]
89 pub fn from_points(points: &[Point]) -> Self {
90 assert_initialized_main_thread!();
91
92 let n = points.len() as u32;
93
94 unsafe {
95 let mut b = Self::uninitialized();
96 ffi::graphene_box2d_init_from_points(
97 b.to_glib_none_mut().0,
98 n,
99 points.to_glib_none().0,
100 );
101 b
102 }
103 }
104
105 /// Initializes the given [`Box2D`][crate::Box2D] with two vertices
106 /// stored inside [`Vec2`][crate::Vec2].
107 /// ## `min`
108 /// the coordinates of the minimum vertex
109 /// ## `max`
110 /// the coordinates of the maximum vertex
111 ///
112 /// # Returns
113 ///
114 /// the initialized [`Box2D`][crate::Box2D]
115 #[doc(alias = "graphene_box2d_init_from_vec2")]
116 #[doc(alias = "init_from_vec2")]
117 pub fn from_vec2(min: Option<&Vec2>, max: Option<&Vec2>) -> Self {
118 assert_initialized_main_thread!();
119 unsafe {
120 let mut b = Self::uninitialized();
121 ffi::graphene_box2d_init_from_vec2(
122 b.to_glib_none_mut().0,
123 min.to_glib_none().0,
124 max.to_glib_none().0,
125 );
126 b
127 }
128 }
129
130 /// Initializes the given [`Box2D`][crate::Box2D] with the given array
131 /// of vertices.
132 ///
133 /// If `n_vectors` is 0, the returned box is initialized with
134 /// [`empty()`][Self::empty()].
135 /// ## `vectors`
136 /// an array of [`Vec2`][crate::Vec2]
137 ///
138 /// # Returns
139 ///
140 /// the initialized [`Box2D`][crate::Box2D]
141 #[doc(alias = "graphene_box2d_init_from_vectors")]
142 #[doc(alias = "init_from_vectors")]
143 pub fn from_vectors(vectors: &[Vec2]) -> Self {
144 assert_initialized_main_thread!();
145
146 let n = vectors.len() as u32;
147
148 unsafe {
149 let mut b = Self::uninitialized();
150 ffi::graphene_box2d_init_from_vectors(
151 b.to_glib_none_mut().0,
152 n,
153 vectors.to_glib_none().0,
154 );
155 b
156 }
157 }
158
159 /// Initializes the given [`Box2D`][crate::Box2D] with the origin and
160 /// size of a [`Rect`][crate::Rect].
161 /// ## `src`
162 /// a [`Rect`][crate::Rect]
163 ///
164 /// # Returns
165 ///
166 /// the initialized [`Box2D`][crate::Box2D]
167 #[doc(alias = "graphene_box2d_init_from_rect")]
168 pub fn from_rect(src: &Rect) -> Self {
169 assert_initialized_main_thread!();
170 unsafe {
171 let mut b = Self::uninitialized();
172 ffi::graphene_box2d_init_from_rect(b.to_glib_none_mut().0, src.to_glib_none().0);
173 b
174 }
175 }
176}
177
178impl Default for Box2D {
179 fn default() -> Self {
180 Self::zero()
181 }
182}
183
184impl fmt::Debug for Box2D {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 f.debug_struct("Box2D")
187 .field("min", &self.min())
188 .field("max", &self.max())
189 .finish()
190 }
191}