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
use crate::color::LinSrgba;
use crate::draw::primitive::{PathStroke, Primitive};
use crate::draw::properties::spatial::{orientation, position};
use crate::draw::properties::{
ColorScalar, Draw, Drawn, IntoDrawn, SetColor, SetOrientation, SetPosition, SetStroke,
};
use crate::draw::{self, Drawing};
use crate::geom::{self, pt2, Point2};
use crate::math::BaseFloat;
use lyon::tessellation::StrokeOptions;
#[derive(Clone, Debug)]
pub struct Line<S = geom::scalar::Default> {
path: PathStroke<S>,
start: Option<Point2<S>>,
end: Option<Point2<S>>,
}
pub type DrawingLine<'a, S = geom::scalar::Default> = Drawing<'a, Line<S>, S>;
impl<S> Line<S> {
pub fn weight(self, weight: f32) -> Self {
self.map_path(|p| p.stroke_weight(weight))
}
pub fn tolerance(self, tolerance: f32) -> Self {
self.map_path(|p| p.stroke_tolerance(tolerance))
}
pub fn start(mut self, start: Point2<S>) -> Self {
self.start = Some(start);
self
}
pub fn end(mut self, end: Point2<S>) -> Self {
self.end = Some(end);
self
}
pub fn points(self, start: Point2<S>, end: Point2<S>) -> Self {
self.start(start).end(end)
}
fn map_path<F>(self, map: F) -> Self
where
F: FnOnce(PathStroke<S>) -> PathStroke<S>,
{
let Line { path, start, end } = self;
let path = map(path);
Line { path, start, end }
}
}
impl<'a, S> DrawingLine<'a, S>
where
S: BaseFloat,
{
pub fn weight(self, weight: f32) -> Self {
self.map_ty(|ty| ty.weight(weight))
}
pub fn tolerance(self, tolerance: f32) -> Self {
self.map_ty(|ty| ty.tolerance(tolerance))
}
pub fn start(self, start: Point2<S>) -> Self {
self.map_ty(|ty| ty.start(start))
}
pub fn end(self, end: Point2<S>) -> Self {
self.map_ty(|ty| ty.end(end))
}
pub fn points(self, start: Point2<S>, end: Point2<S>) -> Self {
self.map_ty(|ty| ty.points(start, end))
}
}
impl<S> SetStroke for Line<S> {
fn stroke_options_mut(&mut self) -> &mut StrokeOptions {
SetStroke::stroke_options_mut(&mut self.path)
}
}
impl<S> SetOrientation<S> for Line<S> {
fn properties(&mut self) -> &mut orientation::Properties<S> {
SetOrientation::properties(&mut self.path)
}
}
impl<S> SetPosition<S> for Line<S> {
fn properties(&mut self) -> &mut position::Properties<S> {
SetPosition::properties(&mut self.path)
}
}
impl<S> SetColor<ColorScalar> for Line<S> {
fn rgba_mut(&mut self) -> &mut Option<LinSrgba> {
SetColor::rgba_mut(&mut self.path)
}
}
impl<S> From<Line<S>> for Primitive<S> {
fn from(prim: Line<S>) -> Self {
Primitive::Line(prim)
}
}
impl<S> Into<Option<Line<S>>> for Primitive<S> {
fn into(self) -> Option<Line<S>> {
match self {
Primitive::Line(prim) => Some(prim),
_ => None,
}
}
}
impl<S> IntoDrawn<S> for Line<S>
where
S: BaseFloat,
{
type Vertices = draw::properties::VerticesFromRanges;
type Indices = draw::properties::IndicesFromRange;
fn into_drawn(self, mut draw: Draw<S>) -> Drawn<S, Self::Vertices, Self::Indices> {
let Line { path, start, end } = self;
let start = start.unwrap_or_else(|| pt2(S::zero(), S::zero()));
let end = end.unwrap_or_else(|| pt2(S::zero(), S::zero()));
let points = [start, end];
let path = draw.drawing_context(|ctxt| path.points(ctxt, points.iter().cloned()));
path.into_drawn(draw)
}
}
impl<S> Default for Line<S> {
fn default() -> Self {
Line {
path: Default::default(),
start: Default::default(),
end: Default::default(),
}
}
}