2022-05-16 13:53:20 +00:00
|
|
|
//! Demonstrates how to grab and hide the mouse cursor.
|
|
|
|
|
2023-01-19 00:38:28 +00:00
|
|
|
use bevy::{prelude::*, window::CursorGrabMode};
|
2022-03-08 17:14:08 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins)
|
2023-03-18 01:45:34 +00:00
|
|
|
.add_systems(Update, grab_mouse)
|
2022-03-08 17:14:08 +00:00
|
|
|
.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(
|
2023-01-19 00:38:28 +00:00
|
|
|
mut windows: Query<&mut Window>,
|
2022-03-08 17:14:08 +00:00
|
|
|
mouse: Res<Input<MouseButton>>,
|
|
|
|
key: Res<Input<KeyCode>>,
|
|
|
|
) {
|
2023-01-19 00:38:28 +00:00
|
|
|
let mut window = windows.single_mut();
|
|
|
|
|
2022-03-08 17:14:08 +00:00
|
|
|
if mouse.just_pressed(MouseButton::Left) {
|
2023-01-19 00:38:28 +00:00
|
|
|
window.cursor.visible = false;
|
|
|
|
window.cursor.grab_mode = CursorGrabMode::Locked;
|
2022-03-08 17:14:08 +00:00
|
|
|
}
|
2023-01-19 00:38:28 +00:00
|
|
|
|
2022-03-08 17:14:08 +00:00
|
|
|
if key.just_pressed(KeyCode::Escape) {
|
2023-01-19 00:38:28 +00:00
|
|
|
window.cursor.visible = true;
|
|
|
|
window.cursor.grab_mode = CursorGrabMode::None;
|
2022-03-08 17:14:08 +00:00
|
|
|
}
|
|
|
|
}
|