From f81431df6ee7e4dd2a34bcd7cb9266825d74f921 Mon Sep 17 00:00:00 2001 From: Zicklag Date: Mon, 16 Oct 2023 13:56:04 +0000 Subject: [PATCH] feat: make game start in fullscreen and add fullscreen setting. (#859) You can also toggle fullscreen with the F11 key. --- Cargo.lock | 18 ++++++++--------- assets/locales/en-US/settings.ftl | 4 ++++ src/fullscreen.rs | 24 ++++++++++++++++++++++ src/main.rs | 2 ++ src/settings.rs | 14 ++++++++++++- src/ui/main_menu/settings.rs | 7 +++++++ src/ui/main_menu/settings/graphics.rs | 29 +++++++++++++++++++++++++++ 7 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 src/fullscreen.rs create mode 100644 src/ui/main_menu/settings/graphics.rs diff --git a/Cargo.lock b/Cargo.lock index 593391dad2..1af93aaaa1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1046,7 +1046,7 @@ dependencies = [ [[package]] name = "bones_asset" version = "0.3.0" -source = "git+https://github.com/fishfolk/bones#04d55b1d3fe8f25b8bf4d1c1b6c1045abc1c1661" +source = "git+https://github.com/fishfolk/bones#f84d07c2f2847d9acd5c07098fe1575abc496400" dependencies = [ "anyhow", "append-only-vec", @@ -1075,7 +1075,7 @@ dependencies = [ [[package]] name = "bones_bevy_renderer" version = "0.3.0" -source = "git+https://github.com/fishfolk/bones#04d55b1d3fe8f25b8bf4d1c1b6c1045abc1c1661" +source = "git+https://github.com/fishfolk/bones#f84d07c2f2847d9acd5c07098fe1575abc496400" dependencies = [ "anyhow", "bevy", @@ -1092,7 +1092,7 @@ dependencies = [ [[package]] name = "bones_ecs" version = "0.3.0" -source = "git+https://github.com/fishfolk/bones#04d55b1d3fe8f25b8bf4d1c1b6c1045abc1c1661" +source = "git+https://github.com/fishfolk/bones#f84d07c2f2847d9acd5c07098fe1575abc496400" dependencies = [ "anyhow", "atomicell", @@ -1107,7 +1107,7 @@ dependencies = [ [[package]] name = "bones_framework" version = "0.3.0" -source = "git+https://github.com/fishfolk/bones#04d55b1d3fe8f25b8bf4d1c1b6c1045abc1c1661" +source = "git+https://github.com/fishfolk/bones#f84d07c2f2847d9acd5c07098fe1575abc496400" dependencies = [ "bones_asset", "bones_lib", @@ -1135,7 +1135,7 @@ dependencies = [ [[package]] name = "bones_lib" version = "0.3.0" -source = "git+https://github.com/fishfolk/bones#04d55b1d3fe8f25b8bf4d1c1b6c1045abc1c1661" +source = "git+https://github.com/fishfolk/bones#f84d07c2f2847d9acd5c07098fe1575abc496400" dependencies = [ "bones_ecs", "instant", @@ -1144,7 +1144,7 @@ dependencies = [ [[package]] name = "bones_schema" version = "0.3.0" -source = "git+https://github.com/fishfolk/bones#04d55b1d3fe8f25b8bf4d1c1b6c1045abc1c1661" +source = "git+https://github.com/fishfolk/bones#f84d07c2f2847d9acd5c07098fe1575abc496400" dependencies = [ "append-only-vec", "bones_schema_macros", @@ -1161,7 +1161,7 @@ dependencies = [ [[package]] name = "bones_schema_macros" version = "0.3.0" -source = "git+https://github.com/fishfolk/bones#04d55b1d3fe8f25b8bf4d1c1b6c1045abc1c1661" +source = "git+https://github.com/fishfolk/bones#f84d07c2f2847d9acd5c07098fe1575abc496400" dependencies = [ "proc-macro2", "quote", @@ -1171,7 +1171,7 @@ dependencies = [ [[package]] name = "bones_utils" version = "0.3.0" -source = "git+https://github.com/fishfolk/bones#04d55b1d3fe8f25b8bf4d1c1b6c1045abc1c1661" +source = "git+https://github.com/fishfolk/bones#f84d07c2f2847d9acd5c07098fe1575abc496400" dependencies = [ "bevy_ptr", "bones_utils_macros", @@ -1193,7 +1193,7 @@ dependencies = [ [[package]] name = "bones_utils_macros" version = "0.3.0" -source = "git+https://github.com/fishfolk/bones#04d55b1d3fe8f25b8bf4d1c1b6c1045abc1c1661" +source = "git+https://github.com/fishfolk/bones#f84d07c2f2847d9acd5c07098fe1575abc496400" dependencies = [ "quote", "venial", diff --git a/assets/locales/en-US/settings.ftl b/assets/locales/en-US/settings.ftl index 8adb5e74c2..51b25d4e7e 100644 --- a/assets/locales/en-US/settings.ftl +++ b/assets/locales/en-US/settings.ftl @@ -18,3 +18,7 @@ action = Action # Networking settings networking = Networking matchmaking-server = Matchmaking Server + +# Graphics settings +graphics = Graphics +fullscreen = Fullscreen diff --git a/src/fullscreen.rs b/src/fullscreen.rs new file mode 100644 index 0000000000..ba1f6e0182 --- /dev/null +++ b/src/fullscreen.rs @@ -0,0 +1,24 @@ +use crate::prelude::*; + +pub fn game_plugin(game: &mut Game) { + game.systems.add_before_system(update_fullscreen); +} + +fn update_fullscreen(game: &mut Game) { + let storage = game.shared_resource_cell::().unwrap(); + let mut storage = storage.borrow_mut(); + let window = game.shared_resource_cell::().unwrap(); + let mut window = window.borrow_mut(); + let keyboard = game.shared_resource::().unwrap(); + + let f11_pressed = keyboard + .key_events + .iter() + .any(|x| x.key_code == Set(KeyCode::F11) && x.button_state.pressed()); + + let settings = storage.get_mut::().unwrap(); + if f11_pressed { + settings.fullscreen = !settings.fullscreen; + } + window.fullscreen = settings.fullscreen; +} diff --git a/src/main.rs b/src/main.rs index fc422344c3..846fcdb893 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use bones_bevy_renderer::BonesBevyRenderer; use bones_framework::prelude::*; pub mod core; +pub mod fullscreen; pub mod input; pub mod music; pub mod sessions; @@ -78,6 +79,7 @@ fn main() { .install_plugin(DefaultGamePlugin) .install_plugin(music::game_plugin) .install_plugin(settings::game_plugin) + .install_plugin(fullscreen::game_plugin) .install_plugin(input::game_plugin) .install_plugin(core::game_plugin) // We initialize the asset server and register asset types diff --git a/src/settings.rs b/src/settings.rs index 22f252b89f..433ad491d5 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -19,15 +19,27 @@ fn load_settings(game: &mut Game) { } /// Global settings, stored and accessed through [`Storage`]. -#[derive(HasSchema, Debug, Clone, Default)] +#[derive(HasSchema, Debug, Clone)] #[repr(C)] pub struct Settings { + /// Whether to display the game fullscreen. + pub fullscreen: bool, /// The player controller bindings pub player_controls: PlayerControlMapping, /// The address of the matchmaking server to connect to for online games. pub matchmaking_server: String, } +impl Default for Settings { + fn default() -> Self { + Self { + fullscreen: true, + player_controls: default(), + matchmaking_server: default(), + } + } +} + #[derive(HasSchema, Clone, Debug, Default)] #[repr(C)] pub struct PlayerControlMapping { diff --git a/src/ui/main_menu/settings.rs b/src/ui/main_menu/settings.rs index a186ec0544..b035dbebb4 100644 --- a/src/ui/main_menu/settings.rs +++ b/src/ui/main_menu/settings.rs @@ -6,6 +6,7 @@ use crate::{ use super::MenuPage; mod controls; +mod graphics; mod networking; #[derive(Clone, Default)] @@ -22,12 +23,14 @@ enum SettingsTab { #[default] Controls, Networking, + Graphics, } impl SettingsTab { const TABS: &'static [(Self, &'static str)] = &[ (Self::Controls, "controls"), (Self::Networking, "networking"), + (Self::Graphics, "graphics"), ]; } @@ -173,6 +176,10 @@ pub fn widget( networking::widget, (ui, &mut state, should_reset), ), + SettingsTab::Graphics => world.run_initialized_system( + graphics::widget, + (ui, &mut state, should_reset), + ), }); }); }); diff --git a/src/ui/main_menu/settings/graphics.rs b/src/ui/main_menu/settings/graphics.rs new file mode 100644 index 0000000000..ee4797d885 --- /dev/null +++ b/src/ui/main_menu/settings/graphics.rs @@ -0,0 +1,29 @@ +use super::*; + +pub(super) fn widget( + mut args: In<(&mut egui::Ui, &mut SettingsState, bool)>, + meta: Root, + localization: Localization, +) { + let (ui, state, should_reset) = &mut *args; + + let normal_font = meta + .theme + .font_styles + .normal + .with_color(meta.theme.panel.font_color); + + if *should_reset { + state.modified_settings.fullscreen = meta.default_settings.fullscreen; + } + + ui.add_space(normal_font.size / 2.0); + + ui.horizontal(|ui| { + ui.add_space(normal_font.size * 3.0); + ui.checkbox( + &mut state.modified_settings.fullscreen, + normal_font.rich(localization.get("fullscreen")), + ); + }); +}