clutter/auto/
rect.rs

1use crate::Point;
2use glib::translate::*;
3use std::mem;
4
5glib_wrapper! {
6    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7    pub struct Rect(Boxed<ffi::ClutterRect>);
8
9    match fn {
10        copy => |ptr| ffi::clutter_rect_copy(mut_override(ptr)),
11        free => |ptr| ffi::clutter_rect_free(ptr),
12        get_type => || ffi::clutter_rect_get_type(),
13    }
14}
15
16impl Rect {
17    /// Creates a new, empty `Rect`.
18    ///
19    /// You can use `Rect::init` to initialize the returned rectangle,
20    /// for instance:
21    ///
22    ///
23    /// ```text
24    ///   rect = clutter_rect_init (clutter_rect_alloc (), x, y, width, height);
25    /// ```
26    ///
27    /// # Returns
28    ///
29    /// the newly allocated `Rect`.
30    ///  Use `Rect::free` to free its resources
31    pub fn alloc() -> Rect {
32        unsafe { from_glib_full(ffi::clutter_rect_alloc()) }
33    }
34
35    /// Rounds the origin of `self` downwards to the nearest integer, and rounds
36    /// the size of `self` upwards to the nearest integer, so that `self` is
37    /// updated to the smallest rectangle capable of fully containing the
38    /// original, fractional rectangle.
39    pub fn clamp_to_pixel(&mut self) {
40        unsafe {
41            ffi::clutter_rect_clamp_to_pixel(self.to_glib_none_mut().0);
42        }
43    }
44
45    /// Checks whether `point` is contained by `self`, after normalizing the
46    /// rectangle.
47    /// ## `point`
48    /// the point to check
49    ///
50    /// # Returns
51    ///
52    /// `true` if the `point` is contained by `self`.
53    pub fn contains_point(&mut self, point: &mut Point) -> bool {
54        unsafe {
55            from_glib(ffi::clutter_rect_contains_point(
56                self.to_glib_none_mut().0,
57                point.to_glib_none_mut().0,
58            ))
59        }
60    }
61
62    /// Checks whether `self` contains `b`.
63    ///
64    /// The first rectangle contains the second if the union of the
65    /// two `Rect` is equal to the first rectangle.
66    /// ## `b`
67    /// a `Rect`
68    ///
69    /// # Returns
70    ///
71    /// `true` if the first rectangle contains the second.
72    pub fn contains_rect(&mut self, b: &mut Rect) -> bool {
73        unsafe {
74            from_glib(ffi::clutter_rect_contains_rect(
75                self.to_glib_none_mut().0,
76                b.to_glib_none_mut().0,
77            ))
78        }
79    }
80
81    /// Checks whether `self` and `b` are equals.
82    ///
83    /// This function will normalize both `self` and `b` before comparing
84    /// their origin and size.
85    /// ## `b`
86    /// a `Rect`
87    ///
88    /// # Returns
89    ///
90    /// `true` if the rectangles match in origin and size.
91    pub fn equals(&mut self, b: &mut Rect) -> bool {
92        unsafe {
93            from_glib(ffi::clutter_rect_equals(
94                self.to_glib_none_mut().0,
95                b.to_glib_none_mut().0,
96            ))
97        }
98    }
99
100    /// Retrieves the center of `self`, after normalizing the rectangle,
101    /// and updates `center` with the correct coordinates.
102    /// ## `center`
103    /// a `Point`
104    pub fn get_center(&mut self) -> Point {
105        unsafe {
106            let mut center = Point::uninitialized();
107            ffi::clutter_rect_get_center(self.to_glib_none_mut().0, center.to_glib_none_mut().0);
108            center
109        }
110    }
111
112    /// Retrieves the height of `self`.
113    ///
114    /// # Returns
115    ///
116    /// the height of the rectangle
117    pub fn get_height(&mut self) -> f32 {
118        unsafe { ffi::clutter_rect_get_height(self.to_glib_none_mut().0) }
119    }
120
121    /// Retrieves the width of `self`.
122    ///
123    /// # Returns
124    ///
125    /// the width of the rectangle
126    pub fn get_width(&mut self) -> f32 {
127        unsafe { ffi::clutter_rect_get_width(self.to_glib_none_mut().0) }
128    }
129
130    /// Retrieves the X coordinate of the origin of `self`.
131    ///
132    /// # Returns
133    ///
134    /// the X coordinate of the origin of the rectangle
135    pub fn get_x(&mut self) -> f32 {
136        unsafe { ffi::clutter_rect_get_x(self.to_glib_none_mut().0) }
137    }
138
139    /// Retrieves the Y coordinate of the origin of `self`.
140    ///
141    /// # Returns
142    ///
143    /// the Y coordinate of the origin of the rectangle
144    pub fn get_y(&mut self) -> f32 {
145        unsafe { ffi::clutter_rect_get_y(self.to_glib_none_mut().0) }
146    }
147
148    /// Initializes a `Rect` with the given origin and size.
149    /// ## `x`
150    /// X coordinate of the origin
151    /// ## `y`
152    /// Y coordinate of the origin
153    /// ## `width`
154    /// width of the rectangle
155    /// ## `height`
156    /// height of the rectangle
157    ///
158    /// # Returns
159    ///
160    /// the updated rectangle
161    pub fn init(&mut self, x: f32, y: f32, width: f32, height: f32) -> Option<Rect> {
162        unsafe {
163            from_glib_none(ffi::clutter_rect_init(
164                self.to_glib_none_mut().0,
165                x,
166                y,
167                width,
168                height,
169            ))
170        }
171    }
172
173    /// Normalizes the `self` and offsets its origin by the `d_x` and `d_y` values;
174    /// the size is adjusted by (2 * `d_x`, 2 * `d_y`).
175    ///
176    /// If `d_x` and `d_y` are positive the size of the rectangle is decreased; if
177    /// the values are negative, the size of the rectangle is increased.
178    ///
179    /// If the resulting rectangle has a negative width or height, the size is
180    /// set to 0.
181    /// ## `d_x`
182    /// an horizontal value; a positive `d_x` will create an inset rectangle,
183    ///  and a negative value will create a larger rectangle
184    /// ## `d_y`
185    /// a vertical value; a positive `d_x` will create an inset rectangle,
186    ///  and a negative value will create a larger rectangle
187    pub fn inset(&mut self, d_x: f32, d_y: f32) {
188        unsafe {
189            ffi::clutter_rect_inset(self.to_glib_none_mut().0, d_x, d_y);
190        }
191    }
192
193    /// Computes the intersection of `self` and `b`, and places it in `res`, if `res`
194    /// is not `None`.
195    ///
196    /// This function will normalize both `self` and `b` prior to computing their
197    /// intersection.
198    ///
199    /// This function can be used to simply check if the intersection of `self` and `b`
200    /// is not empty, by using `None` for `res`.
201    /// ## `b`
202    /// a `Rect`
203    /// ## `res`
204    /// a `Rect`, or `None`
205    ///
206    /// # Returns
207    ///
208    /// `true` if the intersection of `self` and `b` is not empty
209    pub fn intersection(&mut self, b: &mut Rect) -> Option<Rect> {
210        unsafe {
211            let mut res = Rect::uninitialized();
212            let ret = from_glib(ffi::clutter_rect_intersection(
213                self.to_glib_none_mut().0,
214                b.to_glib_none_mut().0,
215                res.to_glib_none_mut().0,
216            ));
217            if ret {
218                Some(res)
219            } else {
220                None
221            }
222        }
223    }
224
225    /// Normalizes a `Rect`.
226    ///
227    /// A `Rect` is defined by the area covered by its size; this means
228    /// that a `Rect` with `Rect.origin` in [ 0, 0 ] and a
229    /// `Rect.size` of [ 10, 10 ] is equivalent to a `Rect` with
230    /// `Rect.origin` in [ 10, 10 ] and a `Rect.size` of [ -10, -10 ].
231    ///
232    /// This function is useful to ensure that a rectangle has positive width
233    /// and height; it will modify the passed `self` and normalize its size.
234    pub fn normalize(&mut self) -> Option<Rect> {
235        unsafe { from_glib_full(ffi::clutter_rect_normalize(self.to_glib_none_mut().0)) }
236    }
237
238    /// Offsets the origin of `self` by the given values, after normalizing
239    /// the rectangle.
240    /// ## `d_x`
241    /// the horizontal offset value
242    /// ## `d_y`
243    /// the vertical offset value
244    pub fn offset(&mut self, d_x: f32, d_y: f32) {
245        unsafe {
246            ffi::clutter_rect_offset(self.to_glib_none_mut().0, d_x, d_y);
247        }
248    }
249
250    /// Computes the smallest possible rectangle capable of fully containing
251    /// both `self` and `b`, and places it into `res`.
252    ///
253    /// This function will normalize both `self` and `b` prior to computing their
254    /// union.
255    /// ## `b`
256    /// a `Rect`
257    /// ## `res`
258    /// a `Rect`
259    pub fn union(&mut self, b: &mut Rect) -> Rect {
260        unsafe {
261            let mut res = Rect::uninitialized();
262            ffi::clutter_rect_union(
263                self.to_glib_none_mut().0,
264                b.to_glib_none_mut().0,
265                res.to_glib_none_mut().0,
266            );
267            res
268        }
269    }
270
271    /// A `Rect` with `Rect.origin` set at (0, 0) and a size
272    /// of 0.
273    ///
274    /// The returned value can be used as a guard.
275    ///
276    /// # Returns
277    ///
278    /// a rectangle with origin in (0, 0) and a size of 0.
279    ///  The returned `Rect` is owned by Clutter and it should not
280    ///  be modified or freed.
281    pub fn zero() -> Option<Rect> {
282        unsafe { from_glib_none(ffi::clutter_rect_zero()) }
283    }
284}
285
286#[doc(hidden)]
287impl Uninitialized for Rect {
288    #[inline]
289    unsafe fn uninitialized() -> Self {
290        Self::alloc()
291    }
292}