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
use crate::{fragment::Bounds, util, Cell, Point};
use std::fmt;
use ncollide2d::shape::{Polyline, Segment, Shape};
use sauron::{
html::attributes::*,
svg::{attributes::*, *},
Node,
};
use std::cmp::Ordering;
#[derive(Debug, Clone)]
pub struct Rect {
pub start: Point,
pub end: Point,
pub is_filled: bool,
pub radius: Option<f32>,
pub is_broken: bool,
}
impl Rect {
pub(in crate) fn new(
start: Point,
end: Point,
is_filled: bool,
is_broken: bool,
) -> Self {
let mut rect = Rect {
start,
end,
is_filled,
radius: None,
is_broken,
};
rect.sort_reorder_end_points();
rect
}
pub(in crate) fn rounded_new(
start: Point,
end: Point,
is_filled: bool,
radius: f32,
is_broken: bool,
) -> Self {
let mut rect = Rect {
start,
end,
is_filled,
radius: Some(radius),
is_broken,
};
rect.sort_reorder_end_points();
rect
}
pub(in crate) fn sort_reorder_end_points(&mut self) {
if self.start > self.end {
let tmp_start = self.start;
self.start = self.end;
self.end = tmp_start;
}
}
pub(in crate) fn absolute_position(&self, cell: Cell) -> Self {
Rect {
start: cell.absolute_position(self.start),
end: cell.absolute_position(self.end),
..*self
}
}
pub(in crate) fn scale(&self, scale: f32) -> Self {
Rect {
start: self.start.scale(scale),
end: self.end.scale(scale),
radius: self.radius.map(|r| r * scale),
..*self
}
}
pub(crate) fn width(&self) -> f32 {
self.end.x - self.start.x
}
pub(crate) fn height(&self) -> f32 {
self.end.y - self.start.y
}
pub(crate) fn is_broken(&self) -> bool {
self.is_broken
}
pub fn is_rounded(&self) -> bool {
if let Some(ref r) = &self.radius {
*r > 0.0
} else {
false
}
}
}
impl Bounds for Rect {
fn bounds(&self) -> (Point, Point) {
let aabb = Segment::new(*self.start, *self.end).local_aabb();
(Point::from(*aabb.mins), Point::from(*aabb.maxs))
}
}
impl fmt::Display for Rect {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "R {} {}", self.start, self.end)
}
}
impl Into<Polyline<f32>> for Rect {
fn into(self) -> Polyline<f32> {
Polyline::new(
vec![
*self.start,
*Point::new(self.end.x, self.start.y),
*self.end,
*Point::new(self.start.x, self.end.y),
*self.start,
],
None,
)
}
}
impl<MSG> Into<Node<MSG>> for Rect {
fn into(self) -> Node<MSG> {
rect(
vec![
x(self.start.x),
y(self.start.y),
width(self.width()),
height(self.height()),
classes_flag([
("broken", self.is_broken),
("solid", !self.is_broken),
("filled", self.is_filled),
("nofill", !self.is_filled),
]),
if let Some(radius) = self.radius {
rx(radius)
} else {
rx(0)
},
],
vec![],
)
}
}
impl Eq for Rect {}
impl Ord for Rect {
fn cmp(&self, other: &Self) -> Ordering {
self.start
.cmp(&other.start)
.then(self.end.cmp(&other.end))
.then(self.is_filled.cmp(&other.is_filled))
.then(util::opt_ord(self.radius, other.radius))
.then(self.is_broken.cmp(&other.is_broken))
}
}
impl PartialOrd for Rect {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Rect {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}