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/path.rs | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 wgpu/src/widget/canvas/path.rs (limited to 'wgpu/src/widget/canvas/path.rs') 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/path.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'wgpu/src/widget/canvas/path.rs') 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/path.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'wgpu/src/widget/canvas/path.rs') 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/path.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'wgpu/src/widget/canvas/path.rs') 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 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/path.rs') 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/path.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'wgpu/src/widget/canvas/path.rs') 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 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/path.rs | 168 +++++------------------------------------ 1 file changed, 17 insertions(+), 151 deletions(-) (limited to 'wgpu/src/widget/canvas/path.rs') 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, - } - } -} -- cgit