sketchbook 0.0.2

Interactive visual applications in Rust
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! Draw on page.
//!
//! This aspect enables 2D drawing for an environment.
//!
//! ```
//! use sketchbook::aspects::draw;
//! # sketchbook::draw_env!();
//!
//! #[apply(derive_sketch)]
//! #[sketch(env=single_aspect, aspects=(draw))]
//! struct App {
//!     #[page]
//!     page: single_aspect::Page,
//! }
//!
//! impl draw::Handlers for App {
//!     fn draw(&mut self) {
//!         self.background((100, 100, 100));
//!
//!         self.circle((60, 60, 10));
//!         self.rect((40, 80, 80, 100));
//!     }
//! }
//! ```

/// Create test environment for draw aspect for use in doc tests.
#[doc(hidden)]
#[macro_export]
macro_rules! draw_env {
    {} => {
        use sketchbook::derive_sketch;
        use macro_rules_attribute::apply;
        use sketchbook::aspects::draw::SketchExt;
        mod single_aspect {
            use sketchbook::aspects::draw;
            use sketchbook::aspects::draw::*;
            use sketchbook::Size2;

            sketchbook::env_for_aspect!(draw);

            sketchbook::compose! {
                pub enum Events {
                    #[part]
                    Aspect(draw::Event<EnvSpecificMarker>),
                }
            }

            sketchbook::compose! {
                #[derive(Default)]
                pub struct Page {
                    #[part]
                    pub aspect: draw::EnvData<EnvSpecificMarker>,
                }
            }

            impl draw::EnvSpecific for EnvSpecificMarker {
                type Num = f32;
                type Color = (u8, u8, u8);
                type ShapeBuffer = Vec<Shape<f32, (u8, u8, u8)>>;
                fn default_size() -> Size2<Self::Num> { Size2 { width: 0.0, height: 0.0 } }
                fn default_context() -> Context<Self::Num, Self::Color> { Context::default() }
                fn default_background() -> Self::Color { (0, 0, 0) }
                fn default_frame_rate() -> Self::Num { 1.0 }
                fn no_color() -> Self::Color { (0, 0, 0) }
            }
        }
    }
}

mod circle;
mod context;
mod mode;
mod rectangle;
mod shape;

#[cfg(feature = "color")]
mod color;

pub use circle::*;
pub use context::*;
pub use mode::*;
pub use rectangle::*;
pub use shape::*;

#[cfg(feature = "color")]
pub use color::*;

use crate::{geometry::*, Real};
use crate::real::ToReal;

use core::marker::PhantomData;

use crate::{compose::AsPart, Environment, PageOf, Sketch};

/// Event handlers for draw aspect.
///
/// Each handler has a default implementation that does nothing.
/// Implement a method to perform actions when the associated event happens.
pub trait Handlers
where
    Self: Sketch,
    Self::Env: AssociatedEnvSpecificMarker,
{
    /// Handler for [`Event::Draw`] event.
    #[inline]
    fn draw(&mut self) {
        // by default do nothing
    }
}

