1use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
6
7#[derive(Debug, PartialEq)]
9pub enum CursorMessage {
10 SetPosition(Position),
12 SetVisibility(bool),
14}
15
16#[derive(Clone, Copy, Debug, PartialEq)]
18pub struct Position {
19 pub x: f32,
21
22 pub y: f32,
24}
25
26impl Position {
27 pub fn clamp(target: &mut Position, min: Position, max: Position) {
28 if (*target).x < min.x {
29 (*target).x = min.x;
30 }
31 if (*target).x > max.x {
32 (*target).x = max.x;
33 }
34
35 if (*target).y < min.y {
36 (*target).y = min.y;
37 }
38 if (*target).y > max.y {
39 (*target).y = max.y;
40 }
41 }
42
43 pub fn clamp_size(target: &mut Position, min: Size, max: Size) {
44 if (*target).x < min.width {
45 (*target).x = min.width;
46 }
47 if (*target).x > max.width {
48 (*target).x = max.width;
49 }
50
51 if (*target).y < min.height {
52 (*target).y = min.height;
53 }
54 if (*target).y > max.height {
55 (*target).y = max.height;
56 }
57 }
58
59 pub fn zero() -> Position {
60 Position { x: 0.0, y: 0.0 }
61 }
62}
63
64impl Add for Position {
65 type Output = Self;
66
67 #[inline]
68 fn add(self, other: Self) -> Self {
69 Self { x: self.x + other.x, y: self.y + other.y }
70 }
71}
72
73impl AddAssign for Position {
74 #[inline]
75 fn add_assign(&mut self, other: Self) {
76 *self = Self { x: self.x + other.x, y: self.y + other.y };
77 }
78}
79
80impl Sub for Position {
81 type Output = Self;
82
83 #[inline]
84 fn sub(self, other: Self) -> Self {
85 Self { x: self.x - other.x, y: self.y - other.y }
86 }
87}
88
89impl SubAssign for Position {
90 #[inline]
91 fn sub_assign(&mut self, other: Self) {
92 *self = Self { x: self.x - other.x, y: self.y - other.y };
93 }
94}
95
96impl Div for Position {
97 type Output = Self;
98
99 #[inline]
100 fn div(self, other: Self) -> Self {
101 Self { x: self.x / other.x, y: self.y / other.y }
102 }
103}
104
105impl DivAssign for Position {
106 #[inline]
107 fn div_assign(&mut self, other: Self) {
108 *self = Self { x: self.x / other.x, y: self.y / other.y };
109 }
110}
111
112impl Mul for Position {
113 type Output = Self;
114
115 #[inline]
116 fn mul(self, other: Self) -> Self {
117 Self { x: self.x * other.x, y: self.y * other.y }
118 }
119}
120
121impl MulAssign for Position {
122 #[inline]
123 fn mul_assign(&mut self, other: Self) {
124 *self = Self { x: self.x * other.x, y: self.y * other.y };
125 }
126}
127
128impl Mul<Size> for Position {
129 type Output = Self;
130
131 #[inline]
132 fn mul(self, other: Size) -> Position {
133 Self { x: self.x * other.width, y: self.y * other.height }
134 }
135}
136
137macro_rules! scale_position_impl {
138 ($($t:ty)*) => ($(
139 impl Div<$t> for Position {
140 type Output = Position;
141
142 #[inline]
143 fn div(self, other: $t) -> Position {
144 Self { x: self.x / other as f32, y: self.y / other as f32 }
145 }
146 }
147
148 impl DivAssign<$t> for Position {
149 #[inline]
150 fn div_assign(&mut self, other: $t) {
151 *self = Self { x: self.x / other as f32, y: self.y / other as f32 };
152 }
153 }
154
155 impl Mul<$t> for Position {
156 type Output = Position;
157
158 #[inline]
159 fn mul(self, other: $t) -> Position {
160 Self { x: self.x * other as f32, y: self.y * other as f32 }
161 }
162 }
163
164 impl Mul<Position> for $t {
165 type Output = Position;
166
167 #[inline]
168 fn mul(self, other: Position) -> Position {
169 Position { x: self as f32 * other.x, y: self as f32 * other.y }
170 }
171 }
172
173 impl MulAssign<$t> for Position {
174 #[inline]
175 fn mul_assign(&mut self, other: $t) {
176 *self = Self { x: self.x * other as f32, y: self.y * other as f32 };
177 }
178 }
179 )*)
180}
181
182scale_position_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
183
184#[derive(Clone, Copy, Debug, PartialEq)]
186pub struct Size {
187 pub width: f32,
189
190 pub height: f32,
192}
193
194impl Size {
195 pub fn zero() -> Size {
196 Size { width: 0.0, height: 0.0 }
197 }
198}
199
200impl Add for Size {
201 type Output = Self;
202
203 #[inline]
204 fn add(self, other: Self) -> Self {
205 Self { width: self.width + other.width, height: self.height + other.height }
206 }
207}
208
209impl AddAssign for Size {
210 #[inline]
211 fn add_assign(&mut self, other: Self) {
212 *self = Self { width: self.width + other.width, height: self.height + other.height };
213 }
214}
215
216impl Sub for Size {
217 type Output = Self;
218
219 #[inline]
220 fn sub(self, other: Self) -> Self {
221 Self { width: self.width - other.width, height: self.height - other.height }
222 }
223}
224
225impl SubAssign for Size {
226 fn sub_assign(&mut self, other: Self) {
227 *self = Self { width: self.width - other.width, height: self.height - other.height };
228 }
229}
230
231impl Div for Size {
232 type Output = Self;
233
234 #[inline]
235 fn div(self, other: Self) -> Self {
236 Self { width: self.width / other.width, height: self.height / other.height }
237 }
238}
239
240impl DivAssign for Size {
241 #[inline]
242 fn div_assign(&mut self, other: Self) {
243 *self = Self { width: self.width / other.width, height: self.height / other.height };
244 }
245}
246
247impl Mul for Size {
248 type Output = Self;
249
250 #[inline]
251 fn mul(self, other: Self) -> Self {
252 Self { width: self.width * other.width, height: self.height * other.height }
253 }
254}
255
256impl MulAssign for Size {
257 #[inline]
258 fn mul_assign(&mut self, other: Self) {
259 *self = Self { width: self.width * other.width, height: self.height * other.height };
260 }
261}
262
263macro_rules! scale_size_impl {
264 ($($t:ty)*) => ($(
265 impl Div<$t> for Size {
266 type Output = Size;
267
268 #[inline]
269 fn div(self, other: $t) -> Size {
270 Self { width: self.width / other as f32, height: self.height / other as f32 }
271 }
272 }
273
274 impl DivAssign<$t> for Size {
275 #[inline]
276 fn div_assign(&mut self, other: $t) {
277 *self = Self { width: self.width / other as f32, height: self.height / other as f32 };
278 }
279 }
280
281 impl Mul<$t> for Size {
282 type Output = Size;
283
284 #[inline]
285 fn mul(self, other: $t) -> Size {
286 Self { width: self.width * other as f32, height: self.height * other as f32 }
287 }
288 }
289
290 impl MulAssign<$t> for Size {
291 #[inline]
292 fn mul_assign(&mut self, other: $t) {
293 *self = Self { width: self.width * other as f32, height: self.height * other as f32 };
294 }
295 }
296 )*)
297}
298
299scale_size_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
300
301pub fn euclidean_distance(p1: Position, p2: Position) -> f32 {
304 ((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)).sqrt()
305}