forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_grab.rs
More file actions
31 lines (27 loc) · 826 Bytes
/
mouse_grab.rs
File metadata and controls
31 lines (27 loc) · 826 Bytes
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
30
31
//! Demonstrates how to grab and hide the mouse cursor.
use bevy::{
prelude::*,
window::{CursorGrabMode, CursorOptions},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Update, 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 cursor_options: Single<&mut CursorOptions>,
mouse: Res<ButtonInput<MouseButton>>,
key: Res<ButtonInput<KeyCode>>,
) {
if mouse.just_pressed(MouseButton::Left) {
cursor_options.visible = false;
cursor_options.grab_mode = CursorGrabMode::Locked;
}
if key.just_pressed(KeyCode::Escape) {
cursor_options.visible = true;
cursor_options.grab_mode = CursorGrabMode::None;
}
}