From 1aff709d27da15096e4740c64d7058495f751a2c Mon Sep 17 00:00:00 2001 From: Nathan Jeffords Date: Thu, 3 Dec 2020 12:39:03 -0800 Subject: [PATCH] Set cursor updates (#993) * update `Window::set_cursor_position` to take a `Vec2` instead of `i32`s this allows fractional coordinates to work correctly --- crates/bevy_window/src/window.rs | 7 +++---- crates/bevy_winit/src/lib.rs | 8 ++++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 4cbeadd634328..ac0fce771a19f 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -80,8 +80,7 @@ pub enum WindowCommand { visible: bool, }, SetCursorPosition { - x: i32, - y: i32, + position: Vec2, }, } @@ -237,9 +236,9 @@ impl Window { self.cursor_position } - pub fn set_cursor_position(&mut self, x: i32, y: i32) { + pub fn set_cursor_position(&mut self, position: Vec2) { self.command_queue - .push(WindowCommand::SetCursorPosition { x, y }); + .push(WindowCommand::SetCursorPosition { position }); } #[allow(missing_docs)] diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 54fb32dd31087..ca1a328c49051 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -94,10 +94,14 @@ fn change_window(_: &mut World, resources: &mut Resources) { let window = winit_windows.get_window(id).unwrap(); window.set_cursor_visible(visible); } - bevy_window::WindowCommand::SetCursorPosition { x, y } => { + bevy_window::WindowCommand::SetCursorPosition { position } => { let window = winit_windows.get_window(id).unwrap(); + let inner_size = window.inner_size().to_logical::(window.scale_factor()); window - .set_cursor_position(winit::dpi::LogicalPosition::new(x, y)) + .set_cursor_position(winit::dpi::LogicalPosition::new( + position.x, + inner_size.height - position.y, + )) .unwrap_or_else(|e| error!("Unable to set cursor position: {}", e)); } }