summaryrefslogtreecommitdiffstats
path: root/wgpu/src/widget/canvas/frame.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-12 07:08:49 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-12 07:08:49 +0100
commitf34407bfdaf06c4bf204dc31b152be9451c243b8 (patch)
treeaa9c48f24c9babb5722c2a7cdc098365b010796c /wgpu/src/widget/canvas/frame.rs
parent74dd79e97f83d3e9e13d87444740edeb353f9be8 (diff)
downloadiced-f34407bfdaf06c4bf204dc31b152be9451c243b8.tar.gz
iced-f34407bfdaf06c4bf204dc31b152be9451c243b8.tar.bz2
iced-f34407bfdaf06c4bf204dc31b152be9451c243b8.zip
Implement `Frame::fill` and `Frame::stroke`
Diffstat (limited to 'wgpu/src/widget/canvas/frame.rs')
-rw-r--r--wgpu/src/widget/canvas/frame.rs76
1 files changed, 74 insertions, 2 deletions
diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs
index e5daeedb..82ff526b 100644
--- a/wgpu/src/widget/canvas/frame.rs
+++ b/wgpu/src/widget/canvas/frame.rs
@@ -33,7 +33,79 @@ impl Frame {
Point::new(self.width as f32 / 2.0, self.height as f32 / 2.0)
}
- pub fn fill(&mut self, path: &Path, fill: Fill) {}
+ pub fn fill(&mut self, path: &Path, fill: Fill) {
+ use lyon::tessellation::{
+ BuffersBuilder, FillOptions, FillTessellator,
+ };
- pub fn stroke(&mut self, path: &Path, stroke: Stroke) {}
+ let mut buffers = BuffersBuilder::new(
+ &mut self.buffers,
+ FillVertex(match fill {
+ Fill::Color(color) => color.into_linear(),
+ }),
+ );
+
+ let mut tessellator = FillTessellator::new();
+
+ let _ = tessellator
+ .tessellate_path(path.raw(), &FillOptions::default(), &mut buffers)
+ .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 _ = tessellator
+ .tessellate_path(path.raw(), &options, &mut buffers)
+ .expect("Stroke path");
+ }
+}
+
+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,
+ }
+ }
}