Skip to content

Commit

Permalink
Add set_cursor_position method to Window (#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
smokku authored Nov 26, 2020
1 parent 86d724e commit b2c8295
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 9 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub enum WindowCommand {
SetCursorVisibility {
visible: bool,
},
SetCursorPosition {
x: i32,
y: i32,
},
}

/// Defines the way a window is displayed
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}
}
}
}
Expand Down

0 comments on commit b2c8295

Please sign in to comment.