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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! Combinator framework
use std::time::Duration;
use bevy::{ecs::system::EntityCommands, prelude::*};
use bevy_time_runner::{
Repeat, RepeatStyle, SkipTimeRunner, TimeContext, TimeDirection,
TimeRunner, TimeSpan,
};
mod animation_combinators;
mod state;
pub use animation_combinators::*;
pub use state::{TargetState, TransformTargetState, TransformTargetStateExt};
/// Commands to use within an animation combinator
pub struct AnimationCommands<'r, 'a> {
child_builder: &'r mut ChildSpawnerCommands<'a>,
}
impl<'r, 'a> AnimationCommands<'r, 'a> {
pub(crate) fn new(
child_builder: &'r mut ChildSpawnerCommands<'a>,
) -> AnimationCommands<'r, 'a> {
AnimationCommands { child_builder }
}
/// Spawn an entity as a child.
/// Currently always spawn as a child of animation root that should contains [`bevy_time_runner::TimeRunner`].
pub fn spawn(&mut self, bundle: impl Bundle) -> EntityCommands<'_> {
self.child_builder.spawn(bundle)
}
}
/// Extension trait for types that can be used to make an animation.
pub trait AnimationBuilderExt {
/// Construct [`AnimationBuilder`] from [`Self`]
fn animation(&mut self) -> AnimationBuilder<'_>;
/// Construct [`AnimationBuilder`] from [`Self`] within the specified time context.
fn animation_in_time_context<TimeCtx>(
&mut self,
) -> AnimationBuilder<'_, TimeCtx>
where
TimeCtx: Default + Send + Sync + 'static;
}
impl AnimationBuilderExt for EntityCommands<'_> {
/// Construct [`AnimationBuilder`] from [`EntityCommands`].
/// Use this entity as the animator.
/// Tweens will be spawned as children of this entity.
fn animation(&mut self) -> AnimationBuilder<'_> {
self.animation_in_time_context()
}
/// Construct [`AnimationBuilder`] from [`EntityCommands`].
/// Use this entity as the animator.
/// Tweens will be spawned as children of this entity and only runs within the specified time context.
fn animation_in_time_context<TimeCtx>(
&mut self,
) -> AnimationBuilder<'_, TimeCtx>
where
TimeCtx: Default + Send + Sync + 'static,
{
AnimationBuilder::new(self.reborrow())
}
}
impl AnimationBuilderExt for Commands<'_, '_> {
/// Construct [`AnimationBuilder`] from [`Commands`].
/// This will automatically spawn an entity as the animator.
fn animation(&mut self) -> AnimationBuilder<'_> {
self.animation_in_time_context()
}
/// Construct [`AnimationBuilder`] from [`Commands`].
/// This will automatically spawn an entity as the animator and only runs within the specified time context.
fn animation_in_time_context<TimeCtx>(
&mut self,
) -> AnimationBuilder<'_, TimeCtx>
where
TimeCtx: Default + Send + Sync + 'static,
{
AnimationBuilder::new(self.spawn_empty())
}
}
impl AnimationBuilderExt for ChildSpawnerCommands<'_> {
/// Construct [`AnimationBuilder`] from [`ChildSpawnerCommands`].
/// This will automatically spawn a child entity as the animator.
fn animation(&mut self) -> AnimationBuilder<'_> {
self.animation_in_time_context()
}
/// Construct [`AnimationBuilder`] from [`ChildSpawnerCommands`].
/// This will automatically spawn a child entity as the animator and only runs within the specified time context.
fn animation_in_time_context<TimeCtx>(
&mut self,
) -> AnimationBuilder<'_, TimeCtx>
where
TimeCtx: Default + Send + Sync + 'static,
{
AnimationBuilder::new(self.spawn_empty())
}
}
/// Configure [`TimeRunner`] through a builder API and add animation entities
pub struct AnimationBuilder<'a, TimeCtx = ()>
where
TimeCtx: Default + Send + Sync + 'static,
{
entity_commands: EntityCommands<'a>,
time_runner: Option<TimeRunner>,
time_context_marker: Option<TimeContext<TimeCtx>>,
custom_length: Option<Duration>,
skipped: bool,
}
impl<'a, TimeCtx> AnimationBuilder<'a, TimeCtx>
where
TimeCtx: Default + Send + Sync + 'static,
{
/// Create new [`AnimationBuilder`]
pub fn new(
entity_commands: EntityCommands<'a>,
) -> AnimationBuilder<'a, TimeCtx> {
AnimationBuilder {
entity_commands,
time_runner: None,
time_context_marker: None,
custom_length: None,
skipped: false,
}
}
/// Get the inner [`EntityCommands`]
pub fn entity_commands(&mut self) -> &mut EntityCommands<'a> {
&mut self.entity_commands
}
/// Get the inner building [`TimeRunner`]
pub fn time_runner(&self) -> &Option<TimeRunner> {
&self.time_runner
}
/// Get the inner building [`TimeRunner`] mutably
pub fn time_runner_mut(&mut self) -> &mut Option<TimeRunner> {
&mut self.time_runner
}
/// Configure [`TimeRunner`]'s [`Repeat`]
pub fn repeat(mut self, repeat: Repeat) -> Self {
let time_runner = self.time_runner_or_default();
match TimeRunner::repeat(time_runner) {
Some((_, repeat_style)) => {
time_runner.set_repeat(Some((repeat, repeat_style)));
}
None => {
time_runner.set_repeat(Some((repeat, RepeatStyle::default())));
}
}
self
}
/// Configure [`TimeRunner`]'s [`RepeatStyle`]
pub fn repeat_style(mut self, repeat_style: RepeatStyle) -> Self {
let time_runner = self.time_runner_or_default();
match TimeRunner::repeat(time_runner) {
Some((repeat, _)) => {
time_runner.set_repeat(Some((repeat, repeat_style)));
}
None => {
time_runner
.set_repeat(Some((Repeat::Infinitely, repeat_style)));
}
}
self
}
/// Configure [`TimeRunner`]'s `paused`. Note that pausing only pauses the timer
/// but not the animation it self.
pub fn paused(mut self, paused: bool) -> Self {
self.time_runner_or_default().set_paused(paused);
self
}
/// Skip [`TimeRunner`] from inserting [`TimeSpanProgress`](bevy_time_runner::TimeSpanProgress) which is a signal
/// for an animation entity to execute animation code.
pub fn skipped(mut self, skipped: bool) -> Self {
self.skipped = skipped;
self
}
/// [`Self::paused`] and [`Self::skipped`]
pub fn disabled(self, disabled: bool) -> Self {
self.paused(disabled).skipped(disabled)
}
/// Use custom duration instead of determined by [`insert`](Self::insert).
pub fn length(mut self, duration: Duration) -> Self {
self.custom_length = Some(duration);
self
}
/// Configure [`TimeRunner`]'s time scale to adjust animation speed.
/// Negative scale cause animation play in the opposite of [`TimeDirection`] and
/// [`Repeat`] counter will tick backward.
pub fn time_scale(mut self, scale: f32) -> Self {
self.time_runner_or_default().set_time_scale(scale);
self
}
/// Configure [`TimeRunner`]'s direction to play animation backward or forward.
pub fn direction(mut self, direction: TimeDirection) -> Self {
self.time_runner_or_default().set_direction(direction);
self
}
fn time_runner_or_default(&mut self) -> &mut TimeRunner {
self.time_runner.get_or_insert_with(TimeRunner::default)
}
/// Add animations from a closure. Animation entities will be subjected
/// as a children of this entity.
/// [`TimeRunner`]'s length is determined by last `&mut Duration` value unless use
/// [`Self::length`].
/// It's also possible to use combinator like [`go`], [`forward`], and [`backward`]
/// as the last combinator to customize the length.
pub fn insert<F>(self, animation: F) -> EntityCommands<'a>
where
F: FnOnce(&mut AnimationCommands, &mut Duration),
{
let AnimationBuilder {
mut entity_commands,
time_runner,
time_context_marker,
custom_length,
skipped,
} = self;
let mut dur = Duration::ZERO;
entity_commands.with_children(|c| {
let mut a = AnimationCommands::new(c);
animation(&mut a, &mut dur);
});
let mut time_runner = time_runner.unwrap_or_default();
match custom_length {
Some(length) => {
time_runner.set_length(length);
}
None => {
time_runner.set_length(dur);
}
}
entity_commands
.insert((time_runner, time_context_marker.unwrap_or_default()));
if skipped {
entity_commands.insert(SkipTimeRunner);
}
entity_commands
}
/// Insert tween components directly to this entity.
/// Can be used to create a simple animation quickly.
/// [`TimeRunner`]'s length is determined by provided `duration` unless use
/// [`Self::length`]
pub fn insert_tween_here<I, T>(
self,
duration: Duration,
interpolation: I,
tweens: T,
) -> EntityCommands<'a>
where
I: Bundle,
T: Bundle,
{
let AnimationBuilder {
mut entity_commands,
time_runner,
time_context_marker,
custom_length,
skipped,
} = self;
let mut time_runner = time_runner.unwrap_or_default();
match custom_length {
Some(length) => {
time_runner.set_length(length);
}
None => {
time_runner.set_length(duration);
}
}
entity_commands.insert((
TimeSpan::try_from(Duration::ZERO..duration).unwrap(),
interpolation,
tweens,
time_runner,
time_context_marker.unwrap_or_default(),
));
if skipped {
entity_commands.insert(SkipTimeRunner);
}
entity_commands
}
/// Insert tween components directly to this entity.
/// Can be used to create a simple animation quickly.
/// [`TimeRunner`]'s length is determined by provided `duration` unless use
/// [`Self::length`]
///
/// # Note
///
/// If the entity does not exist when this command is executed,
/// the resulting error will be ignored.
pub fn try_insert_tween_here<I, T>(
self,
duration: Duration,
interpolation: I,
tweens: T,
) -> EntityCommands<'a>
where
I: Bundle,
T: Bundle,
{
let AnimationBuilder {
mut entity_commands,
time_runner,
time_context_marker,
custom_length,
skipped,
} = self;
let mut time_runner = time_runner.unwrap_or_default();
match custom_length {
Some(length) => {
time_runner.set_length(length);
}
None => {
time_runner.set_length(duration);
}
}
entity_commands.try_insert((
TimeSpan::try_from(Duration::ZERO..duration).unwrap(),
interpolation,
tweens,
time_runner,
time_context_marker.unwrap_or_default(),
));
if skipped {
entity_commands.try_insert(SkipTimeRunner);
}
entity_commands
}
}