From a768e70a7114fc83abdc0fa256afcb68d7d70b6a Mon Sep 17 00:00:00 2001 From: Jerome Humbert Date: Sat, 28 Oct 2023 16:09:01 +0100 Subject: [PATCH] Asset animator uses `Handle` on same entity Change asset animation to behave like component animation, and retrieve the `Handle` component referencing the asset to animate directly from a Bevy query on the same `Entity` instead of specifying it manually in the `AssetAnimator` itself. Task: #101 --- CHANGELOG.md | 4 ++++ examples/colormaterial_color.rs | 4 ++-- src/lens.rs | 12 ++++++----- src/lib.rs | 37 +++++++++++++++------------------ src/plugin.rs | 6 +++--- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ababef3..6e4b5ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- The `AssetAnimator` doesn't take any `Handle` anymore; instead the `asset_animator_system::()` retrieves the handle of the asset to animate from the same `Entity` the `AssetAnimator` is attached to. This aligns the behavior with component animation. (#101) + ### Fixed - Fixed a panic when a `TextLens` tried to change a text section that wasn't present. diff --git a/examples/colormaterial_color.rs b/examples/colormaterial_color.rs index 7e39941..eac6b80 100644 --- a/examples/colormaterial_color.rs +++ b/examples/colormaterial_color.rs @@ -93,10 +93,10 @@ fn setup( mesh: quad_mesh.clone(), transform: Transform::from_translation(Vec3::new(x, y, 0.)) .with_scale(Vec3::splat(size)), - material: unique_material.clone(), + material: unique_material, ..default() }, - AssetAnimator::new(unique_material.clone(), tween), + AssetAnimator::new(tween), )); y -= size * spacing; if y < -screen_y { diff --git a/src/lens.rs b/src/lens.rs index 0608660..47ee9dc 100644 --- a/src/lens.rs +++ b/src/lens.rs @@ -99,7 +99,7 @@ impl Lens for TextColorLens { let end: Vec4 = self.end.into(); let value = start.lerp(end, ratio); - if let Some(section) = target.sections.get_mut(self.section){ + if let Some(section) = target.sections.get_mut(self.section) { section.style.color = value.into(); } } @@ -332,7 +332,6 @@ pub struct UiBackgroundColorLens { pub end: Color, } - #[cfg(feature = "bevy_ui")] impl Lens for UiBackgroundColorLens { fn lerp(&mut self, target: &mut BackgroundColor, ratio: f32) { @@ -418,17 +417,20 @@ mod tests { lens.lerp(&mut text, 0.3); assert_eq!(text.sections[0].style.color, Color::rgba(0.7, 0., 0.3, 1.0)); - let mut lens_section1 = TextColorLens{ + let mut lens_section1 = TextColorLens { start: Color::RED, end: Color::BLUE, section: 1, }; lens_section1.lerp(&mut text, 1.); - //Should not have changed because the lens targets section 1 + // Should not have changed because the lens targets section 1 assert_eq!(text.sections[0].style.color, Color::rgba(0.7, 0., 0.3, 1.0)); - text.sections.push(TextSection { value: "".to_string(), style: Default::default() }); + text.sections.push(TextSection { + value: "".to_string(), + style: Default::default(), + }); lens_section1.lerp(&mut text, 0.3); assert_eq!(text.sections[1].style.color, Color::rgba(0.7, 0., 0.3, 1.0)); diff --git a/src/lib.rs b/src/lib.rs index 5bde6d1..b97a0cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -469,6 +469,9 @@ macro_rules! animator_impl { } /// Component to control the animation of another component. +/// +/// The animated component is the component located on the same entity as the +/// [`Animator`] itself. #[derive(Component)] pub struct Animator { /// Control if this animation is played or not. @@ -500,13 +503,15 @@ impl Animator { } /// Component to control the animation of an asset. +/// +/// The animated asset is the asset referenced by a [`Handle`] component +/// located on the same entity as the [`AssetAnimator`] itself. #[cfg(feature = "bevy_asset")] #[derive(Component)] pub struct AssetAnimator { /// Control if this animation is played or not. pub state: AnimatorState, tweenable: BoxedTweenable, - handle: Handle, speed: f32, } @@ -523,21 +528,15 @@ impl std::fmt::Debug for AssetAnimator { impl AssetAnimator { /// Create a new asset animator component from a single tweenable. #[must_use] - pub fn new(handle: Handle, tween: impl Tweenable + 'static) -> Self { + pub fn new(tween: impl Tweenable + 'static) -> Self { Self { state: default(), tweenable: Box::new(tween), - handle, speed: 1., } } animator_impl!(); - - #[must_use] - fn handle(&self) -> Handle { - self.handle.clone() - } } #[cfg(test)] @@ -773,9 +772,8 @@ mod tests { Duration::from_secs(1), DummyLens { start: 0., end: 1. }, ); - let animator = AssetAnimator::new(Handle::::default(), tween); + let animator = AssetAnimator::new(tween); assert_eq!(animator.state, AnimatorState::default()); - assert_eq!(animator.handle(), Handle::::default()); let tween = animator; assert_eq!(tween.tweenable().progress(), 0.); } @@ -789,8 +787,7 @@ mod tests { Duration::from_secs(1), DummyLens { start: 0., end: 1. }, ); - let animator = - AssetAnimator::new(Handle::::default(), tween).with_state(state); + let animator = AssetAnimator::new(tween).with_state(state); assert_eq!(animator.state, state); // impl Debug @@ -805,12 +802,12 @@ mod tests { #[cfg(feature = "bevy_asset")] #[test] fn asset_animator_controls() { - let tween = Tween::new( + let tween: Tween = Tween::new( EaseFunction::QuadraticInOut, Duration::from_secs(1), DummyLens { start: 0., end: 1. }, ); - let mut animator = AssetAnimator::new(Handle::::default(), tween); + let mut animator = AssetAnimator::new(tween); assert_eq!(animator.state, AnimatorState::Playing); assert_approx_eq!(animator.tweenable().progress(), 0.); @@ -843,37 +840,37 @@ mod tests { #[cfg(feature = "bevy_asset")] #[test] fn asset_animator_speed() { - let tween = Tween::new( + let tween: Tween = Tween::new( EaseFunction::QuadraticInOut, Duration::from_secs(1), DummyLens { start: 0., end: 1. }, ); - let mut animator = AssetAnimator::new(Handle::::default(), tween); + let mut animator = AssetAnimator::new(tween); assert_approx_eq!(animator.speed(), 1.); // default speed animator.set_speed(2.4); assert_approx_eq!(animator.speed(), 2.4); - let tween = Tween::new( + let tween: Tween = Tween::new( EaseFunction::QuadraticInOut, Duration::from_secs(1), DummyLens { start: 0., end: 1. }, ); - let animator = AssetAnimator::new(Handle::::default(), tween).with_speed(3.5); + let animator = AssetAnimator::new(tween).with_speed(3.5); assert_approx_eq!(animator.speed(), 3.5); } #[cfg(feature = "bevy_asset")] #[test] fn asset_animator_set_tweenable() { - let tween = Tween::new( + let tween: Tween = Tween::new( EaseFunction::QuadraticInOut, Duration::from_secs(1), DummyLens { start: 0., end: 1. }, ); - let mut animator = AssetAnimator::new(Handle::::default(), tween); + let mut animator = AssetAnimator::new(tween); let tween2 = Tween::new( EaseFunction::QuadraticInOut, diff --git a/src/plugin.rs b/src/plugin.rs index 1e728a6..7425578 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -116,14 +116,14 @@ pub fn component_animator_system( pub fn asset_animator_system( time: Res