From 4df95384ba3913b224208c60a61569d41c7ea4a1 Mon Sep 17 00:00:00 2001 From: Mincong Lu <139340600+mintlu8@users.noreply.github.com> Date: Sat, 8 Jun 2024 05:51:24 +0800 Subject: [PATCH] Make the component types of the new animation players clonable. (#13736) # Objective Some use cases might require holding onto the previous state of the animation player for change detection. ## Solution Added `clone` and `copy` implementation to most animation types. Added optimized `clone_from` implementations for the specific use case of holding a `PreviousAnimationPlayer` component. --------- Co-authored-by: Alice Cecile --- crates/bevy_animation/src/lib.rs | 19 +++++++++++++++++-- crates/bevy_animation/src/transition.rs | 17 ++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/crates/bevy_animation/src/lib.rs b/crates/bevy_animation/src/lib.rs index f634d13259..0023532a18 100644 --- a/crates/bevy_animation/src/lib.rs +++ b/crates/bevy_animation/src/lib.rs @@ -236,7 +236,7 @@ impl Hash for AnimationTargetId { /// Note that each entity can only be animated by one animation player at a /// time. However, you can change [`AnimationTarget`]'s `player` property at /// runtime to change which player is responsible for animating the entity. -#[derive(Clone, Component, Reflect)] +#[derive(Clone, Copy, Component, Reflect)] #[reflect(Component, MapEntities)] pub struct AnimationTarget { /// The ID of this animation target. @@ -326,7 +326,7 @@ pub enum RepeatAnimation { /// playing, but is presently paused. /// /// An stopped animation is considered no longer active. -#[derive(Debug, Reflect)] +#[derive(Debug, Clone, Copy, Reflect)] pub struct ActiveAnimation { /// The factor by which the weight from the [`AnimationGraph`] is multiplied. weight: f32, @@ -515,6 +515,21 @@ pub struct AnimationPlayer { blend_weights: HashMap, } +// This is needed since `#[derive(Clone)]` does not generate optimized `clone_from`. +impl Clone for AnimationPlayer { + fn clone(&self) -> Self { + Self { + active_animations: self.active_animations.clone(), + blend_weights: self.blend_weights.clone(), + } + } + + fn clone_from(&mut self, source: &Self) { + self.active_animations.clone_from(&source.active_animations); + self.blend_weights.clone_from(&source.blend_weights); + } +} + /// The components that we might need to read or write during animation of each /// animation target. struct AnimationTargetContext<'a> { diff --git a/crates/bevy_animation/src/transition.rs b/crates/bevy_animation/src/transition.rs index e783398629..1f3baf5478 100644 --- a/crates/bevy_animation/src/transition.rs +++ b/crates/bevy_animation/src/transition.rs @@ -33,8 +33,23 @@ pub struct AnimationTransitions { transitions: Vec, } +// This is needed since `#[derive(Clone)]` does not generate optimized `clone_from`. +impl Clone for AnimationTransitions { + fn clone(&self) -> Self { + Self { + main_animation: self.main_animation, + transitions: self.transitions.clone(), + } + } + + fn clone_from(&mut self, source: &Self) { + self.main_animation = source.main_animation; + self.transitions.clone_from(&source.transitions); + } +} + /// An animation that is being faded out as part of a transition -#[derive(Debug, Reflect)] +#[derive(Debug, Clone, Copy, Reflect)] pub struct AnimationTransition { /// The current weight. Starts at 1.0 and goes to 0.0 during the fade-out. current_weight: f32,