Skip to main content

Module animation

Module animation 

Source
Expand description

Animation playback: plays animations on a Skeleton, with tracks, queuing, crossfades and events.

Start from AnimationState. It reads crossfade durations from an AnimationStateData and poses a skeleton from its tracks. Each frame, call AnimationState::update with the elapsed time, then AnimationState::apply, then compute world transforms with Skeleton::update_world_transform. Keyframe Events come back through apply’s out-parameter; lifecycle and keyframe StateEvents come from AnimationState::drain_events.

To pose one animation directly without tracks or mixing, use Skeleton::apply_animation. The curve functions sample raw curve timeline data.

use std::sync::Arc;

use spine_runtime::animation::{AnimationState, AnimationStateData, EventType};
use spine_runtime::data::SkeletonData;
use spine_runtime::skeleton::{Physics, Skeleton};

fn play(data: Arc<SkeletonData>) -> Result<(), Box<dyn std::error::Error>> {
    let mut mixes = AnimationStateData::new(Arc::clone(&data));
    mixes.set_default_mix(0.2);
    mixes.set_mix_by_name("walk", "run", 0.4)?;

    let mut skeleton = Skeleton::new(data);
    let mut state = AnimationState::new(Arc::new(mixes));
    state.set_animation_by_name(0, "walk", true)?;
    state.add_animation_by_name(0, "run", true, 2.0)?;

    let mut events = Vec::new();
    let dt = 1.0 / 60.0;
    for _frame in 0..120 {
        state.update(dt);
        events.clear();
        state.apply(&mut skeleton, &mut events);
        skeleton.update(dt);
        skeleton.update_world_transform(Physics::Update);
        for e in state.drain_events() {
            if e.kind == EventType::Complete {
                println!("track {} completed a loop", e.track_index);
            }
        }
    }
    Ok(())
}

The timeline data lives in crate::data::animation.

Re-exports§

pub use crate::data::animation::Property;
pub use crate::data::animation::PropertyId;
pub use curve::bezier_value;
pub use curve::compute_bezier_samples;
pub use curve::curve_value1;
pub use curve::curve_value2;
pub use curve::search;
pub use state::AnimationNotFound;
pub use state::AnimationState;
pub use state::EMPTY_ANIMATION_ID;
pub use state::EntryId;
pub use state::EventType;
pub use state::Interpolation;
pub use state::StateEvent;
pub use state::TrackEntry;
pub use state_data::AnimationStateData;
pub use state_data::MixAnimationNotFound;

Modules§

apply
Applying an animation’s timelines to a skeleton, through Skeleton::apply_animation.
curve
Curve-timeline sampling and blending.
state
AnimationState: plays animations on tracks with crossfades, queuing and events.
state_data
Mix durations for AnimationState transitions.

Structs§

Event
A keyframe event that fired between the last and current apply time. Its values are copied from the key’s AnimationEvent.

Enums§

MixFrom
What a timeline mixes from before and between its keys.

Constants§

BEZIER_SIZE
Floats per bezier segment in CurveFrames::curves: 9 (x, y) samples.
CURVE_BEZIER
Bezier curve-type code. The stored value is this plus the sample index in CurveFrames::curves.
CURVE_LINEAR
Linear interpolation curve-type code.
CURVE_STEPPED
Stepped (no interpolation) curve-type code.