buffer_graphics_lib/shapes/
mod.rs1pub mod collection;
2pub mod polyline;
3
4use crate::drawable::{DrawType, Drawable};
5use crate::drawing::Renderable;
6use crate::Graphics;
7use graphics_shapes::prelude::*;
8use graphics_shapes::shape_box::ShapeBox;
9
10impl<S: Shape + Clone> Renderable<S> for Drawable<S> {
11 fn render(&self, graphics: &mut Graphics) {
12 let color = self.draw_type().color();
13
14 for px in self.drawing_points() {
15 graphics.set_pixel(px.x, px.y, color);
16 }
17 }
18}
19
20pub trait CreateDrawable<T: Clone> {
21 fn from_obj(obj: T, draw_type: DrawType) -> Drawable<T>;
22}
23
24impl CreateDrawable<Line> for Drawable<Line> {
25 fn from_obj(line: Line, draw_type: DrawType) -> Drawable<Line> {
26 let drawing_points = line.outline_pixels();
27 Drawable::new(line, draw_type, drawing_points)
28 }
29}
30
31macro_rules! create_drawable_from_points {
32 ($shape: ty) => {
33 impl CreateDrawable<$shape> for Drawable<$shape> {
34 fn from_obj(shape: $shape, draw_type: DrawType) -> Drawable<$shape> {
35 let drawing_points = if draw_type.is_stroke() {
36 shape.outline_pixels()
37 } else {
38 shape.filled_pixels()
39 };
40 Drawable::new(shape, draw_type, drawing_points)
41 }
42 }
43 };
44}
45
46create_drawable_from_points!(Triangle);
47create_drawable_from_points!(Rect);
48create_drawable_from_points!(Polygon);
49create_drawable_from_points!(Circle);
50create_drawable_from_points!(Ellipse);
51create_drawable_from_points!(ShapeBox);