/// Extension methods for sketch.
///
/// These methods are automatically implemented for sketches where the 
/// environment implements the draw aspect. These methods are also available
/// on the page for the environment.
pub trait SketchExt<M>
where
    Self: AsPart<EnvData<M>>,
    M: EnvSpecific + 'static,
{
    /// Get the width of the page.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         assert_eq!(self.width(), self.page.width());
    ///     }
    /// }
    /// ```
    #[inline]
    fn width(&self) -> NumFor<M> {
        self.as_part().size.width
    }

    /// Get the height of the page.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         assert_eq!(self.height(), self.page.height());
    ///     }
    /// }
    /// ```
    #[inline]
    fn height(&self) -> NumFor<M> {
        self.as_part().size.height
    }

    /// Get the size of the page.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         assert_eq!(self.size(), self.page.size());
    ///     }
    /// }
    /// ```
    #[inline]
    fn size(&self) -> Size2<NumFor<M>> {
        self.as_part().size
    }

    /// Set the rectangle mode.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # use sketchbook::aspects::draw::Mode;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.rect_mode(Mode::Corner);
    ///         self.page.rect_mode(Mode::Corner);
    ///
    ///         assert_eq!(self.context().rectangle_mode, Mode::Corner);
    ///     }
    /// }
    /// ```
    #[inline]
    fn rect_mode(&mut self, mode: Mode) {
        self.as_part_mut().context.rectangle_mode = mode;
    }

    /// Get the ellipse mode.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # use sketchbook::aspects::draw::Mode;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.ellipse_mode(Mode::Radius);
    ///         self.page.ellipse_mode(Mode::Radius);
    ///
    ///         assert_eq!(self.context().ellipse_mode, Mode::Corner);
    ///     }
    /// }
    /// ```
    #[inline]
    fn ellipse_mode(&mut self, mode: Mode) {
        self.as_part_mut().context.ellipse_mode = mode;
    }

    /// Set the fill color for shapes.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.fill((100, 100, 100));
    ///         self.page.fill((100, 100, 100));
    ///
    ///         assert_eq!(self.context().fill, (100, 100, 100));
    ///     }
    /// }
    /// ```
    #[inline]
    fn fill<C: Into<ColorFor<M>>>(&mut self, color: C) {
        self.as_part_mut().context.fill = color.into();
    }
    
    /// Disable fill for shapes.
    ///
    /// This uses the color defined by [`EnvSpecific::no_color()`].
    /// As such, it is equivalent to calling `.fill(no_color())`.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.no_fill();
    ///         self.page.no_fill();
    ///
    ///         assert_eq!(self.context().fill, (0, 0, 0));
    ///     }
    /// }
    /// ```
    #[inline]
    fn no_fill(&mut self) {
        self.as_part_mut().context.fill = M::no_color();
    }

    /// Set the stroke color for shapes.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.stroke((100, 100, 100));
    ///         self.page.stroke((100, 100, 100));
    ///
    ///         assert_eq!(self.context().stroke, (100, 100, 100));
    ///     }
    /// }
    /// ```
    #[inline]
    fn stroke<C: Into<ColorFor<M>>>(&mut self, color: C) {
        self.as_part_mut().context.stroke = color.into();
    }

    /// Disable stroke for shapes.
    ///
    /// This uses the color defined by [`EnvSpecific::no_color()`].
    /// Additionally, the weight of the stroke is set to zero.
    /// As such, it is equivalent to calling 
    /// ```ignore
    /// page.stroke(no_color());
    /// page.weight(0);
    /// ```
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.no_stroke();
    ///         self.page.no_stroke();
    ///
    ///         assert_eq!(self.context().stroke, (0, 0, 0));
    ///         assert_eq!(self.context().weight, 0.0);
    ///     }
    /// }
    /// ```
    #[inline]
    fn no_stroke(&mut self) {
        self.as_part_mut().context.stroke = M::no_color();
        self.as_part_mut().context.weight = M::Num::zero();
    }

    /// Set the stroke weight for shapes.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.weight(10.0);
    ///         self.page.weight(10.0);
    ///
    ///         assert_eq!(self.context().weight, 10.0);
    ///     }
    /// }
    /// ```
    #[inline]
    fn weight<R: ToReal<NumFor<M>>>(&mut self, width: R) {
        self.as_part_mut().context.weight = width.to_real();
    }

    /// Set the background color of the page.
    ///
    /// This also clears all previously drawn shapes.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.background((100, 100, 100));
    ///         self.page.background((100, 100, 100));
    ///     }
    /// }
    /// ```
    fn background<C: Into<ColorFor<M>>>(&mut self, color: C) {
        let color = color.into();

        // clear shape buffer
        let part = self.as_part_mut();
        part.shapes.shape_buffer_clear();
        part.shapes
            .shape_buffer_push(Shape::Background(color.clone()));

        // set background color
        part.background = color;
    }

    /// Draw a circle.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.circle((43, 12, 100));
    ///         self.page.circle((43, 12, 100));
    ///     }
    /// }
    /// ```
    fn circle<S: IntoWithContext<Circle<NumFor<M>, ColorFor<M>>, NumFor<M>, ColorFor<M>>>(
        &mut self,
        circle: S,
    ) {
        let circle = circle.into_with_context(&self.as_part().context);
        self.as_part_mut().shapes.shape_buffer_push(Shape::Circle(circle));
    }

    /// Draw a rectangle.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.rect((43, 12, 100, 200));
    ///         self.page.rect((43, 12, 100, 200));
    ///     }
    /// }
    /// ```
    fn rect<S: IntoWithContext<Rectangle<NumFor<M>, ColorFor<M>>, NumFor<M>, ColorFor<M>>>(
        &mut self,
        rectangle: S,
    ) {
        let rect = rectangle.into_with_context(&self.as_part().context);
        self.as_part_mut().shapes.shape_buffer_push(Shape::Rectangle(rect));
    }

    /// set the frame rate.
    ///
    /// The environment may not be able to provide the requested rate.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         self.frame_rate(60);
    ///         self.page.frame_rate(60);
    ///     }
    /// }
    /// ```
    #[inline]
    fn frame_rate<R: ToReal<NumFor<M>>>(&mut self, rate: R) {
        self.as_part_mut().frame_rate = rate.to_real()
    }
    
    /// Get the current context.
    ///
    /// ```
    /// # use sketchbook::aspects::draw;
    /// # sketchbook::draw_env!();
    /// # #[apply(derive_sketch)]
    /// # #[sketch(env=single_aspect, aspects=(draw))]
    /// # struct App {
    /// #     #[page]
    /// #     page: single_aspect::Page,
    /// # }
    /// #
    /// impl draw::Handlers for App {
    ///     fn draw(&mut self) {
    ///         assert_eq!(self.context(), self.page.context());
    ///     }
    /// }
    /// ```
    #[inline]
    fn context(&self) -> &Context<NumFor<M>, ColorFor<M>> {
        &self.as_part().context
    }
}

