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
|
use iced_native::{Point, Size, Vector};
use crate::{
canvas::{Fill, Path, Stroke},
triangle,
};
#[derive(Debug)]
pub struct Frame {
width: f32,
height: f32,
buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u16>,
transforms: Transforms,
}
#[derive(Debug)]
struct Transforms {
previous: Vec<Transform>,
current: Transform,
}
#[derive(Debug, Clone, Copy)]
struct Transform {
raw: lyon::math::Transform,
is_identity: bool,
}
impl Frame {
pub fn new(width: f32, height: f32) -> Frame {
Frame {
width,
height,
buffers: lyon::tessellation::VertexBuffers::new(),
transforms: Transforms {
previous: Vec::new(),
current: Transform {
raw: lyon::math::Transform::identity(),
is_identity: true,
},
},
}
}
#[inline]
pub fn width(&self) -> f32 {
self.width
}
#[inline]
pub fn height(&self) -> f32 {
self.height
}
#[inline]
pub fn size(&self) -> Size {
Size::new(self.width, self.height)
}
#[inline]
pub fn center(&self) -> Point {
Point::new(self.width / 2.0, self.height / 2.0)
}
pub fn fill(&mut self, path: &Path, fill: Fill) {
use lyon::tessellation::{
BuffersBuilder, FillOptions, FillTessellator,
};
let mut buffers = BuffersBuilder::new(
&mut self.buffers,
FillVertex(match fill {
Fill::Color(color) => color.into_linear(),
}),
);
let mut tessellator = FillTessellator::new();
let result = if self.transforms.current.is_identity {
tessellator.tessellate_path(
path.raw(),
&FillOptions::default(),
&mut buffers,
)
} else {
let path = path.transformed(&self.transforms.current.raw);
tessellator.tessellate_path(
path.raw(),
&FillOptions::default(),
&mut buffers,
)
};
let _ = result.expect("Tessellate path");
}
pub fn stroke(&mut self, path: &Path, stroke: Stroke) {
use lyon::tessellation::{
BuffersBuilder, StrokeOptions, StrokeTessellator,
};
let mut buffers = BuffersBuilder::new(
&mut self.buffers,
StrokeVertex(stroke.color.into_linear()),
);
let mut tessellator = StrokeTessellator::new();
let mut options = StrokeOptions::default();
options.line_width = stroke.width;
options.start_cap = stroke.line_cap.into();
options.end_cap = stroke.line_cap.into();
options.line_join = stroke.line_join.into();
let result = if self.transforms.current.is_identity {
tessellator.tessellate_path(path.raw(), &options, &mut buffers)
} else {
let path = path.transformed(&self.transforms.current.raw);
tessellator.tessellate_path(path.raw(), &options, &mut buffers)
};
let _ = result.expect("Stroke path");
}
#[inline]
pub fn with_save(&mut self, f: impl FnOnce(&mut Frame)) {
self.transforms.previous.push(self.transforms.current);
f(self);
self.transforms.current = self.transforms.previous.pop().unwrap();
}
#[inline]
pub fn translate(&mut self, translation: Vector) {
self.transforms.current.raw = self
.transforms
.current
.raw
.pre_translate(lyon::math::Vector::new(
translation.x,
translation.y,
));
self.transforms.current.is_identity = false;
}
#[inline]
pub fn rotate(&mut self, angle: f32) {
self.transforms.current.raw = self
.transforms
.current
.raw
.pre_rotate(lyon::math::Angle::radians(-angle));
self.transforms.current.is_identity = false;
}
#[inline]
pub fn scale(&mut self, scale: f32) {
self.transforms.current.raw =
self.transforms.current.raw.pre_scale(scale, scale);
self.transforms.current.is_identity = false;
}
pub fn into_mesh(self) -> triangle::Mesh2D {
triangle::Mesh2D {
vertices: self.buffers.vertices,
indices: self.buffers.indices,
}
}
}
struct FillVertex([f32; 4]);
impl lyon::tessellation::FillVertexConstructor<triangle::Vertex2D>
for FillVertex
{
fn new_vertex(
&mut self,
position: lyon::math::Point,
_attributes: lyon::tessellation::FillAttributes<'_>,
) -> triangle::Vertex2D {
triangle::Vertex2D {
position: [position.x, position.y],
color: self.0,
}
}
}
struct StrokeVertex([f32; 4]);
impl lyon::tessellation::StrokeVertexConstructor<triangle::Vertex2D>
for StrokeVertex
{
fn new_vertex(
&mut self,
position: lyon::math::Point,
_attributes: lyon::tessellation::StrokeAttributes<'_, '_>,
) -> triangle::Vertex2D {
triangle::Vertex2D {
position: [position.x, position.y],
color: self.0,
}
}
}
|