-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinterpolate.rs
More file actions
401 lines (363 loc) · 12.8 KB
/
Copy pathinterpolate.rs
File metadata and controls
401 lines (363 loc) · 12.8 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//! Module containing some basic built-in interpolator
//!
//! **Plugins**:
//! - [`DefaultDynInterpolatorsPlugin`]
//! - [`DefaultInterpolatorsPlugin`]
//!
//! **Built-in interpolators**:
//! - [`Translation`]
//! - [`Rotation`]
//! - [`Scale`]
//! - [`AngleZ`]
//! - [`SpriteColor`]
//! - [`ColorMaterial`]
//!
//! # Your own [`Interpolator`]
//!
//! There are a few amount of built-in interpolator because this crate only
//! implemented the most common ones such as [`Translation`] or
//! [`SpriteColor`] and some more.
//! For others, you must implement your own!
//!
//! Let's say you've created some custom component and you want to interpolate it:
//! ```no_run
//! use bevy::prelude::*;
//!
//! #[derive(Component)]
//! struct Foo(f32);
//! ```
//!
//! You'll need to create a specific interpolator for this component by:
//! ```no_run
//! # use bevy::prelude::*;
//! # #[derive(Component)]
//! # struct Foo(f32);
//! use bevy_tween::prelude::*;
//!
//! // First we define an interpolator type for `Foo`.
//! struct InterpolateFoo {
//! start: f32,
//! end: f32,
//! }
//!
//! impl Interpolator for InterpolateFoo {
//! // We define the asscioate type `Item` as the `Foo` component
//! type Item = Foo;
//!
//! // Then we define how we want to interpolate `Foo`
//! fn interpolate(&self, item: &mut Self::Item, value: f32, _previous_value: f32) {
//! // Usually if the type already have the `.lerp` function provided
//! // by the `FloatExt` trait then we can use just that
//! item.0 = self.start.lerp(self.end, value);
//! }
//! }
//! ```
//!
//! If you've created a custom interpolator or a custom component/asset/resource,
//! you may want to [register some systems](crate::tween#registering-systems).
//!
//! While it's recommended to use the [`Interpolator`] trait, it's not required
//! to make your interpolators work in this crate. the [`Interpolator`] as of
//! currently is only used for registering built-in simple interpolator systems
//! such as [`component_tween_system`], [`resource_tween_system`], and
//! [`asset_tween_system`]. Its next use is being object-safe for dynamic interpolator.
//!
//! If you need interpolators with more specific or complex system param, you
//! have to define your own system!
//!
//! [`component_tween_system`]: crate::tween::component_tween_system
//! [`resource_tween_system`]: crate::tween::resource_tween_system
//! [`asset_tween_system`]: crate::tween::asset_tween_system
mod blanket_impl;
#[cfg(feature = "bevy_sprite")]
mod sprite;
mod transform;
#[cfg(feature = "bevy_ui")]
mod ui;
use std::marker::PhantomData;
pub use transform::*;
#[cfg(feature = "bevy_sprite")]
pub use sprite::*;
#[cfg(feature = "bevy_ui")]
pub use ui::*;
use crate::{BevyTweenRegisterSystems, tween};
use bevy::ecs::schedule::{InternedScheduleLabel, ScheduleLabel};
use bevy::prelude::*;
/// Alias for an `Interpolator` as a boxed trait object.
pub type BoxedInterpolator<Item> = Box<dyn Interpolator<Item = Item>>;
/// A marker type for the tweens current value, for ease of closure readability
pub type CurrentValue = f32;
/// A marker type for the tweens previous value, for ease of closure readability
pub type PreviousValue = f32;
type InterpolatorClosure<I> =
Box<dyn Fn(&mut I, CurrentValue, PreviousValue) + Send + Sync + 'static>;
/// Create boxed closure in order to be used with dynamic [`Interpolator`]
pub fn closure<I, F>(f: F) -> InterpolatorClosure<I>
where
I: 'static,
F: Fn(&mut I, CurrentValue, PreviousValue) + Send + Sync + 'static,
{
Box::new(f)
}
/// [`Interpolator`] is used to specify how to interpolate an [`Self::Item`] by the
/// implementor.
///
/// Currently only used for registering systems
/// and being object-safe for dynamic interpolator.
///
/// See [module-level documentation](self) for more info.
pub trait Interpolator: Send + Sync + 'static {
/// Type to be interpolated.
type Item;
/// Interpolate an item using `value` which is typically between 0–1.
/// The value should be already sampled from an [`Interpolation`]
///
/// [`Interpolation`]: crate::interpolation::Interpolation
fn interpolate(
&self,
item: &mut Self::Item,
value: CurrentValue,
previous_value: PreviousValue,
);
}
// /// Reflect [`Interpolator`] trait
// #[allow(clippy::type_complexity)]
// pub struct ReflectInterpolator<Item> {
// get_func: fn(&dyn Reflect) -> Option<&dyn Interpolator<Item = Item>>,
// get_mut_func:
// fn(&mut dyn Reflect) -> Option<&mut dyn Interpolator<Item = Item>>,
// get_boxed_func:
// fn(
// Box<dyn Reflect>,
// )
// -> Result<Box<dyn Interpolator<Item = Item>>, Box<dyn Reflect>>,
// }
// impl<Item> Clone for ReflectInterpolator<Item> {
// #[inline]
// fn clone(&self) -> ReflectInterpolator<Item> {
// ReflectInterpolator {
// get_func: Clone::clone(&self.get_func),
// get_mut_func: Clone::clone(&self.get_mut_func),
// get_boxed_func: Clone::clone(&self.get_boxed_func),
// }
// }
// }
// impl<Item> ReflectInterpolator<Item> {
// /** Downcast a `&dyn Reflect` type to `&dyn Interpolator`.
// If the type cannot be downcast, `None` is returned.*/
// pub fn get<'a>(
// &self,
// reflect_value: &'a dyn Reflect,
// ) -> Option<&'a dyn Interpolator<Item = Item>> {
// (self.get_func)(reflect_value)
// }
// // /** Downcast a `&mut dyn Reflect` type to `&mut dyn Interpolator`.
// // If the type cannot be downcast, `None` is returned.*/
// // pub fn get_mut<'a>(
// // &self,
// // reflect_value: &'a mut dyn Reflect,
// // ) -> Option<&'a mut dyn Interpolator<Item = Item>> {
// // (self.get_mut_func)(reflect_value)
// // }
// /** Downcast a `Box<dyn Reflect>` type to `Box<dyn Interpolator>`.
// If the type cannot be downcast, this will return `Err(Box<dyn Reflect>)`.*/
// pub fn get_boxed(
// &self,
// reflect_value: Box<dyn Reflect>,
// ) -> Result<Box<dyn Interpolator<Item = Item>>, Box<dyn Reflect>> {
// (self.get_boxed_func)(reflect_value)
// }
// }
// impl<Item, T> bevy::reflect::FromType<T> for ReflectInterpolator<Item>
// where
// T: Interpolator<Item = Item> + Reflect,
// {
// fn from_type() -> Self {
// Self {
// get_func: |reflect_value| {
// <dyn Reflect>::downcast_ref::<T>(reflect_value)
// .map(|value| value as &dyn Interpolator<Item = Item>)
// },
// get_mut_func: |reflect_value| {
// <dyn Reflect>::downcast_mut::<T>(reflect_value)
// .map(|value| value as &mut dyn Interpolator<Item = Item>)
// },
// get_boxed_func: |reflect_value| {
// <dyn Reflect>::downcast::<T>(reflect_value)
// .map(|value| value as Box<dyn Interpolator<Item = Item>>)
// },
// }
// }
// }
/// Default interpolators
///
/// Register type and systems for the following interpolators and their delta interpolators:
/// - [`Translation`]
/// - [`Rotation`]
/// - [`Scale`]
/// - [`AngleZ`]
/// - [`SpriteColor`] and [`ColorMaterial`] if `"bevy_sprite"` feature is enabled.
/// - [`BackgroundColor`] and [`BorderColor`] if `"bevy_ui"` feature is enabled.
pub struct DefaultInterpolatorsPlugin<TimeCtx = ()>
where
TimeCtx: Default + Send + Sync + 'static,
{
/// Register all systems from this plugin to the specified schedule.
pub schedule: InternedScheduleLabel,
marker: PhantomData<TimeCtx>,
}
impl<TimeCtx> Plugin for DefaultInterpolatorsPlugin<TimeCtx>
where
TimeCtx: Default + Send + Sync + 'static,
{
fn build(&self, app: &mut App) {
app.register_type::<tween::ComponentTween<Translation>>()
.register_type::<tween::ComponentTween<Rotation>>()
.register_type::<tween::ComponentTween<Scale>>()
.register_type::<tween::ComponentTween<AngleZ>>();
#[cfg(feature = "bevy_sprite")]
app.register_type::<tween::ComponentTween<SpriteColor>>();
#[cfg(feature = "bevy_ui")]
app.register_type::<tween::ComponentTween<ui::BackgroundColor>>()
.register_type::<tween::ComponentTween<ui::BorderColor>>();
#[cfg(all(feature = "bevy_sprite", feature = "bevy_asset",))]
app.register_type::<tween::AssetTween<sprite::ColorMaterial>>();
app.add_tween_systems(
self.schedule,
(
tween::component_tween_system_with_time_context::<Translation, TimeCtx>(),
tween::component_tween_system_with_time_context::<Rotation, TimeCtx>(),
tween::component_tween_system_with_time_context::<Scale, TimeCtx>(),
tween::component_tween_system_with_time_context::<AngleZ, TimeCtx>(),
),
);
#[cfg(feature = "bevy_sprite")]
app.add_tween_systems(
self.schedule,
tween::component_tween_system_with_time_context::<
SpriteColor,
TimeCtx,
>(),
);
#[cfg(feature = "bevy_ui")]
app.add_tween_systems(
self.schedule,
(
tween::component_tween_system_with_time_context::<
ui::BackgroundColor,
TimeCtx,
>(),
tween::component_tween_system_with_time_context::<
ui::BorderColor,
TimeCtx,
>(),
),
);
#[cfg(all(feature = "bevy_sprite", feature = "bevy_asset",))]
app.add_tween_systems(
self.schedule,
tween::asset_tween_system::<sprite::ColorMaterial, TimeCtx>(),
);
}
}
impl<TimeCtx> DefaultInterpolatorsPlugin<TimeCtx>
where
TimeCtx: Default + Send + Sync + 'static,
{
/// Register all systems from this plugin to the specified schedule.
pub fn in_schedule(schedule: impl ScheduleLabel) -> Self {
Self {
schedule: schedule.intern(),
marker: PhantomData,
}
}
}
impl Default for DefaultInterpolatorsPlugin<()> {
fn default() -> Self {
Self {
schedule: PostUpdate.intern(),
marker: Default::default(),
}
}
}
/// Default dynamic interpolators
///
/// Register systems for the following:
/// - [`Transform`] component.
/// - [`Sprite`] component if `"bevy_sprite"` feature is enabled.
/// - [`ColorMaterial`] asset if `"bevy_sprite"` feature is enabled.
/// - [`BackgroundColor`] and [`BorderColor`] components if `"bevy_ui"` feature is enabled.
///
/// [`ColorMaterial`]: bevy::sprite_render::ColorMaterial
pub struct DefaultDynInterpolatorsPlugin<TimeCtx = ()>
where
TimeCtx: Default + Send + Sync + 'static,
{
/// Register all systems from this plugin to the specified schedule.
pub schedule: InternedScheduleLabel,
marker: PhantomData<TimeCtx>,
}
impl<TimeCtx> Plugin for DefaultDynInterpolatorsPlugin<TimeCtx>
where
TimeCtx: Default + Send + Sync + 'static,
{
fn build(&self, app: &mut App) {
app.add_tween_systems(
self.schedule,
tween::component_tween_system_with_time_context::<
BoxedInterpolator<Transform>,
TimeCtx,
>(),
);
#[cfg(feature = "bevy_sprite")]
app.add_tween_systems(
self.schedule,
tween::component_tween_system_with_time_context::<
BoxedInterpolator<Sprite>,
TimeCtx,
>(),
);
#[cfg(feature = "bevy_ui")]
app.add_tween_systems(
self.schedule,
(
tween::component_tween_system_with_time_context::<
BoxedInterpolator<bevy::prelude::BackgroundColor>,
TimeCtx,
>(),
tween::component_tween_system_with_time_context::<
BoxedInterpolator<bevy::prelude::BorderColor>,
TimeCtx,
>(),
),
);
#[cfg(all(feature = "bevy_sprite", feature = "bevy_asset"))]
app.add_tween_systems(
self.schedule,
tween::asset_tween_system::<
BoxedInterpolator<bevy::sprite_render::ColorMaterial>,
TimeCtx,
>(),
);
}
}
impl<TimeCtx> DefaultDynInterpolatorsPlugin<TimeCtx>
where
TimeCtx: Default + Send + Sync + 'static,
{
/// Register all systems from this plugin to the specified schedule.
pub fn in_schedule(schedule: impl ScheduleLabel) -> Self {
Self {
schedule: schedule.intern(),
marker: PhantomData,
}
}
}
impl Default for DefaultDynInterpolatorsPlugin<()> {
fn default() -> Self {
Self {
schedule: PostUpdate.intern(),
marker: Default::default(),
}
}
}