AnimatableProperty

Trait AnimatableProperty 

Source
pub trait AnimatableProperty:
    Send
    + Sync
    + 'static {
    type Property: Animatable;

    // Required methods
    fn get_mut<'a>(
        &self,
        entity: &'a mut AnimationEntityMut<'_, '_>,
    ) -> Result<&'a mut Self::Property, AnimationEvaluationError>;
    fn evaluator_id(&self) -> EvaluatorId<'_>;
}
Expand description

A trait for exposing a value in an entity so that it can be animated.

AnimatableProperty allows any value contained in an entity to be animated as long as it can be obtained by mutable reference. This makes it more flexible than animated_field.

Here, AnimatableProperty is used to animate a value inside an Option, returning an error if the option is None.

#[derive(Component)]
struct ExampleComponent {
    power_level: Option<f32>
}

#[derive(Clone)]
struct PowerLevelProperty;

impl AnimatableProperty for PowerLevelProperty {
    type Property = f32;
    fn get_mut<'a>(
        &self,
        entity: &'a mut AnimationEntityMut
    ) -> Result<&'a mut Self::Property, AnimationEvaluationError> {
        let component = entity
            .get_mut::<ExampleComponent>()
            .ok_or(AnimationEvaluationError::ComponentNotPresent(
              TypeId::of::<ExampleComponent>()
            ))?
            .into_inner();
        component.power_level.as_mut().ok_or(AnimationEvaluationError::PropertyNotPresent(
            TypeId::of::<Option<f32>>()
        ))
    }

    fn evaluator_id(&self) -> EvaluatorId {
        EvaluatorId::Type(TypeId::of::<Self>())
    }
}

You can then create an AnimatableCurve to animate this property like so:

AnimatableCurve::new(
    PowerLevelProperty,
    AnimatableKeyframeCurve::new([
        (0.0, 0.0),
        (1.0, 9001.0),
    ]).expect("Failed to create power level curve")
);

Required Associated Types§

Source

type Property: Animatable

The animated property type.

Required Methods§

Source

fn get_mut<'a>( &self, entity: &'a mut AnimationEntityMut<'_, '_>, ) -> Result<&'a mut Self::Property, AnimationEvaluationError>

Retrieves the property from the given entity.

Source

fn evaluator_id(&self) -> EvaluatorId<'_>

The EvaluatorId used to look up the AnimationCurveEvaluator for this AnimatableProperty. For a given animated property, this ID should always be the same to allow things like animation blending to occur.

Implementors§

Source§

impl<C, A, F> AnimatableProperty for AnimatedField<C, A, F>
where C: Component<Mutability = Mutable>, A: Animatable + Clone + Sync + Debug, F: Fn(&mut C) -> &mut A + Send + Sync + 'static,