Implement `Frame::fill` and `Frame::stroke`

This commit is contained in:
Héctor Ramón Jiménez 2020-02-12 07:08:49 +01:00
parent 74dd79e97f
commit f34407bfda
3 changed files with 100 additions and 2 deletions

View File

@ -133,6 +133,16 @@ impl Default for LineCap {
}
}
impl From<LineCap> for lyon::tessellation::LineCap {
fn from(line_cap: LineCap) -> lyon::tessellation::LineCap {
match line_cap {
LineCap::Butt => lyon::tessellation::LineCap::Butt,
LineCap::Square => lyon::tessellation::LineCap::Square,
LineCap::Round => lyon::tessellation::LineCap::Round,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum LineJoin {
Miter,
@ -146,6 +156,16 @@ impl Default for LineJoin {
}
}
impl From<LineJoin> for lyon::tessellation::LineJoin {
fn from(line_join: LineJoin) -> lyon::tessellation::LineJoin {
match line_join {
LineJoin::Miter => lyon::tessellation::LineJoin::Miter,
LineJoin::Round => lyon::tessellation::LineJoin::Round,
LineJoin::Bevel => lyon::tessellation::LineJoin::Bevel,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum Fill {
Color(Color),

View File

@ -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,
}
}
}

View File

@ -9,10 +9,16 @@ impl Path {
pub fn new(f: impl FnOnce(&mut Builder)) -> Self {
let mut builder = Builder::new();
// TODO: Make it pure instead of side-effect-based (?)
f(&mut builder);
builder.build()
}
#[inline]
pub(crate) fn raw(&self) -> &lyon::path::Path {
&self.raw
}
}
#[allow(missing_debug_implementations)]