From 20c37e1439113a9f83b601f70d9f01290951a849 Mon Sep 17 00:00:00 2001 From: Franklin Date: Tue, 7 Nov 2023 13:47:06 -0400 Subject: [PATCH] Added quit and resume buttons toESC menu. With functionality --- src/ui/game/plugin.rs | 4 +- src/ui/game/settings_screen.rs | 75 +++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/ui/game/plugin.rs b/src/ui/game/plugin.rs index f21907b..bdf829a 100644 --- a/src/ui/game/plugin.rs +++ b/src/ui/game/plugin.rs @@ -15,6 +15,8 @@ impl Plugin for MainGameUIPlugin { fps_counter::setup_fps_counter)); app.add_systems(Update, ( settings_screen::toggle_settings_screen, - fps_counter::tick_fps_counter)); + settings_screen::handle_settings_button_click, + fps_counter::tick_fps_counter, + )); } } \ No newline at end of file diff --git a/src/ui/game/settings_screen.rs b/src/ui/game/settings_screen.rs index f1e7cf3..77f437a 100644 --- a/src/ui/game/settings_screen.rs +++ b/src/ui/game/settings_screen.rs @@ -1,10 +1,16 @@ -use bevy::{prelude::*, ui::FocusPolicy}; +use bevy::{prelude::*, ui::FocusPolicy, app::AppExit}; use super::settings::SettingsScreenUIConfiguration; #[derive(Component)] pub struct SettingsScreenMarker; +#[derive(Component)] +pub enum SettingsScreenActions { + Resume, + Quit, +} + pub fn setup_settings_screen(mut commands: Commands) { commands.spawn( NodeBundle { @@ -14,6 +20,7 @@ pub fn setup_settings_screen(mut commands: Commands) { height: Val::Percent(100.0), justify_content: JustifyContent::Center, align_items: AlignItems::Center, + flex_direction: FlexDirection::Column, ..Default::default() }, visibility: Visibility::Hidden, @@ -21,7 +28,47 @@ pub fn setup_settings_screen(mut commands: Commands) { focus_policy: FocusPolicy::Block, ..Default::default() } - ).insert(SettingsScreenMarker); + ) + .insert(SettingsScreenMarker) + .with_children(|parent| { + // RESUME BUTTON + parent.spawn( + ButtonBundle { + style: Style { + ..Default::default() + }, + background_color: BackgroundColor(Color::NONE), + //focus_policy: FocusPolicy::Block, + ..Default::default() + } + ) + .insert(SettingsScreenActions::Resume) + .with_children(|parent| { + parent.spawn(TextBundle { + text: Text::from_section("Resume", TextStyle { font_size: 32.0, ..Default::default() }), + ..Default::default() + }); + }); + // QUIT BUTTON + parent.spawn( + ButtonBundle { + style: Style { + ..Default::default() + }, + background_color: BackgroundColor(Color::NONE), + //focus_policy: FocusPolicy::Block, + ..Default::default() + } + ) + .insert(SettingsScreenActions::Quit) + .with_children(|parent| { + parent.spawn(TextBundle { + text: Text::from_section("Quit", TextStyle { font_size: 32.0, ..Default::default() }), + ..Default::default() + }); + }); + // END BUTTONS + }); } pub fn toggle_settings_screen( @@ -39,4 +86,28 @@ pub fn toggle_settings_screen( } } } +} + +pub fn handle_settings_button_click( + query: Query<(&Interaction, &SettingsScreenActions), Changed>, + mut exit: EventWriter, + mut settings: ResMut, +) { + for (interaction, action) in query.iter() { + match interaction { + Interaction::Pressed => { + match action { + SettingsScreenActions::Quit => { + // QUIT GAME + exit.send(AppExit); + }, + SettingsScreenActions::Resume => { + // RESUME GAME + settings.settings_menu_shown = false; + } + } + }, + _ => {} + } + } } \ No newline at end of file