bevy_fps/src/ui/window.rs

46 lines
1.3 KiB
Rust

use bevy::prelude::*;
use bevy::window::{CursorGrabMode, PrimaryWindow, WindowResolution};
use super::cursor::CursorGrabState;
pub struct WindowPlugin;
impl Plugin for WindowPlugin
{
fn build(&self, app: &mut App)
{
app.add_systems(PreStartup, setup_window);
app.add_systems(OnEnter(CursorGrabState(true)), on_cursor_grab_toggled);
app.add_systems(OnEnter(CursorGrabState(false)), on_cursor_grab_toggled);
}
}
fn setup_window(
mut window: Query<&mut Window, With<PrimaryWindow>>,
) {
let mut window = window.single_mut();
window.title = "Bevy FPS Game".to_string();
window.resolution = WindowResolution::new(1920.0, 1080.0);
window.position = WindowPosition::Centered(MonitorSelection::Primary);
}
fn on_cursor_grab_toggled(
mut window: Query<&mut Window, With<PrimaryWindow>>,
cursor_state: Res<State<CursorGrabState>>,
) {
let mut window = window.single_mut();
match cursor_state.get()
{
CursorGrabState(true) => {
window.cursor_options.visible = false;
window.cursor_options.grab_mode = CursorGrabMode::Locked;
}
CursorGrabState(false) => {
window.cursor_options.visible = true;
window.cursor_options.grab_mode = CursorGrabMode::None;
}
}
}