mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 22:50:19 +00:00
1f97717a3d
# Objective - Resolves #10853 ## Solution - ~~Changed the name of `Input` struct to `PressableInput`.~~ - Changed the name of `Input` struct to `ButtonInput`. ## Migration Guide - Breaking Change: Users need to rename `Input` to `ButtonInput` in their projects.
30 lines
823 B
Rust
30 lines
823 B
Rust
//! Demonstrates how to grab and hide the mouse cursor.
|
|
|
|
use bevy::{prelude::*, window::CursorGrabMode};
|
|
|
|
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 windows: Query<&mut Window>,
|
|
mouse: Res<ButtonInput<MouseButton>>,
|
|
key: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
let mut window = windows.single_mut();
|
|
|
|
if mouse.just_pressed(MouseButton::Left) {
|
|
window.cursor.visible = false;
|
|
window.cursor.grab_mode = CursorGrabMode::Locked;
|
|
}
|
|
|
|
if key.just_pressed(KeyCode::Escape) {
|
|
window.cursor.visible = true;
|
|
window.cursor.grab_mode = CursorGrabMode::None;
|
|
}
|
|
}
|