forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmouse_grab.rs
29 lines (26 loc) · 820 Bytes
/
mouse_grab.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//! Demonstrates how to grab and hide the mouse cursor.
use bevy::prelude::*;
use bevy::window::CursorGrabMode;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_system(grab_mouse)
.run();
}
// This system grabs the mouse when the left mouse button is pressed
// and releases it when the escape key is pressed
fn grab_mouse(
mut windows: ResMut<Windows>,
mouse: Res<Input<MouseButton>>,
key: Res<Input<KeyCode>>,
) {
let window = windows.primary_mut();
if mouse.just_pressed(MouseButton::Left) {
window.set_cursor_visibility(false);
window.set_cursor_grab_mode(CursorGrabMode::Locked);
}
if key.just_pressed(KeyCode::Escape) {
window.set_cursor_visibility(true);
window.set_cursor_grab_mode(CursorGrabMode::None);
}
}