//! Demonstrates the behavior of the built-in easing functions. use bevy::prelude::*; #[derive(Component)] #[require(Visibility, Transform)] struct EaseFunctionPlot(EaseFunction, Color); fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .add_systems(Update, display_curves) .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); let text_font = TextFont { font_size: 10.0, ..default() }; let chunks = [ // "In" row EaseFunction::SineIn, EaseFunction::QuadraticIn, EaseFunction::CubicIn, EaseFunction::QuarticIn, EaseFunction::QuinticIn, EaseFunction::SmoothStepIn, EaseFunction::SmootherStepIn, EaseFunction::CircularIn, EaseFunction::ExponentialIn, EaseFunction::ElasticIn, EaseFunction::BackIn, 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, JumpAt::End), EaseFunction::Steps(4, JumpAt::Start), EaseFunction::Steps(4, JumpAt::Both), EaseFunction::Steps(4, JumpAt::None), EaseFunction::Elastic(50.0), ] .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(( EaseFunctionPlot(*function, color), Transform::from_xyz( -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, ), )) .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(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(-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), )); }); } } commands.spawn(( Text::default(), Node { position_type: PositionType::Absolute, top: Val::Px(12.0), left: Val::Px(12.0), ..default() }, )); } fn display_curves( mut gizmos: Gizmos, ease_functions: Query<(&EaseFunctionPlot, &Transform, &Children)>, mut transforms: Query<&mut Transform, Without>, mut ui_text: Single<&mut Text>, time: Res