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(
|
2024-10-13 20:32:06 +00:00
|
|
|
mut window: Single<&mut Window>,
|
2023-12-06 20:32:34 +00:00
|
|
|
mouse: Res<ButtonInput<MouseButton>>,
|
|
|
|
key: Res<ButtonInput<KeyCode>>,
|
2022-03-08 17:14:08 +00:00
|
|
|
) {
|
|
|
|
if mouse.just_pressed(MouseButton::Left) {
|
2024-08-12 15:49:03 +00:00
|
|
|
window.cursor_options.visible = false;
|
|
|
|
window.cursor_options.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) {
|
2024-08-12 15:49:03 +00:00
|
|
|
window.cursor_options.visible = true;
|
|
|
|
window.cursor_options.grab_mode = CursorGrabMode::None;
|
2022-03-08 17:14:08 +00:00
|
|
|
}
|
|
|
|
}
|