From bf600a0811649392008927578afc32a18a93de34 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 27 Jan 2025 02:50:51 +0100 Subject: Draft basic `Animation` API in `iced_core` --- core/src/animation.rs | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++ core/src/lib.rs | 2 + 2 files changed, 111 insertions(+) create mode 100644 core/src/animation.rs (limited to 'core/src') diff --git a/core/src/animation.rs b/core/src/animation.rs new file mode 100644 index 00000000..eae6785e --- /dev/null +++ b/core/src/animation.rs @@ -0,0 +1,109 @@ +//! Animate your applications. +use crate::time::{Duration, Instant}; + +pub use lilt::{Easing, FloatRepresentable as Float, Interpolable}; + +/// The animation of some particular state. +/// +/// It tracks state changes and allows projecting interpolated values +/// through time. +#[derive(Debug, Clone)] +pub struct Animation +where + T: Clone + Copy + PartialEq + Float, +{ + raw: lilt::Animated, +} + +impl Animation +where + T: Clone + Copy + PartialEq + Float, +{ + /// Creates a new [`Animation`] with the given initial state. + pub fn new(state: T) -> Self { + Self { + raw: lilt::Animated::new(state), + } + } + + /// Sets the [`Easing`] function of the [`Animation`]. + /// + /// See the [Easing Functions Cheat Sheet](https://easings.net) for + /// details! + pub fn easing(mut self, easing: Easing) -> Self { + self.raw = self.raw.easing(easing); + self + } + + /// Sets the duration of the [`Animation`] to 100ms. + pub fn very_quick(self) -> Self { + self.duration(Duration::from_millis(100)) + } + + /// Sets the duration of the [`Animation`] to 200ms. + pub fn quick(self) -> Self { + self.duration(Duration::from_millis(200)) + } + + /// Sets the duration of the [`Animation`] to 400ms. + pub fn slow(self) -> Self { + self.duration(Duration::from_millis(400)) + } + + /// Sets the duration of the [`Animation`] to 500ms. + pub fn very_slow(self) -> Self { + self.duration(Duration::from_millis(500)) + } + + /// Sets the duration of the [`Animation`] to the given value. + pub fn duration(mut self, duration: Duration) -> Self { + self.raw = self.raw.duration(duration.as_secs_f32() * 1_000.0); + self + } + + /// Sets a delay for the [`Animation`]. + pub fn delay(mut self, duration: Duration) -> Self { + self.raw = self.raw.delay(duration.as_secs_f64() as f32 * 1000.0); + self + } + + /// Transitions the [`Animation`] from its current state to the given new state. + pub fn go(mut self, new_state: T) -> Self { + self.go_mut(new_state); + self + } + + /// Transitions the [`Animation`] from its current state to the given new state, by reference. + pub fn go_mut(&mut self, new_state: T) { + self.raw.transition(new_state, Instant::now()); + } + + /// Returns true if the [`Animation`] is currently in progress. + /// + /// An [`Animation`] is in progress when it is transitioning to a different state. + pub fn in_progress(&self, at: Instant) -> bool { + self.raw.in_progress(at) + } + + /// Projects the [`Animation`] into an interpolated value at the given [`Instant`]; using the + /// closure provided to calculate the different keyframes of interpolated values. + /// + /// If the [`Animation`] state is a `bool`, you can use the simpler [`interpolate`] method. + pub fn interpolate_with(&self, f: impl Fn(T) -> I, at: Instant) -> I + where + I: Interpolable, + { + self.raw.animate(f, at) + } +} + +impl Animation { + /// Projects the [`Animation`] into an interpolated value at the given [`Instant`]; using the + /// `start` and `end` values as the origin and destination keyframes. + pub fn interpolate(&self, start: I, end: I, at: Instant) -> I + where + I: Interpolable + Clone, + { + self.raw.animate_bool(start, end, at) + } +} diff --git a/core/src/lib.rs b/core/src/lib.rs index 645f7a90..16b3aa0f 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -10,6 +10,7 @@ html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" )] pub mod alignment; +pub mod animation; pub mod border; pub mod clipboard; pub mod event; @@ -49,6 +50,7 @@ mod vector; pub use alignment::Alignment; pub use angle::{Degrees, Radians}; +pub use animation::Animation; pub use background::Background; pub use border::Border; pub use clipboard::Clipboard; -- cgit From 890d852e0544ffbf06ac7eb8c3904dc04a0008cf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 27 Jan 2025 05:01:24 +0100 Subject: Implement `Animation::value` --- core/src/animation.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'core/src') diff --git a/core/src/animation.rs b/core/src/animation.rs index eae6785e..fe2b29b2 100644 --- a/core/src/animation.rs +++ b/core/src/animation.rs @@ -95,6 +95,11 @@ where { self.raw.animate(f, at) } + + /// Retuns the current state of the [`Animation`]. + pub fn value(&self) -> T { + self.raw.value + } } impl Animation { -- cgit From 2753103942afc262103c15faf9ab2e7495b3f5c2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 27 Jan 2025 05:22:37 +0100 Subject: Fix broken intra-doc link in `animation` --- core/src/animation.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'core/src') diff --git a/core/src/animation.rs b/core/src/animation.rs index fe2b29b2..29dacd4d 100644 --- a/core/src/animation.rs +++ b/core/src/animation.rs @@ -89,6 +89,8 @@ where /// closure provided to calculate the different keyframes of interpolated values. /// /// If the [`Animation`] state is a `bool`, you can use the simpler [`interpolate`] method. + /// + /// [`interpolate`]: Animation::interpolate pub fn interpolate_with(&self, f: impl Fn(T) -> I, at: Instant) -> I where I: Interpolable, -- cgit From 8fd87f94eb11f4c76e9aa67ef6c2a3452add0730 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 27 Jan 2025 05:24:10 +0100 Subject: Make `animation` module only available in native --- core/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'core/src') diff --git a/core/src/lib.rs b/core/src/lib.rs index 16b3aa0f..3a61a5ee 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -10,6 +10,7 @@ html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" )] pub mod alignment; +#[cfg(not(target_arch = "wasm32"))] pub mod animation; pub mod border; pub mod clipboard; @@ -50,6 +51,7 @@ mod vector; pub use alignment::Alignment; pub use angle::{Degrees, Radians}; +#[cfg(not(target_arch = "wasm32"))] pub use animation::Animation; pub use background::Background; pub use border::Border; -- cgit From 23d42d2827a68468bc520440c6396eb114d793bc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Jan 2025 03:04:31 +0100 Subject: Rename `Animation::in_progress` to `is_animating` --- core/src/animation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/animation.rs b/core/src/animation.rs index 29dacd4d..5ff31544 100644 --- a/core/src/animation.rs +++ b/core/src/animation.rs @@ -81,7 +81,7 @@ where /// Returns true if the [`Animation`] is currently in progress. /// /// An [`Animation`] is in progress when it is transitioning to a different state. - pub fn in_progress(&self, at: Instant) -> bool { + pub fn is_animating(&self, at: Instant) -> bool { self.raw.in_progress(at) } -- cgit From ae35992048e7717a6e4308841859bfb6c0a7d51c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Jan 2025 03:24:48 +0100 Subject: Add `repeat`, `repeat_forever`, and `auto_reverse` to `Animation` --- core/src/animation.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'core/src') diff --git a/core/src/animation.rs b/core/src/animation.rs index 5ff31544..258fd084 100644 --- a/core/src/animation.rs +++ b/core/src/animation.rs @@ -67,6 +67,26 @@ where self } + /// Makes the [`Animation`] repeat a given amount of times. + /// + /// Providing 1 repetition plays the animation twice in total. + pub fn repeat(mut self, repetitions: u32) -> Self { + self.raw = self.raw.repeat(repetitions); + self + } + + /// Makes the [`Animation`] repeat forever. + pub fn repeat_forever(mut self) -> Self { + self.raw = self.raw.repeat_forever(); + self + } + + /// Makes the [`Animation`] automatically reverse when repeating. + pub fn auto_reverse(mut self) -> Self { + self.raw = self.raw.auto_reverse(); + self + } + /// Transitions the [`Animation`] from its current state to the given new state. pub fn go(mut self, new_state: T) -> Self { self.go_mut(new_state); -- cgit