Macro mina::animator

source ·
animator!() { /* proc-macro */ }
Expand description

Configures and creates a StateAnimator for an Animate type.

Animators hold one or more Timeline instances mapped to particular state values and will automatically switch and blend timelines when the state is changed. The animator macro uses a CSS-like syntax to define the states and timelines.

Use of this macro requires both an Animate type for the values/timeline and a State derived type for the state (note: State is a re-export of the Enum derive macro from the enum-map crate; you will need to add this crate to the downstream project in order for State to compile. In addition, the state type must implement Clone, Default and PartialEq.

Example

use mina::prelude::*;

#[derive(Clone, Default, PartialEq, State)]
enum State {
    #[default] Idle,
    Active,
}

#[derive(Animate, Clone, Debug, Default, PartialEq)]
struct Style {
    alpha: f32,
    size: u16,
}

let mut animator = animator!(Style {
    default(State::Idle, { alpha: 0.5, size: 60 }),
    State::Idle => 2s Easing::OutQuad to default,
    State::Active => 1s Easing::Linear to { alpha: 1.0, size: 80 }
});

animator.advance(12.0);
assert_eq!(animator.current_values(), &Style { alpha: 0.5, size: 60 });
animator.set_state(&State::Active);
assert_eq!(animator.current_values(), &Style { alpha: 0.5, size: 60 });
animator.advance(0.5);
assert_eq!(animator.current_values(), &Style { alpha: 0.75, size: 70 });
animator.set_state(&State::Idle);
assert_eq!(animator.current_values(), &Style { alpha: 0.75, size: 70 });
animator.advance(0.8);
assert_eq!(animator.current_values(), &Style { alpha: 0.554, size: 62 });
animator.advance(1.2);
assert_eq!(animator.current_values(), &Style { alpha: 0.5, size: 60 });