From af0732fdc491a0ff123cda9fca5b88db9c50f46f Mon Sep 17 00:00:00 2001 From: copygirl Date: Mon, 5 Jan 2026 07:32:05 +0100 Subject: [PATCH] Fix picking `PointerLocation` not being centered --- client/src/input/cursor_grab.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/client/src/input/cursor_grab.rs b/client/src/input/cursor_grab.rs index 571e777..052a2b8 100644 --- a/client/src/input/cursor_grab.rs +++ b/client/src/input/cursor_grab.rs @@ -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 {