Skip to content

Commit

Permalink
feat: add a debug menu ( F12 ) with snapshot and restore buttons. (#878)
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag authored Dec 17, 2023
1 parent 58cf781 commit 3d6550c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! Debug tools and menus.
use crate::prelude::*;

pub fn game_plugin(game: &mut Game) {
game.sessions
.create(SessionNames::DEBUG)
.install_plugin(session_plugin);
}

fn session_plugin(session: &mut Session) {
session
.stages
.add_system_to_stage(CoreStage::First, debug_menu);
}

#[derive(HasSchema, Clone, Debug, Default)]
struct DebugMenuState {
pub show_menu: bool,
pub snapshot: Option<World>,
}

fn debug_menu(
mut sessions: ResMut<Sessions>,
keyboard_inputs: Res<KeyboardInputs>,
mut state: ResMutInit<DebugMenuState>,
ctx: ResMut<EguiCtx>,
localization: Localization<GameMeta>,
) {
let DebugMenuState {
snapshot,
show_menu,
} = &mut *state;

let toggle_debug = keyboard_inputs
.key_events
.iter()
.any(|x| x.key_code.option() == Some(KeyCode::F12) && !x.button_state.pressed());

if toggle_debug {
*show_menu = !*show_menu;
}
let game_session = sessions.get_mut(SessionNames::GAME);

// Delete the snapshot if there is one and we are not in the middle of a game.
if game_session.is_none() && snapshot.is_some() {
*snapshot = None;
}

egui::Window::new(localization.get("debug-tools"))
.id(egui::Id::from("debug"))
.open(show_menu)
.show(&ctx, |ui| {
ui.horizontal(|ui| {
ui.set_enabled(game_session.is_some());

if ui.button(localization.get("take-snapshot")).clicked() {
if let Some(session) = game_session {
*snapshot = Some(session.snapshot());
}
} else if ui.button(localization.get("restore-snapshot")).clicked() {
if let Some(session) = game_session {
if let Some(mut snapshot) = snapshot.clone() {
session.restore(&mut snapshot);
}
}
}
})
});
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use bones_bevy_renderer::BonesBevyRenderer;
use bones_framework::prelude::*;

pub mod core;
pub mod debug;
pub mod fullscreen;
pub mod input;
pub mod music;
Expand Down Expand Up @@ -124,6 +125,7 @@ fn main() {
.install_plugin(fullscreen::game_plugin)
.install_plugin(input::game_plugin)
.install_plugin(core::game_plugin)
.install_plugin(debug::game_plugin)
// We initialize the asset server and register asset types
.init_shared_resource::<AssetServer>()
.register_default_assets();
Expand Down
1 change: 1 addition & 0 deletions src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::prelude::*;
pub struct SessionNames;
impl SessionNames {
pub const GAME: &'static str = "game";
pub const DEBUG: &'static str = "debug";
pub const MAIN_MENU: &'static str = "main_menu";
pub const PAUSE_MENU: &'static str = "pause_menu";
pub const MUSIC_PLAYER: &'static str = "music_player";
Expand Down

0 comments on commit 3d6550c

Please sign in to comment.