1
1
//! Demonstrates the behavior of the built-in easing functions.
2
2
3
- use bevy:: { prelude:: * , sprite :: Anchor } ;
3
+ use bevy:: prelude:: * ;
4
4
5
5
#[ derive( Component ) ]
6
- struct SelectedEaseFunction ( EaseFunction , Color ) ;
6
+ #[ require( Visibility , Transform ) ]
7
+ struct EaseFunctionPlot ( EaseFunction , Color ) ;
7
8
8
9
fn main ( ) {
9
10
App :: new ( )
@@ -13,6 +14,10 @@ fn main() {
13
14
. run ( ) ;
14
15
}
15
16
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
+
16
21
fn setup ( mut commands : Commands ) {
17
22
commands. spawn ( Camera2d ) ;
18
23
@@ -21,73 +26,88 @@ fn setup(mut commands: Commands) {
21
26
..default ( )
22
27
} ;
23
28
24
- for ( i, functions) in [
29
+ let chunks = [
30
+ // "In" row
25
31
EaseFunction :: SineIn ,
26
- EaseFunction :: SineOut ,
27
- EaseFunction :: SineInOut ,
28
32
EaseFunction :: QuadraticIn ,
29
- EaseFunction :: QuadraticOut ,
30
- EaseFunction :: QuadraticInOut ,
31
33
EaseFunction :: CubicIn ,
32
- EaseFunction :: CubicOut ,
33
- EaseFunction :: CubicInOut ,
34
34
EaseFunction :: QuarticIn ,
35
- EaseFunction :: QuarticOut ,
36
- EaseFunction :: QuarticInOut ,
37
35
EaseFunction :: QuinticIn ,
38
- EaseFunction :: QuinticOut ,
39
- EaseFunction :: QuinticInOut ,
40
36
EaseFunction :: SmoothStepIn ,
41
- EaseFunction :: SmoothStepOut ,
42
- EaseFunction :: SmoothStep ,
43
37
EaseFunction :: SmootherStepIn ,
44
- EaseFunction :: SmootherStepOut ,
45
- EaseFunction :: SmootherStep ,
46
38
EaseFunction :: CircularIn ,
47
- EaseFunction :: CircularOut ,
48
- EaseFunction :: CircularInOut ,
49
39
EaseFunction :: ExponentialIn ,
50
- EaseFunction :: ExponentialOut ,
51
- EaseFunction :: ExponentialInOut ,
52
40
EaseFunction :: ElasticIn ,
53
- EaseFunction :: ElasticOut ,
54
- EaseFunction :: ElasticInOut ,
55
41
EaseFunction :: BackIn ,
56
- EaseFunction :: BackOut ,
57
- EaseFunction :: BackInOut ,
58
42
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 ,
59
55
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 ,
60
68
EaseFunction :: BounceInOut ,
69
+ // "Other" row
61
70
EaseFunction :: Linear ,
62
71
EaseFunction :: Steps ( 4 ) ,
63
72
EaseFunction :: Elastic ( 50.0 ) ,
64
73
]
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 ( ) ;
70
84
commands
71
85
. spawn ( (
72
- Text2d ( format ! ( "{:?}" , function) ) ,
73
- text_font. clone ( ) ,
74
- TextColor ( color) ,
86
+ EaseFunctionPlot ( * function, color) ,
75
87
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 ,
78
90
0.0 ,
79
91
) ,
80
- Anchor :: TopLeft ,
81
- SelectedEaseFunction ( * function, color) ,
82
92
) )
83
93
. with_children ( |p| {
94
+ // Marks the y value on the right side of the plot
84
95
p. spawn ( (
85
96
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 ) ,
87
98
) ) ;
99
+ // Marks the x and y value inside the plot
88
100
p. spawn ( (
89
101
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 ) ,
91
111
) ) ;
92
112
} ) ;
93
113
}
@@ -96,19 +116,17 @@ fn setup(mut commands: Commands) {
96
116
Text :: default ( ) ,
97
117
Node {
98
118
position_type : PositionType :: Absolute ,
99
- bottom : Val :: Px ( 12.0 ) ,
119
+ top : Val :: Px ( 12.0 ) ,
100
120
left : Val :: Px ( 12.0 ) ,
101
121
..default ( )
102
122
} ,
103
123
) ) ;
104
124
}
105
125
106
- const SIZE_PER_FUNCTION : f32 = 80.0 ;
107
-
108
126
fn display_curves (
109
127
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 > > ,
112
130
mut ui_text : Single < & mut Text > ,
113
131
time : Res < Time > ,
114
132
) {
@@ -121,54 +139,45 @@ fn display_curves(
121
139
122
140
ui_text. 0 = format ! ( "Progress: {:.2}" , now) ;
123
141
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
+
125
146
// Draw a box around the curve
126
147
gizmos. linestrip_2d (
127
148
[
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,
142
154
] ,
143
155
color. darker ( 0.4 ) ,
144
156
) ;
145
157
146
158
// Draw the curve
147
159
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 ) ;
154
164
gizmos. curve_2d (
155
165
& drawn_curve,
156
166
drawn_curve. domain ( ) . spaced_points ( samples) . unwrap ( ) ,
157
167
* color,
158
168
) ;
159
169
160
170
// 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;
163
173
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
165
177
gizmos. linestrip_2d (
166
178
[
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) ,
172
181
] ,
173
182
color. darker ( 0.2 ) ,
174
183
) ;
0 commit comments