dora_ssr/dora/
rect.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn rect_release(raw: i64);
11	fn rect_set_origin(slf: i64, val: i64);
12	fn rect_get_origin(slf: i64) -> i64;
13	fn rect_set_size(slf: i64, val: i64);
14	fn rect_get_size(slf: i64) -> i64;
15	fn rect_set_x(slf: i64, val: f32);
16	fn rect_get_x(slf: i64) -> f32;
17	fn rect_set_y(slf: i64, val: f32);
18	fn rect_get_y(slf: i64) -> f32;
19	fn rect_set_width(slf: i64, val: f32);
20	fn rect_get_width(slf: i64) -> f32;
21	fn rect_set_height(slf: i64, val: f32);
22	fn rect_get_height(slf: i64) -> f32;
23	fn rect_set_left(slf: i64, val: f32);
24	fn rect_get_left(slf: i64) -> f32;
25	fn rect_set_right(slf: i64, val: f32);
26	fn rect_get_right(slf: i64) -> f32;
27	fn rect_set_center_x(slf: i64, val: f32);
28	fn rect_get_center_x(slf: i64) -> f32;
29	fn rect_set_center_y(slf: i64, val: f32);
30	fn rect_get_center_y(slf: i64) -> f32;
31	fn rect_set_bottom(slf: i64, val: f32);
32	fn rect_get_bottom(slf: i64) -> f32;
33	fn rect_set_top(slf: i64, val: f32);
34	fn rect_get_top(slf: i64) -> f32;
35	fn rect_set_lower_bound(slf: i64, val: i64);
36	fn rect_get_lower_bound(slf: i64) -> i64;
37	fn rect_set_upper_bound(slf: i64, val: i64);
38	fn rect_get_upper_bound(slf: i64) -> i64;
39	fn rect_set(slf: i64, x: f32, y: f32, width: f32, height: f32);
40	fn rect_contains_point(slf: i64, point: i64) -> i32;
41	fn rect_intersects_rect(slf: i64, rect: i64) -> i32;
42	fn rect_equals(slf: i64, other: i64) -> i32;
43	fn rect_new(origin: i64, size: i64) -> i64;
44	fn rect_zero() -> i64;
45}
46impl PartialEq for Rect {
47	/// Checks if two rectangles are equal.
48	///
49	/// # Arguments
50	///
51	/// * `other` - The other rectangle to compare to, represented by a Rect object.
52	///
53	/// # Returns
54	///
55	/// * `bool` - Whether or not the two rectangles are equal.
56	fn eq(&self, other: &Self) -> bool {
57		unsafe { return rect_equals(self.raw(), other.raw()) != 0 }
58	}
59}
60/// A rectangle object with a left-bottom origin position and a size.
61pub struct Rect { raw: i64 }
62impl Drop for Rect {
63	fn drop(&mut self) { unsafe { rect_release(self.raw); } }
64}
65impl Rect {
66	pub(crate) fn raw(&self) -> i64 {
67		self.raw
68	}
69	pub(crate) fn from(raw: i64) -> Rect {
70		Rect { raw: raw }
71	}
72	/// Sets the position of the origin of the rectangle.
73	pub fn set_origin(&mut self, val: &crate::dora::Vec2) {
74		unsafe { rect_set_origin(self.raw(), val.into_i64()) };
75	}
76	/// Gets the position of the origin of the rectangle.
77	pub fn get_origin(&self) -> crate::dora::Vec2 {
78		return unsafe { crate::dora::Vec2::from(rect_get_origin(self.raw())) };
79	}
80	/// Sets the dimensions of the rectangle.
81	pub fn set_size(&mut self, val: &crate::dora::Size) {
82		unsafe { rect_set_size(self.raw(), val.into_i64()) };
83	}
84	/// Gets the dimensions of the rectangle.
85	pub fn get_size(&self) -> crate::dora::Size {
86		return unsafe { crate::dora::Size::from(rect_get_size(self.raw())) };
87	}
88	/// Sets the x-coordinate of the origin of the rectangle.
89	pub fn set_x(&mut self, val: f32) {
90		unsafe { rect_set_x(self.raw(), val) };
91	}
92	/// Gets the x-coordinate of the origin of the rectangle.
93	pub fn get_x(&self) -> f32 {
94		return unsafe { rect_get_x(self.raw()) };
95	}
96	/// Sets the y-coordinate of the origin of the rectangle.
97	pub fn set_y(&mut self, val: f32) {
98		unsafe { rect_set_y(self.raw(), val) };
99	}
100	/// Gets the y-coordinate of the origin of the rectangle.
101	pub fn get_y(&self) -> f32 {
102		return unsafe { rect_get_y(self.raw()) };
103	}
104	/// Sets the width of the rectangle.
105	pub fn set_width(&mut self, val: f32) {
106		unsafe { rect_set_width(self.raw(), val) };
107	}
108	/// Gets the width of the rectangle.
109	pub fn get_width(&self) -> f32 {
110		return unsafe { rect_get_width(self.raw()) };
111	}
112	/// Sets the height of the rectangle.
113	pub fn set_height(&mut self, val: f32) {
114		unsafe { rect_set_height(self.raw(), val) };
115	}
116	/// Gets the height of the rectangle.
117	pub fn get_height(&self) -> f32 {
118		return unsafe { rect_get_height(self.raw()) };
119	}
120	/// Sets the left edge in x-axis of the rectangle.
121	pub fn set_left(&mut self, val: f32) {
122		unsafe { rect_set_left(self.raw(), val) };
123	}
124	/// Gets the left edge in x-axis of the rectangle.
125	pub fn get_left(&self) -> f32 {
126		return unsafe { rect_get_left(self.raw()) };
127	}
128	/// Sets the right edge in x-axis of the rectangle.
129	pub fn set_right(&mut self, val: f32) {
130		unsafe { rect_set_right(self.raw(), val) };
131	}
132	/// Gets the right edge in x-axis of the rectangle.
133	pub fn get_right(&self) -> f32 {
134		return unsafe { rect_get_right(self.raw()) };
135	}
136	/// Sets the x-coordinate of the center of the rectangle.
137	pub fn set_center_x(&mut self, val: f32) {
138		unsafe { rect_set_center_x(self.raw(), val) };
139	}
140	/// Gets the x-coordinate of the center of the rectangle.
141	pub fn get_center_x(&self) -> f32 {
142		return unsafe { rect_get_center_x(self.raw()) };
143	}
144	/// Sets the y-coordinate of the center of the rectangle.
145	pub fn set_center_y(&mut self, val: f32) {
146		unsafe { rect_set_center_y(self.raw(), val) };
147	}
148	/// Gets the y-coordinate of the center of the rectangle.
149	pub fn get_center_y(&self) -> f32 {
150		return unsafe { rect_get_center_y(self.raw()) };
151	}
152	/// Sets the bottom edge in y-axis of the rectangle.
153	pub fn set_bottom(&mut self, val: f32) {
154		unsafe { rect_set_bottom(self.raw(), val) };
155	}
156	/// Gets the bottom edge in y-axis of the rectangle.
157	pub fn get_bottom(&self) -> f32 {
158		return unsafe { rect_get_bottom(self.raw()) };
159	}
160	/// Sets the top edge in y-axis of the rectangle.
161	pub fn set_top(&mut self, val: f32) {
162		unsafe { rect_set_top(self.raw(), val) };
163	}
164	/// Gets the top edge in y-axis of the rectangle.
165	pub fn get_top(&self) -> f32 {
166		return unsafe { rect_get_top(self.raw()) };
167	}
168	/// Sets the lower bound (left-bottom) of the rectangle.
169	pub fn set_lower_bound(&mut self, val: &crate::dora::Vec2) {
170		unsafe { rect_set_lower_bound(self.raw(), val.into_i64()) };
171	}
172	/// Gets the lower bound (left-bottom) of the rectangle.
173	pub fn get_lower_bound(&self) -> crate::dora::Vec2 {
174		return unsafe { crate::dora::Vec2::from(rect_get_lower_bound(self.raw())) };
175	}
176	/// Sets the upper bound (right-top) of the rectangle.
177	pub fn set_upper_bound(&mut self, val: &crate::dora::Vec2) {
178		unsafe { rect_set_upper_bound(self.raw(), val.into_i64()) };
179	}
180	/// Gets the upper bound (right-top) of the rectangle.
181	pub fn get_upper_bound(&self) -> crate::dora::Vec2 {
182		return unsafe { crate::dora::Vec2::from(rect_get_upper_bound(self.raw())) };
183	}
184	/// Sets the properties of the rectangle.
185	///
186	/// # Arguments
187	///
188	/// * `x` - The x-coordinate of the origin of the rectangle.
189	/// * `y` - The y-coordinate of the origin of the rectangle.
190	/// * `width` - The width of the rectangle.
191	/// * `height` - The height of the rectangle.
192	pub fn set(&mut self, x: f32, y: f32, width: f32, height: f32) {
193		unsafe { rect_set(self.raw(), x, y, width, height); }
194	}
195	/// Checks if a point is inside the rectangle.
196	///
197	/// # Arguments
198	///
199	/// * `point` - The point to check, represented by a Vec2 object.
200	///
201	/// # Returns
202	///
203	/// * `bool` - Whether or not the point is inside the rectangle.
204	pub fn contains_point(&self, point: &crate::dora::Vec2) -> bool {
205		unsafe { return rect_contains_point(self.raw(), point.into_i64()) != 0; }
206	}
207	/// Checks if the rectangle intersects with another rectangle.
208	///
209	/// # Arguments
210	///
211	/// * `rect` - The other rectangle to check for intersection with, represented by a Rect object.
212	///
213	/// # Returns
214	///
215	/// * `bool` - Whether or not the rectangles intersect.
216	pub fn intersects_rect(&self, rect: &crate::dora::Rect) -> bool {
217		unsafe { return rect_intersects_rect(self.raw(), rect.raw()) != 0; }
218	}
219	/// Creates a new rectangle object using a Vec2 object for the origin and a Size object for the size.
220	///
221	/// # Arguments
222	///
223	/// * `origin` - The origin of the rectangle, represented by a Vec2 object.
224	/// * `size` - The size of the rectangle, represented by a Size object.
225	///
226	/// # Returns
227	///
228	/// * `Rect` - A new rectangle object.
229	pub fn new(origin: &crate::dora::Vec2, size: &crate::dora::Size) -> crate::dora::Rect {
230		unsafe { return crate::dora::Rect::from(rect_new(origin.into_i64(), size.into_i64())); }
231	}
232	/// Gets a rectangle object with all properties set to 0.
233	pub fn zero() -> crate::dora::Rect {
234		unsafe { return crate::dora::Rect::from(rect_zero()); }
235	}
236}