Skip to content

Commit

Permalink
Merge pull request #4 from Multirious/unified-tween-system
Browse files Browse the repository at this point in the history
Unified tween system

New `component_tween_system` impl,
deprecate `component_dyn_tween_system` and `component_tween_system_full`

New `resource_tween_system` impl,
deprecate `resource_dyn_tween_system` and `resource_tween_system_full`

New `asset_tween_system` impl,
deprecate `asset_dyn_tween_system` and `asset_tween_system_full`
  • Loading branch information
Multirious authored Mar 25, 2024
2 parents 44403e0 + d1ea2fa commit dcf1331
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 136 deletions.
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ required-features = [
"bevy/bevy_winit",
]

[[example]]
name = "hold"
path = "examples/demo/hold.rs"
required-features = [
"bevy_sprite",
"bevy_asset",
"span_tween",
"bevy/bevy_winit",
]

[[example]]
name = "span_tween"
path = "examples/span_tween/span_tween.rs"
Expand Down
28 changes: 5 additions & 23 deletions examples/animation/banner_bounce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,10 @@ use bevy::{
prelude::*,
window,
};
use bevy_tween::{component_tween_system, prelude::*};
use bevy_tween::prelude::*;

const SCALE: f32 = 2.0;

mod my_interpolate {
use bevy::prelude::*;
use bevy_tween::prelude::*;
pub struct Angle {
pub start: f32,
pub end: f32,
}
impl Interpolator for Angle {
type Item = Transform;

fn interpolate(&self, item: &mut Self::Item, value: f32) {
let angle = (self.end - self.start).mul_add(value, self.start);
item.rotation = Quat::from_rotation_z(angle);
}
}
}

