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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use crate::Point;
use glib::translate::*;
use std::mem;
glib_wrapper! {
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Rect(Boxed<ffi::ClutterRect>);
match fn {
copy => |ptr| ffi::clutter_rect_copy(mut_override(ptr)),
free => |ptr| ffi::clutter_rect_free(ptr),
get_type => || ffi::clutter_rect_get_type(),
}
}
impl Rect {
/// Creates a new, empty `Rect`.
///
/// You can use `Rect::init` to initialize the returned rectangle,
/// for instance:
///
///
/// ```text
/// rect = clutter_rect_init (clutter_rect_alloc (), x, y, width, height);
/// ```
///
/// # Returns
///
/// the newly allocated `Rect`.
/// Use `Rect::free` to free its resources
pub fn alloc() -> Rect {
unsafe { from_glib_full(ffi::clutter_rect_alloc()) }
}
/// Rounds the origin of `self` downwards to the nearest integer, and rounds
/// the size of `self` upwards to the nearest integer, so that `self` is
/// updated to the smallest rectangle capable of fully containing the
/// original, fractional rectangle.
pub fn clamp_to_pixel(&mut self) {
unsafe {
ffi::clutter_rect_clamp_to_pixel(self.to_glib_none_mut().0);
}
}
/// Checks whether `point` is contained by `self`, after normalizing the
/// rectangle.
/// ## `point`
/// the point to check
///
/// # Returns
///
/// `true` if the `point` is contained by `self`.
pub fn contains_point(&mut self, point: &mut Point) -> bool {
unsafe {
from_glib(ffi::clutter_rect_contains_point(
self.to_glib_none_mut().0,
point.to_glib_none_mut().0,
))
}
}
/// Checks whether `self` contains `b`.
///
/// The first rectangle contains the second if the union of the
/// two `Rect` is equal to the first rectangle.
/// ## `b`
/// a `Rect`
///
/// # Returns
///
/// `true` if the first rectangle contains the second.
pub fn contains_rect(&mut self, b: &mut Rect) -> bool {
unsafe {
from_glib(ffi::clutter_rect_contains_rect(
self.to_glib_none_mut().0,
b.to_glib_none_mut().0,
))
}
}
/// Checks whether `self` and `b` are equals.
///
/// This function will normalize both `self` and `b` before comparing
/// their origin and size.
/// ## `b`
/// a `Rect`
///
/// # Returns
///
/// `true` if the rectangles match in origin and size.
pub fn equals(&mut self, b: &mut Rect) -> bool {
unsafe {
from_glib(ffi::clutter_rect_equals(
self.to_glib_none_mut().0,
b.to_glib_none_mut().0,
))
}
}
/// Retrieves the center of `self`, after normalizing the rectangle,
/// and updates `center` with the correct coordinates.
/// ## `center`
/// a `Point`
pub fn get_center(&mut self) -> Point {
unsafe {
let mut center = Point::uninitialized();
ffi::clutter_rect_get_center(self.to_glib_none_mut().0, center.to_glib_none_mut().0);
center
}
}
/// Retrieves the height of `self`.
///
/// # Returns
///
/// the height of the rectangle
pub fn get_height(&mut self) -> f32 {
unsafe { ffi::clutter_rect_get_height(self.to_glib_none_mut().0) }
}
/// Retrieves the width of `self`.
///
/// # Returns
///
/// the width of the rectangle
pub fn get_width(&mut self) -> f32 {
unsafe { ffi::clutter_rect_get_width(self.to_glib_none_mut().0) }
}
/// Retrieves the X coordinate of the origin of `self`.
///
/// # Returns
///
/// the X coordinate of the origin of the rectangle
pub fn get_x(&mut self) -> f32 {
unsafe { ffi::clutter_rect_get_x(self.to_glib_none_mut().0) }
}
/// Retrieves the Y coordinate of the origin of `self`.
///
/// # Returns
///
/// the Y coordinate of the origin of the rectangle
pub fn get_y(&mut self) -> f32 {
unsafe { ffi::clutter_rect_get_y(self.to_glib_none_mut().0) }
}
/// Initializes a `Rect` with the given origin and size.
/// ## `x`
/// X coordinate of the origin
/// ## `y`
/// Y coordinate of the origin
/// ## `width`
/// width of the rectangle
/// ## `height`
/// height of the rectangle
///
/// # Returns
///
/// the updated rectangle
pub fn init(&mut self, x: f32, y: f32, width: f32, height: f32) -> Option<Rect> {
unsafe {
from_glib_none(ffi::clutter_rect_init(
self.to_glib_none_mut().0,
x,
y,
width,
height,
))
}
}
/// Normalizes the `self` and offsets its origin by the `d_x` and `d_y` values;
/// the size is adjusted by (2 * `d_x`, 2 * `d_y`).
///
/// If `d_x` and `d_y` are positive the size of the rectangle is decreased; if
/// the values are negative, the size of the rectangle is increased.
///
/// If the resulting rectangle has a negative width or height, the size is
/// set to 0.
/// ## `d_x`
/// an horizontal value; a positive `d_x` will create an inset rectangle,
/// and a negative value will create a larger rectangle
/// ## `d_y`
/// a vertical value; a positive `d_x` will create an inset rectangle,
/// and a negative value will create a larger rectangle
pub fn inset(&mut self, d_x: f32, d_y: f32) {
unsafe {
ffi::clutter_rect_inset(self.to_glib_none_mut().0, d_x, d_y);
}
}
/// Computes the intersection of `self` and `b`, and places it in `res`, if `res`
/// is not `None`.
///
/// This function will normalize both `self` and `b` prior to computing their
/// intersection.
///
/// This function can be used to simply check if the intersection of `self` and `b`
/// is not empty, by using `None` for `res`.
/// ## `b`
/// a `Rect`
/// ## `res`
/// a `Rect`, or `None`
///
/// # Returns
///
/// `true` if the intersection of `self` and `b` is not empty
pub fn intersection(&mut self, b: &mut Rect) -> Option<Rect> {
unsafe {
let mut res = Rect::uninitialized();
let ret = from_glib(ffi::clutter_rect_intersection(
self.to_glib_none_mut().0,
b.to_glib_none_mut().0,
res.to_glib_none_mut().0,
));
if ret {
Some(res)
} else {
None
}
}
}
/// Normalizes a `Rect`.
///
/// A `Rect` is defined by the area covered by its size; this means
/// that a `Rect` with `Rect.origin` in [ 0, 0 ] and a
/// `Rect.size` of [ 10, 10 ] is equivalent to a `Rect` with
/// `Rect.origin` in [ 10, 10 ] and a `Rect.size` of [ -10, -10 ].
///
/// This function is useful to ensure that a rectangle has positive width
/// and height; it will modify the passed `self` and normalize its size.
pub fn normalize(&mut self) -> Option<Rect> {
unsafe { from_glib_full(ffi::clutter_rect_normalize(self.to_glib_none_mut().0)) }
}
/// Offsets the origin of `self` by the given values, after normalizing
/// the rectangle.
/// ## `d_x`
/// the horizontal offset value
/// ## `d_y`
/// the vertical offset value
pub fn offset(&mut self, d_x: f32, d_y: f32) {
unsafe {
ffi::clutter_rect_offset(self.to_glib_none_mut().0, d_x, d_y);
}
}
/// Computes the smallest possible rectangle capable of fully containing
/// both `self` and `b`, and places it into `res`.
///
/// This function will normalize both `self` and `b` prior to computing their
/// union.
/// ## `b`
/// a `Rect`
/// ## `res`
/// a `Rect`
pub fn union(&mut self, b: &mut Rect) -> Rect {
unsafe {
let mut res = Rect::uninitialized();
ffi::clutter_rect_union(
self.to_glib_none_mut().0,
b.to_glib_none_mut().0,
res.to_glib_none_mut().0,
);
res
}
}
/// A `Rect` with `Rect.origin` set at (0, 0) and a size
/// of 0.
///
/// The returned value can be used as a guard.
///
/// # Returns
///
/// a rectangle with origin in (0, 0) and a size of 0.
/// The returned `Rect` is owned by Clutter and it should not
/// be modified or freed.
pub fn zero() -> Option<Rect> {
unsafe { from_glib_none(ffi::clutter_rect_zero()) }
}
}
#[doc(hidden)]
impl Uninitialized for Rect {
#[inline]
unsafe fn uninitialized() -> Self {
Self::alloc()
}
}