|
|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
use bevy::prelude::*; |
|
|
|
|
|
|
|
|
|
use bevy::input::common_conditions::input_just_pressed; |
|
|
|
|
use bevy::picking::pointer::{PointerId, PointerLocation}; |
|
|
|
|
use bevy::window::{CursorGrabMode, CursorOptions}; |
|
|
|
|
|
|
|
|
|
use crate::Screen; |
|
|
|
|
@ -31,9 +32,12 @@ fn ungrab_cursor(mut cursor: Single<&mut CursorOptions>) { |
|
|
|
|
cursor.visible = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sets the cursor position to the center of the window,
|
|
|
|
|
/// so that once it is ungrabbed, it will reappear there.
|
|
|
|
|
fn center_cursor(mut window: Single<&mut Window>) { |
|
|
|
|
/// Sets the cursor (and pointer) position to the center of the
|
|
|
|
|
/// window, so that once it is ungrabbed, it will reappear there.
|
|
|
|
|
fn center_cursor( |
|
|
|
|
mut window: Single<&mut Window>, |
|
|
|
|
pointers: Query<(&PointerId, &mut PointerLocation)>, |
|
|
|
|
) { |
|
|
|
|
let center = window.resolution.size() / 2.; |
|
|
|
|
|
|
|
|
|
// On Wayland, since the cursor is locked into place, this only needs to be
|
|
|
|
|
@ -49,6 +53,16 @@ fn center_cursor(mut window: Single<&mut Window>) { |
|
|
|
|
// supported at all, so this would instead log a bunch of errors.
|
|
|
|
|
#[cfg(not(target_family = "wasm"))] |
|
|
|
|
window.set_cursor_position(Some(center)); |
|
|
|
|
|
|
|
|
|
// Update the `PointerLocation` for the mouse to be at the center
|
|
|
|
|
// of the window. Otherwise it might use the last known position.
|
|
|
|
|
for (id, mut pointer) in pointers { |
|
|
|
|
if id.is_mouse() { |
|
|
|
|
if let Some(location) = pointer.location.as_mut() { |
|
|
|
|
location.position = center; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn is_cursor_grabbed(cursor: Single<&CursorOptions>) -> bool { |
|
|
|
|
|