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
pub mod collection;
pub mod polyline;

use crate::drawable::{DrawType, Drawable};
use crate::drawing::Renderable;
use crate::Graphics;
use graphics_shapes::prelude::*;
use graphics_shapes::shape_box::ShapeBox;

impl<S: Shape + Clone> Renderable<S> for Drawable<S> {
    fn render(&self, graphics: &mut Graphics) {
        let color = self.draw_type().color();

        for px in self.drawing_points() {
            graphics.set_pixel(px.x, px.y, color);
        }
    }
}

pub trait CreateDrawable<T: Clone> {
    fn from_obj(obj: T, draw_type: DrawType) -> Drawable<T>;
}

impl CreateDrawable<Line> for Drawable<Line> {
    fn from_obj(line: Line, draw_type: DrawType) -> Drawable<Line> {
        let drawing_points = line.outline_pixels();
        Drawable::new(line, draw_type, drawing_points)
    }
}

macro_rules! create_drawable_from_points {
    ($shape: ty) => {
        impl CreateDrawable<$shape> for Drawable<$shape> {
            fn from_obj(shape: $shape, draw_type: DrawType) -> Drawable<$shape> {
                let drawing_points = if draw_type.is_stroke() {
                    shape.outline_pixels()
                } else {
                    shape.filled_pixels()
                };
                Drawable::new(shape, draw_type, drawing_points)
            }
        }
    };
}

create_drawable_from_points!(Triangle);
create_drawable_from_points!(Rect);
create_drawable_from_points!(Polygon);
create_drawable_from_points!(Circle);
create_drawable_from_points!(Ellipse);
create_drawable_from_points!(ShapeBox);