/// Implemented on sketches and environment pages.
impl<M, T> SketchExt<M> for T
where
    Self: AsPart<EnvData<M>>,
    M: EnvSpecific,
{
    // default impls used
}

/// Run event handler methods based on event.
///
/// A user usually doesn't need to use this directly as the
/// [`crate::derive_sketch`] macro will use it automatically.
/// This trait is implemented for the sketch when the `draw`
/// aspect is passed to the `aspects=(...)` list as seen in the
/// example below.
///
/// ```
/// use sketchbook::aspects::draw;
/// # sketchbook::draw_env!();
/// #[apply(derive_sketch)]
/// #[sketch(env=single_aspect, aspects=(draw))]
/// struct App {
///     #[page]
///     page: single_aspect::Page,
/// }
/// 
/// impl draw::Handlers for App {
///     fn draw(&mut self) {
///         // This handler will now be called whenever an event happens.
///     }
/// }
/// ```
pub trait RunHandlers
where
    Self: Sketch + Handlers,
    Self::Env: AssociatedEnvSpecificMarker + Environment,
    PageOf<Self::Env>: AsPart<EnvData<SpecificallyFor<Self::Env>>>,
{
    /// Run event handler for event.
    fn run_handlers(&mut self, event: &Event<SpecificallyFor<Self::Env>>) {
        match event {
            Event::Draw => {
                // let context = &mut self.as_part_mut().context;
                // context.scale = 1.;
                // context.rotation = 0.;
                // context.translation = Point { x: 0., y: 0. };
                //
                self.draw();
            }
            Event::_MetaPhantom(_, _) => unreachable!(),
        }
    }
}

/// Possible events for draw aspect.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Event<M>
where
    M: EnvSpecific,
{
    /// Needed to allow the use of `M`.
    #[doc(hidden)]
    #[cfg_attr(feature = "serde", serde(skip))]
    _MetaPhantom(core::convert::Infallible, PhantomData<M>),

    /// Redraw requested.
    ///
    /// It is common for the environment to emit this event when the page is resized.
    Draw,
}

/// Marker trait for getting environment specific marker type.
pub trait AssociatedEnvSpecificMarker {
    /// Marker type that has environment specific implementations on it.
    type EnvSpecificMarker: EnvSpecific;
}

/// Marker trait for environment specific types.
pub trait EnvSpecific: 'static {
    /// Type for numbers.
    type Num: Real;

    /// Type for colors.
    type Color: Clone;

    /// Type for drawn shape buffer.
    type ShapeBuffer: ShapeBuffer<Self::Num, Self::Color>;

    /// Get the default size of page.
    fn default_size() -> Size2<Self::Num>;

    /// Get the default context for a page.
    fn default_context() -> Context<Self::Num, Self::Color>;

    /// Get the default background color for a page.
    fn default_background() -> Self::Color;

    /// Get the default frame rate for a page.
    fn default_frame_rate() -> Self::Num;

    /// Get value for no color.
    ///
    /// Used by [`SketchExt::no_stroke()`] and [`SketchExt::no_fill()`].
    fn no_color() -> Self::Color;
}

/// Trait for types that can be used as shape buffers.
///
/// This allows environments to do real time drawing instead of having it buffered.
pub trait ShapeBuffer<N, C> {
    /// Push a shape onto the buffer.
    fn shape_buffer_push(&mut self, shape: Shape<N, C>);
    
    /// Clear the buffer.
    fn shape_buffer_clear(&mut self);
}

