From f0aa88b0f6624b2b77420aedcd9e5c4330ef7bda Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 01:02:32 +0200 Subject: [PATCH] Centre the cursor when a screen opens from gameplay Releasing the pointer only ungrabbed it, so the cursor showed up wherever the system had it: on Wayland, where the locked pointer last stopped (often where the previous screen was closed), elsewhere wherever the hidden pointer had drifted. Opening the inventory, a chest, the pause menu, chat, a sign or a book could put it anywhere. Game::capture now follows MouseHandler.grabMouse/releaseMouse: both move the pointer to the window centre, and releasing also sets Horizon's own cursor (what every screen hit-tests and draws hover with) to that point. On Wayland the position can only be set as the locked pointer's hint, applied by the compositor as the lock goes, so it is set before ungrabbing; X11, Windows and macOS warp, so it is set again once the pointer is free. Releasing an already released pointer does nothing, so screen-to-screen changes keep the cursor where it is, as in vanilla, and nothing warps while the window is unfocused (GLFW's glfwSetCursorPos does the same), so losing focus never pulls the pointer back into the game. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 38 ++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7f0221119..00abc6a89 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3258,18 +3258,54 @@ impl Game<'_> { | Screen::WorldOptions ) || self.book.is_some()) } + /// `MouseHandler.grabMouse` / `releaseMouse`. Both put the pointer at + /// the window's centre, so a screen opened from gameplay (inventory, + /// chest, pause, chat, sign, book, death) starts with the cursor in the + /// middle instead of wherever the system last left it. Releasing when + /// already released does nothing, so moving from screen to screen keeps + /// the cursor where it is. Like GLFW's `glfwSetCursorPos`, nothing warps + /// while the window is unfocused (losing focus releases the pointer). fn capture(&mut self, capture: bool) { // The real pointer takes over from an agent's. self.agent_pointer = false; let Some(window) = &self.window else { return }; + let size = self.renderer.as_ref().map_or_else( + || window.inner_size().into(), + |r| [r.config.width, r.config.height], + ); + let centre = + winit::dpi::PhysicalPosition::new(f64::from(size[0]) / 2.0, f64::from(size[1]) / 2.0); + let warp = |window: &Window| { + let _ = window.set_cursor_position(centre); + }; if capture { self.mouse.set_ignore_first_move(); self.captured = window .set_cursor_grab(CursorGrabMode::Locked) .or_else(|_| window.set_cursor_grab(CursorGrabMode::Confined)) .is_ok(); + if self.captured && self.focused { + warp(window); + } } else { - let _ = window.set_cursor_grab(CursorGrabMode::None); + if self.captured { + // Wayland moves a pointer only as the locked pointer's + // position hint, which the compositor applies as the lock + // goes (sway and Hyprland do): set it before ungrabbing. + // X11, Windows and macOS warp, so warp again once free + // (Wayland refuses that one). + if self.focused { + warp(window); + } + let _ = window.set_cursor_grab(CursorGrabMode::None); + if self.focused { + warp(window); + } + let scale = HudBatch::new(size).scale; + self.cursor = (size[0] as f32 / 2.0 / scale, size[1] as f32 / 2.0 / scale); + } else { + let _ = window.set_cursor_grab(CursorGrabMode::None); + } self.captured = false; } window.set_cursor_visible(!self.captured);