2020-02-14 04:59:31 +01:00
|
|
|
use iced_native::{Point, Size, Vector};
|
2020-02-12 03:47:36 +01:00
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
canvas::{Fill, Path, Stroke},
|
|
|
|
|
triangle,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Frame {
|
2020-02-12 08:49:42 +01:00
|
|
|
width: f32,
|
|
|
|
|
height: f32,
|
2020-02-14 05:42:19 +01:00
|
|
|
buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u32>,
|
2020-02-14 04:59:31 +01:00
|
|
|
|
|
|
|
|
transforms: Transforms,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct Transforms {
|
|
|
|
|
previous: Vec<Transform>,
|
|
|
|
|
current: Transform,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
struct Transform {
|
|
|
|
|
raw: lyon::math::Transform,
|
|
|
|
|
is_identity: bool,
|
2020-02-12 03:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Frame {
|
2020-02-12 08:49:42 +01:00
|
|
|
pub fn new(width: f32, height: f32) -> Frame {
|
2020-02-12 03:47:36 +01:00
|
|
|
Frame {
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
buffers: lyon::tessellation::VertexBuffers::new(),
|
2020-02-14 04:59:31 +01:00
|
|
|
transforms: Transforms {
|
|
|
|
|
previous: Vec::new(),
|
|
|
|
|
current: Transform {
|
|
|
|
|
raw: lyon::math::Transform::identity(),
|
|
|
|
|
is_identity: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-02-12 03:47:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 04:59:31 +01:00
|
|
|
#[inline]
|
2020-02-12 08:49:42 +01:00
|
|
|
pub fn width(&self) -> f32 {
|
2020-02-12 03:47:36 +01:00
|
|
|
self.width
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 04:59:31 +01:00
|
|
|
#[inline]
|
2020-02-12 08:49:42 +01:00
|
|
|
pub fn height(&self) -> f32 {
|
2020-02-12 03:47:36 +01:00
|
|
|
self.height
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 04:59:31 +01:00
|
|
|
#[inline]
|
|
|
|
|
pub fn size(&self) -> Size {
|
|
|
|
|
Size::new(self.width, self.height)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-02-12 03:47:36 +01:00
|
|
|
pub fn center(&self) -> Point {
|
2020-02-12 08:49:42 +01:00
|
|
|
Point::new(self.width / 2.0, self.height / 2.0)
|
2020-02-12 03:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-12 07:08:49 +01:00
|
|
|
pub fn fill(&mut self, path: &Path, fill: Fill) {
|
|
|
|
|
use lyon::tessellation::{
|
|
|
|
|
BuffersBuilder, FillOptions, FillTessellator,
|
|
|
|
|
};
|
2020-02-12 03:47:36 +01:00
|
|
|
|
2020-02-12 07:08:49 +01:00
|
|
|
let mut buffers = BuffersBuilder::new(
|
|
|
|
|
&mut self.buffers,
|
|
|
|
|
FillVertex(match fill {
|
|
|
|
|
Fill::Color(color) => color.into_linear(),
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut tessellator = FillTessellator::new();
|
|
|
|
|
|
2020-02-14 04:59:31 +01:00
|
|
|
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");
|
2020-02-12 07:08:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
2020-02-14 04:59:31 +01:00
|
|
|
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;
|
2020-02-12 07:08:49 +01:00
|
|
|
}
|
2020-02-12 08:49:42 +01:00
|
|
|
|
|
|
|
|
pub fn into_mesh(self) -> triangle::Mesh2D {
|
|
|
|
|
triangle::Mesh2D {
|
|
|
|
|
vertices: self.buffers.vertices,
|
|
|
|
|
indices: self.buffers.indices,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-12 07:08:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-12 03:47:36 +01:00
|
|
|
}
|