use crate::animation::animation_clip::{AnimationBlendMode, AnimationClip};
use crate::animation::animation_mixer::{BindingPool, ControlHandle, ControlPool, RootId};
use crate::animation::keyframe_track::TrackInterpolant;
use crate::math::interpolant::{Ending, InterpolantSettings};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LoopMode {
Once,
Repeat,
PingPong,
}
impl LoopMode {
pub fn as_number(self) -> u32 {
match self {
Self::Once => 2200,
Self::Repeat => 2201,
Self::PingPong => 2202,
}
}
pub fn from_number(n: u32) -> Option<Self> {
match n {
2200 => Some(Self::Once),
2201 => Some(Self::Repeat),
2202 => Some(Self::PingPong),
_ => None,
}
}
}
pub struct AnimationAction {
pub(crate) clip: AnimationClip,
pub(crate) local_root: Option<RootId>,
pub blend_mode: AnimationBlendMode,
pub(crate) interpolants: Vec<Option<TrackInterpolant>>,
pub(crate) interpolant_settings: InterpolantSettings,
pub(crate) property_bindings: Vec<Option<usize>>,
pub(crate) cache_index: Option<usize>,
pub(crate) by_clip_cache_index: Option<usize>,
pub(crate) time_scale_interpolant: Option<ControlHandle>,
pub(crate) restore_time_scale: Option<f64>,
pub(crate) weight_interpolant: Option<ControlHandle>,
pub loop_mode: LoopMode,
pub loop_count: i32,
pub start_time: Option<f64>,
pub time: f64,
pub time_scale: f64,
pub(crate) effective_time_scale: f64,
pub weight: f64,
pub(crate) effective_weight: f64,
pub repetitions: f64,
pub paused: bool,
pub enabled: bool,
pub clamp_when_finished: bool,
pub zero_slope_at_start: bool,
pub zero_slope_at_end: bool,
}
impl AnimationAction {
pub fn new(
clip: AnimationClip,
local_root: Option<RootId>,
blend_mode: Option<AnimationBlendMode>,
) -> Self {
let blend_mode = blend_mode.unwrap_or(clip.blend_mode);
let interpolants: Vec<Option<TrackInterpolant>> = clip
.tracks
.iter()
.map(|track| track.create_interpolant(None))
.collect();
let n_tracks = interpolants.len();
Self {
clip,
local_root,
blend_mode,
interpolants,
interpolant_settings: InterpolantSettings {
ending_start: Ending::ZeroCurvature,
ending_end: Ending::ZeroCurvature,
},
property_bindings: vec![None; n_tracks],
cache_index: None,
by_clip_cache_index: None,
time_scale_interpolant: None,
restore_time_scale: None,
weight_interpolant: None,
loop_mode: LoopMode::Repeat,
loop_count: -1,
start_time: None,
time: 0.0,
time_scale: 1.0,
effective_time_scale: 1.0,
weight: 1.0,
effective_weight: 1.0,
repetitions: f64::INFINITY,
paused: false,
enabled: true,
clamp_when_finished: false,
zero_slope_at_start: true,
zero_slope_at_end: true,
}
}
pub fn get_clip(&self) -> &AnimationClip {
&self.clip
}
pub fn local_root(&self) -> Option<RootId> {
self.local_root
}
pub(crate) fn reset_(&mut self, control: &mut ControlPool) -> &mut Self {
self.paused = false;
self.enabled = true;
self.time = 0.0; self.loop_count = -1; self.start_time = None;
self.stop_fading_(control).stop_warping_(control)
}
pub(crate) fn is_running_locally(&self) -> bool {
self.enabled && !self.paused && self.time_scale != 0.0 && self.start_time.is_none()
}
pub(crate) fn start_at_(&mut self, time: f64) -> &mut Self {
self.start_time = Some(time);
self
}
pub(crate) fn set_loop_(&mut self, mode: LoopMode, repetitions: f64) -> &mut Self {
self.loop_mode = mode;
self.repetitions = repetitions;
self
}
pub(crate) fn set_effective_weight_(
&mut self,
weight: f64,
control: &mut ControlPool,
) -> &mut Self {
self.weight = weight;
self.effective_weight = if self.enabled { weight } else { 0.0 };
self.stop_fading_(control)
}
pub fn get_effective_weight(&self) -> f64 {
self.effective_weight
}
pub(crate) fn fade_in_(
&mut self,
duration: f64,
now: f64,
control: &mut ControlPool,
) -> &mut Self {
self.schedule_fading_(duration, 0.0, 1.0, now, control)
}
pub(crate) fn fade_out_(
&mut self,
duration: f64,
now: f64,
control: &mut ControlPool,
) -> &mut Self {
self.schedule_fading_(duration, 1.0, 0.0, now, control)
}
pub(crate) fn stop_fading_(&mut self, control: &mut ControlPool) -> &mut Self {
if let Some(handle) = self.weight_interpolant.take() {
control.take_back_control_interpolant(handle);
}
self
}
pub(crate) fn set_effective_time_scale_(
&mut self,
time_scale: f64,
control: &mut ControlPool,
) -> &mut Self {
self.time_scale = time_scale;
self.effective_time_scale = if self.paused { 0.0 } else { time_scale };
self.stop_warping_(control)
}
pub fn get_effective_time_scale(&self) -> f64 {
self.effective_time_scale
}
pub(crate) fn set_duration_(&mut self, duration: f64, control: &mut ControlPool) -> &mut Self {
self.time_scale = self.clip.duration / duration;
self.stop_warping_(control)
}
pub(crate) fn sync_with_(
&mut self,
other_time: f64,
other_time_scale: f64,
control: &mut ControlPool,
) -> &mut Self {
self.time = other_time;
self.time_scale = other_time_scale;
self.stop_warping_(control)
}
pub(crate) fn halt_(
&mut self,
duration: f64,
now: f64,
control: &mut ControlPool,
) -> &mut Self {
let from = self.effective_time_scale;
self.warp_(from, 0.0, duration, now, control)
}
pub(crate) fn warp_(
&mut self,
start_time_scale: f64,
end_time_scale: f64,
duration: f64,
now: f64,
control: &mut ControlPool,
) -> &mut Self {
let time_scale = self.time_scale;
let handle = match self.time_scale_interpolant {
Some(handle) => handle,
None => {
let handle = control.lend_control_interpolant();
self.time_scale_interpolant = Some(handle);
handle
}
};
let data = control.data_mut(handle);
data.parameter_positions[0] = now;
data.parameter_positions[1] = now + duration;
data.sample_values[0] = start_time_scale / time_scale;
data.sample_values[1] = end_time_scale / time_scale;
self
}
pub(crate) fn stop_warping_(&mut self, control: &mut ControlPool) -> &mut Self {
if let Some(handle) = self.time_scale_interpolant.take() {
control.take_back_control_interpolant(handle);
}
self.restore_time_scale = None;
self
}
pub(crate) fn update_(
&mut self,
time: f64,
mut delta_time: f64,
time_direction: f64,
accu_index: usize,
bindings: &mut BindingPool,
control: &mut ControlPool,
) {
if !self.enabled {
self.update_weight_(time, control);
return;
}
if let Some(start_time) = self.start_time {
let time_running = (time - start_time) * time_direction;
if time_running < 0.0 || time_direction == 0.0 {
delta_time = 0.0;
} else {
self.start_time = None; delta_time = time_direction * time_running;
}
}
delta_time *= self.update_time_scale_(time, control);
let clip_time = self.update_time_(delta_time);
let weight = self.update_weight_(time, control);
if weight > 0.0 {
let blend_mode = self.blend_mode;
for j in 0..self.interpolants.len() {
let Some(binding_handle) = self.property_bindings[j] else {
continue;
};
let Some(interpolant) = self.interpolants[j].as_mut() else {
continue;
};
interpolant.data().settings = Some(self.interpolant_settings);
let values = interpolant.evaluate(clip_time);
let Some(property_mixer) = bindings.get_mut(binding_handle) else {
continue;
};
let n = property_mixer.value_size;
property_mixer.buffer[..n].copy_from_slice(&values[..n]);
match blend_mode {
AnimationBlendMode::Additive => {
property_mixer.accumulate_additive(weight);
}
AnimationBlendMode::Normal => {
property_mixer.accumulate(accu_index, weight);
}
}
}
}
}
pub(crate) fn update_weight_(&mut self, time: f64, control: &mut ControlPool) -> f64 {
let mut weight = 0.0;
if self.enabled {
weight = self.weight;
if let Some(handle) = self.weight_interpolant {
let interpolant_value = control.evaluate(handle, time);
weight *= interpolant_value;
if time > control.data_mut(handle).parameter_positions[1] {
self.stop_fading_(control);
if interpolant_value == 0.0 {
self.enabled = false;
}
}
}
}
self.effective_weight = weight;
weight
}
pub(crate) fn update_time_scale_(&mut self, time: f64, control: &mut ControlPool) -> f64 {
let mut time_scale = 0.0;
if !self.paused {
time_scale = self.time_scale;
if let Some(handle) = self.time_scale_interpolant {
let interpolant_value = control.evaluate(handle, time);
time_scale *= interpolant_value;
if time > control.data_mut(handle).parameter_positions[1] {
if time_scale == 0.0 {
self.paused = true;
} else {
if let Some(restore) = self.restore_time_scale {
time_scale = restore;
}
self.time_scale = time_scale;
}
self.stop_warping_(control);
}
}
}
self.effective_time_scale = time_scale;
time_scale
}
pub(crate) fn update_time_(&mut self, delta_time: f64) -> f64 {
let duration = self.clip.duration;
let loop_mode = self.loop_mode;
let mut time = self.time + delta_time;
let mut loop_count = self.loop_count;
let ping_pong = loop_mode == LoopMode::PingPong;
if delta_time == 0.0 {
if loop_count == -1 {
return time;
}
return if ping_pong && (loop_count & 1) == 1 {
duration - time
} else {
time
};
}
if loop_mode == LoopMode::Once {
if loop_count == -1 {
self.loop_count = 0;
self.set_endings_(true, true, false);
}
#[allow(clippy::never_loop)]
loop {
if time >= duration {
time = duration;
} else if time < 0.0 {
time = 0.0;
} else {
self.time = time;
break;
}
if self.clamp_when_finished {
self.paused = true;
} else {
self.enabled = false;
}
self.time = time;
break;
}
} else {
if loop_count == -1 {
if delta_time >= 0.0 {
loop_count = 0;
self.set_endings_(true, self.repetitions == 0.0, ping_pong);
} else {
self.set_endings_(self.repetitions == 0.0, true, ping_pong);
}
}
if time >= duration || time < 0.0 {
let loop_delta = (time / duration).floor(); time -= duration * loop_delta;
loop_count += loop_delta.abs() as i32;
let pending = self.repetitions - loop_count as f64;
if pending <= 0.0 {
if self.clamp_when_finished {
self.paused = true;
} else {
self.enabled = false;
}
time = if delta_time > 0.0 { duration } else { 0.0 };
self.time = time;
} else {
if pending == 1.0 {
let at_start = delta_time < 0.0;
self.set_endings_(at_start, !at_start, ping_pong);
} else {
self.set_endings_(false, false, ping_pong);
}
self.loop_count = loop_count;
self.time = time;
}
} else {
self.loop_count = loop_count;
self.time = time;
}
if ping_pong && (loop_count & 1) == 1 {
return duration - time;
}
}
time
}
pub(crate) fn set_endings_(&mut self, at_start: bool, at_end: bool, ping_pong: bool) {
let settings = &mut self.interpolant_settings;
if ping_pong {
settings.ending_start = Ending::ZeroSlope;
settings.ending_end = Ending::ZeroSlope;
} else {
settings.ending_start = if at_start {
if self.zero_slope_at_start {
Ending::ZeroSlope
} else {
Ending::ZeroCurvature
}
} else {
Ending::WrapAround
};
settings.ending_end = if at_end {
if self.zero_slope_at_end {
Ending::ZeroSlope
} else {
Ending::ZeroCurvature
}
} else {
Ending::WrapAround
};
}
}
pub(crate) fn schedule_fading_(
&mut self,
duration: f64,
weight_now: f64,
weight_then: f64,
now: f64,
control: &mut ControlPool,
) -> &mut Self {
let handle = match self.weight_interpolant {
Some(handle) => handle,
None => {
let handle = control.lend_control_interpolant();
self.weight_interpolant = Some(handle);
handle
}
};
let data = control.data_mut(handle);
data.parameter_positions[0] = now;
data.sample_values[0] = weight_now;
data.parameter_positions[1] = now + duration;
data.sample_values[1] = weight_then;
self
}
}