#[cfg(feature = "alloc")]
#[allow(unused_qualifications)]
impl<N, C> ShapeBuffer<N, C> for alloc::vec::Vec<Shape<N, C>> {
    #[inline]
    fn shape_buffer_push(&mut self, shape: Shape<N, C>) {
        self.push(shape)
    }

    #[inline]
    fn shape_buffer_clear(&mut self) {
        self.clear()
    }
}

/// Data for draw aspect that the environment's page will hold.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct EnvData<M>
where
    M: EnvSpecific,
{
    /// Store the marker type.
    marker: PhantomData<M>,

    /// Requested frame rate for drawing.
    frame_rate: M::Num,

    /// Shape buffer to push shapes onto.
    shapes: M::ShapeBuffer,

    /// Currently set background color.
    background: M::Color,

    /// Size of the draw area.
    size: Size2<M::Num>,

    /// Current context for creating shapes.
    context: Context<M::Num, M::Color>,
}

impl<M> EnvData<M>
where
    M: EnvSpecific,
{
    /// Set the page's size.
    #[inline]
    pub fn set_size(&mut self, size: Size2<M::Num>) {
        self.size = size;
    }

    /// Get the frame rate.
    #[inline]
    pub fn frame_rate(&self) -> M::Num {
        self.frame_rate
    }

    /// Get the background color.
    #[inline]
    pub fn background_color(&self) -> &M::Color {
        &self.background
    }

    /// Get drawn frame.
    #[inline]
    pub fn shape_buffer_mut(&mut self) -> &mut M::ShapeBuffer {
        &mut self.shapes
    }
}

impl<M> Default for EnvData<M>
where
    M: EnvSpecific,
    M::ShapeBuffer: Default,
{
    #[inline]
    fn default() -> Self {
        Self {
            marker: PhantomData,
            size: M::default_size(),
            context: M::default_context(),
            shapes: M::ShapeBuffer::default(),
            background: M::default_background(),
            frame_rate: M::default_frame_rate(),
        }
    }
}

/// Helper type alias to get number type for environment.
pub type NumFor<T> = <T as EnvSpecific>::Num;

/// Helper type alias to get color type for environment.
pub type ColorFor<T> = <T as EnvSpecific>::Color;

/// Helper type alias to get type specifically for environment.
pub type SpecificallyFor<T> = <T as AssociatedEnvSpecificMarker>::EnvSpecificMarker;

#[test]
fn test_draw() {
    use crate::aspects::draw;
    use crate::env::*;
    use crate::*;

    // create environment for test
    mod single_aspect {
        use crate::aspects::draw;
        use crate::aspects::draw::*;

        crate::env_for_aspect!(draw);

        crate::compose! {
            pub enum Events {
                #[part]
                Aspect(Event<EnvSpecificMarker>),
            }
        }

        crate::compose! {
            #[derive(Default)]
            pub struct Page {
                #[part]
                pub aspect: EnvData<EnvSpecificMarker>,
                pub time: i16,
            }
        }

        impl EnvSpecific for EnvSpecificMarker {
            type Num = f32;

            type Color = u8;

            type ShapeBuffer = Vec<Shape<f32, u8>>;

            fn default_size() -> Size2<Self::Num> {
                Size2 {
                    width: 0.0,
                    height: 0.0,
                }
            }

            fn default_context() -> Context<Self::Num, Self::Color> {
                Context {
                    fill: 0,
                    ellipse_mode: Mode::Center,
                    rectangle_mode: Mode::Center,
                    rotation: 0.0,
                    stroke: 0,
                    weight: 0.0,
                }
            }

            fn default_background() -> Self::Color {
                0
            }

            fn default_frame_rate() -> Self::Num {
                0.0
            }

            fn no_color() -> Self::Color {
                0 
            }
        }
    }

    // create sketch
    derive_sketch! {
        #[sketch(
            env = single_aspect,
            aspects = (draw),
        )]
        struct App {
            #[page] page: single_aspect::Page,
            drew: bool,
        }
    }

    impl Setup for App {
        fn setup(page: single_aspect::Page, _: ()) -> Self {
            App { page, drew: false }
        }
    }

    impl Handlers for App {
        fn draw(&mut self) {
            // mark flag when this handler is run
            self.drew = true;
        }
    }

    // create sketch instance.
    let mut mill = single_aspect::Mill;
    let mut app = App::setup(mill.new_page(), ());

    assert!(!app.drew);

    // handle draw event
    app.handle_environment_event(&single_aspect::Events::Aspect(draw::Event::Draw));

    // check that handler ran
    assert!(app.drew);
}