release cursor when alt-tabbing out of the game

This commit is contained in:
yuni 2024-10-10 01:51:31 +02:00
parent 7412eb082f
commit c0bb39958a

View file

@ -619,7 +619,9 @@ fn check_achievements(
}
fn handle_window_focus(
mut local_paused: Local<bool>,
mut settings: ResMut<Settings>,
mut q_window: Query<&mut Window, With<PrimaryWindow>>,
mut er_focus: EventReader<bevy::window::WindowFocused>,
mut physicstime: ResMut<Time<Physics>>,
) {
@ -627,9 +629,23 @@ fn handle_window_focus(
settings.window_focused = event.focused;
}
if settings.is_game_running() {
physicstime.unpause();
} else {
let paused = !settings.is_game_running();
if paused != *local_paused {
if paused {
physicstime.pause();
} else {
physicstime.unpause();
}
for mut window in &mut q_window {
window.cursor.grab_mode = if paused {
bevy::window::CursorGrabMode::None
} else {
bevy::window::CursorGrabMode::Locked
};
window.cursor.visible = paused;
}
*local_paused = paused;
}
}