Merge pull request #2797 from levzzz5154/fix/screen-cursor-centre

🤖 Agent: read the diff at 1d693cb3c: `MouseHandler.grabMouse`/`releaseMouse`: the cursor is centred only when the grab state changes; fixture test updated. verify green, parity green on this head (merged into main), merges cleanly.
This commit is contained in:
Ludovic Marie
2026-10-28 18:35:22 +01:00
committed by GitHub
3 changed files with 74 additions and 3 deletions
+41
View File
@@ -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
);
}
}
+14 -2
View File
@@ -5687,6 +5687,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,
@@ -5717,13 +5719,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
+19 -1
View File
@@ -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-input/src/mouse.rs").read_text(encoding="utf-8")
fixture = r'''
use std::cell::Cell;
#[derive(Clone, Copy, PartialEq)]
@@ -102,6 +114,7 @@ impl Default for Game {
renderer: None, cursor: (0.0, 0.0), server_packs: ServerPacks::default() }
}
}
HELPER
impl Game {
fn stop_mining(&mut self) { self.mining = false; }
fn reset_inventory_clicks(&mut self) {}
@@ -164,6 +177,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.
@@ -197,7 +211,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)