Skip to content

Commit a9de516

Browse files
authored
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" />
1 parent 7c2d54c commit a9de516

File tree

1 file changed

+82
-73
lines changed

1 file changed

+82
-73
lines changed

examples/animation/easing_functions.rs

Lines changed: 82 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Demonstrates the behavior of the built-in easing functions.
22
3-
use bevy::{prelude::*, sprite::Anchor};
3+
use bevy::prelude::*;
44

55
#[derive(Component)]
6-
struct SelectedEaseFunction(EaseFunction, Color);
6+
#[require(Visibility, Transform)]
7+
struct EaseFunctionPlot(EaseFunction, Color);
78

89
fn main() {
910
App::new()
@@ -13,6 +14,10 @@ fn main() {
1314
.run();
1415
}
1516

17+
const COLS: usize = 12;
18+
const EXTENT: Vec2 = Vec2::new(1172.0, 520.0);
19+
const PLOT_SIZE: Vec2 = Vec2::splat(80.0);
20+
1621
fn setup(mut commands: Commands) {
1722
commands.spawn(Camera2d);
1823

@@ -21,73 +26,88 @@ fn setup(mut commands: Commands) {
2126
..default()
2227
};
2328

24-
for (i, functions) in [
29+
let chunks = [
30+
// "In" row
2531
EaseFunction::SineIn,
26-
EaseFunction::SineOut,
27-
EaseFunction::SineInOut,
2832
EaseFunction::QuadraticIn,
29-
EaseFunction::QuadraticOut,
30-
EaseFunction::QuadraticInOut,
3133
EaseFunction::CubicIn,
32-
EaseFunction::CubicOut,
33-
EaseFunction::CubicInOut,
3434
EaseFunction::QuarticIn,
35-
EaseFunction::QuarticOut,
36-
EaseFunction::QuarticInOut,
3735
EaseFunction::QuinticIn,
38-
EaseFunction::QuinticOut,
39-
EaseFunction::QuinticInOut,
4036
EaseFunction::SmoothStepIn,
41-
EaseFunction::SmoothStepOut,
42-
EaseFunction::SmoothStep,
4337
EaseFunction::SmootherStepIn,
44-
EaseFunction::SmootherStepOut,
45-
EaseFunction::SmootherStep,
4638
EaseFunction::CircularIn,
47-
EaseFunction::CircularOut,
48-
EaseFunction::CircularInOut,
4939
EaseFunction::ExponentialIn,
50-
EaseFunction::ExponentialOut,
51-
EaseFunction::ExponentialInOut,
5240
EaseFunction::ElasticIn,
53-
EaseFunction::ElasticOut,
54-
EaseFunction::ElasticInOut,
5541
EaseFunction::BackIn,
56-
EaseFunction::BackOut,
57-
EaseFunction::BackInOut,
5842
EaseFunction::BounceIn,
43+
// "Out" row
44+
EaseFunction::SineOut,
45+
EaseFunction::QuadraticOut,
46+
EaseFunction::CubicOut,
47+
EaseFunction::QuarticOut,
48+
EaseFunction::QuinticOut,
49+
EaseFunction::SmoothStepOut,
50+
EaseFunction::SmootherStepOut,
51+
EaseFunction::CircularOut,
52+
EaseFunction::ExponentialOut,
53+
EaseFunction::ElasticOut,
54+
EaseFunction::BackOut,
5955
EaseFunction::BounceOut,
56+
// "InOut" row
57+
EaseFunction::SineInOut,
58+
EaseFunction::QuadraticInOut,
59+
EaseFunction::CubicInOut,
60+
EaseFunction::QuarticInOut,
61+
EaseFunction::QuinticInOut,
62+
EaseFunction::SmoothStep,
63+
EaseFunction::SmootherStep,
64+
EaseFunction::CircularInOut,
65+
EaseFunction::ExponentialInOut,
66+
EaseFunction::ElasticInOut,
67+
EaseFunction::BackInOut,
6068
EaseFunction::BounceInOut,
69+
// "Other" row
6170
EaseFunction::Linear,
6271
EaseFunction::Steps(4),
6372
EaseFunction::Elastic(50.0),
6473
]
65-
.chunks(3)
66-
.enumerate()
67-
{
68-
for (j, function) in functions.iter().enumerate() {
69-
let color = Hsla::hsl(i as f32 / 11.0 * 360.0, 0.8, 0.75).into();
74+
.chunks(COLS);
75+
76+
let max_rows = chunks.clone().count();
77+
78+
let half_extent = EXTENT / 2.;
79+
let half_size = PLOT_SIZE / 2.;
80+
81+
for (row, functions) in chunks.enumerate() {
82+
for (col, function) in functions.iter().enumerate() {
83+
let color = Hsla::hsl(col as f32 / COLS as f32 * 360.0, 0.8, 0.75).into();
7084
commands
7185
.spawn((
72-
Text2d(format!("{:?}", function)),
73-
text_font.clone(),
74-
TextColor(color),
86+
EaseFunctionPlot(*function, color),
7587
Transform::from_xyz(
76-
i as f32 * 95.0 - 1280.0 / 2.0 + 25.0,
77-
-100.0 - ((j as f32 * 250.0) - 300.0),
88+
-half_extent.x + EXTENT.x / (COLS - 1) as f32 * col as f32,
89+
half_extent.y - EXTENT.y / (max_rows - 1) as f32 * row as f32,
7890
0.0,
7991
),
80-
Anchor::TopLeft,
81-
SelectedEaseFunction(*function, color),
8292
))
8393
.with_children(|p| {
94+
// Marks the y value on the right side of the plot
8495
p.spawn((
8596
Sprite::from_color(color, Vec2::splat(5.0)),
86-
Transform::from_xyz(SIZE_PER_FUNCTION + 5.0, 15.0, 0.0),
97+
Transform::from_xyz(half_size.x + 5.0, -half_size.y, 0.0),
8798
));
99+
// Marks the x and y value inside the plot
88100
p.spawn((
89101
Sprite::from_color(color, Vec2::splat(4.0)),
90-
Transform::from_xyz(0.0, 0.0, 0.0),
102+
Transform::from_xyz(-half_size.x, -half_size.y, 0.0),
103+
));
104+
105+
// Label
106+
p.spawn((
107+
Text2d(format!("{:?}", function)),
108+
text_font.clone(),
109+
TextColor(color),
110+
Transform::from_xyz(0.0, -half_size.y - 15.0, 0.0),
91111
));
92112
});
93113
}
@@ -96,19 +116,17 @@ fn setup(mut commands: Commands) {
96116
Text::default(),
97117
Node {
98118
position_type: PositionType::Absolute,
99-
bottom: Val::Px(12.0),
119+
top: Val::Px(12.0),
100120
left: Val::Px(12.0),
101121
..default()
102122
},
103123
));
104124
}
105125

106-
const SIZE_PER_FUNCTION: f32 = 80.0;
107-
108126
fn display_curves(
109127
mut gizmos: Gizmos,
110-
ease_functions: Query<(&SelectedEaseFunction, &Transform, &Children)>,
111-
mut transforms: Query<&mut Transform, Without<SelectedEaseFunction>>,
128+
ease_functions: Query<(&EaseFunctionPlot, &Transform, &Children)>,
129+
mut transforms: Query<&mut Transform, Without<EaseFunctionPlot>>,
112130
mut ui_text: Single<&mut Text>,
113131
time: Res<Time>,
114132
) {
@@ -121,54 +139,45 @@ fn display_curves(
121139

122140
ui_text.0 = format!("Progress: {:.2}", now);
123141

124-
for (SelectedEaseFunction(function, color), transform, children) in &ease_functions {
142+
for (EaseFunctionPlot(function, color), transform, children) in &ease_functions {
143+
let center = transform.translation.xy();
144+
let half_size = PLOT_SIZE / 2.0;
145+
125146
// Draw a box around the curve
126147
gizmos.linestrip_2d(
127148
[
128-
Vec2::new(transform.translation.x, transform.translation.y + 15.0),
129-
Vec2::new(
130-
transform.translation.x + SIZE_PER_FUNCTION,
131-
transform.translation.y + 15.0,
132-
),
133-
Vec2::new(
134-
transform.translation.x + SIZE_PER_FUNCTION,
135-
transform.translation.y + 15.0 + SIZE_PER_FUNCTION,
136-
),
137-
Vec2::new(
138-
transform.translation.x,
139-
transform.translation.y + 15.0 + SIZE_PER_FUNCTION,
140-
),
141-
Vec2::new(transform.translation.x, transform.translation.y + 15.0),
149+
center + half_size,
150+
center + half_size * Vec2::new(-1., 1.),
151+
center + half_size * Vec2::new(-1., -1.),
152+
center + half_size * Vec2::new(1., -1.),
153+
center + half_size,
142154
],
143155
color.darker(0.4),
144156
);
145157

146158
// Draw the curve
147159
let f = EasingCurve::new(0.0, 1.0, *function);
148-
let drawn_curve = f.by_ref().graph().map(|(x, y)| {
149-
Vec2::new(
150-
x * SIZE_PER_FUNCTION + transform.translation.x,
151-
y * SIZE_PER_FUNCTION + transform.translation.y + 15.0,
152-
)
153-
});
160+
let drawn_curve = f
161+
.by_ref()
162+
.graph()
163+
.map(|(x, y)| center - half_size + Vec2::new(x, y) * PLOT_SIZE);
154164
gizmos.curve_2d(
155165
&drawn_curve,
156166
drawn_curve.domain().spaced_points(samples).unwrap(),
157167
*color,
158168
);
159169

160170
// Show progress along the curve for the current time
161-
let y = f.sample(now).unwrap() * SIZE_PER_FUNCTION + 15.0;
162-
transforms.get_mut(children[0]).unwrap().translation.y = y;
171+
let y = f.sample(now).unwrap() * PLOT_SIZE.y;
172+
transforms.get_mut(children[0]).unwrap().translation.y = -half_size.y + y;
163173
transforms.get_mut(children[1]).unwrap().translation =
164-
Vec3::new(now * SIZE_PER_FUNCTION, y, 0.0);
174+
-half_size.extend(0.0) + Vec3::new(now * PLOT_SIZE.x, y, 0.0);
175+
176+
// Show horizontal bar at y value
165177
gizmos.linestrip_2d(
166178
[
167-
Vec2::new(transform.translation.x, transform.translation.y + y),
168-
Vec2::new(
169-
transform.translation.x + SIZE_PER_FUNCTION,
170-
transform.translation.y + y,
171-
),
179+
center - half_size + Vec2::Y * y,
180+
center - half_size + Vec2::new(PLOT_SIZE.x, y),
172181
],
173182
color.darker(0.2),
174183
);

0 commit comments

Comments
 (0)