From bab52f267a576f7c40043176d89c1c5a5bd7cdc4 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Fri, 25 Nov 2022 18:02:46 -0500 Subject: [PATCH] Fix grab mode --- crates/bevy_window/src/window.rs | 10 +++++----- crates/bevy_winit/src/lib.rs | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index ab76fa1dc379a0..9ed44e44fd349a 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -834,12 +834,12 @@ impl Window { .push(WindowCommand::SetAlwaysOnTop { always_on_top }); } /// Close the operating system window corresponding to this [`Window`]. - /// + /// /// This will also lead to this [`Window`] being removed from the /// [`Windows`] resource. /// /// If the default [`WindowPlugin`] is used, when no windows are - /// open, the [app will exit](bevy_app::AppExit). + /// open, the [app will exit](bevy_app::AppExit). /// To disable this behaviour, set `exit_on_all_closed` on the [`WindowPlugin`] /// to `false` /// @@ -869,7 +869,7 @@ impl Window { /// The "html canvas" element selector. /// /// If set, this selector will be used to find a matching html canvas element, - /// rather than creating a new one. + /// rather than creating a new one. /// Uses the [CSS selector format](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector). /// /// This value has no effect on non-web platforms. @@ -978,7 +978,7 @@ pub struct WindowDescriptor { pub decorations: bool, /// Sets whether the cursor is visible when the window has focus. pub cursor_visible: bool, - /// Sets whether and how the window grabs the cursor. + /// Sets whether or how the window grabs the cursor. pub cursor_grab_mode: CursorGrabMode, /// Sets whether or not the window listens for 'hits' of mouse activity over _this_ window. pub hittest: bool, @@ -996,7 +996,7 @@ pub struct WindowDescriptor { /// The "html canvas" element selector. /// /// If set, this selector will be used to find a matching html canvas element, - /// rather than creating a new one. + /// rather than creating a new one. /// Uses the [CSS selector format](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector). /// /// This value has no effect on non-web platforms. diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index dfcfa5c0cd76eb..749d2568825008 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -4,7 +4,7 @@ mod web_resize; mod winit_config; mod winit_windows; -use converters::convert_cursor_grab_mode; +use winit::window::CursorGrabMode; pub use winit_config::*; pub use winit_windows::*; @@ -136,9 +136,18 @@ fn change_window( } bevy_window::WindowCommand::SetCursorGrabMode { grab_mode } => { let window = winit_windows.get_window(id).unwrap(); - window - .set_cursor_grab(convert_cursor_grab_mode(grab_mode)) - .unwrap_or_else(|e| error!("Unable to un/grab cursor: {}", e)); + match grab_mode { + bevy_window::CursorGrabMode::None => { + window.set_cursor_grab(CursorGrabMode::None) + } + bevy_window::CursorGrabMode::Confined => window + .set_cursor_grab(CursorGrabMode::Confined) + .or_else(|_e| window.set_cursor_grab(CursorGrabMode::Locked)), + bevy_window::CursorGrabMode::Locked => window + .set_cursor_grab(CursorGrabMode::Locked) + .or_else(|_e| window.set_cursor_grab(CursorGrabMode::Confined)), + } + .unwrap_or_else(|e| error!("Unable to un/grab cursor: {}", e)); } bevy_window::WindowCommand::SetCursorVisibility { visible } => { let window = winit_windows.get_window(id).unwrap();