From 7bd79b0662e5b23221ce5c8983c22e4e8015b54b Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Sun, 18 Aug 2024 10:58:44 -0700 Subject: [PATCH] Switched to released easing-function Also added new easings example. --- Cargo.lock | 3 +- Cargo.toml | 2 +- examples/easings.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 examples/easings.rs diff --git a/Cargo.lock b/Cargo.lock index 06f36f6..305f5c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -762,7 +762,8 @@ checksum = "f25c0e292a7ca6d6498557ff1df68f32c99850012b6ea401cf8daf771f22ff53" [[package]] name = "easing-function" version = "0.1.0" -source = "git+https://github.com/khonsulabs/easing-function#12a233464b0142d86c68d474598befa1d0298031" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5699e572f2ece3d6ea49a528341097d26117751ea0a8ed3adab747bb6e96c480" [[package]] name = "either" diff --git a/Cargo.toml b/Cargo.toml index 9ee33c6..cc540ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ image = { version = "0.25.0", features = ["png"] } plotters = { version = "0.3.5", default-features = false, optional = true } nominals = "0.3.0" parking_lot = "0.12.1" -easing-function = { git = "https://github.com/khonsulabs/easing-function" } +easing-function = "0.1.0" # [patch.crates-io] diff --git a/examples/easings.rs b/examples/easings.rs new file mode 100644 index 0000000..bfcb64a --- /dev/null +++ b/examples/easings.rs @@ -0,0 +1,82 @@ +use cushy::animation::easings::StandardEasing; +use cushy::context::GraphicsContext; +use cushy::widget::{MakeWidget, WidgetList}; +use cushy::widgets::Canvas; +use cushy::Run; +use easing_function::Easing; +use figures::units::{Lp, Px}; +use figures::{IntoSigned, Point, Rect, Size, Zero}; +use kludgine::shapes::{PathBuilder, Shape, StrokeOptions}; + +fn main() -> cushy::Result { + StandardEasing::all() + .iter() + .map(|easing| { + let name = format!("Ease{easing:?}"); + + Canvas::new(|context| { + draw_easing_graph(easing, context); + }) + .expand() + .and(name) + .into_rows() + .contain() + .make_widget() + }) + .collect::>() + .chunks(3) + .map(|widgets| { + WidgetList::from_iter(widgets.iter().map(|w| w.clone().expand())) + .into_columns() + .height(Lp::inches(3)) + }) + .collect::() + .into_wrap() + .pad() + .vertical_scroll() + .expand() + .run() +} + +fn draw_easing_graph(easing: &StandardEasing, context: &mut GraphicsContext<'_, '_, '_, '_>) { + let height = context.gfx.size().height.into_signed(); + let padding = height / 4; + let height = height - padding * 2; + let width = context.gfx.size().width.into_signed().get(); + let steps = width.max(50); + let mut path = PathBuilder::new(Point::new( + Px::ZERO, + padding + height * (1.0 - easing.ease(0.)), + )); + + for i in 1..=steps { + path = path.line_to(Point::new( + Px::new(width * i) / steps, + padding + height * (1.0 - easing.ease(i as f32 / steps as f32)), + )); + } + + let text_color = context.theme().surface.on_color; + let bg = context.theme().surface.low_container; + let outline = context.theme().surface.outline; + context.gfx.draw_shape(&Shape::filled_rect( + Rect::new( + Point::new(Px::ZERO, padding), + Size::new(Px::new(width), height), + ), + bg, + )); + context.gfx.draw_shape(&Shape::stroked_rect( + Rect::new( + Point::new(Px::ZERO, padding), + Size::new(Px::new(width), height), + ), + outline, + )); + + context.gfx.draw_shape( + &path + .build() + .stroke(StrokeOptions::px_wide(1).colored(text_color)), + ); +}