Skip to content

Commit

Permalink
Bevy 0.15 support (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
rendaoer authored Dec 7, 2024
1 parent f08891c commit 0125aa3
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 408 deletions.
26 changes: 14 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "bevy_tweening"
version = "0.11.0"
authors = ["François Mockers <[email protected]>", "Jerome Humbert <[email protected]>"]
authors = [
"François Mockers <[email protected]>",
"Jerome Humbert <[email protected]>",
]
edition = "2021"
description = "Tweening animation plugin for the Bevy game engine"
repository = "https://github.com/djeedai/bevy_tweening"
Expand All @@ -24,43 +27,42 @@ bevy_ui = ["bevy/bevy_ui", "bevy/bevy_render"]
bevy_text = ["bevy/bevy_text", "bevy/bevy_render", "bevy/bevy_sprite"]

[dependencies]
interpolation = "0.3"
bevy = { version = "0.14", default-features = false }
bevy = { version = "0.15", default-features = false, features = ["x11"] }

[dev-dependencies]
bevy-inspector-egui = "0.25"
bevy-inspector-egui = "0.28"

[[example]]
name = "menu"
required-features = [ "bevy_ui", "bevy_text", "bevy/bevy_winit" ]
required-features = ["bevy_ui", "bevy_text", "bevy/bevy_winit"]

[[example]]
name = "colormaterial_color"
required-features = [ "bevy_asset", "bevy_sprite", "bevy/bevy_winit" ]
required-features = ["bevy_asset", "bevy_sprite", "bevy/bevy_winit"]

[[example]]
name = "sprite_color"
required-features = [ "bevy_sprite", "bevy/bevy_winit" ]
required-features = ["bevy_sprite", "bevy/bevy_winit"]

[[example]]
name = "transform_translation"
required-features = [ "bevy_sprite", "bevy/bevy_winit" ]
required-features = ["bevy_sprite", "bevy/bevy_winit"]

[[example]]
name = "transform_rotation"
required-features = [ "bevy_sprite", "bevy/bevy_winit" ]
required-features = ["bevy_sprite", "bevy/bevy_winit"]

[[example]]
name = "ui_position"
required-features = [ "bevy_sprite", "bevy_ui", "bevy/bevy_winit" ]
required-features = ["bevy_sprite", "bevy_ui", "bevy/bevy_winit"]

[[example]]
name = "text_color"
required-features = [ "bevy_ui", "bevy_text", "bevy/bevy_winit" ]
required-features = ["bevy_ui", "bevy_text", "bevy/bevy_winit"]

[[example]]
name = "sequence"
required-features = [ "bevy_sprite", "bevy_text", "bevy/bevy_winit" ]
required-features = ["bevy_sprite", "bevy_text", "bevy/bevy_winit"]

[workspace]
resolver = "2"
Expand Down
75 changes: 54 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ By default, 🍃 Bevy Tweening adopts a minimalist approach, and the `TweeningPl
| `Transform` | Yes |
| `Sprite` | Only if `bevy_sprite` feature |
| `ColorMaterial` | Only if `bevy_sprite` feature |
| `Style` | Only if `bevy_ui` feature |
| `Text` | Only if `bevy_text` feature |
| `Node` | Only if `bevy_ui` feature |
| `TextColor` | Only if `bevy_text` feature |
| All other components | No |

To add a system for a component `C`, use:
Expand Down Expand Up @@ -103,12 +103,9 @@ let tween = Tween::new(

commands.spawn((
// Spawn a Sprite entity to animate the position of.
SpriteBundle {
sprite: Sprite {
color: Color::RED,
custom_size: Some(Vec2::new(size, size)),
..default()
},
Sprite {
color: Color::RED,
custom_size: Some(Vec2::new(size, size)),
..default()
},
// Add an Animator component to control and execute the animation.
Expand Down Expand Up @@ -144,17 +141,17 @@ The naming scheme for predefined lenses is `"<TargetName><FieldName>Lens"`, wher

| Target Component | Animated Field | Lens | Feature |
|---|---|---|---|
| [`Transform`](https://docs.rs/bevy/0.14.0/bevy/transform/components/struct.Transform.html) | [`translation`](https://docs.rs/bevy/0.14.0/bevy/transform/components/struct.Transform.html#structfield.translation) | [`TransformPositionLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformPositionLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.14.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (`Quat`| [`TransformRotationLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotationLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.14.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (angle)² | [`TransformRotateXLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotateXLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.14.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (angle)² | [`TransformRotateYLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotateYLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.14.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (angle)² | [`TransformRotateZLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotateZLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.14.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (angle)² | [`TransformRotateAxisLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotateAxisLens.html) | |
| | [`scale`](https://docs.rs/bevy/0.14.0/bevy/transform/components/struct.Transform.html#structfield.scale) | [`TransformScaleLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformScaleLens.html) | |
| [`Sprite`](https://docs.rs/bevy/0.14.0/bevy/sprite/struct.Sprite.html) | [`color`](https://docs.rs/bevy/0.14.0/bevy/sprite/struct.Sprite.html#structfield.color) | [`SpriteColorLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.SpriteColorLens.html) | `bevy_sprite` |
| [`Style`](https://docs.rs/bevy/0.14.0/bevy/ui/struct.Style.html) | [`position`](https://docs.rs/bevy/0.14.0/bevy/ui/struct.Style.html#structfield.position) | [`UiPositionLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.UiPositionLens.html) | `bevy_ui` |
| [`BackgroundColor`](https://docs.rs/bevy/0.14.0/bevy/ui/struct.BackgroundColor.html)| | [`UiBackgroundColorLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.UiBackgroundColorLens.html) | `bevy_ui` |
| [`Text`](https://docs.rs/bevy/0.14.0/bevy/text/struct.Text.html) | [`TextStyle::color`](https://docs.rs/bevy/0.14.0/bevy/text/struct.TextStyle.html#structfield.color) | [`TextColorLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TextColorLens.html) | `bevy_text` |
| [`Transform`](https://docs.rs/bevy/0.15.0/bevy/transform/components/struct.Transform.html) | [`translation`](https://docs.rs/bevy/0.15.0/bevy/transform/components/struct.Transform.html#structfield.translation) | [`TransformPositionLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformPositionLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.15.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (`Quat`| [`TransformRotationLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotationLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.15.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (angle)² | [`TransformRotateXLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotateXLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.15.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (angle)² | [`TransformRotateYLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotateYLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.15.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (angle)² | [`TransformRotateZLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotateZLens.html) | |
| | [`rotation`](https://docs.rs/bevy/0.15.0/bevy/transform/components/struct.Transform.html#structfield.rotation) (angle)² | [`TransformRotateAxisLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformRotateAxisLens.html) | |
| | [`scale`](https://docs.rs/bevy/0.15.0/bevy/transform/components/struct.Transform.html#structfield.scale) | [`TransformScaleLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TransformScaleLens.html) | |
| [`Sprite`](https://docs.rs/bevy/0.15.0/bevy/sprite/struct.Sprite.html) | [`color`](https://docs.rs/bevy/0.15.0/bevy/sprite/struct.Sprite.html#structfield.color) | [`SpriteColorLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.SpriteColorLens.html) | `bevy_sprite` |
| [`Node`](https://docs.rs/bevy/0.15.0/bevy/ui/struct.Node.html) | [`position`](https://docs.rs/bevy/0.15.0/bevy/ui/struct.Node.html) | [`UiPositionLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.UiPositionLens.html) | `bevy_ui` |
| [`BackgroundColor`](https://docs.rs/bevy/0.15.0/bevy/ui/struct.BackgroundColor.html)| | [`UiBackgroundColorLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.UiBackgroundColorLens.html) | `bevy_ui` |
| [`Text`](https://docs.rs/bevy/latest/bevy/text/index.html) | [`TextColor`](https://docs.rs/bevy/0.15.0/bevy/text/struct.TextColor.html) | [`TextColorLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.TextColorLens.html) | `bevy_text` |

¹ Shortest-path interpolation between two rotations, using `Quat::slerp()`.

Expand All @@ -168,7 +165,7 @@ Asset animation always requires the `bevy_asset` feature.

| Target Asset | Animated Field | Lens | Feature |
|---|---|---|---|
| [`ColorMaterial`](https://docs.rs/bevy/0.14.0/bevy/sprite/struct.ColorMaterial.html) | [`color`](https://docs.rs/bevy/0.14.0/bevy/sprite/struct.ColorMaterial.html#structfield.color) | [`ColorMaterialColorLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.ColorMaterialColorLens.html) | `bevy_asset` + `bevy_sprite` |
| [`ColorMaterial`](https://docs.rs/bevy/0.15.0/bevy/sprite/struct.ColorMaterial.html) | [`color`](https://docs.rs/bevy/0.14.0/bevy/sprite/struct.ColorMaterial.html#structfield.color) | [`ColorMaterialColorLens`](https://docs.rs/bevy_tweening/latest/bevy_tweening/lens/struct.ColorMaterialColorLens.html) | `bevy_asset` + `bevy_sprite` |

## Custom lens

Expand Down Expand Up @@ -286,38 +283,74 @@ cargo run --example sequence --features="bevy/bevy_winit"

## Ease Functions

Many [ease functions](https://docs.rs/interpolation/0.2.0/interpolation/enum.EaseFunction.html) are available:
Many [ease functions](https://docs.rs/bevy/latest/bevy/math/curve/enum.EaseFunction.html) are available:

- Linear
> `f(t) = t`
- QuadraticIn
> `f(t) = t²`
- QuadraticOut
> `f(t) = -(t * (t - 2.0))`
- QuadraticInOut
> Behaves as `EaseFunction::QuadraticIn` for t < 0.5 and as `EaseFunction::QuadraticOut` for t >= 0.5
- CubicIn
> `f(t) = t³`
- CubicOut
> `f(t) = (t - 1.0)³ + 1.0`
- CubicInOut
> Behaves as `EaseFunction::CubicIn` for t < 0.5 and as `EaseFunction::CubicOut` for t >= 0.5
- QuarticIn
> `f(t) = t⁴`
- QuarticOut
> `f(t) = (t - 1.0)³ * (1.0 - t) + 1.0`
- QuarticInOut
> Behaves as `EaseFunction::QuarticIn` for t < 0.5 and as `EaseFunction::QuarticOut` for t >= 0.5
- QuinticIn
> `f(t) = t⁵`
- QuinticOut
> `f(t) = (t - 1.0)⁵ + 1.0`
- QuinticInOut
> Behaves as `EaseFunction::QuinticIn` for t < 0.5 and as `EaseFunction::QuinticOut` for t >= 0.5
- SineIn
> `f(t) = 1.0 - cos(t * π / 2.0)`
- SineOut
> `f(t) = sin(t * π / 2.0)`
- SineInOut
> Behaves as `EaseFunction::SineIn` for t < 0.5 and as `EaseFunction::SineOut` for t >= 0.5
- CircularIn
> `f(t) = 1.0 - sqrt(1.0 - t²)`
- CircularOut
> `f(t) = sqrt((2.0 - t) * t)`
- CircularInOut
> Behaves as `EaseFunction::CircularIn` for t < 0.5 and as `EaseFunction::CircularOut` for t >= 0.5
- ExponentialIn
> `f(t) = 2.0^(10.0 * (t - 1.0))`
- ExponentialOut
> `f(t) = 1.0 - 2.0^(-10.0 * t)`
- ExponentialInOut
> Behaves as `EaseFunction::ExponentialIn` for t < 0.5 and as `EaseFunction::ExponentialOut` for t >= 0.5
- ElasticIn
> `f(t) = -2.0^(10.0 * t - 10.0) * sin((t * 10.0 - 10.75) * 2.0 * π / 3.0)`
- ElasticOut
> `f(t) = 2.0^(-10.0 * t) * sin((t * 10.0 - 0.75) * 2.0 * π / 3.0) + 1.0`
- ElasticInOut
> Behaves as `EaseFunction::ElasticIn` for t < 0.5 and as `EaseFunction::ElasticOut` for t >= 0.5
- BackIn
> `f(t) = 2.70158 * t³ - 1.70158 * t²`
- BackOut
> `f(t) = 1.0 + 2.70158 * (t - 1.0)³ - 1.70158 * (t - 1.0)²`
- BackInOut
> Behaves as `EaseFunction::BackIn` for t < 0.5 and as `EaseFunction::BackOut` for t >= 0.5
- BounceIn
> bouncy at the start!
- BounceOut
> bouncy at the end!
- BounceInOut
> Behaves as `EaseFunction::BounceIn` for t < 0.5 and as `EaseFunction::BounceOut` for t >= 0.5
- Steps(usize)
> `n` steps connecting the start and the end
- Elastic(f32)
> `f(omega,t) = 1 - (1 - t)²(2sin(omega * t) / omega + cos(omega * t))`, parametrized by omega
## Compatible Bevy versions

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ criterion = { version = "0.5", features = ["html_reports"] }
bevy_tweening = { path = "../" }

[dependencies.bevy]
version = "0.14"
version = "0.15"
default-features = false
features = ["bevy_render", "bevy_sprite", "bevy_text", "bevy_ui"]

Expand Down
13 changes: 2 additions & 11 deletions benchmarks/benches/lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,12 @@ fn text_color_lens(c: &mut Criterion) {
let mut lens = TextColorLens {
start: RED.into(),
end: BLUE.into(),
section: 0,
};
let mut text = Text::from_section(
"test".to_string(),
TextStyle {
font: Default::default(),
font_size: 60.0,
color: Color::WHITE,
},
)
.with_justify(JustifyText::Center);
let mut text_color = TextColor::default();
let mut added = Tick::new(0);
let mut last_changed = Tick::new(0);
let mut target = ComponentTarget::new(Mut::new(
&mut text,
&mut text_color,
&mut added,
&mut last_changed,
Tick::new(0),
Expand Down
20 changes: 6 additions & 14 deletions examples/colormaterial_color.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use bevy::{
color::palettes::css::*,
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
use bevy::{color::palettes::css::*, prelude::*};
use bevy_tweening::{lens::*, *};
use std::time::Duration;

Expand Down Expand Up @@ -30,7 +26,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d::default());

let size = 80.;

Expand All @@ -40,7 +36,7 @@ fn setup(
let mut x = -screen_x;
let mut y = screen_y;

let quad_mesh: Mesh2dHandle = meshes.add(Rectangle::new(1., 1.)).into();
let quad_mesh = meshes.add(Rectangle::new(1., 1.));

for ease_function in &[
EaseFunction::QuadraticIn,
Expand Down Expand Up @@ -92,13 +88,9 @@ fn setup(
.with_repeat_strategy(RepeatStrategy::MirroredRepeat);

commands.spawn((
MaterialMesh2dBundle {
mesh: quad_mesh.clone(),
transform: Transform::from_translation(Vec3::new(x, y, 0.))
.with_scale(Vec3::splat(size)),
material: unique_material,
..default()
},
Mesh2d(quad_mesh.clone()),
MeshMaterial2d(unique_material),
Transform::from_translation(Vec3::new(x, y, 0.)).with_scale(Vec3::splat(size)),
AssetAnimator::new(tween),
));
y -= size * spacing;
Expand Down
84 changes: 38 additions & 46 deletions examples/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,28 @@ fn main() {
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d::default());

let font = asset_server.load("fonts/FiraMono-Regular.ttf");

commands
.spawn((
NodeBundle {
style: Style {
position_type: PositionType::Absolute,
left: Val::Px(0.),
right: Val::Px(0.),
top: Val::Px(0.),
bottom: Val::Px(0.),
margin: UiRect::all(Val::Px(16.)),
padding: UiRect::all(Val::Px(16.)),
flex_direction: FlexDirection::Column,
align_content: AlignContent::Center,
align_items: AlignItems::Center,
align_self: AlignSelf::Center,
justify_content: JustifyContent::Center,
..default()
},
background_color: BackgroundColor(Color::NONE),
Name::new("menu"),
Node {
position_type: PositionType::Absolute,
left: Val::Px(0.),
right: Val::Px(0.),
top: Val::Px(0.),
bottom: Val::Px(0.),
margin: UiRect::all(Val::Px(16.)),
padding: UiRect::all(Val::Px(16.)),
flex_direction: FlexDirection::Column,
align_content: AlignContent::Center,
align_items: AlignItems::Center,
align_self: AlignSelf::Center,
justify_content: JustifyContent::Center,
..default()
},
Name::new("menu"),
))
.with_children(|container| {
let mut start_time_ms = 0;
Expand Down Expand Up @@ -98,39 +94,35 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
start_time_ms += 500;
container
.spawn((
ButtonBundle {
style: Style {
min_width: Val::Px(300.),
min_height: Val::Px(80.),
margin: UiRect::all(Val::Px(8.)),
padding: UiRect::all(Val::Px(8.)),
align_content: AlignContent::Center,
align_items: AlignItems::Center,
align_self: AlignSelf::Center,
justify_content: JustifyContent::Center,
..default()
},
background_color: BackgroundColor(NORMAL_COLOR),
transform: Transform::from_scale(Vec3::splat(0.01)),
Name::new(format!("button:{}", text)),
Button,
Node {
min_width: Val::Px(300.),
min_height: Val::Px(80.),
margin: UiRect::all(Val::Px(8.)),
padding: UiRect::all(Val::Px(8.)),
align_content: AlignContent::Center,
align_items: AlignItems::Center,
align_self: AlignSelf::Center,
justify_content: JustifyContent::Center,
..default()
},
Name::new(format!("button:{}", text)),
BackgroundColor(NORMAL_COLOR),
Transform::from_scale(Vec3::splat(0.01)),
animator,
label,
))
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text::from_section(
text.to_string(),
TextStyle {
font: font.clone(),
font_size: 48.0,
color: TEXT_COLOR,
},
)
.with_justify(JustifyText::Center),
..default()
});
parent.spawn((
Text::new(text.to_string()),
TextFont {
font: font.clone(),
font_size: 48.0,
..default()
},
TextColor(TEXT_COLOR),
TextLayout::new_with_justify(JustifyText::Center),
));
});
}
});
Expand Down
Loading

0 comments on commit 0125aa3

Please sign in to comment.