bevy/examples/input/mouse_grab.rs
Mateusz Wachowiak 1f97717a3d
Rename Input to ButtonInput (#10859)
# 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.
2023-12-06 20:32:34 +00:00

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;
}
}