vizia_core/animation/
animation_state.rs
use crate::animation::Interpolator;
use hashbrown::HashSet;
use crate::prelude::*;
use super::TimingFunction;
#[derive(Debug, Clone)]
pub(crate) struct Keyframe<T: Interpolator> {
pub time: f32,
pub value: T,
pub timing_function: TimingFunction,
}
#[derive(Clone, Debug)]
pub(crate) struct AnimationState<T: Interpolator> {
pub id: Animation,
pub start_time: Instant,
pub duration: Duration,
pub delay: Duration,
pub keyframes: Vec<Keyframe<T>>,
pub output: Option<T>,
pub persistent: bool,
pub t: f32,
pub dt: f32,
pub active: bool,
pub from_rule: usize,
pub to_rule: usize,
pub entities: HashSet<Entity>,
}
impl<T> AnimationState<T>
where
T: Interpolator,
{
pub(crate) fn new(id: Animation) -> Self {
AnimationState {
id,
start_time: Instant::now(),
duration: Duration::new(0, 0),
delay: Duration::new(0, 0),
keyframes: Vec::new(),
output: None,
persistent: false,
t: 0.0,
dt: 0.0,
active: false,
entities: HashSet::new(),
from_rule: usize::MAX,
to_rule: usize::MAX,
}
}
pub(crate) fn with_duration(mut self, duration: Duration) -> Self {
self.duration = duration;
self
}
pub(crate) fn with_delay(mut self, delay: Duration) -> Self {
self.delay = delay;
self
}
pub(crate) fn with_keyframe(mut self, key: Keyframe<T>) -> Self {
self.keyframes.push(key);
self
}
pub(crate) fn get_output(&self) -> Option<&T> {
self.output.as_ref()
}
pub(crate) fn play(&mut self, entity: Entity) {
self.active = true;
self.t = 0.0;
self.start_time = Instant::now();
self.entities.insert(entity);
}
pub(crate) fn is_transition(&self) -> bool {
!(self.from_rule == usize::MAX && self.to_rule == usize::MAX)
}
}
impl<Prop> Default for AnimationState<Prop>
where
Prop: Interpolator,
{
fn default() -> Self {
AnimationState {
id: Animation::null(),
start_time: Instant::now(),
duration: Duration::new(0, 0),
delay: Duration::new(0, 0),
keyframes: Vec::new(),
output: None,
persistent: true,
t: 0.0,
dt: 0.0,
active: false,
entities: HashSet::new(),
from_rule: usize::MAX,
to_rule: usize::MAX,
}
}
}