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 <alice.i.cecile@gmail.com>
This commit is contained in:
Mincong Lu 2024-06-08 05:51:24 +08:00 committed by GitHub
parent d45bcfd043
commit 651f3d08d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 3 deletions

View file

@ -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<AnimationNodeIndex, f32>,
}
// 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> {

View file

@ -33,8 +33,23 @@ pub struct AnimationTransitions {
transitions: Vec<AnimationTransition>,
}
// 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,