pizarra 3.0.1

The backend for a simple vector hand-drawing application
Documentation
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
use crate::shape::{ShapeBuilder, ShapeFinished};
use crate::draw_commands::{DrawCommand, circle_helper, cancel_helper};
use crate::path_command::PathCommand::*;
use crate::point::{Vec2D, WorldUnit, ScreenUnit};
use crate::transform::Transform;
use crate::style::Style;
use crate::geom::{rombus_to_rectangle, project};
use crate::shape::builder::grid::grid_from_cell_and_point;
use crate::shape::ShapeStored;
use crate::shape::stored::path::Path;

#[derive(Debug, Copy, Clone)]
enum State {
    Initial(Vec2D<WorldUnit>),

    OneDimension {
        start: Vec2D<WorldUnit>,
        free: Vec2D<WorldUnit>
    },

    TwoDimension {
        start: Vec2D<WorldUnit>,
        p1: Vec2D<WorldUnit>,
        free: Vec2D<WorldUnit>,
    },

    DefineGrid {
        start: Vec2D<WorldUnit>,
        p1: Vec2D<WorldUnit>,
        p2: Vec2D<WorldUnit>,
        free: Vec2D<WorldUnit>,

        /// A transform matrix that turns the parallelogram defined by start, p1
        /// and p2 into a square of size 1 in the first quadrant
        t: Transform,
    },
}

/// This tools builds a grid through a 5-step process. It might seem complicated
/// at first but it is flexible and easy to learn.
///
/// 1. First place a point, this will be a corner of the grid.
#[derive(Debug, Copy, Clone)]
pub struct FreeGrid {
    style: Style<WorldUnit>,
    state: State,
}

impl FreeGrid {
    pub fn start(initial: Vec2D<WorldUnit>, style: Style<WorldUnit>) -> FreeGrid {
        FreeGrid {
            style,
            state: State::Initial(initial),
        }
    }
}

impl ShapeBuilder for FreeGrid {
    fn handle_mouse_moved(&mut self, pos: Vec2D<ScreenUnit>, t: Transform, _snap: ScreenUnit) {
        let wpos = t.to_world_coordinates(pos);

        match self.state {
            State::Initial(_) => {
                self.state = State::Initial(wpos);
            }

            State::OneDimension { start, .. } => {
                self.state = State::OneDimension { start, free: wpos };
            }

            State::TwoDimension { start, p1, .. } => {
                self.state = State::TwoDimension { start, p1, free: wpos };
            }

            State::DefineGrid { start, p1, p2, t, .. } => {
                self.state = State::DefineGrid { start, p1, p2, t, free: wpos };
            }
        }
    }

    fn handle_button_pressed(&mut self, _pos: Vec2D<ScreenUnit>, _t: Transform, _snap: ScreenUnit) { }

    fn handle_button_released(&mut self, pos: Vec2D<ScreenUnit>, t: Transform, snap: ScreenUnit) -> ShapeFinished {
        let wpos = t.to_world_coordinates(pos);

        match self.state {
            State::Initial(p) => {
                self.state = State::OneDimension { start: p, free: wpos };

                ShapeFinished::No
            }

            State::OneDimension { start, free } => {
                self.state = State::TwoDimension { start, p1: free, free: wpos };

                if t.to_screen_coordinates(start).distance(pos) < snap {
                    ShapeFinished::Cancelled
                } else {
                    ShapeFinished::No
                }
            }

            State::TwoDimension { start, p1, free } => {
                let p2 = free;

                self.state = State::DefineGrid {
                    start, p1, p2,
                    t: rombus_to_rectangle(start.to_vec2d(), p1.to_vec2d(), p2.to_vec2d()),
                    free: wpos,
                };

                let sstart = t.to_screen_coordinates(start);
                let sp1 = t.to_screen_coordinates(p1);
                let proj = t.to_screen_coordinates(project(free, [start, p1]));
                let proj2 = t.to_screen_coordinates(project(start, [p1, free]));

                if sstart.distance(pos) < snap || sp1.distance(pos) < snap || proj.distance(pos) < snap || proj2.distance(sstart) < snap {
                    ShapeFinished::Cancelled
                } else {
                    ShapeFinished::No
                }
            }

            State::DefineGrid { start, p2, free, t, ..} => {
                let cell = (
                    t.to_screen_coordinates(start),
                    t.to_screen_coordinates(p2),
                );
                let free = t.to_screen_coordinates(free);
                let grid = grid_from_cell_and_point(cell, free);

                let mut shapes: Vec<Box<dyn ShapeStored>> = Vec::with_capacity(
                    grid.row_count() * (grid.col_count() - 1) +
                    grid.col_count() * (grid.row_count() - 1)
                );

                let rows = grid.rows();
                let cols = grid.cols();

                for row in rows.iter() {
                    for (&a, &b) in row.iter().zip(row.iter().skip(1)) {
                        shapes.push(Box::new(Path::from_parts(
                            vec![
                                MoveTo(t.to_world_coordinates(a)),
                                LineTo(t.to_world_coordinates(b)),
                            ],
                            self.style,
                        )));
                    }
                }

                for col in cols.iter() {
                    for (&a, &b) in col.iter().zip(col.iter().skip(1)) {
                        shapes.push(Box::new(Path::from_parts(
                            vec![
                                MoveTo(t.to_world_coordinates(a)),
                                LineTo(t.to_world_coordinates(b)),
                            ],
                            self.style,
                        )));
                    }
                }

                ShapeFinished::Yes(shapes)
            }
        }
    }

