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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use crate::colors::PaletteColor;
use crate::coords::Coord;
use enigo::{Enigo, MouseButton, MouseControllable};
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::f32;
use std::fs::File;
use std::i32;
use std::io::{Read, Write};
use std::thread;
use std::time::Duration;
#[derive(Serialize, Deserialize)]
pub struct EaselCoords {
pub portrait_bounds: (Coord, Coord),
pub landscape_bounds: (Coord, Coord),
pub paintbrush: Coord,
pub spray_can: Coord,
pub pen: Coord,
pub decrease_brush: Coord,
pub increase_brush: Coord,
pub change_orientation: Coord,
pub color_start: Coord,
pub color_row_step: i32,
pub color_col_step: i32,
}
impl EaselCoords {
pub fn new(path: String) -> Result<EaselCoords, Box<dyn Error>> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let win: EaselCoords = serde_json::from_str(&contents)?;
Ok(win)
}
pub fn save(&self, path: &str) -> Result<(), Box<dyn Error>> {
let j = serde_json::to_string_pretty(self)?;
let mut file = File::create(&path)?;
file.write_all(j.as_bytes())?;
Ok(())
}
}
#[derive(PartialEq)]
pub enum Orientation {
Portrait,
Landscape,
}
#[allow(dead_code)]
#[derive(PartialEq)]
pub enum Tool {
Paintbrush,
Pen,
Spraycan,
}
const NUM_BRUSH_STEPS: i32 = 16;
const STARTING_COLOR: PaletteColor = PaletteColor::Black;
const STARTING_TOOL: Tool = Tool::Paintbrush;
#[derive(Debug)]
pub enum EaselError {
OutOfBounds,
NoCoord,
}
impl std::fmt::Display for EaselError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EaselError::OutOfBounds => write!(f, "out of bounds"),
EaselError::NoCoord => write!(f, "invalid coordinates"),
}
}
}
impl Error for EaselError {}
pub struct Easel {
pub mouse: Enigo,
pub mouse_wait: Duration,
pub easel_coords: EaselCoords,
pub orientation: Orientation,
pub brush_size: i32,
pub current_color: PaletteColor,
pub current_tool: Tool,
}
impl Easel {
pub fn new(path: String, mouse: Enigo, mouse_wait: Duration) -> Result<Easel, Box<dyn Error>> {
let easel_coords = EaselCoords::new(path)?;
let orientation = Orientation::Portrait;
let mut easel = Easel {
mouse,
mouse_wait,
easel_coords,
orientation,
brush_size: 0,
current_color: STARTING_COLOR,
current_tool: STARTING_TOOL,
};
println!("Resetting easel to known configuration.");
println!("Assuming a default orientation of portrait.");
easel.change_brush_size(16);
easel.change_brush_size(0);
easel.change_tool(STARTING_TOOL);
easel.change_color(&STARTING_COLOR);
println!("Easel configuration complete.");
Ok(easel)
}
fn click_custom_wait(&mut self, wait: Duration) {
self.mouse.mouse_down(MouseButton::Left);
thread::sleep(wait);
self.mouse.mouse_up(MouseButton::Left);
thread::sleep(wait);
}
fn click(&mut self) {
self.click_custom_wait(self.mouse_wait);
}
fn move_and_click(&mut self, coord: &Coord) {
self.mouse.mouse_move_to(coord.x, coord.y);
self.click();
}
pub fn change_orientation(&mut self) {
let orient_coords = self.easel_coords.change_orientation;
self.move_and_click(&orient_coords);
self.orientation = match self.orientation {
Orientation::Portrait => Orientation::Landscape,
Orientation::Landscape => Orientation::Portrait,
};
}
pub fn change_tool(&mut self, tool: Tool) {
let coords = match tool {
Tool::Paintbrush => self.easel_coords.paintbrush,
Tool::Pen => self.easel_coords.pen,
Tool::Spraycan => self.easel_coords.spray_can,
};
self.move_and_click(&coords);
self.current_tool = tool;
}
pub fn get_bounds(&self) -> (Coord, Coord) {
match self.orientation {
Orientation::Portrait => self.easel_coords.portrait_bounds,
Orientation::Landscape => self.easel_coords.landscape_bounds,
}
}
pub fn change_color(&mut self, color: &PaletteColor) {
if *color != self.current_color {
let color_pos = color.get_row_col();
let row_step = self.easel_coords.color_row_step;
let col_step = self.easel_coords.color_col_step;
let color_coords = Coord::new(
self.easel_coords.color_start.x + (color_pos.x * row_step),
self.easel_coords.color_start.y + (color_pos.y * col_step),
);
self.move_and_click(&color_coords);
self.current_color = *color;
}
}
pub fn change_brush_size(&mut self, brush_size: i32) {
let mouse_wait = Duration::from_millis(32);
let brush_size = brush_size.max(0).min(NUM_BRUSH_STEPS);
let brush_coords = if brush_size > self.brush_size {
self.easel_coords.increase_brush
} else {
self.easel_coords.decrease_brush
};
let num_clicks = (brush_size - self.brush_size).abs();
self.move_and_click(&brush_coords);
for _ in 1..num_clicks {
self.click_custom_wait(mouse_wait);
}
self.brush_size = brush_size;
}
pub fn draw_line(
&mut self,
start_line: Coord,
end_line: Coord,
color: &PaletteColor,
) -> Result<(), Box<dyn Error>> {
self.draw_shape(&[start_line, end_line], color, false, false)
}
pub fn draw_shape(
&mut self,
points: &[Coord],
color: &PaletteColor,
close_shape: bool,
fill: bool,
) -> Result<(), Box<dyn Error>> {
let (start, end) = self.get_bounds();
self.change_color(color);
let start_point = match points.get(0) {
Some(p) => p,
None => Err(EaselError::NoCoord)?,
};
let start_point = start + start_point;
if start_point.x > end.x || start_point.y > end.y {
println!("point: {:?}, end: {:?}", start_point, end);
Err(EaselError::OutOfBounds)?
}
self.mouse.mouse_move_to(start_point.x, start_point.y);
thread::sleep(self.mouse_wait);
self.mouse.mouse_down(MouseButton::Left);
for point in points.iter() {
let point = start + point;
if point.x > end.x || point.y > end.y {
println!("point: {:?}, end: {:?}", point, end);
Err(EaselError::OutOfBounds)?
}
self.mouse.mouse_move_to(point.x, point.y);
thread::sleep(self.mouse_wait);
}
if close_shape || fill {
self.mouse.mouse_move_to(start_point.x, start_point.y);
thread::sleep(self.mouse_wait);
}
self.mouse.mouse_up(MouseButton::Left);
thread::sleep(self.mouse_wait);
if fill {
self.fill(points, color)?;
}
Ok(())
}
fn fill(&mut self, points: &[Coord], color: &PaletteColor) -> Result<(), Box<dyn Error>> {
let mut edges: Vec<[&Coord; 2]> = points.windows(2).map(|pts| [&pts[0], &pts[1]]).collect();
edges.push([points.first().unwrap(), points.last().unwrap()]);
let slope: Vec<f32> = edges
.iter()
.map(|pts| {
if pts[1].x == pts[0].x {
0.0
} else {
(pts[1].y - pts[0].y) as f32 / (pts[1].x - pts[0].x) as f32
}
})
.collect();
let start_y = points.iter().fold(i32::MAX, |acc, pnt| acc.min(pnt.y));
let end_y = points.iter().fold(0, |acc, pnt| acc.max(pnt.y));
let mut iy = start_y;
let old_brush_size = self.brush_size;
self.change_brush_size(0);
while iy < end_y {
let mut active_edges = vec![];
for (edge, slope) in edges.iter().zip(slope.iter()) {
let max = edge[0].y.max(edge[1].y);
let min = edge[0].y.min(edge[1].y);
if max > iy && min < iy && max != min {
active_edges.push((edge, slope));
}
}
let x_draw: Vec<i32> = active_edges
.iter()
.map(|&(pts, m)| {
if *m == 0.0 {
pts[0].x
} else {
(pts[0].x as f32 + 1.0 / m * (iy - pts[0].y) as f32) as i32
}
})
.collect();
let mut in_poly = true;
for x in x_draw.windows(2) {
if x[0] != x[1] && in_poly {
self.draw_line(Coord::new(x[0], iy), Coord::new(x[1], iy), color)?;
}
in_poly = !in_poly;
}
iy += 6;
}
self.change_brush_size(old_brush_size);
Ok(())
}
}