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
use macroquad::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct URect {
pub x: u32,
pub y: u32,
#[serde(rename = "width", alias = "w")]
pub w: u32,
#[serde(rename = "height", alias = "h")]
pub h: u32,
}
impl URect {
pub fn new(x: u32, y: u32, w: u32, h: u32) -> Self {
URect { x, y, w, h }
}
pub fn point(&self) -> UVec2 {
uvec2(self.x, self.y)
}
pub fn size(&self) -> UVec2 {
uvec2(self.w, self.h)
}
pub fn left(&self) -> u32 {
self.x
}
pub fn right(&self) -> u32 {
self.x + self.w
}
pub fn top(&self) -> u32 {
self.y
}
pub fn bottom(&self) -> u32 {
self.y + self.h
}
pub fn move_to(&mut self, destination: UVec2) {
self.x = destination.x;
self.y = destination.y;
}
pub fn scale(&mut self, sx: u32, sy: u32) {
self.w *= sx;
self.h *= sy;
}
pub fn contains(&self, point: UVec2) -> bool {
point.x >= self.left()
&& point.x < self.right()
&& point.y < self.bottom()
&& point.y >= self.top()
}
pub fn overlaps(&self, other: &URect) -> bool {
self.left() <= other.right()
&& self.right() >= other.left()
&& self.top() <= other.bottom()
&& self.bottom() >= other.top()
}
pub fn combine_with(self, other: URect) -> Self {
let x = u32::min(self.x, other.x);
let y = u32::min(self.y, other.y);
let w = u32::max(self.right(), other.right()) - x;
let h = u32::max(self.bottom(), other.bottom()) - y;
URect { x, y, w, h }
}
pub fn intersect(&self, other: URect) -> Option<Self> {
let left = self.x.max(other.x);
let top = self.y.max(other.y);
let right = self.right().min(other.right());
let bottom = self.bottom().min(other.bottom());
if right < left || bottom < top {
return None;
}
Some(URect {
x: left,
y: top,
w: right - left,
h: bottom - top,
})
}
pub fn offset(self, offset: UVec2) -> Self {
URect::new(self.x + offset.x, self.y + offset.y, self.w, self.h)
}
}
impl From<Rect> for URect {
fn from(rect: Rect) -> Self {
URect {
x: rect.x as u32,
y: rect.y as u32,
w: rect.w as u32,
h: rect.h as u32,
}
}
}
impl From<(UVec2, UVec2)> for URect {
fn from((position, size): (UVec2, UVec2)) -> Self {
URect {
x: position.x,
y: position.y,
w: size.x,
h: size.y,
}
}
}
impl From<URect> for Rect {
fn from(urect: URect) -> Rect {
Rect {
x: urect.x as f32,
y: urect.y as f32,
w: urect.w as f32,
h: urect.h as f32,
}
}
}