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::*;
pub type BoxedInterpolator<Item> = Box<dyn Interpolator<Item = Item>>;
pub type CurrentValue = f32;
pub type PreviousValue = f32;
type InterpolatorClosure<I> =
Box<dyn Fn(&mut I, CurrentValue, PreviousValue) + Send + Sync + 'static>;
pub fn closure<I, F>(f: F) -> InterpolatorClosure<I>
where
I: 'static,
F: Fn(&mut I, CurrentValue, PreviousValue) + Send + Sync + 'static,
{
Box::new(f)
}
pub trait Interpolator: Send + Sync + 'static {
type Item;
fn interpolate(
&self,
item: &mut Self::Item,
value: CurrentValue,
previous_value: PreviousValue,
);
}
pub struct DefaultInterpolatorsPlugin<TimeCtx = ()>
where
TimeCtx: Default + Send + Sync + 'static,
{
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,
{
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(),
}
}
}
pub struct DefaultDynInterpolatorsPlugin<TimeCtx = ()>
where
TimeCtx: Default + Send + Sync + 'static,
{
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,
{
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(),
}
}
}