diff --git a/examples/animation/easing_functions.rs b/examples/animation/easing_functions.rs index 4e579aff40..d923542c03 100644 --- a/examples/animation/easing_functions.rs +++ b/examples/animation/easing_functions.rs @@ -1,9 +1,10 @@ //! Demonstrates the behavior of the built-in easing functions. -use bevy::{prelude::*, sprite::Anchor}; +use bevy::prelude::*; #[derive(Component)] -struct SelectedEaseFunction(EaseFunction, Color); +#[require(Visibility, Transform)] +struct EaseFunctionPlot(EaseFunction, Color); fn main() { App::new() @@ -13,6 +14,10 @@ fn main() { .run(); } +const COLS: usize = 12; +const EXTENT: Vec2 = Vec2::new(1172.0, 520.0); +const PLOT_SIZE: Vec2 = Vec2::splat(80.0); + fn setup(mut commands: Commands) { commands.spawn(Camera2d); @@ -21,73 +26,88 @@ fn setup(mut commands: Commands) { ..default() }; - for (i, functions) in [ + let chunks = [ + // "In" row EaseFunction::SineIn, - EaseFunction::SineOut, - EaseFunction::SineInOut, EaseFunction::QuadraticIn, - EaseFunction::QuadraticOut, - EaseFunction::QuadraticInOut, EaseFunction::CubicIn, - EaseFunction::CubicOut, - EaseFunction::CubicInOut, EaseFunction::QuarticIn, - EaseFunction::QuarticOut, - EaseFunction::QuarticInOut, EaseFunction::QuinticIn, - EaseFunction::QuinticOut, - EaseFunction::QuinticInOut, EaseFunction::SmoothStepIn, - EaseFunction::SmoothStepOut, - EaseFunction::SmoothStep, EaseFunction::SmootherStepIn, - EaseFunction::SmootherStepOut, - EaseFunction::SmootherStep, EaseFunction::CircularIn, - EaseFunction::CircularOut, - EaseFunction::CircularInOut, EaseFunction::ExponentialIn, - EaseFunction::ExponentialOut, - EaseFunction::ExponentialInOut, EaseFunction::ElasticIn, - EaseFunction::ElasticOut, - EaseFunction::ElasticInOut, EaseFunction::BackIn, - EaseFunction::BackOut, - EaseFunction::BackInOut, EaseFunction::BounceIn, + // "Out" row + EaseFunction::SineOut, + EaseFunction::QuadraticOut, + EaseFunction::CubicOut, + EaseFunction::QuarticOut, + EaseFunction::QuinticOut, + EaseFunction::SmoothStepOut, + EaseFunction::SmootherStepOut, + EaseFunction::CircularOut, + EaseFunction::ExponentialOut, + EaseFunction::ElasticOut, + EaseFunction::BackOut, EaseFunction::BounceOut, + // "InOut" row + EaseFunction::SineInOut, + EaseFunction::QuadraticInOut, + EaseFunction::CubicInOut, + EaseFunction::QuarticInOut, + EaseFunction::QuinticInOut, + EaseFunction::SmoothStep, + EaseFunction::SmootherStep, + EaseFunction::CircularInOut, + EaseFunction::ExponentialInOut, + EaseFunction::ElasticInOut, + EaseFunction::BackInOut, EaseFunction::BounceInOut, + // "Other" row EaseFunction::Linear, EaseFunction::Steps(4), EaseFunction::Elastic(50.0), ] - .chunks(3) - .enumerate() - { - for (j, function) in functions.iter().enumerate() { - let color = Hsla::hsl(i as f32 / 11.0 * 360.0, 0.8, 0.75).into(); + .chunks(COLS); + + let max_rows = chunks.clone().count(); + + let half_extent = EXTENT / 2.; + let half_size = PLOT_SIZE / 2.; + + for (row, functions) in chunks.enumerate() { + for (col, function) in functions.iter().enumerate() { + let color = Hsla::hsl(col as f32 / COLS as f32 * 360.0, 0.8, 0.75).into(); commands .spawn(( - Text2d(format!("{:?}", function)), - text_font.clone(), - TextColor(color), + EaseFunctionPlot(*function, color), Transform::from_xyz( - i as f32 * 95.0 - 1280.0 / 2.0 + 25.0, - -100.0 - ((j as f32 * 250.0) - 300.0), + -half_extent.x + EXTENT.x / (COLS - 1) as f32 * col as f32, + half_extent.y - EXTENT.y / (max_rows - 1) as f32 * row as f32, 0.0, ), - Anchor::TopLeft, - SelectedEaseFunction(*function, color), )) .with_children(|p| { + // Marks the y value on the right side of the plot p.spawn(( Sprite::from_color(color, Vec2::splat(5.0)), - Transform::from_xyz(SIZE_PER_FUNCTION + 5.0, 15.0, 0.0), + Transform::from_xyz(half_size.x + 5.0, -half_size.y, 0.0), )); + // Marks the x and y value inside the plot p.spawn(( Sprite::from_color(color, Vec2::splat(4.0)), - Transform::from_xyz(0.0, 0.0, 0.0), + Transform::from_xyz(-half_size.x, -half_size.y, 0.0), + )); + + // Label + p.spawn(( + Text2d(format!("{:?}", function)), + text_font.clone(), + TextColor(color), + Transform::from_xyz(0.0, -half_size.y - 15.0, 0.0), )); }); } @@ -96,19 +116,17 @@ fn setup(mut commands: Commands) { Text::default(), Node { position_type: PositionType::Absolute, - bottom: Val::Px(12.0), + top: Val::Px(12.0), left: Val::Px(12.0), ..default() }, )); } -const SIZE_PER_FUNCTION: f32 = 80.0; - fn display_curves( mut gizmos: Gizmos, - ease_functions: Query<(&SelectedEaseFunction, &Transform, &Children)>, - mut transforms: Query<&mut Transform, Without>, + ease_functions: Query<(&EaseFunctionPlot, &Transform, &Children)>, + mut transforms: Query<&mut Transform, Without>, mut ui_text: Single<&mut Text>, time: Res