diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 0b6c45bdff2ad..8ff34df074d9b 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -76,6 +76,10 @@ pub enum WindowCommand { SetCursorVisibility { visible: bool, }, + SetCursorPosition { + x: i32, + y: i32, + }, } /// Defines the way a window is displayed @@ -203,6 +207,11 @@ impl Window { }); } + pub fn set_cursor_position(&mut self, x: i32, y: i32) { + self.command_queue + .push(WindowCommand::SetCursorPosition { x, y }); + } + #[inline] pub fn mode(&self) -> WindowMode { self.mode diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index f5c05e8db6ee2..bfb595dc67021 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -12,7 +12,7 @@ pub use winit_windows::*; use bevy_app::{prelude::*, AppExit}; use bevy_ecs::{Resources, World}; use bevy_math::Vec2; -use bevy_utils::tracing::trace; +use bevy_utils::tracing::{error, trace}; use bevy_window::{ CreateWindow, CursorMoved, ReceivedCharacter, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows, @@ -86,12 +86,20 @@ fn change_window(_: &mut World, resources: &mut Resources) { } bevy_window::WindowCommand::SetCursorLockMode { locked } => { let window = winit_windows.get_window(id).unwrap(); - window.set_cursor_grab(locked).unwrap(); + window + .set_cursor_grab(locked) + .unwrap_or_else(|e| error!("Unable to un/grab cursor: {}", e)); } bevy_window::WindowCommand::SetCursorVisibility { visible } => { let window = winit_windows.get_window(id).unwrap(); window.set_cursor_visible(visible); } + bevy_window::WindowCommand::SetCursorPosition { x, y } => { + let window = winit_windows.get_window(id).unwrap(); + window + .set_cursor_position(winit::dpi::PhysicalPosition::new(x, y)) + .unwrap_or_else(|e| error!("Unable to set cursor position: {}", e)); + } } } }