fn main() {
App::new()
.add_plugins((
Expand All @@ -48,7 +31,6 @@ fn main() {
DefaultTweenPlugins,
))
.add_systems(Startup, (animation, setup_camera))
.add_tween_systems(component_tween_system::<my_interpolate::Angle>())
.run();
}

Expand Down Expand Up @@ -232,7 +214,7 @@ fn animation(mut commands: Commands, asset_server: Res<AssetServer>) {
EaseFunction::QuinticOut,
ComponentTween::new_target(
bevy_tween_text,
my_interpolate::Angle {
interpolate::AngleZ {
start: PI,
end: PI * 4.,
},
Expand Down Expand Up @@ -287,7 +269,7 @@ fn animation(mut commands: Commands, asset_server: Res<AssetServer>) {
EaseFunction::QuinticIn,
ComponentTween::new_target(
bevy_tween_text,
my_interpolate::Angle {
interpolate::AngleZ {
start: PI * 4.,
end: PI * 7.,
},
Expand Down Expand Up @@ -336,7 +318,7 @@ fn animation(mut commands: Commands, asset_server: Res<AssetServer>) {
EaseFunction::ExponentialOut,
ComponentTween::new_target(
square,
my_interpolate::Angle {
interpolate::AngleZ {
start: 0.,
end: PI * 10.,
},
Expand All @@ -347,7 +329,7 @@ fn animation(mut commands: Commands, asset_server: Res<AssetServer>) {
EaseFunction::ExponentialOut,
ComponentTween::new_target(
triangle,
my_interpolate::Angle {
interpolate::AngleZ {
start: 0.,
end: -PI * 10.,
},
Expand Down
22 changes: 4 additions & 18 deletions examples/demo/hold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,16 @@ mod my_interpolate {
item.0 = self.start.lerp(self.end, value)
}
}
pub struct Angle {
pub start: f32,
pub end: f32,
}

impl Interpolator for Angle {
type Item = Transform;

fn interpolate(&self, item: &mut Self::Item, value: f32) {
let angle = (self.end - self.start).mul_add(value, self.start);
item.rotation = Quat::from_rotation_z(angle);
}
}
}

fn main() {
App::new()
.add_plugins((DefaultPlugins, DefaultTweenPlugins))
.add_systems(Startup, setup)
.add_systems(Update, (big_x_do_effect, mouse_hold))
.add_tween_systems((
resource_tween_system::<my_interpolate::EffectIntensity>(),
component_tween_system::<my_interpolate::Angle>(),
))
.add_tween_systems(
resource_tween_system::<my_interpolate::EffectIntensity>,
)
.init_resource::<EffectIntensitiy>()
.run();
}
Expand Down Expand Up @@ -102,7 +88,7 @@ fn setup(
EaseFunction::Linear,
ComponentTween::new_target(
big_x,
my_interpolate::Angle {
interpolate::AngleZ {
start: 0.,
end: PI * 0.5,
},
Expand Down
102 changes: 55 additions & 47 deletions examples/span_tween/span_tween.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use std::time::Duration;

use bevy::prelude::*;
use bevy_tween::prelude::*;

// Prefer the shortcuts
use bevy_tween::tween::{TargetComponent, Tween};
use bevy_tween::tween::TargetComponent;

fn main() {
App::new()
Expand Down Expand Up @@ -37,14 +36,14 @@ fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());

let angle_start = 0.;
let angle_end = std::f32::consts::PI*2.;
let angle_end = std::f32::consts::PI * 2.;

let start_x = -300.;
let end_x = 300.;

let spacing_y = 100.;
let offset_y = -(spacing_y * 3.) / 2.;

// Case 1:
let y = 0. * spacing_y + offset_y;
commands.spawn((
Expand All @@ -53,15 +52,18 @@ fn setup(mut commands: Commands) {
SpanTweenBundle::new(..Duration::from_secs(5)),
EaseFunction::QuadraticInOut,
ComponentTween::new_target(
bevy_tween::tween::TargetComponent::tweener_entity(),
TargetComponent::tweener_entity(),
interpolate::Translation {
start: Vec3::new(start_x, y, 0.),
end: Vec3::new(end_x, y, 0.)
}
end: Vec3::new(end_x, y, 0.),
},
),
ComponentTween::new_target(
bevy_tween::tween::TargetComponent::tweener_entity(),
interpolate::AngleZ { start: angle_start, end: angle_end }
TargetComponent::tweener_entity(),
interpolate::AngleZ {
start: angle_start,
end: angle_end,
},
),
// ----- or -----
// ComponentTween::tweener_entity( ... ),
Expand All @@ -81,15 +83,18 @@ fn setup(mut commands: Commands) {
SpanTweenBundle::new(..Duration::from_secs(5)),
EaseFunction::QuadraticInOut,
ComponentTween::new_target(
bevy_tween::tween::TargetComponent::tweener_entity(),
TargetComponent::tweener_entity(),
interpolate::Translation {
start: Vec3::new(start_x, y, 0.),
end: Vec3::new(end_x, y, 0.)
}
end: Vec3::new(end_x, y, 0.),
},
),
ComponentTween::new_target(
bevy_tween::tween::TargetComponent::tweener_entity(),
interpolate::AngleZ { start: angle_start, end: angle_end }
TargetComponent::tweener_entity(),
interpolate::AngleZ {
start: angle_start,
end: angle_end,
},
),
// ----- or -----
// ComponentTween::tweener_entity( ... ),
Expand All @@ -100,52 +105,55 @@ fn setup(mut commands: Commands) {

// Case 3:
let y = 2. * spacing_y + offset_y;
commands
.spawn(sprite(start_x, y))
.with_children(|c| {
c.spawn((
SpanTweenerBundle::new(Duration::from_secs(5)),
SpanTweenBundle::new(..Duration::from_secs(5)),
EaseFunction::QuadraticInOut,
ComponentTween::new_target(
bevy_tween::tween::TargetComponent::tweener_parent(),
interpolate::Translation {
start: Vec3::new(start_x, y, 0.),
end: Vec3::new(end_x, y, 0.)
}
),
ComponentTween::new_target(
bevy_tween::tween::TargetComponent::tweener_parent(),
interpolate::AngleZ { start: angle_start, end: angle_end }
),
// ----- or -----
// ComponentTween::tweener_parent( ... ),
));
});
commands.spawn(sprite(start_x, y)).with_children(|c| {
c.spawn((
SpanTweenerBundle::new(Duration::from_secs(5)),
SpanTweenBundle::new(..Duration::from_secs(5)),
EaseFunction::QuadraticInOut,
ComponentTween::new_target(
TargetComponent::tweener_parent(),
interpolate::Translation {
start: Vec3::new(start_x, y, 0.),
end: Vec3::new(end_x, y, 0.),
},
),
ComponentTween::new_target(
TargetComponent::tweener_parent(),
interpolate::AngleZ {
start: angle_start,
end: angle_end,
},
),
// ----- or -----
// ComponentTween::tweener_parent( ... ),
));
});

// Case 4:
let y = 3. * spacing_y + offset_y;
commands
.spawn(sprite(start_x, y))
.with_children(|c| {
c.spawn(SpanTweenerBundle::new(Duration::from_secs(5)))
commands.spawn(sprite(start_x, y)).with_children(|c| {
c.spawn(SpanTweenerBundle::new(Duration::from_secs(5)))
.with_children(|c| {
c.spawn((SpanTweenBundle::new(..Duration::from_secs(5)),
c.spawn((
SpanTweenBundle::new(..Duration::from_secs(5)),
EaseFunction::QuadraticInOut,
ComponentTween::new_target(
bevy_tween::tween::TargetComponent::tweener_parent(),
TargetComponent::tweener_parent(),
interpolate::Translation {
start: Vec3::new(start_x, y, 0.),
end: Vec3::new(end_x, y, 0.)
}
end: Vec3::new(end_x, y, 0.),
},
),
ComponentTween::new_target(
bevy_tween::tween::TargetComponent::tweener_parent(),
interpolate::AngleZ { start: angle_start, end: angle_end }
TargetComponent::tweener_parent(),
interpolate::AngleZ {
start: angle_start,
end: angle_end,
},
),
// ----- or -----
// ComponentTween::tweener_parent( ... ),
));
});
});
});
}
31 changes: 20 additions & 11 deletions src/interpolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ use bevy::prelude::*;
use crate::utils::color_lerp;
use crate::{tween, BevyTweenRegisterSystems};

/// Alias for an `Interpolator` trait object.
pub type BoxedInterpolator<Item> = Box<dyn Interpolator<Item = Item>>;

/// [`Interpolator`] is used to specify how to interpolate an [`Self::Item`] by the
/// implementor.
///
Expand Down Expand Up @@ -148,22 +151,22 @@ pub struct DefaultInterpolatorsPlugin;
impl Plugin for DefaultInterpolatorsPlugin {
fn build(&self, app: &mut App) {
app.add_tween_systems((
tween::component_tween_system::<Translation>(),
tween::component_tween_system::<Rotation>(),
tween::component_tween_system::<Scale>(),
tween::component_tween_system::<AngleZ>(),
tween::component_tween_system::<Translation>,
tween::component_tween_system::<Rotation>,
tween::component_tween_system::<Scale>,
tween::component_tween_system::<AngleZ>,
))
.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.add_tween_systems(tween::component_tween_system::<SpriteColor>())
app.add_tween_systems(tween::component_tween_system::<SpriteColor>)
.register_type::<tween::ComponentTween<SpriteColor>>();

#[cfg(all(feature = "bevy_sprite", feature = "bevy_asset",))]
app.add_tween_systems(tween::asset_tween_system::<ColorMaterial>())
app.add_tween_systems(tween::asset_tween_system::<ColorMaterial>)
.register_type::<tween::AssetTween<ColorMaterial>>();
}
}
Expand All @@ -172,15 +175,21 @@ impl Plugin for DefaultInterpolatorsPlugin {
pub struct DefaultDynInterpolatorsPlugin;
impl Plugin for DefaultDynInterpolatorsPlugin {
fn build(&self, app: &mut App) {
app.add_tween_systems(tween::component_dyn_tween_system::<Transform>());
app.add_tween_systems(
tween::component_tween_system::<BoxedInterpolator<Transform>>,
);

#[cfg(feature = "bevy_sprite")]
app.add_tween_systems(tween::component_dyn_tween_system::<Sprite>());
app.add_tween_systems(
tween::component_tween_system::<BoxedInterpolator<Sprite>>,
);

#[cfg(all(feature = "bevy_sprite", feature = "bevy_asset",))]
app.add_tween_systems(tween::asset_dyn_tween_system::<
bevy::sprite::ColorMaterial,
>());
app.add_tween_systems(
tween::asset_tween_system::<
BoxedInterpolator<bevy::sprite::ColorMaterial>,
>,
);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub mod span_tween;
pub mod prelude {
pub use std::time::Duration;

pub use crate::interpolate::{self, Interpolator};
pub use crate::interpolate::{self, BoxedInterpolator, Interpolator};
pub use crate::interpolation::EaseFunction;
#[cfg(feature = "span_tween")]
#[allow(deprecated)]
Expand All @@ -148,17 +148,23 @@ pub mod prelude {
pub use crate::DefaultTweenPlugins;
}

#[allow(deprecated)]
#[cfg(feature = "bevy_asset")]
pub use tween::asset_dyn_tween_system;
#[cfg(feature = "bevy_asset")]
pub use tween::asset_tween_system;
#[cfg(feature = "bevy_asset")]
#[allow(deprecated)]
pub use tween::asset_tween_system_full;
#[allow(deprecated)]
pub use tween::component_dyn_tween_system;
pub use tween::component_tween_system;
#[allow(deprecated)]
pub use tween::component_tween_system_full;
#[allow(deprecated)]
pub use tween::resource_dyn_tween_system;
pub use tween::resource_tween_system;
#[allow(deprecated)]
pub use tween::resource_tween_system_full;

/// Default plugins for using crate.
Expand Down
Loading

0 comments on commit dcf1331

Please sign in to comment.