From e51753639b9da52c1053e67ab81d89a7cf170566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sat, 8 Jun 2024 10:05:18 +0200 Subject: [PATCH] update for bevy 0.14 --- Cargo.toml | 11 ++- README.md | 5 +- examples/chain.rs | 4 +- examples/controlled.rs | 4 +- examples/custom_component.rs | 1 - examples/pause.rs | 4 +- examples/react_on_end.rs | 4 +- examples/sprite_color.rs | 44 +++++++--- examples/sprite_size.rs | 48 ++++++++--- examples/transform_rotation.rs | 4 +- examples/transform_translation.rs | 10 +-- examples/ui_color.rs | 8 +- examples/ui_style.rs | 6 +- src/implemented.rs | 138 +++++++++++++++++------------- src/lib.rs | 4 - 15 files changed, 178 insertions(+), 117 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ffd9c05..9bc73cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bevy_easings" -version = "0.13.1" +version = "0.14.0" authors = ["François Mockers "] edition = "2021" description = "Easing plugin for Bevy" @@ -17,11 +17,11 @@ categories = ["game-development"] interpolation = "0.3" [dependencies.bevy] -version = "0.13" +version = "0.14.0-rc.2" default-features = false [dev-dependencies.bevy] -version = "0.13" +version = "0.14.0-rc.2" default-features = false features = ["bevy_render", "bevy_sprite", "bevy_ui"] @@ -42,6 +42,11 @@ default = ["render", "sprite", "ui"] "bevy/bevy_asset", "bevy/bevy_core_pipeline", "bevy/default_font", + "bevy/png", + # Should be removed once this is fixed in Bevy 0.14 + "bevy/ktx2", + "bevy/zstd", + "bevy/bevy_pbr", ] [[example]] diff --git a/README.md b/README.md index 6ec4424..bf8d192 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ Easings on Bevy components using [interpolation](https://crates.io/crates/interpolation). +![menu example](https://raw.githubusercontent.com/vleue/bevy_easings/main/examples/menu.webp) + ## Usage ### System setup @@ -229,7 +231,8 @@ Many [ease functions](https://docs.rs/interpolation/0.2.0/interpolation/enum.Eas |Bevy|bevy_easings| |---|---| |main|main| -|0.13|0.13| +|0.14|0.14| +|0.13|0.14| |0.12|0.12| |0.11|0.11| |0.10|0.10| diff --git a/examples/chain.rs b/examples/chain.rs index 5a89e9b..acf91f7 100644 --- a/examples/chain.rs +++ b/examples/chain.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; @@ -20,7 +20,7 @@ fn setup(mut commands: Commands) { SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(100., 100.)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, ..Default::default() diff --git a/examples/controlled.rs b/examples/controlled.rs index 09f9285..21e4d5d 100644 --- a/examples/controlled.rs +++ b/examples/controlled.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; @@ -85,7 +85,7 @@ fn setup(mut commands: Commands) { SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(CUBE_SIZE, CUBE_SIZE)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, ..Default::default() diff --git a/examples/custom_component.rs b/examples/custom_component.rs index d9c657f..371f51a 100644 --- a/examples/custom_component.rs +++ b/examples/custom_component.rs @@ -41,7 +41,6 @@ fn setup(mut commands: Commands) { }, ..Default::default() }, - background_color: BackgroundColor(Color::RED), ..Default::default() }, CustomComponent(0.) diff --git a/examples/pause.rs b/examples/pause.rs index 88ce2a4..abc2a47 100644 --- a/examples/pause.rs +++ b/examples/pause.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; @@ -21,7 +21,7 @@ fn setup(mut commands: Commands) { SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(100., 100.)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, ..Default::default() diff --git a/examples/react_on_end.rs b/examples/react_on_end.rs index 4ac9c92..879f903 100644 --- a/examples/react_on_end.rs +++ b/examples/react_on_end.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; use rand::Rng; @@ -28,7 +28,7 @@ fn setup(mut commands: Commands, windows: Query<&Window>) { SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(100., 100.)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, ..Default::default() diff --git a/examples/sprite_color.rs b/examples/sprite_color.rs index 63a1d43..be1575e 100644 --- a/examples/sprite_color.rs +++ b/examples/sprite_color.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; @@ -12,14 +12,14 @@ fn main() -> Result<(), Box> { Ok(()) } -fn setup(mut commands: Commands) { +fn setup(mut commands: Commands, window: Query<&Window>) { commands.spawn(Camera2dBundle::default()); - let size = 100.; + let size = 140.; - let spacing = 1.25; - let screen_x = 570.; - let screen_y = 150.; + let spacing = 1.15; + let screen_x = window.single().width(); + let screen_y = window.single().height(); let mut x = -screen_x; let mut y = screen_y; @@ -57,31 +57,47 @@ fn setup(mut commands: Commands) { ] { commands.spawn(( SpriteBundle { - transform: Transform::from_translation(Vec3::new(x, y, 0.)), + transform: Transform::from_translation(Vec3::new( + x + size / 2.0, + y - size / 2.0, + 0., + )), ..Default::default() }, Sprite { custom_size: Some(Vec2::new(size, size)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() } .ease_to( Sprite { custom_size: Some(Vec2::new(size, size)), - color: Color::BLUE, + color: palettes::basic::BLUE.into(), ..Default::default() }, *ease_function, bevy_easings::EasingType::PingPong { - duration: std::time::Duration::from_secs(1), + duration: std::time::Duration::from_secs(2), pause: Some(std::time::Duration::from_millis(500)), }, ), )); - y -= size * spacing; - if y < -screen_y { - x += size * spacing; - y = screen_y; + commands.spawn(Text2dBundle { + text: Text::from_section( + format!("{:?}", ease_function), + TextStyle { + font_size: 20.0, + ..default() + }, + ), + transform: Transform::from_translation(Vec3::new(x + size / 2.0, y - size / 2.0, 0.)), + + ..default() + }); + x += size * spacing; + if x + size > screen_x { + x = -screen_x; + y -= size * spacing; } } } diff --git a/examples/sprite_size.rs b/examples/sprite_size.rs index 203f53f..2e74f1c 100644 --- a/examples/sprite_size.rs +++ b/examples/sprite_size.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; @@ -12,15 +12,15 @@ fn main() -> Result<(), Box> { Ok(()) } -fn setup(mut commands: Commands) { +fn setup(mut commands: Commands, window: Query<&Window>) { commands.spawn(Camera2dBundle::default()); let initial_size = 10.; - let final_size = 100.; + let final_size = 140.; - let spacing = 1.25; - let screen_x = 570.; - let screen_y = 150.; + let spacing = 1.15; + let screen_x = window.single().width(); + let screen_y = window.single().height(); let mut x = -screen_x; let mut y = screen_y; @@ -58,31 +58,51 @@ fn setup(mut commands: Commands) { ] { commands.spawn(( SpriteBundle { - transform: Transform::from_translation(Vec3::new(x, y, 0.)), + transform: Transform::from_translation(Vec3::new( + x + final_size / 2.0, + y - final_size / 2.0, + 0., + )), ..Default::default() }, Sprite { custom_size: Some(Vec2::new(initial_size, initial_size)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() } .ease_to( Sprite { custom_size: Some(Vec2::new(final_size, final_size)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, *ease_function, bevy_easings::EasingType::PingPong { - duration: std::time::Duration::from_secs(1), + duration: std::time::Duration::from_secs(2), pause: Some(std::time::Duration::from_millis(500)), }, ), )); - y -= final_size * spacing; - if y < -screen_y { - x += final_size * spacing; - y = screen_y; + commands.spawn(Text2dBundle { + text: Text::from_section( + format!("{:?}", ease_function), + TextStyle { + font_size: 20.0, + ..default() + }, + ), + transform: Transform::from_translation(Vec3::new( + x + final_size / 2.0, + y - final_size / 2.0, + 0., + )), + + ..default() + }); + x += final_size * spacing; + if x + final_size > screen_x { + x = -screen_x; + y -= final_size * spacing; } } } diff --git a/examples/transform_rotation.rs b/examples/transform_rotation.rs index f6ea332..e106f7e 100644 --- a/examples/transform_rotation.rs +++ b/examples/transform_rotation.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; @@ -64,7 +64,7 @@ fn setup(mut commands: Commands) { SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(size, size)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, ..Default::default() diff --git a/examples/transform_translation.rs b/examples/transform_translation.rs index 7250a2a..1909806 100644 --- a/examples/transform_translation.rs +++ b/examples/transform_translation.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; @@ -58,7 +58,7 @@ fn setup(mut commands: Commands) { SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(size, size)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, ..Default::default() @@ -79,7 +79,7 @@ fn setup(mut commands: Commands) { SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(size, size)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, ..Default::default() @@ -99,7 +99,7 @@ fn setup(mut commands: Commands) { SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(size, size)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, ..Default::default() @@ -119,7 +119,7 @@ fn setup(mut commands: Commands) { SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(size, size)), - color: Color::RED, + color: palettes::basic::RED.into(), ..Default::default() }, ..Default::default() diff --git a/examples/ui_color.rs b/examples/ui_color.rs index cf08727..81927ee 100644 --- a/examples/ui_color.rs +++ b/examples/ui_color.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; @@ -51,7 +51,7 @@ fn setup(mut commands: Commands) { .enumerate() { commands.spawn(( - ImageBundle { + NodeBundle { style: Style { width: Val::Percent(5.), height: Val::Percent(8.), @@ -61,8 +61,8 @@ fn setup(mut commands: Commands) { }, ..Default::default() }, - BackgroundColor(Color::RED).ease_to( - BackgroundColor(Color::BLUE), + BackgroundColor(palettes::basic::RED.into()).ease_to( + BackgroundColor(palettes::basic::BLUE.into()), *ease_function, bevy_easings::EasingType::PingPong { duration: std::time::Duration::from_secs(1), diff --git a/examples/ui_style.rs b/examples/ui_style.rs index 05b1ad1..c339e9b 100644 --- a/examples/ui_style.rs +++ b/examples/ui_style.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{color::palettes, prelude::*}; use bevy_easings::*; @@ -51,8 +51,8 @@ fn setup(mut commands: Commands) { .enumerate() { commands.spawn(( - ImageBundle { - background_color: BackgroundColor(Color::RED), + NodeBundle { + background_color: BackgroundColor(palettes::basic::RED.into()), ..Default::default() }, Style { diff --git a/src/implemented.rs b/src/implemented.rs index cfa2a49..cfe9dd5 100644 --- a/src/implemented.rs +++ b/src/implemented.rs @@ -158,64 +158,86 @@ impl Lerp for EaseValue { fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self { let color = match (self.0, other.0) { - ( - Color::Rgba { - red, - green, - blue, - alpha, - }, - Color::Rgba { - red: redo, - green: greeno, - blue: blueo, - alpha: alphao, - }, - ) => Color::Rgba { - red: red + (redo + (red * -1.0)) * *scalar, - green: green + (greeno + (green * -1.0)) * *scalar, - blue: blue + (blueo + (blue * -1.0)) * *scalar, - alpha: alpha + (alphao + (alpha * -1.0)) * *scalar, - }, - ( - Color::RgbaLinear { - red, - green, - blue, - alpha, - }, - Color::RgbaLinear { - red: redo, - green: greeno, - blue: blueo, - alpha: alphao, - }, - ) => Color::RgbaLinear { - red: red + (redo + (red * -1.0)) * *scalar, - green: green + (greeno + (green * -1.0)) * *scalar, - blue: blue + (blueo + (blue * -1.0)) * *scalar, - alpha: alpha + (alphao + (alpha * -1.0)) * *scalar, - }, - ( - Color::Hsla { - hue, - saturation, - lightness, - alpha, - }, - Color::Hsla { - hue: hueo, - saturation: saturationo, - lightness: lightnesso, - alpha: alphao, - }, - ) => Color::Hsla { - hue: hue + (hueo + (hue * -1.0)) * *scalar, - saturation: saturation + (saturationo + (saturation * -1.0)) * *scalar, - lightness: lightness + (lightnesso + (lightness * -1.0)) * *scalar, - alpha: alpha + (alphao + (alpha * -1.0)) * *scalar, - }, - _ => self.0 + (other.0 + (self.0 * -1.)) * *scalar, + (Color::Srgba(color), other) => { + let lerped = Srgba::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::Srgba(lerped) + } + (Color::LinearRgba(color), other) => { + let lerped = LinearRgba::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::LinearRgba(lerped) + } + (Color::Hsla(color), other) => { + let lerped = Hsla::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::Hsla(lerped) + } + (Color::Hsva(color), other) => { + let lerped = Hsva::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::Hsva(lerped) + } + (Color::Hwba(color), other) => { + let lerped = Hwba::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::Hwba(lerped) + } + (Color::Laba(color), other) => { + let lerped = Laba::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::Laba(lerped) + } + (Color::Lcha(color), other) => { + let lerped = Lcha::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::Lcha(lerped) + } + (Color::Oklaba(color), other) => { + let lerped = Oklaba::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::Oklaba(lerped) + } + (Color::Oklcha(color), other) => { + let lerped = Oklcha::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::Oklcha(lerped) + } + (Color::Xyza(color), other) => { + let lerped = Xyza::from_vec4( + color + .to_vec4() + .lerp(Into::::into(other).to_vec4(), *scalar), + ); + Color::Xyza(lerped) + } }; EaseValue(color) } diff --git a/src/lib.rs b/src/lib.rs index 8afbfe4..c1c2881 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -302,10 +302,6 @@ where } } -trait IntermediateLerp: Sized { - fn lerp(start: &EaseValue<&Self>, end: &EaseValue<&Self>, scalar: f32) -> Self; -} - /// Trait to mark custom component that can be eased. It will be automatically implemented if the custom component implement `Lerp` pub trait CustomComponentEase: Sized { /// Create a new easing. If no start is provided, it will try to use the current value of the component for the target entity