properly deal w cursor locking
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
use std::{f32::consts::PI, time::Duration};
|
use std::{f32::consts::PI, time::Duration};
|
||||||
|
|
||||||
use nalgebra::Rotation3;
|
use nalgebra::Rotation3;
|
||||||
use winit::{dpi::LogicalPosition, keyboard::KeyCode as Key, window::CursorGrabMode};
|
use winit::{
|
||||||
|
dpi::{LogicalPosition, PhysicalPosition},
|
||||||
|
keyboard::KeyCode as Key,
|
||||||
|
window::CursorGrabMode,
|
||||||
|
};
|
||||||
|
|
||||||
use super::Client;
|
use super::Client;
|
||||||
|
|
||||||
@@ -12,24 +16,34 @@ impl Client<'_> {
|
|||||||
if input.just_pressed(Key::Escape) {
|
if input.just_pressed(Key::Escape) {
|
||||||
if let Some(window) = &self.window {
|
if let Some(window) = &self.window {
|
||||||
self.grabbed_cursor = !self.grabbed_cursor;
|
self.grabbed_cursor = !self.grabbed_cursor;
|
||||||
let mode = if self.grabbed_cursor {
|
if self.grabbed_cursor {
|
||||||
window.set_cursor_visible(false);
|
window.set_cursor_visible(false);
|
||||||
CursorGrabMode::Locked
|
window
|
||||||
|
.set_cursor_grab(CursorGrabMode::Locked)
|
||||||
|
.map(|_| {
|
||||||
|
self.keep_cursor = false;
|
||||||
|
})
|
||||||
|
.or_else(|_| {
|
||||||
|
self.keep_cursor = true;
|
||||||
|
window.set_cursor_grab(CursorGrabMode::Confined)
|
||||||
|
})
|
||||||
|
.expect("wah");
|
||||||
} else {
|
} else {
|
||||||
|
self.keep_cursor = false;
|
||||||
window.set_cursor_visible(true);
|
window.set_cursor_visible(true);
|
||||||
CursorGrabMode::None
|
window.set_cursor_grab(CursorGrabMode::None).expect("wah");
|
||||||
};
|
};
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
window.set_cursor_grab(mode).expect("wah");
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if self.grabbed_cursor {
|
if self.grabbed_cursor {
|
||||||
if let Some(window) = &self.window {
|
if let Some(window) = &self.window {
|
||||||
let size = window.inner_size();
|
if self.keep_cursor {
|
||||||
window
|
let size = window.inner_size();
|
||||||
.set_cursor_position(LogicalPosition::new(size.width / 2, size.height / 2))
|
window
|
||||||
.expect("wah");
|
.set_cursor_position(PhysicalPosition::new(size.width / 2, size.height / 2))
|
||||||
|
.expect("wah");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let delta = input.mouse_delta;
|
let delta = input.mouse_delta;
|
||||||
if delta.x != 0.0 {
|
if delta.x != 0.0 {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ pub struct Client<'a> {
|
|||||||
prev_frame: Instant,
|
prev_frame: Instant,
|
||||||
prev_update: Instant,
|
prev_update: Instant,
|
||||||
grabbed_cursor: bool,
|
grabbed_cursor: bool,
|
||||||
|
keep_cursor: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client<'_> {
|
impl Client<'_> {
|
||||||
@@ -41,6 +42,7 @@ impl Client<'_> {
|
|||||||
prev_update: Instant::now(),
|
prev_update: Instant::now(),
|
||||||
target: Instant::now(),
|
target: Instant::now(),
|
||||||
grabbed_cursor: false,
|
grabbed_cursor: false,
|
||||||
|
keep_cursor: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user