diff options
Diffstat (limited to 'src/gui/easing.rs')
| -rw-r--r-- | src/gui/easing.rs | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/src/gui/easing.rs b/src/gui/easing.rs deleted file mode 100644 index a4ef652..0000000 --- a/src/gui/easing.rs +++ /dev/null @@ -1,102 +0,0 @@ -use iced::Point; - -use lyon_algorithms::measure::PathMeasurements; -use lyon_algorithms::path::{builder::NoAttributes, path::BuilderImpl, Path}; -use once_cell::sync::Lazy; - -pub static STANDARD: Lazy<Easing> = Lazy::new(|| { - Easing::builder() - .cubic_bezier_to([0.2, 0.0], [0.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<BuilderImpl>); - -impl Builder { - pub fn new() -> Self { - let mut builder = Path::builder(); - builder.begin(lyon_algorithms::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<Point>) -> 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<Point>, - to: impl Into<Point>, - ) -> 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<Point>, - ctrl2: impl Into<Point>, - to: impl Into<Point>, - ) -> 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_algorithms::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<Point>) -> lyon_algorithms::geom::Point<f32> { - let p: Point = p.into(); - lyon_algorithms::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() - } -} |