Skip to content

Commit

Permalink
feat: make game start in fullscreen and add fullscreen setting. (#859)
Browse files Browse the repository at this point in the history
You can also toggle fullscreen with the F11 key.
  • Loading branch information
zicklag authored Oct 16, 2023
1 parent f2961bb commit f81431d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 10 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions assets/locales/en-US/settings.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ action = Action
# Networking settings
networking = Networking
matchmaking-server = Matchmaking Server
# Graphics settings
graphics = Graphics
fullscreen = Fullscreen
24 changes: 24 additions & 0 deletions src/fullscreen.rs
Original file line number Diff line number Diff line change
@@ -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::<Storage>().unwrap();
let mut storage = storage.borrow_mut();
let window = game.shared_resource_cell::<Window>().unwrap();
let mut window = window.borrow_mut();
let keyboard = game.shared_resource::<KeyboardInputs>().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::<Settings>().unwrap();
if f11_pressed {
settings.fullscreen = !settings.fullscreen;
}
window.fullscreen = settings.fullscreen;
}
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 fullscreen;
pub mod input;
pub mod music;
pub mod sessions;
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions src/ui/main_menu/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use super::MenuPage;

mod controls;
mod graphics;
mod networking;

#[derive(Clone, Default)]
Expand All @@ -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"),
];
}

Expand Down Expand Up @@ -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),
),
});
});
});
Expand Down
29 changes: 29 additions & 0 deletions src/ui/main_menu/settings/graphics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use super::*;

pub(super) fn widget(
mut args: In<(&mut egui::Ui, &mut SettingsState, bool)>,
meta: Root<GameMeta>,
localization: Localization<GameMeta>,
) {
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")),
);
});
}

0 comments on commit f81431d

Please sign in to comment.