    fn draw_commands(&self, t: Transform, snap: ScreenUnit) -> Vec<DrawCommand> {
        match self.state {
            State::Initial(p) => vec![
                circle_helper(t.to_screen_coordinates(p), snap),
            ],

            State::OneDimension { start, free } => vec![
                DrawCommand::Path {
                    commands: vec![
                        MoveTo(start),
                        LineTo(free),
                    ],
                    style: self.style,
                },
                cancel_helper(start, free, t, snap),
            ],

            State::TwoDimension { start, p1, free } => vec![
                DrawCommand::Path {
                    commands: vec![
                        MoveTo(start),
                        LineTo(p1),
                        LineTo(free),
                        LineTo(start + (free - p1)),
                        LineTo(start),
                    ],
                    style: self.style,
                },

                cancel_helper(start, free, t, snap),
                cancel_helper(p1, free, t, snap),
                // the projection of the mouse on the line defined by the first
                // two points
                cancel_helper(project(free, [start, p1]), free, t, snap),
                // the projection of the first point on the line defined by the
                // last two
                cancel_helper(project(start, [p1, free]), start, t, snap),
            ],

            State::DefineGrid { start, p1: _, p2, t, free } => {
                let cell = (
                    t.apply(start.to_vec2d()),
                    t.apply(p2.to_vec2d()),
                );
                let free = t.apply(free.to_vec2d());
                let inverse = t.invert();
                let grid = grid_from_cell_and_point(cell, free);

                let mut commands = Vec::with_capacity(grid.row_count() + grid.col_count());

                for row in grid.rows() {
                    commands.push(DrawCommand::Path {
                        commands: vec![
                            MoveTo(inverse.apply(row[0]).into()),
                            LineTo(inverse.apply(*row.last().unwrap()).into()),
                        ],
                        style: self.style,
                    });
                }

                for col in grid.cols() {
                    commands.push(DrawCommand::Path {
                        commands: vec![
                            MoveTo(inverse.apply(col[0]).into()),
                            LineTo(inverse.apply(*col.last().unwrap()).into()),
                        ],
                        style: self.style,
                    });
                }

                commands
            }
        }
    }
}

#[cfg(test)]
mod tests  {
    use pretty_assertions::assert_eq;

    use super::*;

    const SNAP: ScreenUnit = ScreenUnit::from_float(10.0);

