From 43393c22e5ded056ba545e8c666d4b6a11f7c759 Mon Sep 17 00:00:00 2001 From: vitalyr Date: Sat, 8 Oct 2022 03:51:56 +0800 Subject: [PATCH] try new winit, proof of concept for https://github.com/bevyengine/bevy/pull/5413#issuecomment-1271993397 --- crates/bevy_window/Cargo.toml | 3 ++- crates/bevy_window/src/window.rs | 13 +++++++------ crates/bevy_winit/Cargo.toml | 6 +++--- crates/bevy_winit/src/winit_windows.rs | 14 +++++++++----- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/crates/bevy_window/Cargo.toml b/crates/bevy_window/Cargo.toml index f1402447072de..5aced4c47fd20 100644 --- a/crates/bevy_window/Cargo.toml +++ b/crates/bevy_window/Cargo.toml @@ -21,7 +21,8 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.9.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" } # Used for close_on_esc bevy_input = { path = "../bevy_input", version = "0.9.0-dev" } -raw-window-handle = "0.4.2" +raw-window-handle = "0.4.3" +winit = { git = "https://github.com/rust-windowing/winit", default-features = false } # other serde = { version = "1.0", features = ["derive"], optional = true } diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 8ebf978efcfd6..10cef69e2fcbf 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -3,6 +3,7 @@ use bevy_math::{DVec2, IVec2, UVec2, Vec2}; use bevy_reflect::{FromReflect, Reflect}; use bevy_utils::{tracing::warn, Uuid}; use raw_window_handle::RawWindowHandle; +use winit::window::CursorGrabMode; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Reflect, FromReflect)] #[reflect_value(PartialEq, Hash)] @@ -204,7 +205,7 @@ pub struct Window { decorations: bool, cursor_icon: CursorIcon, cursor_visible: bool, - cursor_locked: bool, + cursor_locked: CursorGrabMode, physical_cursor_position: Option, raw_window_handle: RawWindowHandleWrapper, focused: bool, @@ -254,7 +255,7 @@ pub enum WindowCommand { }, /// Set whether or not the cursor's position is locked. SetCursorLockMode { - locked: bool, + locked: CursorGrabMode, }, /// Set the cursor's [`CursorIcon`]. SetCursorIcon { @@ -599,7 +600,7 @@ impl Window { /// - **`macOS`** doesn't support cursor lock, but most windowing plugins can emulate it. See [issue #4875](https://github.com/bevyengine/bevy/issues/4875#issuecomment-1153977546) for more information. /// - **`iOS/Android`** don't have cursors. #[inline] - pub fn cursor_locked(&self) -> bool { + pub fn cursor_locked(&self) -> CursorGrabMode { self.cursor_locked } /// Set whether or not the cursor is locked. @@ -610,7 +611,7 @@ impl Window { /// /// - **`macOS`** doesn't support cursor lock, but most windowing plugins can emulate it. See [issue #4875](https://github.com/bevyengine/bevy/issues/4875#issuecomment-1153977546) for more information. /// - **`iOS/Android`** don't have cursors. - pub fn set_cursor_lock_mode(&mut self, lock_mode: bool) { + pub fn set_cursor_lock_mode(&mut self, lock_mode: CursorGrabMode) { self.cursor_locked = lock_mode; self.command_queue .push(WindowCommand::SetCursorLockMode { locked: lock_mode }); @@ -837,7 +838,7 @@ pub struct WindowDescriptor { /// Sets whether the cursor is visible when the window has focus. pub cursor_visible: bool, /// Sets whether the window locks the cursor inside its borders when the window has focus. - pub cursor_locked: bool, + pub cursor_locked: CursorGrabMode, /// Sets the [`WindowMode`](crate::WindowMode). /// /// The monitor to go fullscreen on can be selected with the `monitor` field. @@ -882,7 +883,7 @@ impl Default for WindowDescriptor { present_mode: PresentMode::Fifo, resizable: true, decorations: true, - cursor_locked: false, + cursor_locked: CursorGrabMode::None, cursor_visible: true, mode: WindowMode::Windowed, transparent: false, diff --git a/crates/bevy_winit/Cargo.toml b/crates/bevy_winit/Cargo.toml index 5c7014ecbfa8a..3e30597d9dc76 100644 --- a/crates/bevy_winit/Cargo.toml +++ b/crates/bevy_winit/Cargo.toml @@ -22,12 +22,12 @@ bevy_window = { path = "../bevy_window", version = "0.9.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" } # other -winit = { version = "0.26.0", default-features = false } +winit = { git = "https://github.com/rust-windowing/winit", default-features = false } approx = { version = "0.5.0", default-features = false } -raw-window-handle = "0.4.2" +raw-window-handle = "0.4.3" [target.'cfg(target_arch = "wasm32")'.dependencies] -winit = { version = "0.26.0", default-features = false } +winit = { git = "https://github.com/rust-windowing/winit", default-features = false } wasm-bindgen = { version = "0.2" } web-sys = "0.3" crossbeam-channel = "0.5" diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 4467574874c61..4b5031561a26f 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -4,7 +4,7 @@ use bevy_window::{MonitorSelection, Window, WindowDescriptor, WindowId, WindowMo use raw_window_handle::HasRawWindowHandle; use winit::{ dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize}, - window::Fullscreen, + window::{CursorGrabMode, Fullscreen}, }; #[derive(Debug, Default)] @@ -158,8 +158,8 @@ impl WinitWindows { } } - if window_descriptor.cursor_locked { - match winit_window.set_cursor_grab(true) { + if window_descriptor.cursor_locked == CursorGrabMode::Locked { + match winit_window.set_cursor_grab(CursorGrabMode::Locked) { Ok(_) | Err(winit::error::ExternalError::NotSupported(_)) => {} Err(err) => Err(err).unwrap(), } @@ -241,7 +241,9 @@ pub fn get_fitting_videomode( match abs_diff(a.size().width, width).cmp(&abs_diff(b.size().width, width)) { Equal => { match abs_diff(a.size().height, height).cmp(&abs_diff(b.size().height, height)) { - Equal => b.refresh_rate().cmp(&a.refresh_rate()), + Equal => b + .refresh_rate_millihertz() + .cmp(&a.refresh_rate_millihertz()), default => default, } } @@ -258,7 +260,9 @@ pub fn get_best_videomode(monitor: &winit::monitor::MonitorHandle) -> winit::mon use std::cmp::Ordering::*; match b.size().width.cmp(&a.size().width) { Equal => match b.size().height.cmp(&a.size().height) { - Equal => b.refresh_rate().cmp(&a.refresh_rate()), + Equal => b + .refresh_rate_millihertz() + .cmp(&a.refresh_rate_millihertz()), default => default, }, default => default,