Skip to content

Commit

Permalink
feat: add settings menu to the in game menu (#1000)
Browse files Browse the repository at this point in the history
Does the minimal thing, I didn't change the code structure, but longer
term I would suggest creating a place for shared menu options.


Closes #877,#999
  • Loading branch information
dignifiedquire authored Jun 2, 2024
1 parent d176351 commit 6bbb037
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/ui/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::ImageMeta;
mod credits;
mod map_select;
pub mod player_select;
mod settings;
pub(super) mod settings;
use shadow_rs::shadow;

// Generate build info.
Expand Down Expand Up @@ -104,12 +104,15 @@ static VERSION_STRING: Lazy<String> = Lazy::new(|| {

fn main_menu_system(world: &World) {
let ctx = (*world.resource::<EguiCtx>()).clone();
let mut close_settings_menu = false;

egui::CentralPanel::default()
.frame(egui::Frame::none())
.show(&ctx, |ui| match ctx.get_state::<MenuPage>() {
MenuPage::Home => world.run_system(home_menu, ui),
MenuPage::Settings => world.run_system(settings::widget, ui),
MenuPage::Settings => {
world.run_system(settings::widget, (ui, &mut close_settings_menu))
}
MenuPage::PlayerSelect => world.run_system(player_select::widget, ui),
MenuPage::MapSelect { .. } => world.run_system(map_select::widget, ui),
MenuPage::Credits => world.run_system(credits::widget, ui),
Expand All @@ -120,6 +123,10 @@ fn main_menu_system(world: &World) {
}
});

if close_settings_menu {
ctx.set_state(MenuPage::Home);
}

egui::CentralPanel::default()
.frame(egui::Frame::none())
.show(&ctx, |ui| {
Expand Down
10 changes: 4 additions & 6 deletions src/ui/main_menu/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use crate::{
settings::{PlayerControlMapping, Settings},
};

use super::MenuPage;

mod audio;
mod controls;
mod graphics;
Expand Down Expand Up @@ -38,15 +36,15 @@ impl SettingsTab {
}

pub fn widget(
mut ui: In<&mut egui::Ui>,
mut ui: In<(&mut egui::Ui, &mut bool)>,
meta: Root<GameMeta>,
localization: Localization<GameMeta>,
input: Res<GlobalPlayerControls>,
mut mapping: ResMut<PlayerControlMapping>,
mut storage: ResMut<Storage>,
world: &World,
) {
let ui = &mut *ui;
let (ui, close_settings_menu) = &mut *ui;
let mut state = ui.ctx().get_state::<SettingsState>();
let screen_rect = ui.max_rect();

Expand Down Expand Up @@ -132,7 +130,7 @@ pub fn widget(
|| input.values().any(|x| x.menu_back_just_pressed))
&& state.currently_binding_input_idx.is_none()
{
ui.ctx().set_state(MenuPage::Home);
**close_settings_menu = true;
// Reset the modified settings to their stored settings.
state.modified_settings = storage.get::<Settings>().unwrap().clone();
// Run cancel callbacks
Expand Down Expand Up @@ -170,7 +168,7 @@ pub fn widget(
*mapping = state.modified_settings.player_controls.clone();
storage.insert(state.modified_settings.clone());
storage.save();
ui.ctx().set_state(MenuPage::Home);
**close_settings_menu = true;
}
});

Expand Down
25 changes: 25 additions & 0 deletions src/ui/pause_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum PauseMenuPage {
#[default]
Pause,
MapSelect,
Settings,
}

pub fn session_plugin(session: &mut Session) {
Expand Down Expand Up @@ -43,6 +44,7 @@ fn pause_menu_system(
let mut back_to_menu = false;
let mut restart_game = false;
let mut close_pause_menu = false;
let mut close_settings_menu = false;
let mut select_map = None;
if let Some(session) = sessions.get_mut(SessionNames::GAME) {
let pause_pressed = controls.values().any(|x| x.pause_just_pressed);
Expand Down Expand Up @@ -97,6 +99,16 @@ fn pause_menu_system(
}
}
}
PauseMenuPage::Settings => {
egui::CentralPanel::default()
.frame(egui::Frame::none())
.show(&ctx, |ui| {
world.run_system(
super::main_menu::settings::widget,
(ui, &mut close_settings_menu),
);
});
}
}
} else if pause_pressed {
pause_menu.menu_open = true;
Expand Down Expand Up @@ -136,6 +148,10 @@ fn pause_menu_system(
if close_pause_menu {
pause_menu.menu_open = false;
}

if close_settings_menu {
ctx.set_state(PauseMenuPage::Pause);
}
}

fn main_pause_menu(
Expand Down Expand Up @@ -217,6 +233,15 @@ fn main_pause_menu(
ui.ctx().set_state(PauseMenuPage::MapSelect);
}

// Settings button
if BorderedButton::themed(&meta.theme.buttons.normal, localization.get("settings"))
.min_size(vec2(width, 0.0))
.show(ui)
.clicked()
{
ui.ctx().set_state(PauseMenuPage::Settings);
}

// Restart button
if BorderedButton::themed(&meta.theme.buttons.normal, localization.get("restart"))
.min_size(vec2(width, 0.0))
Expand Down

0 comments on commit 6bbb037

Please sign in to comment.