Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make window mode changeable #777

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,5 @@ features = [
"tonemapping_luts",
"default_font",
"webgl2",
"serialize",
]
22 changes: 21 additions & 1 deletion crates/conf/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use anyhow::{ensure, Context, Error, Result};
use async_std::path::Path;
use bevy::window::WindowMode;
use conf_macros::Config;
use de_uom::{LogicalPixel, Metre};
use serde::{Deserialize, Serialize};
Expand All @@ -24,6 +25,11 @@ pub struct MultiplayerConf {
connector: SocketAddr,
}

#[derive(Deserialize, Serialize, Config, Clone, Debug)]
pub struct Window {
mode: WindowMode,
}

#[derive(Deserialize, Serialize, Config, Debug, Clone)]
pub struct Camera {
scroll_inverted: bool,
Expand Down Expand Up @@ -103,6 +109,13 @@ impl Default for AudioConf {
}
}

impl Default for Window {
fn default() -> Self {
Self {
mode: WindowMode::BorderlessFullscreen,
}
}
}
// --------------------

// for this more complicated data structure, we need to
Expand Down Expand Up @@ -220,9 +233,16 @@ impl AudioConf {
}
}

impl Window {
pub fn mode(&self) -> WindowMode {
self.mode
}
}

// Bundle configuration neatly into a single struct
bundle_config!(
camera: CameraConf: Camera, // Conf file -> Camera -> CameraConf
multiplayer: MultiplayerConf: MultiplayerConf, // Conf file -> MultiplayerConf
audio: AudioConf: AudioConf
audio: AudioConf: AudioConf,
window: Window: Window,
);
2 changes: 1 addition & 1 deletion crates/conf/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl fmt::Display for ConfigLoadError {
/// the TryInto trait
#[macro_export]
macro_rules! bundle_config {
($($name:ident : $type_into:ty : $type_from:ty),*) => {
($($name:ident : $type_into:ty : $type_from:ty,)*) => {
use $crate::io::load_conf_text;
use $crate::macros::ConfigLoadError;
use tracing::{trace, debug};
Expand Down
3 changes: 3 additions & 0 deletions crates/controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use draft::DraftPlugin;
use hud::HudPlugin;
use mouse::MousePlugin;
use selection::SelectionPlugin;
use window::WindowManagementPlugin;

mod commands;
mod draft;
Expand All @@ -14,6 +15,7 @@ mod hud;
mod mouse;
mod ray;
mod selection;
mod window;

const SELECTION_BAR_ID: u32 = 0;
const POINTER_BAR_ID: u32 = 1;
Expand All @@ -28,5 +30,6 @@ impl PluginGroup for ControllerPluginGroup {
.add(SelectionPlugin)
.add(DraftPlugin)
.add(HudPlugin)
.add(WindowManagementPlugin)
}
}
57 changes: 57 additions & 0 deletions crates/controller/src/window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use bevy::{
prelude::*,
window::{CursorGrabMode, WindowMode},
};
use de_conf::Configuration;
use de_core::state::AppState;

pub struct WindowManagementPlugin;

impl Plugin for WindowManagementPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
JackCrumpLeys marked this conversation as resolved.
Show resolved Hide resolved
OnExit(AppState::AppLoading),
update_window_mode_based_on_config,
)
.add_systems(Update, toggle_fullscreen);
}
}

fn update_window_mode_based_on_config(mut windows: Query<&mut Window>, config: Res<Configuration>) {
let window = &mut windows.single_mut();

window.mode = config.window().mode();
match window.mode {
WindowMode::BorderlessFullscreen => {
if cfg!(target_os = "macos") {
window.cursor.grab_mode = CursorGrabMode::None;
} else {
window.cursor.grab_mode = CursorGrabMode::Confined;
}
}
_ => {
window.cursor.grab_mode = CursorGrabMode::None;
}
};
}

fn toggle_fullscreen(mut windows: Query<&mut Window>, keyboard_input: Res<Input<KeyCode>>) {
if keyboard_input.just_pressed(KeyCode::F11) {
let window = &mut windows.single_mut();
match window.mode {
WindowMode::BorderlessFullscreen => {
window.mode = WindowMode::Windowed;
window.cursor.grab_mode = CursorGrabMode::None;
}
_ => {
window.mode = WindowMode::BorderlessFullscreen;

if cfg!(target_os = "macos") {
window.cursor.grab_mode = CursorGrabMode::None;
} else {
window.cursor.grab_mode = CursorGrabMode::Confined;
}
}
};
}
}
10 changes: 10 additions & 0 deletions docs/src/conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ est as if all properties are missing.
number between `0.0` and `1.0`. If set to 0 sound effects will not play.
* `music_volume` (f32; default: `1.0`) – sets the music volume. It must be a finite
number between `0.0` and `1.0`. If set to 0 music will not play.
* `window` (object) – window configuration
* `mode` (string; default: `BorderlessFullscreen`) – window mode, see [Bevy
docs](https://docs.rs/bevy/0.12.1/bevy/window/enum.WindowMode.html#variants)
for more details. Possible values:
* `Windowed`
* `BorderlessFullscreen`
* `SizedFullscreen`
* `Fullscreen`

## Example Configuration

Expand All @@ -72,4 +80,6 @@ audio:
master_volume: 1.0
sound_volume: 1.0
music_volume: 1.0
window:
mode: BorderlessFullscreen
```
19 changes: 4 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::time::Duration;

use bevy::log::LogPlugin;
#[cfg(not(target_os = "macos"))]
use bevy::window::{CursorGrabMode, PrimaryWindow};
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
Expand Down Expand Up @@ -51,12 +49,15 @@ fn main() {
let _enter = span.enter();

app.insert_resource(Msaa::Sample4)
.add_plugins(ConfigPluginGroup)
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: "Digital Extinction".to_string(),
mode: WindowMode::BorderlessFullscreen,
// This mode is used only temporarily. Mode loaded
// from the config is set after the game fully loads.
mode: WindowMode::Windowed,
..Default::default()
}),
..default()
Expand All @@ -73,7 +74,6 @@ fn main() {
FrameTimeDiagnosticsPlugin,
GamePlugin,
))
.add_plugins(ConfigPluginGroup)
.add_plugins(GuiPluginGroup)
.add_plugins(LobbyClientPluginGroup)
.add_plugins(MenuPluginGroup)
Expand Down Expand Up @@ -104,16 +104,5 @@ struct GamePlugin;
impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.add_state_with_set::<AppState>();

#[cfg(not(target_os = "macos"))]
{
app.add_systems(OnEnter(AppState::AppLoading), cursor_grab_system);
}
}
}

#[cfg(not(target_os = "macos"))]
fn cursor_grab_system(mut window_query: Query<&mut Window, With<PrimaryWindow>>) {
let mut window = window_query.single_mut();
window.cursor.grab_mode = CursorGrabMode::Confined;
}
Loading