-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathpiston.rs
224 lines (208 loc) · 5.9 KB
/
piston.rs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use piston_window::context::Context;
use piston_window::ellipse::circle;
use piston_window::{circle_arc, ellipse, line, rectangle, Event, Loop};
use piston_window::{G2d, PistonWindow};
use super::DummyBackendError;
use crate::drawing::backend::{BackendCoord, BackendStyle, DrawingBackend, DrawingErrorKind};
use crate::style::{Color, RGBAColor};
pub struct PistonBackend<'a, 'b> {
size: (u32, u32),
scale: f64,
context: Context,
graphics: &'b mut G2d<'a>,
}
fn make_piston_rgba(color: &RGBAColor) -> [f32; 4] {
let (r, g, b) = color.rgb();
let a = color.alpha();
[
r as f32 / 255.0,
g as f32 / 255.0,
b as f32 / 255.0,
a as f32,
]
}
fn make_point_pair(a: BackendCoord, b: BackendCoord, scale: f64) -> [f64; 4] {
[
a.0 as f64 * scale,
a.1 as f64 * scale,
b.0 as f64 * scale,
b.1 as f64 * scale,
]
}
fn make_circle(center: BackendCoord, radius: u32, scale: f64) -> [f64; 4] {
circle(
center.0 as f64 * scale,
center.1 as f64 * scale,
radius as f64 * scale,
)
}
impl<'a, 'b> PistonBackend<'a, 'b> {
pub fn new(size: (u32, u32), scale: f64, context: Context, graphics: &'b mut G2d<'a>) -> Self {
Self {
size,
context,
graphics,
scale,
}
}
}
impl<'a, 'b> DrawingBackend for PistonBackend<'a, 'b> {
type ErrorType = DummyBackendError;
fn get_size(&self) -> (u32, u32) {
self.size
}
fn ensure_prepared(&mut self) -> Result<(), DrawingErrorKind<DummyBackendError>> {
Ok(())
}
fn present(&mut self) -> Result<(), DrawingErrorKind<DummyBackendError>> {
Ok(())
}
fn draw_pixel(
&mut self,
point: BackendCoord,
color: &RGBAColor,
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
piston_window::rectangle(
make_piston_rgba(color),
make_point_pair(point, (1, 1), self.scale),
self.context.transform,
self.graphics,
);
Ok(())
}
fn draw_line<S: BackendStyle>(
&mut self,
from: BackendCoord,
to: BackendCoord,
style: &S,
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
line(
make_piston_rgba(&style.as_color()),
self.scale,
make_point_pair(from, to, self.scale),
self.context.transform,
self.graphics,
);
Ok(())
}
fn draw_rect<S: BackendStyle>(
&mut self,
upper_left: BackendCoord,
bottom_right: BackendCoord,
style: &S,
fill: bool,
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
if fill {
rectangle(
make_piston_rgba(&style.as_color()),
make_point_pair(
upper_left,
(bottom_right.0 - upper_left.0, bottom_right.1 - upper_left.1),
self.scale,
),
self.context.transform,
self.graphics,
);
} else {
let color = make_piston_rgba(&style.as_color());
let [x0, y0, x1, y1] = make_point_pair(upper_left, bottom_right, self.scale);
line(
color,
self.scale,
[x0, y0, x0, y1],
self.context.transform,
self.graphics,
);
line(
color,
self.scale,
[x0, y1, x1, y1],
self.context.transform,
self.graphics,
);
line(
color,
self.scale,
[x1, y1, x1, y0],
self.context.transform,
self.graphics,
);
line(
color,
self.scale,
[x1, y0, x0, y0],
self.context.transform,
self.graphics,
);
}
Ok(())
}
fn draw_circle<S: BackendStyle>(
&mut self,
center: BackendCoord,
radius: u32,
style: &S,
fill: bool,
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
let rect = make_circle(center, radius, self.scale);
if fill {
ellipse(
make_piston_rgba(&style.as_color()),
rect,
self.context.transform,
self.graphics,
);
} else {
circle_arc(
make_piston_rgba(&style.as_color()),
self.scale,
std::f64::consts::PI,
0.0,
rect,
self.context.transform,
self.graphics,
);
circle_arc(
make_piston_rgba(&style.as_color()),
self.scale,
0.0,
std::f64::consts::PI,
rect,
self.context.transform,
self.graphics,
);
}
Ok(())
}
}
#[allow(clippy::single_match)]
pub fn draw_piston_window<F: FnOnce(PistonBackend) -> Result<(), Box<dyn std::error::Error>>>(
window: &mut PistonWindow,
draw: F,
) -> Option<Event> {
if let Some(event) = window.next() {
window.draw_2d(&event, |c, g, _| match event {
Event::Loop(Loop::Render(arg)) => {
draw(PistonBackend::new(
(arg.draw_size[0], arg.draw_size[1]),
arg.window_size[0] / arg.draw_size[0] as f64,
c,
g,
))
.ok();
}
_ => {}
});
return Some(event);
}
None
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_make_circle() {
assert_eq!(make_circle((1, 1), 0, 1.0), [1.0, 1.0, 0.0, 0.0]);
assert_eq!(make_circle((1, 2), 3, 4.0), [-8.0, -4.0, 24.0, 24.0]);
}
}