Skip to content

Commit

Permalink
try new winit, proof of concept for bevyengine#5413 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyAnkh committed Oct 7, 2022
1 parent 6b75589 commit 43393c2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
3 changes: 2 additions & 1 deletion crates/bevy_window/Cargo.toml
Expand Up @@ -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 }
Expand Down
13 changes: 7 additions & 6 deletions crates/bevy_window/src/window.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<DVec2>,
raw_window_handle: RawWindowHandleWrapper,
focused: bool,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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 });
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_winit/Cargo.toml
Expand Up @@ -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"
Expand Down
14 changes: 9 additions & 5 deletions crates/bevy_winit/src/winit_windows.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(),
}
Expand Down Expand Up @@ -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,
}
}
Expand All @@ -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,
Expand Down

0 comments on commit 43393c2

Please sign in to comment.