    #[test]
    fn figure_is_cancelled() {
        let t = Default::default();
        let mut grid = FreeGrid::start(Vec2D::new_world(0.0, 0.0), Default::default());

        grid.handle_mouse_moved((1.0, 1.0).into(), t, SNAP);

        // a circle goes around the cursor
        assert_eq!(grid.draw_commands(t, SNAP), vec![
            DrawCommand::ScreenCircle {
                center: Vec2D::new_screen(1.0, 1.0),
                radius: 10.0.into(),
                style: Style::circle_helper(),
            }
        ]);

        // start the grid at the origin
        grid.handle_mouse_moved((0.0, 0.0).into(), t, SNAP);
        grid.handle_button_released((0.0, 0.0).into(), t, SNAP);

        // vove a little bit and observe a circle around the first point
        grid.handle_mouse_moved((1.0, 1.0).into(), t, SNAP);

        assert_eq!(grid.draw_commands(t, SNAP), vec![
            DrawCommand::Path {
                commands: vec![
                    MoveTo((0.0, 0.0).into()),
                    LineTo((1.0, 1.0).into()),
                ],
                style: Default::default(),
            },

            DrawCommand::ScreenCircle {
                center: Vec2D::new_screen(0.0, 0.0),
                radius: SNAP,
                style: Style::red_circle_helper(),
            },
        ]);

        Some(grid).map(|mut grid| {
            // releasing here would cause a cancel
            grid.handle_button_pressed((1.0, 1.0).into(), t, SNAP);
            match grid.handle_button_released((1.0, 1.0).into(), t, SNAP) {
                ShapeFinished::Cancelled => {}
                _ => panic!(),
            }
        }).unwrap();

        // move to the second point and use it to define the grid
        grid.handle_mouse_moved((20.0, 0.0).into(), t, SNAP);
        grid.handle_button_pressed((20.0, 0.0).into(), t, SNAP);
        grid.handle_button_released((20.0, 0.0).into(), t, SNAP);

        // moving around the two original points would threaten to cancel the
        // shape
        grid.handle_mouse_moved((1.0, 1.0).into(), t, SNAP);

        assert_eq!(grid.draw_commands(t, SNAP), vec![
            DrawCommand::Path {
                commands: vec![
                    MoveTo((0.0, 0.0).into()),
                    LineTo((20.0, 0.0).into()),
                    LineTo((1.0, 1.0).into()),
                    LineTo((-19.0, 1.0).into()),
                    LineTo((0.0, 0.0).into()),
                ],
                style: Default::default(),
            },

            // first circle red
            DrawCommand::ScreenCircle {
                center: (0.0, 0.0).into(),
                radius: SNAP,
                style: Style::red_circle_helper(),
            },

            // second circle gray
            DrawCommand::ScreenCircle {
                center: (20.0, 0.0).into(),
                radius: SNAP,
                style: Style::circle_helper(),
            },

            // third circle
            DrawCommand::ScreenCircle {
                center: (1.0, 0.0).into(),
                radius: SNAP,
                style: Style::red_circle_helper(),
            },

            // fourth circle
            DrawCommand::ScreenCircle {
                center: (0.0552486187845318, 1.0497237569060773).into(),
                radius: SNAP,
                style: Style::red_circle_helper(),
            },
        ]);

        // releasing here would cancel
        Some(grid).map(|mut grid| {
            grid.handle_button_pressed((1.0, 1.0).into(), t, SNAP);
            match grid.handle_button_released((1.0, 1.0).into(), t, SNAP) {
                ShapeFinished::Cancelled => {}
                _ => panic!()
            }
        }).unwrap();

        grid.handle_mouse_moved((21.0, 1.0).into(), t, SNAP);

        assert_eq!(grid.draw_commands(t, SNAP), vec![
            DrawCommand::Path {
                commands: vec![
                    MoveTo((0.0, 0.0).into()),
                    LineTo((20.0, 0.0).into()),
                    LineTo((21.0, 1.0).into()),
                    LineTo((1.0, 1.0).into()),
                    LineTo((0.0, 0.0).into()),
                ],
                style: Default::default(),
            },

            // first circle red
            DrawCommand::ScreenCircle {
                center: (0.0, 0.0).into(),
                radius: SNAP,
                style: Style::circle_helper(),
            },

            // second circle gray
            DrawCommand::ScreenCircle {
                center: (20.0, 0.0).into(),
                radius: SNAP,
                style: Style::red_circle_helper(),
            },

            DrawCommand::ScreenCircle {
                center: (21.0, 0.0).into(),
                radius: SNAP,
                style: Style::red_circle_helper(),
            },

            DrawCommand::ScreenCircle {
                center: (10.0, -10.0).into(),
                radius: SNAP,
                style: Style::circle_helper(),
            },
        ]);

        // releasing here would cancel
        Some(grid).map(|mut grid| {
            grid.handle_button_pressed((21.0, 1.0).into(), t, SNAP);
            match grid.handle_button_released((21.0, 1.0).into(), t, SNAP) {
                ShapeFinished::Cancelled => {}
                _ => panic!()
            }
        }).unwrap();
    }
}