move cursor to center on unlock

This commit is contained in:
griffi-gh 2024-05-02 02:07:36 +02:00
parent a5fae8ad2b
commit ce5dd6f011

View file

@ -1,9 +1,7 @@
use shipyard::{AllStoragesView, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View}; use shipyard::{AllStoragesView, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View};
use crate::{events::InputDeviceEvent, rendering::Renderer}; use crate::{events::InputDeviceEvent, rendering::{Renderer, WindowSize}};
use winit::{ use winit::{
event::{DeviceEvent, ElementState, RawKeyEvent}, dpi::PhysicalPosition, event::{DeviceEvent, ElementState, RawKeyEvent}, keyboard::{KeyCode, PhysicalKey}, window::CursorGrabMode
keyboard::{KeyCode, PhysicalKey},
window::CursorGrabMode
}; };
#[derive(Unique)] #[derive(Unique)]
@ -43,6 +41,8 @@ pub fn lock_cursor_now(
pub fn debug_toggle_lock( pub fn debug_toggle_lock(
mut lock: UniqueViewMut<CursorLock>, mut lock: UniqueViewMut<CursorLock>,
device_events: View<InputDeviceEvent>, device_events: View<InputDeviceEvent>,
ren: NonSendSync<UniqueView<Renderer>>,
size: UniqueView<WindowSize>,
) { ) {
for evt in device_events.iter() { for evt in device_events.iter() {
if let DeviceEvent::Key(RawKeyEvent { if let DeviceEvent::Key(RawKeyEvent {
@ -50,6 +50,10 @@ pub fn debug_toggle_lock(
state: ElementState::Pressed, state: ElementState::Pressed,
}) = evt.event { }) = evt.event {
lock.0 = !lock.0; lock.0 = !lock.0;
if !lock.0 {
let center = PhysicalPosition::new(size.0.x as f64 / 2., size.0.y as f64 / 2.);
let _ = ren.window.set_cursor_position(center);
}
} }
} }
} }