forked from djeedai/bevy_tweening
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.rs
217 lines (204 loc) · 7.79 KB
/
menu.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use bevy::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_tweening::{lens::*, *};
use std::time::Duration;
const NORMAL_COLOR: Color = Color::rgba(162. / 255., 226. / 255., 95. / 255., 1.);
const HOVER_COLOR: Color = Color::AZURE;
const CLICK_COLOR: Color = Color::ALICE_BLUE;
const TEXT_COLOR: Color = Color::rgba(83. / 255., 163. / 255., 130. / 255., 1.);
const INIT_TRANSITION_DONE: u64 = 1;
/// The menu in this example has two set of animations:
/// one for appearance, one for interaction. Interaction animations
/// are only enabled after appearance animations finished.
///
/// The logic is handled as:
/// 1. Appearance animations send a `TweenComplete` event with
/// `INIT_TRANSITION_DONE` 2. The `enable_interaction_after_initial_animation`
/// system adds a label component `InitTransitionDone` to any button component
/// which completed its appearance animation, to mark it as active.
/// 3. The `interaction` system only queries buttons with a `InitTransitionDone`
/// marker.
fn main() {
App::default()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Menu".to_string(),
resolution: (800., 400.).into(),
present_mode: bevy::window::PresentMode::Fifo, // vsync
..default()
}),
..default()
}))
.add_systems(Update, bevy::window::close_on_esc)
.add_systems(Update, interaction)
.add_systems(Update, enable_interaction_after_initial_animation)
.add_plugins(TweeningPlugin)
.add_plugins(WorldInspectorPlugin::new())
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
let font = asset_server.load("fonts/FiraMono-Regular.ttf");
commands
.spawn((
NodeBundle {
style: Style {
position_type: PositionType::Absolute,
left: Val::Px(0.),
right: Val::Px(0.),
top: Val::Px(0.),
bottom: Val::Px(0.),
margin: UiRect::all(Val::Px(16.)),
padding: UiRect::all(Val::Px(16.)),
flex_direction: FlexDirection::Column,
align_content: AlignContent::Center,
align_items: AlignItems::Center,
align_self: AlignSelf::Center,
justify_content: JustifyContent::Center,
..default()
},
background_color: BackgroundColor(Color::NONE),
..default()
},
Name::new("menu"),
))
.with_children(|container| {
let mut start_time_ms = 0;
for (text, label) in [
("Continue", ButtonLabel::Continue),
("New Game", ButtonLabel::NewGame),
("Settings", ButtonLabel::Settings),
("Quit", ButtonLabel::Quit),
] {
let tween_scale = Tween::new(
EaseFunction::BounceOut,
Duration::from_secs(2),
TransformScaleLens {
start: Vec3::splat(0.01),
end: Vec3::ONE,
},
)
.with_completed_event(INIT_TRANSITION_DONE);
let animator = if start_time_ms > 0 {
let delay = Delay::new(Duration::from_millis(start_time_ms));
Animator::new(delay.then(tween_scale))
} else {
Animator::new(tween_scale)
};
start_time_ms += 500;
container
.spawn((
ButtonBundle {
style: Style {
min_width: Val::Px(300.),
min_height: Val::Px(80.),
margin: UiRect::all(Val::Px(8.)),
padding: UiRect::all(Val::Px(8.)),
align_content: AlignContent::Center,
align_items: AlignItems::Center,
align_self: AlignSelf::Center,
justify_content: JustifyContent::Center,
..default()
},
background_color: BackgroundColor(NORMAL_COLOR),
transform: Transform::from_scale(Vec3::splat(0.01)),
..default()
},
Name::new(format!("button:{}", text)),
animator,
label,
))
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text::from_section(
text.to_string(),
TextStyle {
font: font.clone(),
font_size: 48.0,
color: TEXT_COLOR,
},
)
.with_alignment(JustifyText::Center),
..default()
});
});
}
});
}
fn enable_interaction_after_initial_animation(
mut commands: Commands,
mut reader: EventReader<TweenCompleted>,
) {
for event in reader.read() {
if event.user_data == INIT_TRANSITION_DONE {
commands.entity(event.entity).insert(InitTransitionDone);
}
}
}
#[derive(Component)]
struct InitTransitionDone;
#[derive(Component, Clone, Copy)]
enum ButtonLabel {
Continue,
NewGame,
Settings,
Quit,
}
fn interaction(
mut interaction_query: Query<
(
&mut Animator<Transform>,
&Transform,
&Interaction,
&mut BackgroundColor,
&ButtonLabel,
),
(Changed<Interaction>, With<InitTransitionDone>),
>,
) {
for (mut animator, transform, interaction, mut color, button_label) in &mut interaction_query {
match *interaction {
Interaction::Pressed => {
*color = CLICK_COLOR.into();
match button_label {
ButtonLabel::Continue => {
println!("Continue clicked");
}
ButtonLabel::NewGame => {
println!("NewGame clicked");
}
ButtonLabel::Settings => {
println!("Settings clicked");
}
ButtonLabel::Quit => {
println!("Quit clicked");
}
}
}
Interaction::Hovered => {
*color = HOVER_COLOR.into();
animator.set_tweenable(Tween::new(
EaseFunction::QuadraticIn,
Duration::from_millis(200),
TransformScaleLens {
start: Vec3::ONE,
end: Vec3::splat(1.1),
},
));
}
Interaction::None => {
*color = NORMAL_COLOR.into();
let start_scale = transform.scale;
animator.set_tweenable(Tween::new(
EaseFunction::QuadraticIn,
Duration::from_millis(200),
TransformScaleLens {
start: start_scale,
end: Vec3::ONE,
},
));
}
}
}
}