Tidy up easing_functions example (#17742)

# Objective

After #17461, the ease function labels in this example are a bit
cramped, especially in the bottom row.

This adjusts the spacing slightly and centers the labels.

## Solution

- The label is now a child of the plot and they are drawn around the
center of the transform
- Plot size and extents are now constants, and this thing has been
banished:
  
  ```rust
  i as f32 * 95.0 - 1280.0 / 2.0 + 25.0,
  -100.0 - ((j as f32 * 250.0) - 300.0),
  0.0,
  ```

- There's room for expansion in another row, so make that easier by
doing the chunking by row
- Other misc tidying of variable names, sprinkled in a few comments,
etc.

## Before

<img width="1280" alt="Screenshot 2025-02-08 at 7 33 14 AM"
src="https://github.com/user-attachments/assets/0b79c619-d295-4ab1-8cd1-d23c862d06c5"
/>

## After

<img width="1280" alt="Screenshot 2025-02-08 at 7 32 45 AM"
src="https://github.com/user-attachments/assets/656ef695-9aa8-42e9-b867-1718294316bd"
/>
This commit is contained in:
Rob Parrett 2025-02-08 09:35:11 -07:00 committed by GitHub
parent 7c2d54c93f
commit a9de5164a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,10 @@
//! Demonstrates the behavior of the built-in easing functions. //! Demonstrates the behavior of the built-in easing functions.
use bevy::{prelude::*, sprite::Anchor}; use bevy::prelude::*;
#[derive(Component)] #[derive(Component)]
struct SelectedEaseFunction(EaseFunction, Color); #[require(Visibility, Transform)]
struct EaseFunctionPlot(EaseFunction, Color);
fn main() { fn main() {
App::new() App::new()
@ -13,6 +14,10 @@ fn main() {
.run(); .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) { fn setup(mut commands: Commands) {
commands.spawn(Camera2d); commands.spawn(Camera2d);
@ -21,73 +26,88 @@ fn setup(mut commands: Commands) {
..default() ..default()
}; };
for (i, functions) in [ let chunks = [
// "In" row
EaseFunction::SineIn, EaseFunction::SineIn,
EaseFunction::SineOut,
EaseFunction::SineInOut,
EaseFunction::QuadraticIn, EaseFunction::QuadraticIn,
EaseFunction::QuadraticOut,
EaseFunction::QuadraticInOut,
EaseFunction::CubicIn, EaseFunction::CubicIn,
EaseFunction::CubicOut,
EaseFunction::CubicInOut,
EaseFunction::QuarticIn, EaseFunction::QuarticIn,
EaseFunction::QuarticOut,
EaseFunction::QuarticInOut,
EaseFunction::QuinticIn, EaseFunction::QuinticIn,
EaseFunction::QuinticOut,
EaseFunction::QuinticInOut,
EaseFunction::SmoothStepIn, EaseFunction::SmoothStepIn,
EaseFunction::SmoothStepOut,
EaseFunction::SmoothStep,
EaseFunction::SmootherStepIn, EaseFunction::SmootherStepIn,
EaseFunction::SmootherStepOut,
EaseFunction::SmootherStep,
EaseFunction::CircularIn, EaseFunction::CircularIn,
EaseFunction::CircularOut,
EaseFunction::CircularInOut,
EaseFunction::ExponentialIn, EaseFunction::ExponentialIn,
EaseFunction::ExponentialOut,
EaseFunction::ExponentialInOut,
EaseFunction::ElasticIn, EaseFunction::ElasticIn,
EaseFunction::ElasticOut,
EaseFunction::ElasticInOut,
EaseFunction::BackIn, EaseFunction::BackIn,
EaseFunction::BackOut,
EaseFunction::BackInOut,
EaseFunction::BounceIn, 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, 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, EaseFunction::BounceInOut,
// "Other" row
EaseFunction::Linear, EaseFunction::Linear,
EaseFunction::Steps(4), EaseFunction::Steps(4),
EaseFunction::Elastic(50.0), EaseFunction::Elastic(50.0),
] ]
.chunks(3) .chunks(COLS);
.enumerate()
{ let max_rows = chunks.clone().count();
for (j, function) in functions.iter().enumerate() {
let color = Hsla::hsl(i as f32 / 11.0 * 360.0, 0.8, 0.75).into(); 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 commands
.spawn(( .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)), Text2d(format!("{:?}", function)),
text_font.clone(), text_font.clone(),
TextColor(color), TextColor(color),
Transform::from_xyz( Transform::from_xyz(0.0, -half_size.y - 15.0, 0.0),
i as f32 * 95.0 - 1280.0 / 2.0 + 25.0,
-100.0 - ((j as f32 * 250.0) - 300.0),
0.0,
),
Anchor::TopLeft,
SelectedEaseFunction(*function, color),
))
.with_children(|p| {
p.spawn((
Sprite::from_color(color, Vec2::splat(5.0)),
Transform::from_xyz(SIZE_PER_FUNCTION + 5.0, 15.0, 0.0),
));
p.spawn((
Sprite::from_color(color, Vec2::splat(4.0)),
Transform::from_xyz(0.0, 0.0, 0.0),
)); ));
}); });
} }
@ -96,19 +116,17 @@ fn setup(mut commands: Commands) {
Text::default(), Text::default(),
Node { Node {
position_type: PositionType::Absolute, position_type: PositionType::Absolute,
bottom: Val::Px(12.0), top: Val::Px(12.0),
left: Val::Px(12.0), left: Val::Px(12.0),
..default() ..default()
}, },
)); ));
} }
const SIZE_PER_FUNCTION: f32 = 80.0;
fn display_curves( fn display_curves(
mut gizmos: Gizmos, mut gizmos: Gizmos,
ease_functions: Query<(&SelectedEaseFunction, &Transform, &Children)>, ease_functions: Query<(&EaseFunctionPlot, &Transform, &Children)>,
mut transforms: Query<&mut Transform, Without<SelectedEaseFunction>>, mut transforms: Query<&mut Transform, Without<EaseFunctionPlot>>,
mut ui_text: Single<&mut Text>, mut ui_text: Single<&mut Text>,
time: Res<Time>, time: Res<Time>,
) { ) {
@ -121,36 +139,28 @@ fn display_curves(
ui_text.0 = format!("Progress: {:.2}", now); ui_text.0 = format!("Progress: {:.2}", now);
for (SelectedEaseFunction(function, color), transform, children) in &ease_functions { for (EaseFunctionPlot(function, color), transform, children) in &ease_functions {
let center = transform.translation.xy();
let half_size = PLOT_SIZE / 2.0;
// Draw a box around the curve // Draw a box around the curve
gizmos.linestrip_2d( gizmos.linestrip_2d(
[ [
Vec2::new(transform.translation.x, transform.translation.y + 15.0), center + half_size,
Vec2::new( center + half_size * Vec2::new(-1., 1.),
transform.translation.x + SIZE_PER_FUNCTION, center + half_size * Vec2::new(-1., -1.),
transform.translation.y + 15.0, center + half_size * Vec2::new(1., -1.),
), center + half_size,
Vec2::new(
transform.translation.x + SIZE_PER_FUNCTION,
transform.translation.y + 15.0 + SIZE_PER_FUNCTION,
),
Vec2::new(
transform.translation.x,
transform.translation.y + 15.0 + SIZE_PER_FUNCTION,
),
Vec2::new(transform.translation.x, transform.translation.y + 15.0),
], ],
color.darker(0.4), color.darker(0.4),
); );
// Draw the curve // Draw the curve
let f = EasingCurve::new(0.0, 1.0, *function); let f = EasingCurve::new(0.0, 1.0, *function);
let drawn_curve = f.by_ref().graph().map(|(x, y)| { let drawn_curve = f
Vec2::new( .by_ref()
x * SIZE_PER_FUNCTION + transform.translation.x, .graph()
y * SIZE_PER_FUNCTION + transform.translation.y + 15.0, .map(|(x, y)| center - half_size + Vec2::new(x, y) * PLOT_SIZE);
)
});
gizmos.curve_2d( gizmos.curve_2d(
&drawn_curve, &drawn_curve,
drawn_curve.domain().spaced_points(samples).unwrap(), drawn_curve.domain().spaced_points(samples).unwrap(),
@ -158,17 +168,16 @@ fn display_curves(
); );
// Show progress along the curve for the current time // Show progress along the curve for the current time
let y = f.sample(now).unwrap() * SIZE_PER_FUNCTION + 15.0; let y = f.sample(now).unwrap() * PLOT_SIZE.y;
transforms.get_mut(children[0]).unwrap().translation.y = y; transforms.get_mut(children[0]).unwrap().translation.y = -half_size.y + y;
transforms.get_mut(children[1]).unwrap().translation = transforms.get_mut(children[1]).unwrap().translation =
Vec3::new(now * SIZE_PER_FUNCTION, y, 0.0); -half_size.extend(0.0) + Vec3::new(now * PLOT_SIZE.x, y, 0.0);
// Show horizontal bar at y value
gizmos.linestrip_2d( gizmos.linestrip_2d(
[ [
Vec2::new(transform.translation.x, transform.translation.y + y), center - half_size + Vec2::Y * y,
Vec2::new( center - half_size + Vec2::new(PLOT_SIZE.x, y),
transform.translation.x + SIZE_PER_FUNCTION,
transform.translation.y + y,
),
], ],
color.darker(0.2), color.darker(0.2),
); );