From 7197cff59c24be756196f3b7fe60930bd9325d86 Mon Sep 17 00:00:00 2001 From: VitalyR Date: Wed, 4 Jan 2023 23:00:12 +0000 Subject: [PATCH] fix cursor grab issue (#7010) # Objective - Set the cursor grab mode after the window is built, fix #7007, clean some conversion code. ## Solution - Set the cursor grab mode after the window is built. --- crates/bevy_winit/src/converters.rs | 11 +---------- crates/bevy_winit/src/winit_windows.rs | 20 +++++++------------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/crates/bevy_winit/src/converters.rs b/crates/bevy_winit/src/converters.rs index 1702d2db96af36..6f2f23e785761c 100644 --- a/crates/bevy_winit/src/converters.rs +++ b/crates/bevy_winit/src/converters.rs @@ -5,7 +5,7 @@ use bevy_input::{ ButtonState, }; use bevy_math::Vec2; -use bevy_window::{CursorGrabMode, CursorIcon}; +use bevy_window::CursorIcon; pub fn convert_keyboard_input(keyboard_input: &winit::event::KeyboardInput) -> KeyboardInput { KeyboardInput { @@ -266,12 +266,3 @@ pub fn convert_cursor_icon(cursor_icon: CursorIcon) -> winit::window::CursorIcon CursorIcon::RowResize => winit::window::CursorIcon::RowResize, } } - -/// Map [`bevy_window::CursorGrabMode`] to [`winit::window::CursorGrabMode`]. -pub fn convert_cursor_grab_mode(mode: CursorGrabMode) -> winit::window::CursorGrabMode { - match mode { - CursorGrabMode::None => winit::window::CursorGrabMode::None, - CursorGrabMode::Confined => winit::window::CursorGrabMode::Confined, - CursorGrabMode::Locked => winit::window::CursorGrabMode::Locked, - } -} diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 5967d08d9caa4a..38ff59d348daa8 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -1,4 +1,3 @@ -use crate::converters::convert_cursor_grab_mode; use bevy_math::{DVec2, IVec2}; use bevy_utils::HashMap; use bevy_window::{ @@ -165,16 +164,6 @@ impl WinitWindows { } } - // Do not set the grab mode on window creation if it's none, this can fail on mobile - if window_descriptor.cursor_grab_mode != CursorGrabMode::None { - match winit_window - .set_cursor_grab(convert_cursor_grab_mode(window_descriptor.cursor_grab_mode)) - { - Ok(_) | Err(winit::error::ExternalError::NotSupported(_)) => {} - Err(err) => Err(err).unwrap(), - } - } - winit_window.set_cursor_visible(window_descriptor.cursor_visible); self.window_id_to_winit.insert(window_id, winit_window.id()); @@ -207,7 +196,7 @@ impl WinitWindows { display_handle: winit_window.raw_display_handle(), }; self.windows.insert(winit_window.id(), winit_window); - Window::new( + let mut window = Window::new( window_id, window_descriptor, inner_size.width, @@ -215,7 +204,12 @@ impl WinitWindows { scale_factor, position, Some(raw_handle), - ) + ); + // Do not set the grab mode on window creation if it's none, this can fail on mobile + if window_descriptor.cursor_grab_mode != CursorGrabMode::None { + window.set_cursor_grab_mode(window_descriptor.cursor_grab_mode); + } + window } pub fn get_window(&self, id: WindowId) -> Option<&winit::window::Window> {