From 2ebc92319711e6fa0dda310939257334625b59c9 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Wed, 7 Jun 2023 15:19:11 -0700 Subject: feat: use lyon for easing --- examples/loading_spinners/src/easing.rs | 132 ++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 examples/loading_spinners/src/easing.rs (limited to 'examples/loading_spinners/src/easing.rs') diff --git a/examples/loading_spinners/src/easing.rs b/examples/loading_spinners/src/easing.rs new file mode 100644 index 00000000..04befa15 --- /dev/null +++ b/examples/loading_spinners/src/easing.rs @@ -0,0 +1,132 @@ +use iced_core::Point; +use lyon::algorithms::measure::PathMeasurements; +use lyon::path::builder::NoAttributes; +use lyon::path::path::BuilderImpl; +use lyon::path::Path; + +use once_cell::sync::Lazy; + +pub static EMPHASIZED: Lazy = Lazy::new(|| { + Easing::builder() + .cubic_bezier_to([0.05, 0.0], [0.133333, 0.06], [0.166666, 0.4]) + .cubic_bezier_to([0.208333, 0.82], [0.25, 1.0], [1.0, 1.0]) + .build() +}); + +pub static EMPHASIZED_DECELERATE: Lazy = Lazy::new(|| { + Easing::builder() + .cubic_bezier_to([0.05, 0.7], [0.1, 1.0], [1.0, 1.0]) + .build() +}); + +pub static EMPHASIZED_ACCELERATE: Lazy = Lazy::new(|| { + Easing::builder() + .cubic_bezier_to([0.3, 0.0], [0.8, 0.15], [1.0, 1.0]) + .build() +}); + +pub static STANDARD: Lazy = Lazy::new(|| { + Easing::builder() + .cubic_bezier_to([0.2, 0.0], [0.0, 1.0], [1.0, 1.0]) + .build() +}); + +pub static STANDARD_DECELERATE: Lazy = Lazy::new(|| { + Easing::builder() + .cubic_bezier_to([0.0, 0.0], [0.0, 1.0], [1.0, 1.0]) + .build() +}); + +pub static STANDARD_ACCELERATE: Lazy = Lazy::new(|| { + Easing::builder() + .cubic_bezier_to([0.3, 0.0], [1.0, 1.0], [1.0, 1.0]) + .build() +}); + +pub struct Easing { + path: Path, + measurements: PathMeasurements, +} + +impl Easing { + pub fn builder() -> Builder { + Builder::new() + } + + pub fn y_at_x(&self, x: f32) -> f32 { + let mut sampler = self.measurements.create_sampler( + &self.path, + lyon::algorithms::measure::SampleType::Normalized, + ); + let sample = sampler.sample(x); + + sample.position().y + } +} + +pub struct Builder(NoAttributes); + +impl Builder { + pub fn new() -> Self { + let mut builder = Path::builder(); + builder.begin(lyon::geom::point(0.0, 0.0)); + + Self(builder) + } + + /// Adds a line segment. Points must be between 0,0 and 1,1 + pub fn line_to(mut self, to: impl Into) -> Self { + self.0.line_to(Self::point(to)); + + self + } + + /// Adds a quadratic bézier curve. Points must be between 0,0 and 1,1 + pub fn quadratic_bezier_to( + mut self, + ctrl: impl Into, + to: impl Into, + ) -> Self { + self.0 + .quadratic_bezier_to(Self::point(ctrl), Self::point(to)); + + self + } + + /// Adds a cubic bézier curve. Points must be between 0,0 and 1,1 + pub fn cubic_bezier_to( + mut self, + ctrl1: impl Into, + ctrl2: impl Into, + to: impl Into, + ) -> Self { + self.0.cubic_bezier_to( + Self::point(ctrl1), + Self::point(ctrl2), + Self::point(to), + ); + + self + } + + pub fn build(mut self) -> Easing { + self.0.line_to(lyon::geom::point(1.0, 1.0)); + self.0.end(false); + + let path = self.0.build(); + let measurements = PathMeasurements::from_path(&path, 0.0); + + Easing { path, measurements } + } + + fn point(p: impl Into) -> lyon::geom::Point { + let p: Point = p.into(); + lyon::geom::point(p.x.min(1.0).max(0.0), p.y.min(1.0).max(0.0)) + } +} + +impl Default for Builder { + fn default() -> Self { + Self::new() + } +} -- cgit From 56eacdb3583e701cde8cca074e151b24c3fd8df3 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Thu, 8 Jun 2023 07:47:57 -0700 Subject: comment: cleanup imports --- examples/loading_spinners/src/easing.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'examples/loading_spinners/src/easing.rs') diff --git a/examples/loading_spinners/src/easing.rs b/examples/loading_spinners/src/easing.rs index 04befa15..665b3329 100644 --- a/examples/loading_spinners/src/easing.rs +++ b/examples/loading_spinners/src/easing.rs @@ -1,9 +1,7 @@ -use iced_core::Point; -use lyon::algorithms::measure::PathMeasurements; -use lyon::path::builder::NoAttributes; -use lyon::path::path::BuilderImpl; -use lyon::path::Path; +use iced::Point; +use lyon_algorithms::measure::PathMeasurements; +use lyon_algorithms::path::{builder::NoAttributes, path::BuilderImpl, Path}; use once_cell::sync::Lazy; pub static EMPHASIZED: Lazy = Lazy::new(|| { @@ -56,7 +54,7 @@ impl Easing { pub fn y_at_x(&self, x: f32) -> f32 { let mut sampler = self.measurements.create_sampler( &self.path, - lyon::algorithms::measure::SampleType::Normalized, + lyon_algorithms::measure::SampleType::Normalized, ); let sample = sampler.sample(x); @@ -69,7 +67,7 @@ pub struct Builder(NoAttributes); impl Builder { pub fn new() -> Self { let mut builder = Path::builder(); - builder.begin(lyon::geom::point(0.0, 0.0)); + builder.begin(lyon_algorithms::geom::point(0.0, 0.0)); Self(builder) } @@ -110,7 +108,7 @@ impl Builder { } pub fn build(mut self) -> Easing { - self.0.line_to(lyon::geom::point(1.0, 1.0)); + self.0.line_to(lyon_algorithms::geom::point(1.0, 1.0)); self.0.end(false); let path = self.0.build(); @@ -119,9 +117,12 @@ impl Builder { Easing { path, measurements } } - fn point(p: impl Into) -> lyon::geom::Point { + fn point(p: impl Into) -> lyon_algorithms::geom::Point { let p: Point = p.into(); - lyon::geom::point(p.x.min(1.0).max(0.0), p.y.min(1.0).max(0.0)) + lyon_algorithms::geom::point( + p.x.min(1.0).max(0.0), + p.y.min(1.0).max(0.0), + ) } } -- cgit