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
#[macro_use]
mod def_macro;
define_two_property_arithmetic_struct!(Position, UPosition, FPosition, x, y, ORIGIN, "({}, {})");
define_two_property_arithmetic_struct!(Size, USize, FSize, width, height, ZERO, "{}x{}");
impl FPosition {
pub fn round(self) -> Position {
Position::new(self.x.round() as i32, self.y.round() as i32)
}
pub fn truncate(self) -> Position {
Position::new(self.x.trunc() as i32, self.y.trunc() as i32)
}
pub fn truncate_u(self) -> UPosition {
UPosition::new(self.x.trunc() as u32, self.y.trunc() as u32)
}
}
impl Size {
pub fn area(self) -> i32 {
self.width * self.height
}
}
impl USize {
pub fn area(self) -> u32 {
self.width * self.height
}
}
impl FSize {
pub fn area(self) -> f32 {
self.width * self.height
}
}
#[derive(Copy, Clone, Default, PartialEq, Eq, Debug)]
#[cfg_attr(
feature = "serialization",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
pub struct Rectangle {
pub position: Position,
pub size: USize,
}
impl Rectangle {
pub fn new(position: Position, size: USize) -> Self {
Self { position, size }
}
pub fn new_from_raw(x: i32, y: i32, width: u32, height: u32) -> Self {
Self {
position: Position::new(x, y),
size: USize::new(width, height),
}
}
pub fn contains_position(&self, position: Position) -> bool {
position.x >= self.position.x
&& position.x <= self.position.x + self.size.width as i32
&& position.y >= self.position.y
&& position.y <= self.position.y + self.size.height as i32
}
pub fn contains_fposition(&self, position: FPosition) -> bool {
position.x >= self.position.x as f32
&& position.x <= self.position.x as f32 + self.size.width as f32
&& position.y >= self.position.y as f32
&& position.y <= self.position.y as f32 + self.size.height as f32
}
}
#[derive(Copy, Clone, Default, PartialEq, Debug)]
#[cfg_attr(
feature = "serialization",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
pub struct FRectangle {
pub position: FPosition,
pub size: FSize,
}
impl FRectangle {
pub fn new(position: FPosition, size: FSize) -> Self {
assert!(size.width >= 0.0);
assert!(size.height >= 0.0);
Self { position, size }
}
pub fn new_from_raw(x: f32, y: f32, width: f32, height: f32) -> Self {
assert!(width >= 0.0);
assert!(height >= 0.0);
Self {
position: FPosition::new(x, y),
size: FSize::new(width, height),
}
}
pub fn contains_position(&self, position: FPosition) -> bool {
position.x >= self.position.x
&& position.x <= self.position.x + self.size.width
&& position.y >= self.position.y
&& position.y <= self.position.y + self.size.height
}
}
impl std::ops::Add<Size> for Position {
type Output = Rectangle;
fn add(self, rhs: Size) -> Self::Output {
Rectangle::new(self, USize::new(rhs.width as u32, rhs.height as u32))
}
}
impl std::ops::Add<USize> for Position {
type Output = Rectangle;
fn add(self, rhs: USize) -> Self::Output {
Rectangle::new(self, rhs)
}
}
impl std::ops::Add<FSize> for FPosition {
type Output = FRectangle;
fn add(self, rhs: FSize) -> Self::Output {
FRectangle::new(self, rhs)
}
}