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
use crate::{
    algorithms::{Bounded, Closest, ClosestPoint, Translate},
    Arc, BoundingBox, DrawingSpace, Line, Point, Vector,
};
use specs::prelude::*;

// for rustdoc links
#[allow(unused_imports)]
use crate::components::Layer;

/// Something which can be drawn on the screen.
#[derive(Debug, Clone, PartialEq)]
pub struct DrawingObject {
    pub geometry: Geometry,
    /// The [`Layer`] this [`DrawingObject`] is attached to.
    pub layer: Entity,
}

impl Component for DrawingObject {
    type Storage = FlaggedStorage<Self, DenseVecStorage<Self>>;
}

/// The geometry of a [`DrawingObject`].
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Geometry {
    Line(Line),
    Arc(Arc),
    Point(Point),
}

impl ClosestPoint<DrawingSpace> for Geometry {
    fn closest_point(&self, target: Point) -> Closest<DrawingSpace> {
        match self {
            Geometry::Point(p) => p.closest_point(target),
            Geometry::Line(l) => l.closest_point(target),
            Geometry::Arc(a) => a.closest_point(target),
        }
    }
}

impl ClosestPoint<DrawingSpace> for DrawingObject {
    fn closest_point(&self, target: Point) -> Closest<DrawingSpace> {
        self.geometry.closest_point(target)
    }
}

impl Bounded<DrawingSpace> for Geometry {
    fn bounding_box(&self) -> BoundingBox<DrawingSpace> {
        match self {
            Geometry::Line(line) => line.bounding_box(),
            Geometry::Arc(arc) => arc.bounding_box(),
            Geometry::Point(point) => point.bounding_box(),
        }
    }
}

impl Translate<DrawingSpace> for Geometry {
    fn translate(&mut self, displacement: Vector) {
        match self {
            Geometry::Point(ref mut point) => point.translate(displacement),
            Geometry::Line(ref mut line) => line.translate(displacement),
            Geometry::Arc(ref mut arc) => arc.translate(displacement),
        }
    }
}

impl Translate<DrawingSpace> for DrawingObject {
    fn translate(&mut self, displacement: Vector) {
        self.geometry.translate(displacement);
    }
}