Skip to content

Commit

Permalink
Prep for next version of winit
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri authored and HackerFoo committed Aug 10, 2022
1 parent 31622dc commit 0fb8570
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
55 changes: 26 additions & 29 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub struct Window {
decorations: bool,
cursor_icon: CursorIcon,
cursor_visible: bool,
cursor_locked: bool,
cursor_grab_mode: CursorGrabMode,
physical_cursor_position: Option<DVec2>,
raw_window_handle: RawWindowHandleWrapper,
focused: bool,
Expand Down Expand Up @@ -248,9 +248,8 @@ pub enum WindowCommand {
SetDecorations {
decorations: bool,
},
/// Set whether or not the cursor's position is locked.
SetCursorLockMode {
locked: bool,
SetCursorGrabMode {
mode: CursorGrabMode,
},
/// Set the cursor's [`CursorIcon`].
SetCursorIcon {
Expand Down Expand Up @@ -285,7 +284,18 @@ pub enum WindowCommand {
Close,
}

/// Defines the way a window is displayed.
/// Defines how the cursor is grabbed.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CursorGrabMode {
/// No grabbing of the cursor is performed
None,
/// The cursor is confined to the window area.
Confined,
/// The cursor is locked inside the window area to a certain position.
Locked,
}

/// Defines the way a window is displayed
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowMode {
/// Creates a window that uses the given size.
Expand Down Expand Up @@ -326,7 +336,7 @@ impl Window {
resizable: window_descriptor.resizable,
decorations: window_descriptor.decorations,
cursor_visible: window_descriptor.cursor_visible,
cursor_locked: window_descriptor.cursor_locked,
cursor_grab_mode: window_descriptor.cursor_grab_mode,
cursor_icon: CursorIcon::Default,
physical_cursor_position: None,
raw_window_handle: RawWindowHandleWrapper::new(raw_window_handle),
Expand Down Expand Up @@ -594,21 +604,14 @@ 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 {
self.cursor_locked
pub fn cursor_grab_mode(&self) -> CursorGrabMode {
self.cursor_grab_mode
}
/// Set whether or not the cursor is locked.
///
/// This doesn't hide the cursor. For that, use [`set_cursor_visibility`](Window::set_cursor_visibility)
///
/// ## Platform-specific
///
/// - **`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) {
self.cursor_locked = lock_mode;

pub fn set_cursor_grab_mode(&mut self, grab_mode: CursorGrabMode) {
self.cursor_grab_mode = grab_mode;
self.command_queue
.push(WindowCommand::SetCursorLockMode { locked: lock_mode });
.push(WindowCommand::SetCursorGrabMode { mode: grab_mode });
}
/// Get whether or not the cursor is visible.
///
Expand All @@ -621,13 +624,7 @@ impl Window {
pub fn cursor_visible(&self) -> bool {
self.cursor_visible
}
/// Set whether or not the cursor is visible.
///
/// ## Platform-specific
///
/// - **`Windows`**, **`X11`**, and **`Wayland`**: The cursor is hidden only when inside the window. To stop the cursor from leaving the window, use [`set_cursor_lock_mode`](Window::set_cursor_lock_mode).
/// - **`macOS`**: The cursor is hidden only when the window is focused.
/// - **`iOS`** and **`Android`** do not have cursors

pub fn set_cursor_visibility(&mut self, visible_mode: bool) {
self.cursor_visible = visible_mode;
self.command_queue.push(WindowCommand::SetCursorVisibility {
Expand Down Expand Up @@ -816,8 +813,8 @@ pub struct WindowDescriptor {
pub decorations: bool,
/// 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,
/// Sets the [`CursorGrabMode`](crate::CursorGrabMode) for whether it is confined/won't move to the window area.
pub cursor_grab_mode: CursorGrabMode,
/// Sets the [`WindowMode`](crate::WindowMode).
pub mode: WindowMode,
/// Sets whether the background of the window should be transparent.
Expand Down Expand Up @@ -859,7 +856,7 @@ impl Default for WindowDescriptor {
present_mode: PresentMode::Fifo,
resizable: true,
decorations: true,
cursor_locked: false,
cursor_grab_mode: CursorGrabMode::None,
cursor_visible: true,
mode: WindowMode::Windowed,
transparent: false,
Expand Down
12 changes: 10 additions & 2 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ fn change_window(
let window = winit_windows.get_window(id).unwrap();
window.set_cursor_icon(converters::convert_cursor_icon(icon));
}
bevy_window::WindowCommand::SetCursorLockMode { locked } => {
bevy_window::WindowCommand::SetCursorGrabMode { mode } => {
let window = winit_windows.get_window(id).unwrap();
window
.set_cursor_grab(locked)
.set_cursor_grab(winit_grab_mode(mode))
.unwrap_or_else(|e| error!("Unable to un/grab cursor: {}", e));
}
bevy_window::WindowCommand::SetCursorVisibility { visible } => {
Expand Down Expand Up @@ -776,3 +776,11 @@ fn handle_create_window_events(
}
}
}

fn winit_grab_mode(mode: bevy_window::CursorGrabMode) -> winit::window::CursorGrabMode {
match mode {
bevy_window::CursorGrabMode::None => winit::window::CursorGrabMode::None,
bevy_window::CursorGrabMode::Confined => winit::window::CursorGrabMode::Confined,
bevy_window::CursorGrabMode::Locked => winit::window::CursorGrabMode::Locked,
}
}
18 changes: 11 additions & 7 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode};
use raw_window_handle::HasRawWindowHandle;
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalPosition};

use crate::winit_grab_mode;

#[derive(Debug, Default)]
pub struct WinitWindows {
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
Expand Down Expand Up @@ -155,11 +157,9 @@ impl WinitWindows {

let winit_window = winit_window_builder.build(event_loop).unwrap();

if window_descriptor.cursor_locked {
match winit_window.set_cursor_grab(true) {
Ok(_) | Err(winit::error::ExternalError::NotSupported(_)) => {}
Err(err) => Err(err).unwrap(),
}
match winit_window.set_cursor_grab(winit_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);
Expand Down Expand Up @@ -238,7 +238,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 @@ -255,7 +257,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 0fb8570

Please sign in to comment.