Newer
Older
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use crate::easing::EaseTween;
use bevy_ecs::component::{Component, Mutable};
use bevy_ecs::entity::Entity;
use bevy_math::Curve;
use bevy_math::curve::{Ease, Interval};
use std::fmt::{Debug, Formatter};
use std::time::Duration;
pub trait Tweenable: Send + Sync + 'static {
type Comp: Component<Mutability = Mutable>;
type Data: Ease + Clone + Send + Sync + 'static;
fn tween(
duration: impl Into<Duration>,
from: Self::Data,
to: Self::Data,
easing: EaseTween,
) -> Tween<Self>
where
Self: Sized,
{
Self::delayed(duration, Duration::ZERO, from, to, easing)
}
fn delayed(
duration: impl Into<Duration>,
delay: impl Into<Duration>,
from: Self::Data,
to: Self::Data,
easing: EaseTween,
) -> Tween<Self>
where
Self: Sized,
{
Tween::delayed(duration, delay, from, to, easing)
}
fn sample(progress: f32, easing: &TweenEasing<Self::Data>) -> Self::Data {
easing.sample_clamped(progress)
}
fn current_value(cmp: &Self::Comp) -> Self::Data;
fn update_component(cmp: &mut Self::Comp, value: Self::Data);
fn interpolate(cmp: &mut Self::Comp, progress: f32, easing: &TweenEasing<Self::Data>) {
let value = Self::sample(progress, easing);
Self::update_component(cmp, value);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Component, Default)]
pub enum TweenMode {
#[default]
Once,
PingPong,
Loop,
}
impl TweenMode {
pub fn is_loop(&self) -> bool {
matches!(self, Self::PingPong | Self::Loop)
}
pub fn is_once(&self) -> bool {
matches!(self, Self::Once)
}
}
#[derive(Component)]
#[require(TweenMode)]
pub struct Tween<T: Tweenable> {
pub delay: Duration,
pub duration: Duration,
pub elapsed: Duration,
pub easing: TweenEasing<T::Data>,
pub user_data: Option<u32>,
}
#[derive(Component)]
#[relationship(relationship_target = ActiveTweens)]
pub struct TweenTarget(pub Entity);
#[derive(Component)]
#[relationship_target(relationship = TweenTarget)]
pub struct ActiveTweens(Vec<Entity>);
impl<T: Tweenable> Debug for Tween<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Tween<T>")
.field("delay", &self.delay)
.field("duration", &self.duration)
.field("elapsed", &self.elapsed)
.field(
"easing",
&format!("TweenEasing<{}>", std::any::type_name::<T::Data>()),
)
.field("user_data", &self.user_data)
.finish()
}
}
impl<T> Clone for Tween<T>
where
T: Tweenable,
T::Data: Clone,
{
fn clone(&self) -> Self {
Self {
delay: self.delay,
duration: self.duration,
elapsed: self.elapsed,
easing: self.easing.clone(),
user_data: self.user_data,
}
}
}
impl<T: Tweenable> Tween<T> {
pub fn new(
duration: impl Into<Duration>,
from: T::Data,
to: T::Data,
easing: EaseTween,
) -> Self {
Self::delayed(duration, Duration::ZERO, from, to, easing)
}
pub fn delayed(
duration: impl Into<Duration>,
delay: impl Into<Duration>,
from: T::Data,
to: T::Data,
easing: EaseTween,
) -> Self {
let easing = TweenEasing::new(from, to, easing);
Self {
delay: delay.into(),
duration: duration.into(),
elapsed: Duration::ZERO,
easing,
user_data: None,
}
}
pub fn linear(duration: impl Into<Duration>, from: T::Data, to: T::Data) -> Self {
Tween::new(duration, from, to, EaseTween::Linear)
}
pub fn with_user_data(self, user_data: Option<u32>) -> Self {
Self { user_data, ..self }
}
}
#[derive(Copy, Clone, Default, Debug)]
#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
pub enum UpdateUserData {
#[default]
Unchanged,
Set(u32),
Remove,
}
impl From<Option<u32>> for UpdateUserData {
fn from(value: Option<u32>) -> Self {
match value {
Some(value) => UpdateUserData::Set(value),
None => UpdateUserData::Unchanged,
}
}
}
impl From<UpdateUserData> for Option<u32> {
fn from(value: UpdateUserData) -> Self {
match value {
UpdateUserData::Unchanged => None,
UpdateUserData::Set(value) => Some(value),
UpdateUserData::Remove => None,
}
}
}
pub struct UpdateTween<T: Tweenable> {
pub entity: Entity,
pub duration: Duration,
pub end: T::Data,
pub easing: EaseTween,
pub user_data: UpdateUserData,
}
#[derive(Clone, Debug)]
pub struct TweenEasing<T> {
start: T,
end: T,
ease_fn: EaseTween,
}
impl<T: Clone> TweenEasing<T> {
pub fn new(start: T, end: T, ease_fn: EaseTween) -> Self {
Self {
start,
end,
ease_fn,
}
}
pub fn start(&self) -> &T {
&self.start
}
pub fn end(&self) -> &T {
&self.end
}
pub fn easing(&self) -> EaseTween {
self.ease_fn
}
pub fn flip(&self) -> Self {
TweenEasing {
start: self.end.clone(),
end: self.start.clone(),
ease_fn: self.ease_fn,
}
}
}
impl<T> Curve<T> for TweenEasing<T>
where
T: Ease + Clone,
{
#[inline]
fn domain(&self) -> Interval {
Interval::UNIT
}
#[inline]
fn sample_unchecked(&self, t: f32) -> T {
let remapped_t = self.ease_fn.eval(t);
T::interpolating_curve_unbounded(self.start.clone(), self.end.clone())
.sample_unchecked(remapped_t)
}
}