From ca74c7799c990bc5744cae383f84bff217d351be Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 2 May 2024 02:07:36 +0200 Subject: [PATCH] move cursor to center on unlock --- kubi/src/cursor_lock.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kubi/src/cursor_lock.rs b/kubi/src/cursor_lock.rs index a595f01..11c572a 100644 --- a/kubi/src/cursor_lock.rs +++ b/kubi/src/cursor_lock.rs @@ -1,9 +1,7 @@ use shipyard::{AllStoragesView, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View}; -use crate::{events::InputDeviceEvent, rendering::Renderer}; +use crate::{events::InputDeviceEvent, rendering::{Renderer, WindowSize}}; use winit::{ - event::{DeviceEvent, ElementState, RawKeyEvent}, - keyboard::{KeyCode, PhysicalKey}, - window::CursorGrabMode + dpi::PhysicalPosition, event::{DeviceEvent, ElementState, RawKeyEvent}, keyboard::{KeyCode, PhysicalKey}, window::CursorGrabMode }; #[derive(Unique)] @@ -43,6 +41,8 @@ pub fn lock_cursor_now( pub fn debug_toggle_lock( mut lock: UniqueViewMut, device_events: View, + ren: NonSendSync>, + size: UniqueView, ) { for evt in device_events.iter() { if let DeviceEvent::Key(RawKeyEvent { @@ -50,6 +50,10 @@ pub fn debug_toggle_lock( state: ElementState::Pressed, }) = evt.event { 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); + } } } }