From f436f20eb86b2324126a54d4164b4cedf2134a45 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 03:47:36 +0100 Subject: Draft `Canvas` types and `clock` example --- wgpu/src/widget/canvas/data.rs | 20 +++++++++++ wgpu/src/widget/canvas/frame.rs | 39 +++++++++++++++++++++ wgpu/src/widget/canvas/layer.rs | 41 ++++++++++++++++++++++ wgpu/src/widget/canvas/path.rs | 78 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 wgpu/src/widget/canvas/data.rs create mode 100644 wgpu/src/widget/canvas/frame.rs create mode 100644 wgpu/src/widget/canvas/layer.rs create mode 100644 wgpu/src/widget/canvas/path.rs (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/data.rs b/wgpu/src/widget/canvas/data.rs new file mode 100644 index 00000000..25d94f4c --- /dev/null +++ b/wgpu/src/widget/canvas/data.rs @@ -0,0 +1,20 @@ +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct Data { + raw: T, + version: usize, +} + +impl Data { + pub fn new(data: T) -> Self { + Data { + raw: data, + version: 0, + } + } + + pub fn update(&mut self, f: impl FnOnce(&mut T)) { + f(&mut self.raw); + + self.version += 1; + } +} diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs new file mode 100644 index 00000000..65c22c0c --- /dev/null +++ b/wgpu/src/widget/canvas/frame.rs @@ -0,0 +1,39 @@ +use iced_native::Point; + +use crate::{ + canvas::{Fill, Path, Stroke}, + triangle, +}; + +#[derive(Debug)] +pub struct Frame { + width: u32, + height: u32, + buffers: lyon::tessellation::VertexBuffers, +} + +impl Frame { + pub(crate) fn new(width: u32, height: u32) -> Frame { + Frame { + width, + height, + buffers: lyon::tessellation::VertexBuffers::new(), + } + } + + pub fn width(&self) -> u32 { + self.width + } + + pub fn height(&self) -> u32 { + self.height + } + + pub fn center(&self) -> Point { + 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 stroke(&mut self, path: Path, stroke: Stroke) {} +} diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs new file mode 100644 index 00000000..f97634e4 --- /dev/null +++ b/wgpu/src/widget/canvas/layer.rs @@ -0,0 +1,41 @@ +use crate::canvas::Frame; + +pub trait Layer: std::fmt::Debug {} + +use std::marker::PhantomData; +use std::sync::{Arc, Weak}; + +#[derive(Debug)] +pub struct Cached { + input: PhantomData, +} + +impl Cached +where + T: Drawable + std::fmt::Debug, +{ + pub fn new() -> Self { + Cached { input: PhantomData } + } + + pub fn clear(&mut self) {} + + pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a { + Bind { + cache: self, + input: input, + } + } +} + +#[derive(Debug)] +struct Bind<'a, T: Drawable> { + cache: &'a Cached, + input: &'a T, +} + +impl<'a, T> Layer for Bind<'a, T> where T: Drawable + std::fmt::Debug {} + +pub trait Drawable { + fn draw(&self, frame: &mut Frame); +} diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs new file mode 100644 index 00000000..2732b1bd --- /dev/null +++ b/wgpu/src/widget/canvas/path.rs @@ -0,0 +1,78 @@ +use iced_native::{Point, Vector}; + +#[allow(missing_debug_implementations)] +pub struct Path { + raw: lyon::path::Builder, +} + +impl Path { + pub fn new() -> Path { + Path { + raw: lyon::path::Path::builder(), + } + } + + #[inline] + pub fn move_to(&mut self, point: Point) { + let _ = self.raw.move_to(lyon::math::Point::new(point.x, point.y)); + } + + #[inline] + pub fn line_to(&mut self, point: Point) { + let _ = self.raw.line_to(lyon::math::Point::new(point.x, point.y)); + } + + #[inline] + pub fn arc(&mut self, arc: Arc) { + self.ellipse(arc.into()) + } + + #[inline] + pub fn ellipse(&mut self, ellipse: Ellipse) { + let arc = lyon::geom::Arc { + center: lyon::math::Point::new(ellipse.center.x, ellipse.center.y), + radii: lyon::math::Vector::new(ellipse.radii.x, ellipse.radii.y), + x_rotation: lyon::math::Angle::radians(ellipse.rotation), + start_angle: lyon::math::Angle::radians(ellipse.start_angle), + sweep_angle: lyon::math::Angle::radians(ellipse.end_angle), + }; + + arc.for_each_quadratic_bezier(&mut |curve| { + let _ = self.raw.quadratic_bezier_to(curve.ctrl, curve.to); + }); + } + + #[inline] + pub fn close(&mut self) { + self.raw.close() + } +} + +#[derive(Debug, Clone, Copy)] +pub struct Arc { + pub center: Point, + pub radius: f32, + pub start_angle: f32, + pub end_angle: f32, +} + +#[derive(Debug, Clone, Copy)] +pub struct Ellipse { + pub center: Point, + pub radii: Vector, + pub rotation: f32, + pub start_angle: f32, + pub end_angle: f32, +} + +impl From for Ellipse { + fn from(arc: Arc) -> Ellipse { + Ellipse { + center: arc.center, + radii: Vector::new(arc.radius, arc.radius), + rotation: 0.0, + start_angle: arc.start_angle, + end_angle: arc.end_angle, + } + } +} -- cgit From 74dd79e97f83d3e9e13d87444740edeb353f9be8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 06:41:24 +0100 Subject: Rename current `Path` to `path::Builder` --- wgpu/src/widget/canvas/frame.rs | 4 ++-- wgpu/src/widget/canvas/path.rs | 32 +++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index 65c22c0c..e5daeedb 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -33,7 +33,7 @@ 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) {} - pub fn stroke(&mut self, path: Path, stroke: Stroke) {} + pub fn stroke(&mut self, path: &Path, stroke: Stroke) {} } diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index 2732b1bd..86326e8e 100644 --- a/wgpu/src/widget/canvas/path.rs +++ b/wgpu/src/widget/canvas/path.rs @@ -1,13 +1,28 @@ use iced_native::{Point, Vector}; -#[allow(missing_debug_implementations)] +#[derive(Debug, Clone)] pub struct Path { - raw: lyon::path::Builder, + raw: lyon::path::Path, } impl Path { - pub fn new() -> Path { - Path { + pub fn new(f: impl FnOnce(&mut Builder)) -> Self { + let mut builder = Builder::new(); + + f(&mut builder); + + builder.build() + } +} + +#[allow(missing_debug_implementations)] +pub struct Builder { + raw: lyon::path::Builder, +} + +impl Builder { + pub fn new() -> Builder { + Builder { raw: lyon::path::Path::builder(), } } @@ -24,7 +39,7 @@ impl Path { #[inline] pub fn arc(&mut self, arc: Arc) { - self.ellipse(arc.into()) + self.ellipse(arc.into()); } #[inline] @@ -46,6 +61,13 @@ impl Path { pub fn close(&mut self) { self.raw.close() } + + #[inline] + pub fn build(self) -> Path { + Path { + raw: self.raw.build(), + } + } } #[derive(Debug, Clone, Copy)] -- cgit From f34407bfdaf06c4bf204dc31b152be9451c243b8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 07:08:49 +0100 Subject: Implement `Frame::fill` and `Frame::stroke` --- wgpu/src/widget/canvas/frame.rs | 76 +++++++++++++++++++++++++++++++++++++++-- wgpu/src/widget/canvas/path.rs | 6 ++++ 2 files changed, 80 insertions(+), 2 deletions(-) (limited to 'wgpu/src/widget/canvas') 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 + 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 + 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, + } + } } diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index 86326e8e..96206256 100644 --- a/wgpu/src/widget/canvas/path.rs +++ b/wgpu/src/widget/canvas/path.rs @@ -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)] -- cgit From 578ea4abb8a2dd0d53d7087322796bf9ad541b56 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 08:49:42 +0100 Subject: Finish `clock` example --- wgpu/src/widget/canvas/frame.rs | 19 +++++++++---- wgpu/src/widget/canvas/layer.rs | 63 +++++++++++++++++++++++++++++++++++------ wgpu/src/widget/canvas/path.rs | 2 ++ 3 files changed, 70 insertions(+), 14 deletions(-) (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index 82ff526b..3c667426 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -7,13 +7,13 @@ use crate::{ #[derive(Debug)] pub struct Frame { - width: u32, - height: u32, + width: f32, + height: f32, buffers: lyon::tessellation::VertexBuffers, } impl Frame { - pub(crate) fn new(width: u32, height: u32) -> Frame { + pub fn new(width: f32, height: f32) -> Frame { Frame { width, height, @@ -21,16 +21,16 @@ impl Frame { } } - pub fn width(&self) -> u32 { + pub fn width(&self) -> f32 { self.width } - pub fn height(&self) -> u32 { + pub fn height(&self) -> f32 { self.height } pub fn center(&self) -> Point { - Point::new(self.width as f32 / 2.0, self.height as f32 / 2.0) + Point::new(self.width / 2.0, self.height / 2.0) } pub fn fill(&mut self, path: &Path, fill: Fill) { @@ -74,6 +74,13 @@ impl Frame { .tessellate_path(path.raw(), &options, &mut buffers) .expect("Stroke path"); } + + pub fn into_mesh(self) -> triangle::Mesh2D { + triangle::Mesh2D { + vertices: self.buffers.vertices, + indices: self.buffers.indices, + } + } } struct FillVertex([f32; 4]); diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs index f97634e4..c239a254 100644 --- a/wgpu/src/widget/canvas/layer.rs +++ b/wgpu/src/widget/canvas/layer.rs @@ -1,13 +1,28 @@ -use crate::canvas::Frame; +use crate::{canvas::Frame, triangle}; -pub trait Layer: std::fmt::Debug {} +use iced_native::Size; +use std::cell::RefCell; +use std::sync::Arc; + +pub trait Layer: std::fmt::Debug { + fn draw(&self, bounds: Size) -> Arc; +} use std::marker::PhantomData; -use std::sync::{Arc, Weak}; #[derive(Debug)] pub struct Cached { input: PhantomData, + cache: RefCell, +} + +#[derive(Debug)] +enum Cache { + Empty, + Filled { + mesh: Arc, + bounds: Size, + }, } impl Cached @@ -15,14 +30,19 @@ where T: Drawable + std::fmt::Debug, { pub fn new() -> Self { - Cached { input: PhantomData } + Cached { + input: PhantomData, + cache: RefCell::new(Cache::Empty), + } } - pub fn clear(&mut self) {} + pub fn clear(&mut self) { + *self.cache.borrow_mut() = Cache::Empty; + } pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a { Bind { - cache: self, + layer: self, input: input, } } @@ -30,11 +50,38 @@ where #[derive(Debug)] struct Bind<'a, T: Drawable> { - cache: &'a Cached, + layer: &'a Cached, input: &'a T, } -impl<'a, T> Layer for Bind<'a, T> where T: Drawable + std::fmt::Debug {} +impl<'a, T> Layer for Bind<'a, T> +where + T: Drawable + std::fmt::Debug, +{ + fn draw(&self, current_bounds: Size) -> Arc { + use std::ops::Deref; + + if let Cache::Filled { mesh, bounds } = + self.layer.cache.borrow().deref() + { + if *bounds == current_bounds { + return mesh.clone(); + } + } + + let mut frame = Frame::new(current_bounds.width, current_bounds.height); + self.input.draw(&mut frame); + + let mesh = Arc::new(frame.into_mesh()); + + *self.layer.cache.borrow_mut() = Cache::Filled { + mesh: mesh.clone(), + bounds: current_bounds, + }; + + mesh + } +} pub trait Drawable { fn draw(&self, frame: &mut Frame); diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index 96206256..c8ba10e1 100644 --- a/wgpu/src/widget/canvas/path.rs +++ b/wgpu/src/widget/canvas/path.rs @@ -58,6 +58,8 @@ impl Builder { sweep_angle: lyon::math::Angle::radians(ellipse.end_angle), }; + let _ = self.raw.move_to(arc.sample(0.0)); + arc.for_each_quadratic_bezier(&mut |curve| { let _ = self.raw.quadratic_bezier_to(curve.ctrl, curve.to); }); -- cgit From de8f06b512ec65f625417e334dca30990248c968 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 09:12:35 +0100 Subject: Split `Fill` and `Stroke` into their own modules --- wgpu/src/widget/canvas/fill.rs | 12 ++++++++ wgpu/src/widget/canvas/stroke.rs | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 wgpu/src/widget/canvas/fill.rs create mode 100644 wgpu/src/widget/canvas/stroke.rs (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/fill.rs b/wgpu/src/widget/canvas/fill.rs new file mode 100644 index 00000000..9c23f997 --- /dev/null +++ b/wgpu/src/widget/canvas/fill.rs @@ -0,0 +1,12 @@ +use iced_native::Color; + +#[derive(Debug, Clone, Copy)] +pub enum Fill { + Color(Color), +} + +impl Default for Fill { + fn default() -> Fill { + Fill::Color(Color::BLACK) + } +} diff --git a/wgpu/src/widget/canvas/stroke.rs b/wgpu/src/widget/canvas/stroke.rs new file mode 100644 index 00000000..9bb260b2 --- /dev/null +++ b/wgpu/src/widget/canvas/stroke.rs @@ -0,0 +1,66 @@ +use iced_native::Color; + +#[derive(Debug, Clone, Copy)] +pub struct Stroke { + pub color: Color, + pub width: f32, + pub line_cap: LineCap, + pub line_join: LineJoin, +} + +impl Default for Stroke { + fn default() -> Stroke { + Stroke { + color: Color::BLACK, + width: 1.0, + line_cap: LineCap::default(), + line_join: LineJoin::default(), + } + } +} + +#[derive(Debug, Clone, Copy)] +pub enum LineCap { + Butt, + Square, + Round, +} + +impl Default for LineCap { + fn default() -> LineCap { + LineCap::Butt + } +} + +impl From 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, + Round, + Bevel, +} + +impl Default for LineJoin { + fn default() -> LineJoin { + LineJoin::Miter + } +} + +impl From 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, + } + } +} -- cgit From 629153582f1a43516092d941d2b4e2372a6ea054 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 09:24:22 +0100 Subject: Remove `canvas::Data` leftover --- wgpu/src/widget/canvas/data.rs | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 wgpu/src/widget/canvas/data.rs (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/data.rs b/wgpu/src/widget/canvas/data.rs deleted file mode 100644 index 25d94f4c..00000000 --- a/wgpu/src/widget/canvas/data.rs +++ /dev/null @@ -1,20 +0,0 @@ -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] -pub struct Data { - raw: T, - version: usize, -} - -impl Data { - pub fn new(data: T) -> Self { - Data { - raw: data, - version: 0, - } - } - - pub fn update(&mut self, f: impl FnOnce(&mut T)) { - f(&mut self.raw); - - self.version += 1; - } -} -- cgit From df90c478e28892537efc0009d468a521d4d7fee5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 13 Feb 2020 03:45:07 +0100 Subject: Move `layer::Cached` to its own module --- wgpu/src/widget/canvas/layer.rs | 80 ++------------------------------- wgpu/src/widget/canvas/layer/cached.rs | 82 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 76 deletions(-) create mode 100644 wgpu/src/widget/canvas/layer/cached.rs (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs index c239a254..8c069f18 100644 --- a/wgpu/src/widget/canvas/layer.rs +++ b/wgpu/src/widget/canvas/layer.rs @@ -1,88 +1,16 @@ +mod cached; + +pub use cached::Cached; + use crate::{canvas::Frame, triangle}; use iced_native::Size; -use std::cell::RefCell; use std::sync::Arc; pub trait Layer: std::fmt::Debug { fn draw(&self, bounds: Size) -> Arc; } -use std::marker::PhantomData; - -#[derive(Debug)] -pub struct Cached { - input: PhantomData, - cache: RefCell, -} - -#[derive(Debug)] -enum Cache { - Empty, - Filled { - mesh: Arc, - bounds: Size, - }, -} - -impl Cached -where - T: Drawable + std::fmt::Debug, -{ - pub fn new() -> Self { - Cached { - input: PhantomData, - cache: RefCell::new(Cache::Empty), - } - } - - pub fn clear(&mut self) { - *self.cache.borrow_mut() = Cache::Empty; - } - - pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a { - Bind { - layer: self, - input: input, - } - } -} - -#[derive(Debug)] -struct Bind<'a, T: Drawable> { - layer: &'a Cached, - input: &'a T, -} - -impl<'a, T> Layer for Bind<'a, T> -where - T: Drawable + std::fmt::Debug, -{ - fn draw(&self, current_bounds: Size) -> Arc { - use std::ops::Deref; - - if let Cache::Filled { mesh, bounds } = - self.layer.cache.borrow().deref() - { - if *bounds == current_bounds { - return mesh.clone(); - } - } - - let mut frame = Frame::new(current_bounds.width, current_bounds.height); - self.input.draw(&mut frame); - - let mesh = Arc::new(frame.into_mesh()); - - *self.layer.cache.borrow_mut() = Cache::Filled { - mesh: mesh.clone(), - bounds: current_bounds, - }; - - mesh - } -} - pub trait Drawable { fn draw(&self, frame: &mut Frame); } diff --git a/wgpu/src/widget/canvas/layer/cached.rs b/wgpu/src/widget/canvas/layer/cached.rs new file mode 100644 index 00000000..c6741372 --- /dev/null +++ b/wgpu/src/widget/canvas/layer/cached.rs @@ -0,0 +1,82 @@ +use crate::{ + canvas::{layer::Drawable, Frame, Layer}, + triangle, +}; + +use iced_native::Size; +use std::cell::RefCell; +use std::marker::PhantomData; +use std::sync::Arc; + +#[derive(Debug)] +pub struct Cached { + input: PhantomData, + cache: RefCell, +} + +#[derive(Debug)] +enum Cache { + Empty, + Filled { + mesh: Arc, + bounds: Size, + }, +} + +impl Cached +where + T: Drawable + std::fmt::Debug, +{ + pub fn new() -> Self { + Cached { + input: PhantomData, + cache: RefCell::new(Cache::Empty), + } + } + + pub fn clear(&mut self) { + *self.cache.borrow_mut() = Cache::Empty; + } + + pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a { + Bind { + layer: self, + input: input, + } + } +} + +#[derive(Debug)] +struct Bind<'a, T: Drawable> { + layer: &'a Cached, + input: &'a T, +} + +impl<'a, T> Layer for Bind<'a, T> +where + T: Drawable + std::fmt::Debug, +{ + fn draw(&self, current_bounds: Size) -> Arc { + use std::ops::Deref; + + if let Cache::Filled { mesh, bounds } = + self.layer.cache.borrow().deref() + { + if *bounds == current_bounds { + return mesh.clone(); + } + } + + let mut frame = Frame::new(current_bounds.width, current_bounds.height); + self.input.draw(&mut frame); + + let mesh = Arc::new(frame.into_mesh()); + + *self.layer.cache.borrow_mut() = Cache::Filled { + mesh: mesh.clone(), + bounds: current_bounds, + }; + + mesh + } +} -- cgit From 76df374624e5d82dcb2670789a6c4ff228dda9e9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Feb 2020 02:23:41 +0100 Subject: Implement additional methods in `path::Builder` --- wgpu/src/widget/canvas/path.rs | 85 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 10 deletions(-) (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index c8ba10e1..8847ea29 100644 --- a/wgpu/src/widget/canvas/path.rs +++ b/wgpu/src/widget/canvas/path.rs @@ -1,4 +1,6 @@ -use iced_native::{Point, Vector}; +use iced_native::{Point, Size, Vector}; + +use lyon::path::builder::{Build, FlatPathBuilder, PathBuilder, SvgBuilder}; #[derive(Debug, Clone)] pub struct Path { @@ -23,13 +25,13 @@ impl Path { #[allow(missing_debug_implementations)] pub struct Builder { - raw: lyon::path::Builder, + raw: lyon::path::builder::SvgPathBuilder, } impl Builder { pub fn new() -> Builder { Builder { - raw: lyon::path::Path::builder(), + raw: lyon::path::Path::builder().with_svg(), } } @@ -48,14 +50,32 @@ impl Builder { self.ellipse(arc.into()); } - #[inline] + pub fn arc_to(&mut self, a: Point, b: Point, radius: f32) { + use lyon::{math, path}; + + let a = math::Point::new(a.x, a.y); + + if self.raw.current_position() != a { + let _ = self.raw.line_to(a); + } + + let _ = self.raw.arc_to( + math::Vector::new(radius, radius), + math::Angle::radians(0.0), + path::ArcFlags::default(), + math::Point::new(b.x, b.y), + ); + } + pub fn ellipse(&mut self, ellipse: Ellipse) { - let arc = lyon::geom::Arc { - center: lyon::math::Point::new(ellipse.center.x, ellipse.center.y), - radii: lyon::math::Vector::new(ellipse.radii.x, ellipse.radii.y), - x_rotation: lyon::math::Angle::radians(ellipse.rotation), - start_angle: lyon::math::Angle::radians(ellipse.start_angle), - sweep_angle: lyon::math::Angle::radians(ellipse.end_angle), + use lyon::{geom, math}; + + let arc = geom::Arc { + center: math::Point::new(ellipse.center.x, ellipse.center.y), + radii: math::Vector::new(ellipse.radii.x, ellipse.radii.y), + x_rotation: math::Angle::radians(ellipse.rotation), + start_angle: math::Angle::radians(ellipse.start_angle), + sweep_angle: math::Angle::radians(ellipse.end_angle), }; let _ = self.raw.move_to(arc.sample(0.0)); @@ -65,6 +85,51 @@ impl Builder { }); } + #[inline] + pub fn bezier_curve_to( + &mut self, + control_a: Point, + control_b: Point, + to: Point, + ) { + use lyon::math; + + let _ = self.raw.cubic_bezier_to( + math::Point::new(control_a.x, control_a.y), + math::Point::new(control_b.x, control_b.y), + math::Point::new(to.x, to.y), + ); + } + + #[inline] + pub fn quadratic_curve_to(&mut self, control: Point, to: Point) { + use lyon::math; + + let _ = self.raw.quadratic_bezier_to( + math::Point::new(control.x, control.y), + math::Point::new(to.x, to.y), + ); + } + + #[inline] + pub fn rectangle(&mut self, p: Point, size: Size) { + self.move_to(p); + self.line_to(Point::new(p.x + size.width, p.y)); + self.line_to(Point::new(p.x + size.width, p.y + size.height)); + self.line_to(Point::new(p.x, p.y + size.height)); + self.close(); + } + + #[inline] + pub fn circle(&mut self, center: Point, radius: f32) { + self.arc(Arc { + center, + radius, + start_angle: 0.0, + end_angle: 2.0 * std::f32::consts::PI, + }); + } + #[inline] pub fn close(&mut self) { self.raw.close() -- cgit From 558abf648bdeb86d92e7092f4b023d5e55cc673c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Feb 2020 04:59:31 +0100 Subject: Add transform stack to `canvas::Frame` --- wgpu/src/widget/canvas/frame.rs | 102 +++++++++++++++++++++++++++++++++++++--- wgpu/src/widget/canvas/path.rs | 10 ++++ 2 files changed, 105 insertions(+), 7 deletions(-) (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index 3c667426..687f6c37 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -1,4 +1,4 @@ -use iced_native::Point; +use iced_native::{Point, Size, Vector}; use crate::{ canvas::{Fill, Path, Stroke}, @@ -10,6 +10,20 @@ pub struct Frame { width: f32, height: f32, buffers: lyon::tessellation::VertexBuffers, + + transforms: Transforms, +} + +#[derive(Debug)] +struct Transforms { + previous: Vec, + current: Transform, +} + +#[derive(Debug, Clone, Copy)] +struct Transform { + raw: lyon::math::Transform, + is_identity: bool, } impl Frame { @@ -18,17 +32,32 @@ impl 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) } @@ -47,9 +76,23 @@ impl Frame { let mut tessellator = FillTessellator::new(); - let _ = tessellator - .tessellate_path(path.raw(), &FillOptions::default(), &mut buffers) - .expect("Tessellate path"); + 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) { @@ -70,9 +113,54 @@ impl Frame { 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"); + 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 { diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index 8847ea29..b70d0aef 100644 --- a/wgpu/src/widget/canvas/path.rs +++ b/wgpu/src/widget/canvas/path.rs @@ -21,6 +21,16 @@ impl Path { pub(crate) fn raw(&self) -> &lyon::path::Path { &self.raw } + + #[inline] + pub(crate) fn transformed( + &self, + transform: &lyon::math::Transform, + ) -> Path { + Path { + raw: self.raw.transformed(transform), + } + } } #[allow(missing_debug_implementations)] -- cgit From f5c80a6d75d5022b175d3562f0965598b6398bd7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Feb 2020 05:42:19 +0100 Subject: Upgrade `Mesh2D` indices from `u16` to `u32` --- wgpu/src/widget/canvas/frame.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index 687f6c37..27d676d6 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -9,7 +9,7 @@ use crate::{ pub struct Frame { width: f32, height: f32, - buffers: lyon::tessellation::VertexBuffers, + buffers: lyon::tessellation::VertexBuffers, transforms: Transforms, } -- cgit From 9c067562fa765cfc49d09cd9b12fbba96d5619fa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 18 Feb 2020 08:48:54 +0100 Subject: Write documentation for new `canvas` module --- wgpu/src/widget/canvas/drawable.rs | 12 +++ wgpu/src/widget/canvas/fill.rs | 2 + wgpu/src/widget/canvas/frame.rs | 51 +++++++++- wgpu/src/widget/canvas/layer.rs | 23 +++-- wgpu/src/widget/canvas/layer/cache.rs | 101 +++++++++++++++++++ wgpu/src/widget/canvas/layer/cached.rs | 82 --------------- wgpu/src/widget/canvas/path.rs | 168 ++++--------------------------- wgpu/src/widget/canvas/path/arc.rs | 44 ++++++++ wgpu/src/widget/canvas/path/builder.rs | 177 +++++++++++++++++++++++++++++++++ wgpu/src/widget/canvas/stroke.rs | 17 ++++ 10 files changed, 436 insertions(+), 241 deletions(-) create mode 100644 wgpu/src/widget/canvas/drawable.rs create mode 100644 wgpu/src/widget/canvas/layer/cache.rs delete mode 100644 wgpu/src/widget/canvas/layer/cached.rs create mode 100644 wgpu/src/widget/canvas/path/arc.rs create mode 100644 wgpu/src/widget/canvas/path/builder.rs (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/drawable.rs b/wgpu/src/widget/canvas/drawable.rs new file mode 100644 index 00000000..6c74071c --- /dev/null +++ b/wgpu/src/widget/canvas/drawable.rs @@ -0,0 +1,12 @@ +use crate::canvas::Frame; + +/// A type that can be drawn on a [`Frame`]. +/// +/// [`Frame`]: struct.Frame.html +pub trait Drawable { + /// Draws the [`Drawable`] on the given [`Frame`]. + /// + /// [`Drawable`]: trait.Drawable.html + /// [`Frame`]: struct.Frame.html + fn draw(&self, frame: &mut Frame); +} diff --git a/wgpu/src/widget/canvas/fill.rs b/wgpu/src/widget/canvas/fill.rs index 9c23f997..5ce24cf3 100644 --- a/wgpu/src/widget/canvas/fill.rs +++ b/wgpu/src/widget/canvas/fill.rs @@ -1,7 +1,9 @@ use iced_native::Color; +/// The style used to fill geometry. #[derive(Debug, Clone, Copy)] pub enum Fill { + /// Fill with a color. Color(Color), } diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index 27d676d6..fa6d8c0a 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -5,12 +5,14 @@ use crate::{ triangle, }; +/// The frame of a [`Canvas`]. +/// +/// [`Canvas`]: struct.Canvas.html #[derive(Debug)] pub struct Frame { width: f32, height: f32, buffers: lyon::tessellation::VertexBuffers, - transforms: Transforms, } @@ -27,6 +29,12 @@ struct Transform { } impl Frame { + /// Creates a new empty [`Frame`] with the given dimensions. + /// + /// The default coordinate system of a [`Frame`] has its origin at the + /// top-left corner of its bounds. + /// + /// [`Frame`]: struct.Frame.html pub fn new(width: f32, height: f32) -> Frame { Frame { width, @@ -42,26 +50,43 @@ impl Frame { } } + /// Returns the width of the [`Frame`]. + /// + /// [`Frame`]: struct.Frame.html #[inline] pub fn width(&self) -> f32 { self.width } + /// Returns the width of the [`Frame`]. + /// + /// [`Frame`]: struct.Frame.html #[inline] pub fn height(&self) -> f32 { self.height } + /// Returns the dimensions of the [`Frame`]. + /// + /// [`Frame`]: struct.Frame.html #[inline] pub fn size(&self) -> Size { Size::new(self.width, self.height) } + /// Returns the coordinate of the center of the [`Frame`]. + /// + /// [`Frame`]: struct.Frame.html #[inline] pub fn center(&self) -> Point { Point::new(self.width / 2.0, self.height / 2.0) } + /// Draws the given [`Path`] on the [`Frame`] by filling it with the + /// provided style. + /// + /// [`Path`]: path/struct.Path.html + /// [`Frame`]: struct.Frame.html pub fn fill(&mut self, path: &Path, fill: Fill) { use lyon::tessellation::{ BuffersBuilder, FillOptions, FillTessellator, @@ -95,6 +120,11 @@ impl Frame { let _ = result.expect("Tessellate path"); } + /// Draws the stroke of the given [`Path`] on the [`Frame`] with the + /// provided style. + /// + /// [`Path`]: path/struct.Path.html + /// [`Frame`]: struct.Frame.html pub fn stroke(&mut self, path: &Path, stroke: Stroke) { use lyon::tessellation::{ BuffersBuilder, StrokeOptions, StrokeTessellator, @@ -124,6 +154,13 @@ impl Frame { let _ = result.expect("Stroke path"); } + /// Stores the current transform of the [`Frame`] and executes the given + /// drawing operations, restoring the transform afterwards. + /// + /// This method is useful to compose transforms and perform drawing + /// operations in different coordinate systems. + /// + /// [`Frame`]: struct.Frame.html #[inline] pub fn with_save(&mut self, f: impl FnOnce(&mut Frame)) { self.transforms.previous.push(self.transforms.current); @@ -133,6 +170,9 @@ impl Frame { self.transforms.current = self.transforms.previous.pop().unwrap(); } + /// Applies a translation to the current transform of the [`Frame`]. + /// + /// [`Frame`]: struct.Frame.html #[inline] pub fn translate(&mut self, translation: Vector) { self.transforms.current.raw = self @@ -146,6 +186,9 @@ impl Frame { self.transforms.current.is_identity = false; } + /// Applies a rotation to the current transform of the [`Frame`]. + /// + /// [`Frame`]: struct.Frame.html #[inline] pub fn rotate(&mut self, angle: f32) { self.transforms.current.raw = self @@ -156,6 +199,9 @@ impl Frame { self.transforms.current.is_identity = false; } + /// Applies a scaling to the current transform of the [`Frame`]. + /// + /// [`Frame`]: struct.Frame.html #[inline] pub fn scale(&mut self, scale: f32) { self.transforms.current.raw = @@ -163,6 +209,9 @@ impl Frame { self.transforms.current.is_identity = false; } + /// Produces the geometry that has been drawn on the [`Frame`]. + /// + /// [`Frame`]: struct.Frame.html pub fn into_mesh(self) -> triangle::Mesh2D { triangle::Mesh2D { vertices: self.buffers.vertices, diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs index 8c069f18..82d647bb 100644 --- a/wgpu/src/widget/canvas/layer.rs +++ b/wgpu/src/widget/canvas/layer.rs @@ -1,16 +1,25 @@ -mod cached; +//! Produce, store, and reuse geometry. +mod cache; -pub use cached::Cached; +pub use cache::Cache; -use crate::{canvas::Frame, triangle}; +use crate::triangle; use iced_native::Size; use std::sync::Arc; +/// A layer that can be presented at a [`Canvas`]. +/// +/// [`Canvas`]: ../struct.Canvas.html pub trait Layer: std::fmt::Debug { + /// Draws the [`Layer`] in the given bounds and produces [`Mesh2D`] as a + /// result. + /// + /// The [`Layer`] may choose to store the produced [`Mesh2D`] locally and + /// only recompute it when the bounds change, its contents change, or is + /// otherwise explicitly cleared by other means. + /// + /// [`Layer`]: trait.Layer.html + /// [`Mesh2D`]: ../../../triangle/struct.Mesh2D.html fn draw(&self, bounds: Size) -> Arc; } - -pub trait Drawable { - fn draw(&self, frame: &mut Frame); -} diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs new file mode 100644 index 00000000..3071cce0 --- /dev/null +++ b/wgpu/src/widget/canvas/layer/cache.rs @@ -0,0 +1,101 @@ +use crate::{ + canvas::{Drawable, Frame, Layer}, + triangle, +}; + +use iced_native::Size; +use std::cell::RefCell; +use std::marker::PhantomData; +use std::sync::Arc; + +/// A simple cache that stores generated geometry to avoid recomputation. +/// +/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer +/// change or it is explicitly cleared. +/// +/// [`Layer`]: ../trait.Layer.html +/// [`Cached`]: struct.Cached.html +#[derive(Debug)] +pub struct Cache { + input: PhantomData, + state: RefCell, +} + +#[derive(Debug)] +enum State { + Empty, + Filled { + mesh: Arc, + bounds: Size, + }, +} + +impl Cache +where + T: Drawable + std::fmt::Debug, +{ + /// Creates a new empty [`Cache`]. + /// + /// [`Cache`]: struct.Cache.html + pub fn new() -> Self { + Cache { + input: PhantomData, + state: RefCell::new(State::Empty), + } + } + + /// Clears the cache, forcing a redraw the next time it is used. + /// + /// [`Cached`]: struct.Cached.html + pub fn clear(&mut self) { + *self.state.borrow_mut() = State::Empty; + } + + /// Binds the [`Cache`] with some data, producing a [`Layer`] that can be + /// added to a [`Canvas`]. + /// + /// [`Cache`]: struct.Cache.html + /// [`Layer`]: ../trait.Layer.html + /// [`Canvas`]: ../../struct.Canvas.html + pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a { + Bind { + cache: self, + input: input, + } + } +} + +#[derive(Debug)] +struct Bind<'a, T: Drawable> { + cache: &'a Cache, + input: &'a T, +} + +impl<'a, T> Layer for Bind<'a, T> +where + T: Drawable + std::fmt::Debug, +{ + fn draw(&self, current_bounds: Size) -> Arc { + use std::ops::Deref; + + if let State::Filled { mesh, bounds } = + self.cache.state.borrow().deref() + { + if *bounds == current_bounds { + return mesh.clone(); + } + } + + let mut frame = Frame::new(current_bounds.width, current_bounds.height); + self.input.draw(&mut frame); + + let mesh = Arc::new(frame.into_mesh()); + + *self.cache.state.borrow_mut() = State::Filled { + mesh: mesh.clone(), + bounds: current_bounds, + }; + + mesh + } +} diff --git a/wgpu/src/widget/canvas/layer/cached.rs b/wgpu/src/widget/canvas/layer/cached.rs deleted file mode 100644 index c6741372..00000000 --- a/wgpu/src/widget/canvas/layer/cached.rs +++ /dev/null @@ -1,82 +0,0 @@ -use crate::{ - canvas::{layer::Drawable, Frame, Layer}, - triangle, -}; - -use iced_native::Size; -use std::cell::RefCell; -use std::marker::PhantomData; -use std::sync::Arc; - -#[derive(Debug)] -pub struct Cached { - input: PhantomData, - cache: RefCell, -} - -#[derive(Debug)] -enum Cache { - Empty, - Filled { - mesh: Arc, - bounds: Size, - }, -} - -impl Cached -where - T: Drawable + std::fmt::Debug, -{ - pub fn new() -> Self { - Cached { - input: PhantomData, - cache: RefCell::new(Cache::Empty), - } - } - - pub fn clear(&mut self) { - *self.cache.borrow_mut() = Cache::Empty; - } - - pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a { - Bind { - layer: self, - input: input, - } - } -} - -#[derive(Debug)] -struct Bind<'a, T: Drawable> { - layer: &'a Cached, - input: &'a T, -} - -impl<'a, T> Layer for Bind<'a, T> -where - T: Drawable + std::fmt::Debug, -{ - fn draw(&self, current_bounds: Size) -> Arc { - use std::ops::Deref; - - if let Cache::Filled { mesh, bounds } = - self.layer.cache.borrow().deref() - { - if *bounds == current_bounds { - return mesh.clone(); - } - } - - let mut frame = Frame::new(current_bounds.width, current_bounds.height); - self.input.draw(&mut frame); - - let mesh = Arc::new(frame.into_mesh()); - - *self.layer.cache.borrow_mut() = Cache::Filled { - mesh: mesh.clone(), - bounds: current_bounds, - }; - - mesh - } -} diff --git a/wgpu/src/widget/canvas/path.rs b/wgpu/src/widget/canvas/path.rs index b70d0aef..15c2e853 100644 --- a/wgpu/src/widget/canvas/path.rs +++ b/wgpu/src/widget/canvas/path.rs @@ -1,13 +1,28 @@ -use iced_native::{Point, Size, Vector}; +//! Build different kinds of 2D shapes. +pub mod arc; -use lyon::path::builder::{Build, FlatPathBuilder, PathBuilder, SvgBuilder}; +mod builder; +pub use arc::Arc; +pub use builder::Builder; + +/// An immutable set of points that may or may not be connected. +/// +/// A single [`Path`] can represent different kinds of 2D shapes! +/// +/// [`Path`]: struct.Path.html #[derive(Debug, Clone)] pub struct Path { raw: lyon::path::Path, } impl Path { + /// Creates a new [`Path`] with the provided closure. + /// + /// Use the [`Builder`] to configure your [`Path`]. + /// + /// [`Path`]: struct.Path.html + /// [`Builder`]: struct.Builder.html pub fn new(f: impl FnOnce(&mut Builder)) -> Self { let mut builder = Builder::new(); @@ -32,152 +47,3 @@ impl Path { } } } - -#[allow(missing_debug_implementations)] -pub struct Builder { - raw: lyon::path::builder::SvgPathBuilder, -} - -impl Builder { - pub fn new() -> Builder { - Builder { - raw: lyon::path::Path::builder().with_svg(), - } - } - - #[inline] - pub fn move_to(&mut self, point: Point) { - let _ = self.raw.move_to(lyon::math::Point::new(point.x, point.y)); - } - - #[inline] - pub fn line_to(&mut self, point: Point) { - let _ = self.raw.line_to(lyon::math::Point::new(point.x, point.y)); - } - - #[inline] - pub fn arc(&mut self, arc: Arc) { - self.ellipse(arc.into()); - } - - pub fn arc_to(&mut self, a: Point, b: Point, radius: f32) { - use lyon::{math, path}; - - let a = math::Point::new(a.x, a.y); - - if self.raw.current_position() != a { - let _ = self.raw.line_to(a); - } - - let _ = self.raw.arc_to( - math::Vector::new(radius, radius), - math::Angle::radians(0.0), - path::ArcFlags::default(), - math::Point::new(b.x, b.y), - ); - } - - pub fn ellipse(&mut self, ellipse: Ellipse) { - use lyon::{geom, math}; - - let arc = geom::Arc { - center: math::Point::new(ellipse.center.x, ellipse.center.y), - radii: math::Vector::new(ellipse.radii.x, ellipse.radii.y), - x_rotation: math::Angle::radians(ellipse.rotation), - start_angle: math::Angle::radians(ellipse.start_angle), - sweep_angle: math::Angle::radians(ellipse.end_angle), - }; - - let _ = self.raw.move_to(arc.sample(0.0)); - - arc.for_each_quadratic_bezier(&mut |curve| { - let _ = self.raw.quadratic_bezier_to(curve.ctrl, curve.to); - }); - } - - #[inline] - pub fn bezier_curve_to( - &mut self, - control_a: Point, - control_b: Point, - to: Point, - ) { - use lyon::math; - - let _ = self.raw.cubic_bezier_to( - math::Point::new(control_a.x, control_a.y), - math::Point::new(control_b.x, control_b.y), - math::Point::new(to.x, to.y), - ); - } - - #[inline] - pub fn quadratic_curve_to(&mut self, control: Point, to: Point) { - use lyon::math; - - let _ = self.raw.quadratic_bezier_to( - math::Point::new(control.x, control.y), - math::Point::new(to.x, to.y), - ); - } - - #[inline] - pub fn rectangle(&mut self, p: Point, size: Size) { - self.move_to(p); - self.line_to(Point::new(p.x + size.width, p.y)); - self.line_to(Point::new(p.x + size.width, p.y + size.height)); - self.line_to(Point::new(p.x, p.y + size.height)); - self.close(); - } - - #[inline] - pub fn circle(&mut self, center: Point, radius: f32) { - self.arc(Arc { - center, - radius, - start_angle: 0.0, - end_angle: 2.0 * std::f32::consts::PI, - }); - } - - #[inline] - pub fn close(&mut self) { - self.raw.close() - } - - #[inline] - pub fn build(self) -> Path { - Path { - raw: self.raw.build(), - } - } -} - -#[derive(Debug, Clone, Copy)] -pub struct Arc { - pub center: Point, - pub radius: f32, - pub start_angle: f32, - pub end_angle: f32, -} - -#[derive(Debug, Clone, Copy)] -pub struct Ellipse { - pub center: Point, - pub radii: Vector, - pub rotation: f32, - pub start_angle: f32, - pub end_angle: f32, -} - -impl From for Ellipse { - fn from(arc: Arc) -> Ellipse { - Ellipse { - center: arc.center, - radii: Vector::new(arc.radius, arc.radius), - rotation: 0.0, - start_angle: arc.start_angle, - end_angle: arc.end_angle, - } - } -} diff --git a/wgpu/src/widget/canvas/path/arc.rs b/wgpu/src/widget/canvas/path/arc.rs new file mode 100644 index 00000000..343191f1 --- /dev/null +++ b/wgpu/src/widget/canvas/path/arc.rs @@ -0,0 +1,44 @@ +//! Build and draw curves. +use iced_native::{Point, Vector}; + +/// A segment of a differentiable curve. +#[derive(Debug, Clone, Copy)] +pub struct Arc { + /// The center of the arc. + pub center: Point, + /// The radius of the arc. + pub radius: f32, + /// The start of the segment's angle, clockwise rotation. + pub start_angle: f32, + /// The end of the segment's angle, clockwise rotation. + pub end_angle: f32, +} + +/// An elliptical [`Arc`]. +/// +/// [`Arc`]: struct.Arc.html +#[derive(Debug, Clone, Copy)] +pub struct Elliptical { + /// The center of the arc. + pub center: Point, + /// The radii of the arc's ellipse, defining its axes. + pub radii: Vector, + /// The rotation of the arc's ellipse. + pub rotation: f32, + /// The start of the segment's angle, clockwise rotation. + pub start_angle: f32, + /// The end of the segment's angle, clockwise rotation. + pub end_angle: f32, +} + +impl From for Elliptical { + fn from(arc: Arc) -> Elliptical { + Elliptical { + center: arc.center, + radii: Vector::new(arc.radius, arc.radius), + rotation: 0.0, + start_angle: arc.start_angle, + end_angle: arc.end_angle, + } + } +} diff --git a/wgpu/src/widget/canvas/path/builder.rs b/wgpu/src/widget/canvas/path/builder.rs new file mode 100644 index 00000000..a013149e --- /dev/null +++ b/wgpu/src/widget/canvas/path/builder.rs @@ -0,0 +1,177 @@ +use crate::canvas::path::{arc, Arc, Path}; + +use iced_native::{Point, Size}; +use lyon::path::builder::{Build, FlatPathBuilder, PathBuilder, SvgBuilder}; + +/// A [`Path`] builder. +/// +/// Once a [`Path`] is built, it can no longer be mutated. +/// +/// [`Path`]: struct.Path.html +#[allow(missing_debug_implementations)] +pub struct Builder { + raw: lyon::path::builder::SvgPathBuilder, +} + +impl Builder { + /// Creates a new [`Builder`]. + /// + /// [`Builder`]: struct.Builder.html + pub fn new() -> Builder { + Builder { + raw: lyon::path::Path::builder().with_svg(), + } + } + + /// Moves the starting point of a new sub-path to the given `Point`. + #[inline] + pub fn move_to(&mut self, point: Point) { + let _ = self.raw.move_to(lyon::math::Point::new(point.x, point.y)); + } + + /// Connects the last point in the [`Path`] to the given `Point` with a + /// straight line. + /// + /// [`Path`]: struct.Path.html + #[inline] + pub fn line_to(&mut self, point: Point) { + let _ = self.raw.line_to(lyon::math::Point::new(point.x, point.y)); + } + + /// Adds an [`Arc`] to the [`Path`] from `start_angle` to `end_angle` in + /// a clockwise direction. + /// + /// [`Arc`]: struct.Arc.html + /// [`Path`]: struct.Path.html + #[inline] + pub fn arc(&mut self, arc: Arc) { + self.ellipse(arc.into()); + } + + /// Adds a circular arc to the [`Path`] with the given control points and + /// radius. + /// + /// The arc is connected to the previous point by a straight line, if + /// necessary. + /// + /// [`Path`]: struct.Path.html + pub fn arc_to(&mut self, a: Point, b: Point, radius: f32) { + use lyon::{math, path}; + + let a = math::Point::new(a.x, a.y); + + if self.raw.current_position() != a { + let _ = self.raw.line_to(a); + } + + let _ = self.raw.arc_to( + math::Vector::new(radius, radius), + math::Angle::radians(0.0), + path::ArcFlags::default(), + math::Point::new(b.x, b.y), + ); + } + + /// Adds an [`Ellipse`] to the [`Path`] using a clockwise direction. + /// + /// [`Ellipse`]: struct.Arc.html + /// [`Path`]: struct.Path.html + pub fn ellipse(&mut self, arc: arc::Elliptical) { + use lyon::{geom, math}; + + let arc = geom::Arc { + center: math::Point::new(arc.center.x, arc.center.y), + radii: math::Vector::new(arc.radii.x, arc.radii.y), + x_rotation: math::Angle::radians(arc.rotation), + start_angle: math::Angle::radians(arc.start_angle), + sweep_angle: math::Angle::radians(arc.end_angle), + }; + + let _ = self.raw.move_to(arc.sample(0.0)); + + arc.for_each_quadratic_bezier(&mut |curve| { + let _ = self.raw.quadratic_bezier_to(curve.ctrl, curve.to); + }); + } + + /// Adds a cubic Bézier curve to the [`Path`] given its two control points + /// and its end point. + /// + /// [`Path`]: struct.Path.html + #[inline] + pub fn bezier_curve_to( + &mut self, + control_a: Point, + control_b: Point, + to: Point, + ) { + use lyon::math; + + let _ = self.raw.cubic_bezier_to( + math::Point::new(control_a.x, control_a.y), + math::Point::new(control_b.x, control_b.y), + math::Point::new(to.x, to.y), + ); + } + + /// Adds a quadratic Bézier curve to the [`Path`] given its control point + /// and its end point. + /// + /// [`Path`]: struct.Path.html + #[inline] + pub fn quadratic_curve_to(&mut self, control: Point, to: Point) { + use lyon::math; + + let _ = self.raw.quadratic_bezier_to( + math::Point::new(control.x, control.y), + math::Point::new(to.x, to.y), + ); + } + + /// Adds a rectangle to the [`Path`] given its top-left corner coordinate + /// and its `Size`. + /// + /// [`Path`]: struct.Path.html + #[inline] + pub fn rectangle(&mut self, p: Point, size: Size) { + self.move_to(p); + self.line_to(Point::new(p.x + size.width, p.y)); + self.line_to(Point::new(p.x + size.width, p.y + size.height)); + self.line_to(Point::new(p.x, p.y + size.height)); + self.close(); + } + + /// Adds a circle to the [`Path`] given its center coordinate and its + /// radius. + /// + /// [`Path`]: struct.Path.html + #[inline] + pub fn circle(&mut self, center: Point, radius: f32) { + self.arc(Arc { + center, + radius, + start_angle: 0.0, + end_angle: 2.0 * std::f32::consts::PI, + }); + } + + /// Closes the current sub-path in the [`Path`] with a straight line to + /// the starting point. + /// + /// [`Path`]: struct.Path.html + #[inline] + pub fn close(&mut self) { + self.raw.close() + } + + /// Builds the [`Path`] of this [`Builder`]. + /// + /// [`Path`]: struct.Path.html + /// [`Builder`]: struct.Builder.html + #[inline] + pub fn build(self) -> Path { + Path { + raw: self.raw.build(), + } + } +} diff --git a/wgpu/src/widget/canvas/stroke.rs b/wgpu/src/widget/canvas/stroke.rs index 9bb260b2..46d669c4 100644 --- a/wgpu/src/widget/canvas/stroke.rs +++ b/wgpu/src/widget/canvas/stroke.rs @@ -1,10 +1,16 @@ use iced_native::Color; +/// The style of a stroke. #[derive(Debug, Clone, Copy)] pub struct Stroke { + /// The color of the stroke. pub color: Color, + /// The distance between the two edges of the stroke. pub width: f32, + /// The shape to be used at the end of open subpaths when they are stroked. pub line_cap: LineCap, + /// The shape to be used at the corners of paths or basic shapes when they + /// are stroked. pub line_join: LineJoin, } @@ -19,10 +25,16 @@ impl Default for Stroke { } } +/// The shape used at the end of open subpaths when they are stroked. #[derive(Debug, Clone, Copy)] pub enum LineCap { + /// The stroke for each sub-path does not extend beyond its two endpoints. Butt, + /// At the end of each sub-path, the shape representing the stroke will be + /// extended by a square. Square, + /// At the end of each sub-path, the shape representing the stroke will be + /// extended by a semicircle. Round, } @@ -42,10 +54,15 @@ impl From for lyon::tessellation::LineCap { } } +/// The shape used at the corners of paths or basic shapes when they are +/// stroked. #[derive(Debug, Clone, Copy)] pub enum LineJoin { + /// A sharp corner. Miter, + /// A round corner. Round, + /// A bevelled corner. Bevel, } -- cgit