Skip to content

Commit

Permalink
Added basic mouse capture API (#679)
Browse files Browse the repository at this point in the history
Added basic cursor lock API
  • Loading branch information
alexb910 authored Oct 16, 2020
1 parent 7ba4584 commit d004bce
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,4 @@ required-features = ["bevy_winit"]
name = "assets_wasm"
path = "examples/wasm/assets_wasm.rs"
required-features = ["bevy_winit"]

35 changes: 35 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub struct Window {
vsync: bool,
resizable: bool,
decorations: bool,
cursor_visible: bool,
cursor_locked: bool,
mode: WindowMode,
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,
Expand Down Expand Up @@ -68,6 +70,12 @@ pub enum WindowCommand {
SetDecorations {
decorations: bool,
},
SetCursorLockMode {
locked: bool,
},
SetCursorVisibility {
visible: bool,
},
}

/// Defines the way a window is displayed
Expand All @@ -92,6 +100,8 @@ impl Window {
vsync: window_descriptor.vsync,
resizable: window_descriptor.resizable,
decorations: window_descriptor.decorations,
cursor_visible: window_descriptor.cursor_visible,
cursor_locked: window_descriptor.cursor_locked,
mode: window_descriptor.mode,
#[cfg(target_arch = "wasm32")]
canvas: window_descriptor.canvas.clone(),
Expand Down Expand Up @@ -165,6 +175,27 @@ impl Window {
.push(WindowCommand::SetDecorations { decorations });
}

pub fn cursor_locked(&self) -> bool {
self.cursor_locked
}

pub fn set_cursor_lock_mode(&mut self, lock_mode: bool) {
self.cursor_locked = lock_mode;
self.command_queue
.push(WindowCommand::SetCursorLockMode { locked: lock_mode });
}

pub fn cursor_visible(&self) -> bool {
self.cursor_visible
}

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

pub fn mode(&self) -> WindowMode {
self.mode
}
Expand All @@ -191,6 +222,8 @@ pub struct WindowDescriptor {
pub vsync: bool,
pub resizable: bool,
pub decorations: bool,
pub cursor_visible: bool,
pub cursor_locked: bool,
pub mode: WindowMode,
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,
Expand All @@ -210,6 +243,8 @@ impl Default for WindowDescriptor {
vsync: true,
resizable: true,
decorations: true,
cursor_locked: false,
cursor_visible: true,
mode: WindowMode::Windowed,
#[cfg(target_arch = "wasm32")]
canvas: None,
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ fn change_window(_: &mut World, resources: &mut Resources) {
let window = winit_windows.get_window(id).unwrap();
window.set_decorations(decorations);
}
bevy_window::WindowCommand::SetCursorLockMode { locked } => {
let window = winit_windows.get_window(id).unwrap();
window.set_cursor_grab(locked).unwrap();
}
bevy_window::WindowCommand::SetCursorVisibility { visible } => {
let window = winit_windows.get_window(id).unwrap();
window.set_cursor_visible(visible);
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ impl WinitWindows {

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

winit_window
.set_cursor_grab(window.cursor_locked())
.unwrap();
winit_window.set_cursor_visible(window.cursor_visible());

self.window_id_to_winit
.insert(window.id(), winit_window.id());
self.winit_to_window_id
Expand Down
10 changes: 10 additions & 0 deletions examples/window/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() {
})
.add_default_plugins()
.add_system(change_title.system())
.add_system(toggle_cursor.system())
.run();
}

Expand All @@ -24,3 +25,12 @@ fn change_title(time: Res<Time>, mut windows: ResMut<Windows>) {
time.seconds_since_startup.round()
));
}

/// This system toggles the cursor's visibility when the space bar is pressed
fn toggle_cursor(input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
let window = windows.get_primary_mut().unwrap();
if input.just_pressed(KeyCode::Space) {
window.set_cursor_lock_mode(!window.cursor_locked());
window.set_cursor_visibility(!window.cursor_visible());
}
}

0 comments on commit d004bce

Please sign in to comment.