1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#![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;
}
}