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
pub(crate) use crate::*;
use std::borrow::Borrow;
pub trait EasingFunction {
fn y(&self, x: f64) -> f64;
}
pub trait CanTween {
fn ease(from: Self, to: Self, time: impl Float) -> Self;
}
impl CanTween for f32 {
#[inline]
fn ease(from: Self, to: Self, time: impl Float) -> Self {
as_t(as_f64(from) + as_f64(to - from) * as_f64(time))
}
}
impl CanTween for f64 {
#[inline]
fn ease(from: Self, to: Self, time: impl Float) -> Self {
as_t(as_f64(from) + as_f64(to - from) * as_f64(time))
}
}
impl<T: CanTween> CanTween for Vec<T> {
fn ease(from: Self, to: Self, time: impl Float) -> Self {
let mut new_vec = Vec::with_capacity(from.len());
let from_iterator = from.into_iter();
let to_iterator = to.into_iter();
if from_iterator.len() != to_iterator.len() {
panic!("Tweening between two vectors can only be done when they are the same size.");
}
for (f, t) in from_iterator.zip(to_iterator) {
new_vec.push(T::ease(f, t, time));
}
new_vec
}
}
#[inline]
pub fn ease_with_unbounded_time<V: CanTween, F: EasingFunction>(function: impl Borrow<F>, from: V, to: V, time: impl Float) -> V {
V::ease(from, to, function.borrow().y(as_f64(time)))
}
#[inline]
pub fn ease<V: CanTween, T: Float, F: EasingFunction>(function: impl Borrow<F>, from: V, to: V, time: T) -> V {
ease_with_unbounded_time(function, from, to, match time {
_ if time < T::zero() => T::zero(),
_ if time > T::one() => T::one(),
_ => time
})
}
#[inline]
pub fn ease_with_scaled_time<V: CanTween, T: Float, F: EasingFunction>(function: impl Borrow<F>, from: V, to: V, time: T, max_time: T) -> V {
ease(function, from, to, match time {
_ if time < T::zero() => T::zero(),
_ if time > max_time => T::one(),
_ => time / max_time
})
}
#[cfg(feature = "mint_types")]
mod mint_type_impls {
use crate::easing::*;
impl<V: CanTween> CanTween for Vector2<V> {
#[inline]
fn ease(from: Self, to: Self, time: impl Float) -> Self {
Self {
x: V::ease(from.x, to.x, time),
y: V::ease(from.y, to.y, time)
}
}
}
impl<V: CanTween> CanTween for Vector3<V> {
#[inline]
fn ease(from: Self, to: Self, time: impl Float) -> Self {
Self {
x: V::ease(from.x, to.x, time),
y: V::ease(from.y, to.y, time),
z: V::ease(from.z, to.z, time)
}
}
}
impl<V: CanTween> CanTween for Vector4<V> {
#[inline]
fn ease(from: Self, to: Self, time: impl Float) -> Self {
Self {
x: V::ease(from.x, to.x, time),
y: V::ease(from.y, to.y, time),
z: V::ease(from.z, to.z, time),
w: V::ease(from.w, to.w, time)
}
}
}
impl<V: CanTween> CanTween for Point2<V> {
#[inline]
fn ease(from: Self, to: Self, time: impl Float) -> Self {
Self {
x: V::ease(from.x, to.x, time),
y: V::ease(from.y, to.y, time)
}
}
}
impl<V: CanTween> CanTween for Point3<V> {
#[inline]
fn ease(from: Self, to: Self, time: impl Float) -> Self {
Self {
x: V::ease(from.x, to.x, time),
y: V::ease(from.y, to.y, time),
z: V::ease(from.z, to.z, time)
}
}
}
}
#[cfg(feature = "mint_types")]
pub use mint_type_impls::*;