From d13b478ca2e7aacff6bdab2c17a39df1b23b2f13 Mon Sep 17 00:00:00 2001 From: levzzz <86072227+levzzz5154@users.noreply.github.com> Date: Tue, 27 Oct 2026 17:44:55 +0500 Subject: [PATCH 1/3] ui: the mouse position is the centre whenever the grab state changes `MouseHandler.grabMouse` and `MouseHandler.releaseMouse` both put the mouse at the window's centre, so a screen opened from gameplay starts with the cursor in the middle. Horizon centred it when the pointer was released, but not when it was grabbed again, so closing a screen left the cursor where the screen had it. The rule is one function now, `mouse_position_after_grab_change`, and the game applies it on both sides of the grab state change. Fixes #1925 Co-Authored-By: minimax-m3.1-flash (Claude Code) --- crates/horizon-client/src/game.rs | 16 ++++++++++-- crates/horizon-client/src/mouse.rs | 41 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 37b0067c9..ced3d3903 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -5345,6 +5345,8 @@ impl Game<'_> { let warp = |window: &Window| { let _ = window.set_cursor_position(centre); }; + let scale = HudBatch::new(size).scale; + let was_grabbed = self.captured; if capture { self.mouse.set_ignore_first_move(); // macOS warps by re-associating the mouse with the pointer, @@ -5375,13 +5377,23 @@ impl Game<'_> { 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; } + // MouseHandler.grabMouse and releaseMouse: the mouse position is + // the window's centre whenever the pointer's grab state changes, so + // a screen opened from gameplay starts with the cursor in the middle + // and the game starts with it away from the screen's last spot. + if let Some(pos) = horizon_client::mouse::mouse_position_after_grab_change( + capture, + was_grabbed, + size, + scale, + ) { + self.cursor = pos; + } window.set_cursor_visible(!self.captured); } /// `GuiGraphicsExtractor.applyCursor` -> `Window.selectCursor` at the end diff --git a/crates/horizon-client/src/mouse.rs b/crates/horizon-client/src/mouse.rs index 1cee2be87..1941c310e 100644 --- a/crates/horizon-client/src/mouse.rs +++ b/crates/horizon-client/src/mouse.rs @@ -324,6 +324,24 @@ pub fn ease_in_out(t: f64) -> f64 { 0.5 - 0.5 * (std::f64::consts::PI * t).cos() } +/// `MouseHandler.grabMouse` / `releaseMouse`: the mouse position is the +/// window's centre whenever the pointer's grab state changes, so a screen +/// opened from gameplay starts with the cursor in the middle. A release of +/// an already free pointer (or a grab of an already grabbed one) leaves the +/// position where it is, as vanilla does. +pub fn mouse_position_after_grab_change( + grab: bool, + was_grabbed: bool, + size: [u32; 2], + scale: f32, +) -> Option<(f32, f32)> { + if grab == was_grabbed { + None + } else { + Some((size[0] as f32 / 2.0 / scale, size[1] as f32 / 2.0 / scale)) + } +} + #[cfg(test)] mod tests { use super::*; @@ -485,4 +503,27 @@ mod tests { assert_eq!(look.on_scroll(0.0, 0.4, false, 1.0), None); assert_eq!(look.on_scroll(0.0, 0.7, false, 1.0), Some((1, (0, 1)))); } + #[test] + fn the_mouse_position_is_the_centre_when_the_grab_changes() { + // A grab of a free pointer and a release of a grabbed one both put + // the position at the window's centre (`MouseHandler.grabMouse`, + // `MouseHandler.releaseMouse`). + assert_eq!( + mouse_position_after_grab_change(true, false, [1920, 1080], 1.0), + Some((960.0, 540.0)) + ); + assert_eq!( + mouse_position_after_grab_change(false, true, [1920, 1080], 2.0), + Some((480.0, 270.0)) + ); + // Without a change of the grab state the position stays where it is. + assert_eq!( + mouse_position_after_grab_change(false, false, [1920, 1080], 1.0), + None + ); + assert_eq!( + mouse_position_after_grab_change(true, true, [1920, 1080], 1.0), + None + ); + } } From 67e0d5406224623248e5bfb8ec4b8e16442e7521 Mon Sep 17 00:00:00 2001 From: levzzz <86072227+levzzz5154@users.noreply.github.com> Date: Tue, 27 Oct 2026 18:18:07 +0500 Subject: [PATCH 2/3] tests: give the capture fixture the mouse position helper The fixture compiles `Game::capture` on its own, so the helper it now calls has to be part of it: the test extracts it from the client, like it extracts the methods, and the call inside the fixture loses the crate path. The fixture also asserts the grab path now: grabbing the pointer puts the mouse at the window's centre, as `MouseHandler.grabMouse` does. Co-Authored-By: minimax-m3.1-flash (Claude Code) --- tests/test_world_input_capture.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/test_world_input_capture.py b/tests/test_world_input_capture.py index 5607c3684..e2d58ba41 100644 --- a/tests/test_world_input_capture.py +++ b/tests/test_world_input_capture.py @@ -27,6 +27,17 @@ def method(source, name): return source[start:end] +def function(source, name): + start = source.index(f"pub fn {name}(") + opening = source.index("{", start) + depth = 1 + end = opening + 1 + while depth: + depth += (source[end] == "{") - (source[end] == "}") + end += 1 + return source[start:end] + + class WorldInputCaptureTests(unittest.TestCase): def test_loading_completion_wiring(self): game = (ROOT / "crates/horizon-client/src/game.rs").read_text(encoding="utf-8") @@ -44,6 +55,7 @@ class WorldInputCaptureTests(unittest.TestCase): @unittest.skipUnless(shutil.which("rustc"), "requires Rust") def test_capture_lifecycle(self): source = (ROOT / "crates/horizon-client/src/game.rs").read_text(encoding="utf-8") + mouse = (ROOT / "crates/horizon-client/src/mouse.rs").read_text(encoding="utf-8") fixture = r''' use std::cell::Cell; #[derive(Clone, Copy, PartialEq)] @@ -99,6 +111,7 @@ impl Default for Game { renderer: None, cursor: (0.0, 0.0) } } } +HELPER impl Game { fn stop_mining(&mut self) { self.mining = false; } fn reset_inventory_clicks(&mut self) {} @@ -150,6 +163,7 @@ fn main() { assert!(!game.captured); // Focus alone does not resume gameplay. game.capture(true); // Click or resume action. assert!(game.captured); + assert_eq!(game.cursor, (213.5, 120.0)); // The grab puts the mouse at the centre. } let mut game = Game::default(); // On a server the chat is ChatScreen (#708): loading never grabs over it. @@ -183,7 +197,11 @@ fn main() { assert!(game.window.as_ref().unwrap().visible.get()); } ''' - fixture = fixture.replace("METHODS", method(source, "capture") + "\n" + + capture = method(source, "capture").replace( + "horizon_client::mouse::mouse_position_after_grab_change", + "mouse_position_after_grab_change") + fixture = fixture.replace("HELPER", function(mouse, "mouse_position_after_grab_change")) + fixture = fixture.replace("METHODS", capture + "\n" + method(source, "capture_loaded_world")) with tempfile.TemporaryDirectory(prefix="horizon-input-") as folder: folder = Path(folder) From 1d693cb3c33915c3892544951a64e8e89d5c7e73 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 27 Oct 2026 22:33:10 +0100 Subject: [PATCH 3/3] Read mouse.rs where the input crate keeps it now #2750 moved it to crates/horizon-client-input. Co-Authored-By: Claude Opus 5.5 --- tests/test_world_input_capture.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_world_input_capture.py b/tests/test_world_input_capture.py index e2d58ba41..9eff5b393 100644 --- a/tests/test_world_input_capture.py +++ b/tests/test_world_input_capture.py @@ -55,7 +55,7 @@ class WorldInputCaptureTests(unittest.TestCase): @unittest.skipUnless(shutil.which("rustc"), "requires Rust") def test_capture_lifecycle(self): source = (ROOT / "crates/horizon-client/src/game.rs").read_text(encoding="utf-8") - mouse = (ROOT / "crates/horizon-client/src/mouse.rs").read_text(encoding="utf-8") + mouse = (ROOT / "crates/horizon-client-input/src/mouse.rs").read_text(encoding="utf-8") fixture = r''' use std::cell::Cell; #[derive(Clone, Copy, PartialEq)]