diff --git a/CHANGELOG.md b/CHANGELOG.md index 17ef9be98974a303098fa08d3c96a600c3b6fc60..b43cc8e0619380ca52eaa2ffc9aac46c923d8a21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unrelease] + +### Added + +- Added `From<u32>` and `From<Duration>` for `RepeatCount`, respectively yielding `RepeatCount::Finite(value)` and `RepeatCount::For(value)`. + +### Changed + +- Changed the signature of `with_repeat_count()` to take an `impl Into<RepeatCount>` instead of a `RepeatCount` by value. + ## [0.6.0] - 2022-11-15 ### Added diff --git a/src/lib.rs b/src/lib.rs index e1e083cf2fe72dd5b64b37937d488fc2752a3075..6f464c9c19fddaf993d450b1f2c94f1b39f7aab3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -187,7 +187,26 @@ pub enum RepeatCount { Infinite, } -/// What to do when a tween animation needs to be repeated. +impl Default for RepeatCount { + fn default() -> Self { + Self::Finite(1) + } +} + +impl From<u32> for RepeatCount { + fn from(value: u32) -> Self { + Self::Finite(value) + } +} + +impl From<Duration> for RepeatCount { + fn from(value: Duration) -> Self { + Self::For(value) + } +} + +/// What to do when a tween animation needs to be repeated. See also +/// [`RepeatCount`]. /// /// Only applicable when [`RepeatCount`] is greater than the animation duration. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -204,12 +223,6 @@ pub enum RepeatStrategy { MirroredRepeat, } -impl Default for RepeatCount { - fn default() -> Self { - Self::Finite(1) - } -} - impl Default for RepeatStrategy { fn default() -> Self { Self::Repeat diff --git a/src/tweenable.rs b/src/tweenable.rs index e8827c1a951e831a1f49d49c968874fc21492997..1e7f6c57dac943c4989250ec79f886840e0f9499 100644 --- a/src/tweenable.rs +++ b/src/tweenable.rs @@ -574,8 +574,8 @@ impl<T> Tween<T> { /// Set the number of times to repeat the animation. #[must_use] - pub fn with_repeat_count(mut self, count: RepeatCount) -> Self { - self.clock.total_duration = compute_total_duration(self.clock.duration, count); + pub fn with_repeat_count(mut self, count: impl Into<RepeatCount>) -> Self { + self.clock.total_duration = compute_total_duration(self.clock.duration, count.into()); self } @@ -1245,6 +1245,37 @@ mod tests { ); } + #[test] + fn into_repeat_count() { + let tween = Tween::new( + EaseMethod::Linear, + Duration::from_secs(1), + TransformPositionLens { + start: Vec3::ZERO, + end: Vec3::ONE, + }, + ) + .with_repeat_count(5); + assert_eq!( + tween.total_duration(), + TotalDuration::Finite(Duration::from_secs(5)) + ); + + let tween = Tween::new( + EaseMethod::Linear, + Duration::from_secs(1), + TransformPositionLens { + start: Vec3::ZERO, + end: Vec3::ONE, + }, + ) + .with_repeat_count(Duration::from_secs(3)); + assert_eq!( + tween.total_duration(), + TotalDuration::Finite(Duration::from_secs(3)) + ); + } + /// Test ticking of a single tween in isolation. #[test] fn tween_tick() {