#![allow(warnings)]
use crate::entity::Entity;
use crate::state::storage::dense_storage::DenseStorage;
pub use crate::state::style::*;
use crate::{PropSet, State};
use std::time::{Duration, Instant};
use crate::state::style::Color;
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
pub struct Animation(usize);
impl Animation {
pub(crate) fn new(id: usize) -> Self {
Self(id)
}
pub fn null() -> Self {
Self(std::usize::MAX)
}
pub fn get_id(&self) -> usize {
self.0
}
}
impl Default for Animation {
fn default() -> Self {
Self::null()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Transition {
pub property: String,
pub duration: f32,
pub delay: f32,
}
impl Transition {
pub fn new() -> Self {
Transition {
property: String::new(),
duration: 0.0,
delay: 0.0,
}
}
}
pub trait Interpolator {
fn interpolate(start: &Self, end: &Self, t: f32) -> Self;
}
#[derive(Clone, Debug)]
pub struct AnimationState<Prop: Interpolator> {
pub indices: Vec<usize>,
pub start_time: Instant,
pub duration: Duration,
pub delay: f32,
pub keyframes: Vec<(f32, Prop)>,
pub output: Option<Prop>,
pub persistent: bool,
pub t0: f32,
pub t: f32,
pub active: bool,
pub from_rule: usize,
pub to_rule: usize,
pub entities: Vec<Entity>,
}
impl<Prop> AnimationState<Prop>
where
Prop: Interpolator,
{
pub fn new() -> Self {
AnimationState {
indices: Vec::new(),
start_time: Instant::now(),
duration: Duration::new(0, 0),
delay: 0.0,
keyframes: Vec::new(),
output: None,
persistent: false,
t0: 0.0,
t: 0.0,
active: false,
entities: Vec::new(),
from_rule: std::usize::MAX,
to_rule: std::usize::MAX,
}
}
pub fn with_duration(mut self, duration: Duration) -> Self {
self.duration = duration;
self
}
pub fn with_delay(mut self, delay: Duration) -> Self {
self.delay = delay.as_secs_f32() / self.duration.as_secs_f32();
self
}
pub fn set_delay(&mut self, delay: Duration) -> &mut Self {
self.delay = delay.as_secs_f32() / self.duration.as_secs_f32();
self
}
pub fn with_keyframe(mut self, key: (f32, Prop)) -> Self {
self.keyframes.push(key);
self
}
pub fn interpolate(&mut self, current_time: Instant) -> bool {
if current_time > self.start_time + self.duration {
return false;
}
true
}
pub fn set_persistent(mut self, flag: bool) -> Self {
self.persistent = flag;
self
}
pub fn get_output(&self) -> Option<&Prop> {
self.output.as_ref()
}
}
impl<Prop> Default for AnimationState<Prop>
where
Prop: Interpolator,
{
fn default() -> Self {
AnimationState {
indices: Vec::new(),
start_time: Instant::now(),
duration: Duration::new(0, 0),
delay: 0.0,
keyframes: Vec::new(),
output: None,
persistent: true,
t0: 0.0,
t: 0.0,
active: false,
entities: Vec::new(),
from_rule: std::usize::MAX,
to_rule: std::usize::MAX,
}
}
}
impl Interpolator for Color {
fn interpolate(start: &Self, end: &Self, t: f32) -> Self {
Color::interpolate(start.clone(), end.clone(), t as f64)
}
}
impl Interpolator for f32 {
fn interpolate(start: &Self, end: &Self, t: f32) -> Self {
return start + (end - start) * t;
}
}
impl Interpolator for i32 {
fn interpolate(start: &Self, end: &Self, t: f32) -> Self {
return ((start + (end - start)) as f32 * t).round() as i32;
}
}