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 committed Jul 17, 2022
1 parent 5ace79f commit 7159438
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
41 changes: 26 additions & 15 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,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 @@ -195,8 +195,8 @@ pub enum WindowCommand {
SetDecorations {
decorations: bool,
},
SetCursorLockMode {
locked: bool,
SetCursorGrabMode {
mode: CursorGrabMode,
},
SetCursorIcon {
icon: CursorIcon,
Expand All @@ -222,6 +222,17 @@ pub enum WindowCommand {
Close,
}

/// 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)]
pub enum WindowMode {
Expand Down Expand Up @@ -261,7 +272,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 @@ -496,25 +507,25 @@ impl Window {
}

#[inline]
pub fn cursor_locked(&self) -> bool {
self.cursor_locked
pub fn cursor_grab_mode(&self) -> CursorGrabMode {
self.cursor_grab_mode
}

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 });
}

#[inline]
pub fn cursor_visible(&self) -> bool {
self.cursor_visible
}

pub fn set_cursor_visibility(&mut self, visibile_mode: bool) {
self.cursor_visible = visibile_mode;
pub fn set_cursor_visibility(&mut self, visible_mode: bool) {
self.cursor_visible = visible_mode;
self.command_queue.push(WindowCommand::SetCursorVisibility {
visible: visibile_mode,
visible: visible_mode,
});
}

Expand Down Expand Up @@ -663,8 +674,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 @@ -703,7 +714,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 @@ -123,10 +123,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 @@ -662,3 +662,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::LogicalSize;

use crate::winit_grab_mode;

#[derive(Debug, Default)]
pub struct WinitWindows {
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
Expand Down Expand Up @@ -124,11 +126,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 @@ -207,7 +207,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 @@ -224,7 +226,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 7159438

Please sign in to comment.