-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
plugin.rs
206 lines (196 loc) · 8.43 KB
/
plugin.rs
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
use bevy::{ecs::component::Component, prelude::*};
use crate::{EasingDirection, MyEaser};
use crate::{
CustomComponentEase, Ease, EaseValue, EasingChainComponent, EasingComponent, EasingState,
EasingType,
};
/// Plugin to add systems related to easing
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Copy)]
pub struct EasingsPlugin;
impl Plugin for EasingsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, ease_system::<Transform>);
#[cfg(feature = "sprite")]
app.add_systems(Update, ease_system::<Sprite>);
#[cfg(feature = "ui")]
app.add_systems(Update, ease_system::<Style>);
#[cfg(feature = "ui")]
app.add_systems(Update, ease_system::<BackgroundColor>);
#[cfg(feature = "ui")]
app.add_systems(Update, ease_system::<BorderColor>);
}
}
pub fn ease_system<T: Ease + Component>(
mut commands: Commands,
time: Res<Time>,
entity_query: Query<Entity, With<T>>,
mut object_query: Query<&mut T>,
mut easing_query: Query<&mut EasingComponent<T>>,
mut chain_query: Query<&mut EasingChainComponent<T>>,
) where
EaseValue<T>: interpolation::Lerp<Scalar = f32>,
T: Default,
{
for entity in entity_query.iter() {
if let Ok(ref mut easing) = easing_query.get_mut(entity) {
if easing.state == EasingState::Play {
easing.timer.tick(time.delta());
} else {
continue;
}
if easing.paused {
if easing.timer.just_finished() {
match easing.easing_type {
EasingType::Once { duration }
| EasingType::Loop { duration, .. }
| EasingType::PingPong { duration, .. } => {
easing.timer.set_duration(duration);
}
}
easing.timer.reset();
easing.paused = false;
}
} else {
if easing.timer.duration().as_secs_f32() != 0. {
let mut object = object_query.get_mut(entity).unwrap();
let progress = if easing.direction == EasingDirection::Forward {
easing.timer.fraction()
} else {
easing.timer.fraction_remaining()
};
let factor = progress.compute(easing.ease_function);
if let Some(ref start) = easing.start {
*object = interpolation::lerp(start, &easing.end, &factor).0;
} else {
*object =
interpolation::lerp(&EaseValue(T::default()), &easing.end, &factor).0;
}
}
if easing.timer.finished() {
match easing.easing_type {
EasingType::Once { .. } => {
commands.entity(entity).remove::<EasingComponent<T>>();
}
EasingType::Loop { pause, .. } => {
if let Some(pause) = pause {
easing.timer.set_duration(pause);
easing.paused = true;
}
easing.timer.reset();
}
EasingType::PingPong { pause, .. } => {
if let Some(pause) = pause {
easing.timer.set_duration(pause);
easing.paused = true;
}
easing.timer.reset();
easing.direction.reverse();
}
}
}
}
} else if let Ok(ref mut easing_chain) = chain_query.get_mut(entity) {
let next = easing_chain.0.pop();
if let Some(mut next) = next {
let mut object = object_query.get_mut(entity).unwrap();
if next.start.is_none() {
next.start = Some(EaseValue(std::mem::take(&mut object)));
}
if let Some(ref start) = next.start {
*object = interpolation::lerp(start, &next.end, &0.).0;
} else {
*object = interpolation::lerp(&EaseValue(T::default()), &next.end, &0.).0;
}
commands.entity(entity).insert(next);
} else {
commands.entity(entity).remove::<EasingChainComponent<T>>();
}
}
}
}
/// Ease system for custom component. Add this system to your application with your component as a type parameter.
pub fn custom_ease_system<T: CustomComponentEase + Component>(
mut commands: Commands,
time: Res<Time>,
entity_query: Query<Entity, With<T>>,
mut object_query: Query<&mut T>,
mut easing_query: Query<&mut EasingComponent<T>>,
mut chain_query: Query<&mut EasingChainComponent<T>>,
) where
T: interpolation::Lerp<Scalar = f32> + Default,
{
for entity in entity_query.iter() {
if let Ok(ref mut easing) = easing_query.get_mut(entity) {
if easing.state == EasingState::Play {
easing.timer.tick(time.delta());
}
if easing.paused {
if easing.timer.just_finished() {
match easing.easing_type {
EasingType::Once { duration }
| EasingType::Loop { duration, .. }
| EasingType::PingPong { duration, .. } => {
easing.timer.set_duration(duration);
}
}
easing.timer.reset();
easing.paused = false;
}
} else {
if easing.timer.duration().as_secs_f32() != 0. {
let mut object = object_query.get_mut(entity).unwrap();
let progress = if easing.direction == EasingDirection::Forward {
easing.timer.fraction()
} else {
easing.timer.fraction_remaining()
};
let factor = progress.compute(easing.ease_function);
if let Some(ref start) = easing.start {
*object = interpolation::lerp(&start.0, &easing.end.0, &factor);
} else {
*object = interpolation::lerp(&T::default(), &easing.end.0, &factor);
}
}
if easing.timer.finished() {
match easing.easing_type {
EasingType::Once { .. } => {
commands.entity(entity).remove::<EasingComponent<T>>();
}
EasingType::Loop { pause, .. } => {
if let Some(pause) = pause {
easing.timer.set_duration(pause);
easing.paused = true;
}
easing.timer.reset();
}
EasingType::PingPong { pause, .. } => {
if let Some(pause) = pause {
easing.timer.set_duration(pause);
easing.paused = true;
}
easing.timer.reset();
easing.direction.reverse();
}
}
}
}
} else if let Ok(ref mut easing_chain) = chain_query.get_mut(entity) {
let next = easing_chain.0.pop();
if let Some(mut next) = next {
let mut object = object_query.get_mut(entity).unwrap();
if next.start.is_none() {
next.start = Some(EaseValue(std::mem::take(&mut object)));
}
if let Some(ref start) = next.start {
*object = interpolation::lerp(&start.0, &next.end.0, &0.);
} else {
*object = interpolation::lerp(&T::default(), &next.end.0, &0.);
}
commands.entity(entity).insert(next);
} else {
commands.entity(entity).remove::<EasingChainComponent<T>>();
}
}
}
}