-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.rs
340 lines (305 loc) · 9.53 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use bevy::prelude::*;
use crate::{
background::UpdateBackgroundTransform, constants::*, editor::ToggleEditor, fonts::Fonts, setup,
LoadLevel, ResetLevel,
};
pub const MENU_WIDTH: f32 = 500.;
pub const MENU_HEIGHT: f32 = 400.;
const NUM_HUB_BUTTONS: usize = 4;
const NUM_LEVEL_BUTTONS: usize = 4;
#[derive(Component)]
pub struct Menu {
kind: MenuKind,
}
#[derive(Event)]
struct ButtonPress;
#[derive(Resource)]
pub struct MenuState {
open_menu: Option<MenuKind>,
selected_button: MenuButtonKind,
}
impl Default for MenuState {
fn default() -> Self {
Self {
open_menu: Some(MenuKind::Hub),
selected_button: MenuButtonKind::Start,
}
}
}
impl MenuState {
pub fn is_open(&self) -> bool {
self.open_menu.is_some()
}
pub fn is_in_hub_menu(&self) -> bool {
self.open_menu == Some(MenuKind::Hub)
}
fn move_selected_button(&mut self, delta: isize) {
let kinds = if self.is_in_hub_menu() {
MenuButtonKind::hub_buttons()
} else {
MenuButtonKind::level_buttons()
};
let current_index = kinds
.iter()
.position(|kind| *kind == self.selected_button)
.unwrap_or_default() as isize;
let num_buttons = kinds.len() as isize;
let new_index = (current_index + num_buttons + delta) % num_buttons;
self.selected_button = kinds[new_index as usize];
}
pub fn set_open(&mut self, menu: MenuKind) {
self.open_menu = Some(menu);
self.selected_button = match menu {
MenuKind::Hub => MenuButtonKind::Start,
MenuKind::Level => MenuButtonKind::Restart,
};
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MenuKind {
Hub,
Level,
}
pub struct MenuPlugin;
impl Plugin for MenuPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup_menus.after(setup))
.init_resource::<MenuState>()
.add_event::<ButtonPress>()
.add_observer(on_button_press)
.add_systems(Update, (on_menu_interaction_input, on_resize))
.add_systems(Update, render_menu.after(on_menu_interaction_input));
}
}
#[derive(Clone, Component, Copy, Eq, PartialEq)]
enum MenuButtonKind {
Start,
Restart,
BackToHub,
Editor,
OtherGames,
Quit,
}
impl MenuButtonKind {
fn hub_buttons() -> [Self; NUM_HUB_BUTTONS] {
[Self::Start, Self::Editor, Self::OtherGames, Self::Quit]
}
fn level_buttons() -> [Self; NUM_LEVEL_BUTTONS] {
[Self::Restart, Self::BackToHub, Self::Editor, Self::Quit]
}
fn label(self) -> &'static str {
match self {
Self::Start => "Start",
Self::Restart => "Restart Level",
Self::BackToHub => "Exit Level",
Self::Editor => "Level Editor",
Self::OtherGames => "Other Games",
Self::Quit => "Quit Game",
}
}
}
fn setup_menus(mut commands: Commands, window_query: Query<&Window>, fonts: Res<Fonts>) {
let window = window_query
.get_single()
.expect("there should be only one window");
commands
.spawn((
Menu {
kind: MenuKind::Hub,
},
BackgroundColor(GRAY_BACKGROUND),
BorderColor(RED),
GlobalZIndex(100),
Node {
display: Display::Flex,
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
width: Val::Px(MENU_WIDTH),
height: Val::Px(MENU_HEIGHT),
border: UiRect::all(Val::Px(2.)),
margin: UiRect::all(Val::Auto)
.with_top(Val::Px(calculate_top_margin(window.size()))),
padding: UiRect::all(Val::Auto),
row_gap: Val::Px(40.),
position_type: PositionType::Absolute,
..default()
},
))
.with_children(|cb| {
for kind in MenuButtonKind::hub_buttons() {
cb.spawn(MenuButton::new(kind))
.with_children(|cb| MenuButton::populate(cb, kind.label(), &fonts));
}
});
commands
.spawn((
Menu {
kind: MenuKind::Level,
},
BackgroundColor(GRAY_BACKGROUND),
BorderColor(RED),
GlobalZIndex(100),
Node {
display: Display::Flex,
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
width: Val::Px(MENU_WIDTH),
height: Val::Px(MENU_HEIGHT),
border: UiRect::all(Val::Px(2.)),
margin: UiRect::all(Val::Auto)
.with_top(Val::Px(calculate_top_margin(window.size()))),
padding: UiRect::all(Val::Auto),
row_gap: Val::Px(40.),
position_type: PositionType::Absolute,
..default()
},
))
.with_children(|cb| {
for kind in MenuButtonKind::level_buttons() {
cb.spawn(MenuButton::new(kind))
.with_children(|cb| MenuButton::populate(cb, kind.label(), &fonts));
}
});
}
fn render_menu(
mut menu_query: Query<(&mut Node, &Menu)>,
mut button_query: Query<(&MenuButtonKind, &mut BackgroundColor)>,
menu_state: Res<MenuState>,
) {
if !menu_state.is_changed() {
return;
}
for (mut menu_node, menu) in &mut menu_query {
menu_node.display = if menu_state.open_menu.is_some_and(|kind| kind == menu.kind) {
Display::Flex
} else {
Display::None
};
}
for (menu_button, mut background_color) in &mut button_query {
*background_color = if menu_button == &menu_state.selected_button {
RED
} else {
BLUE
}
.into();
}
}
struct MenuButton;
impl MenuButton {
#[expect(clippy::new_ret_no_self)]
pub fn new(marker: impl Bundle) -> impl Bundle {
(
marker,
Button,
BackgroundColor(BLUE),
Node {
height: Val::Px(60.),
width: Val::Px(300.),
align_content: AlignContent::Center,
..default()
},
)
}
pub fn populate(cb: &mut ChildBuilder, text: impl Into<String>, fonts: &Fonts) {
cb.spawn((
Text::new(text),
TextColor(WHITE),
TextFont::from_font(fonts.poppins_light.clone()).with_font_size(36.),
Node {
margin: UiRect::all(Val::Auto),
..default()
},
));
}
}
pub fn on_menu_keyboard_input(
mut commands: Commands,
mut app_exit_events: EventWriter<AppExit>,
mut menu_state: ResMut<MenuState>,
keys: Res<ButtonInput<KeyCode>>,
) {
if menu_state.open_menu.is_none() {
return;
}
for key in keys.get_just_pressed() {
use KeyCode::*;
match key {
ArrowUp => menu_state.move_selected_button(-1),
ArrowDown => menu_state.move_selected_button(1),
Enter | Space => {
commands.trigger(ButtonPress);
return;
}
Escape => {
app_exit_events.send(AppExit::Success);
}
_ => continue,
};
}
}
fn on_menu_interaction_input(
mut commands: Commands,
button_query: Query<(&Interaction, &MenuButtonKind), Changed<Interaction>>,
mut menu_state: ResMut<MenuState>,
) {
for (interaction, menu_button) in &button_query {
match *interaction {
Interaction::Pressed => {
menu_state.selected_button = *menu_button;
commands.trigger(ButtonPress);
return;
}
Interaction::Hovered => {
menu_state.selected_button = *menu_button;
}
Interaction::None => {}
}
}
}
fn on_resize(
mut menu_query: Query<&mut Node, With<Menu>>,
window_query: Query<&Window, Changed<Window>>,
) {
for window in &window_query {
for mut node in &mut menu_query {
node.margin.top = Val::Px(calculate_top_margin(window.size()));
}
}
}
fn on_button_press(
_trigger: Trigger<ButtonPress>,
mut commands: Commands,
mut app_exit_events: EventWriter<AppExit>,
mut background_events: EventWriter<UpdateBackgroundTransform>,
mut menu_state: ResMut<MenuState>,
) {
match menu_state.selected_button {
MenuButtonKind::Start => {
background_events.send(UpdateBackgroundTransform::HubIntro);
menu_state.open_menu = None;
}
MenuButtonKind::Restart => {
commands.trigger(ResetLevel);
menu_state.open_menu = None;
}
MenuButtonKind::BackToHub => {
commands.trigger(LoadLevel(0));
menu_state.open_menu = None;
}
MenuButtonKind::Editor => {
commands.trigger(ToggleEditor);
menu_state.open_menu = None;
}
MenuButtonKind::OtherGames => { /* TODO */ }
MenuButtonKind::Quit => {
app_exit_events.send(AppExit::Success);
}
}
}
fn calculate_top_margin(window_size: Vec2) -> f32 {
// Add a small extra margin at the end so the written logo is revealed well.
0.5 * (window_size.y - MENU_HEIGHT) + 50.
}