Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add mouse grab example #4114

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ path = "examples/input/mouse_input.rs"
name = "mouse_input_events"
path = "examples/input/mouse_input_events.rs"

[[example]]
name = "mouse_grab"
path = "examples/input/mouse_grab.rs"

[[example]]
name = "touch_input"
path = "examples/input/touch_input.rs"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Example | File | Description
`keyboard_modifiers` | [`input/keyboard_modifiers.rs`](./input/keyboard_modifiers.rs) | Demonstrates using key modifiers (ctrl, shift)
`mouse_input` | [`input/mouse_input.rs`](./input/mouse_input.rs) | Demonstrates handling a mouse button press/release
`mouse_input_events` | [`input/mouse_input_events.rs`](./input/mouse_input_events.rs) | Prints out all mouse events (buttons, movement, etc.)
`mouse_grab` | [`input/mouse_grab.rs`](./input/mouse_grab.rs) | Demonstrates how to grab the mouse
emersonmx marked this conversation as resolved.
Show resolved Hide resolved
`touch_input` | [`input/touch_input.rs`](./input/touch_input.rs) | Displays touch presses, releases, and cancels
`touch_input_events` | [`input/touch_input_events.rs`](./input/touch_input_events.rs) | Prints out all touch inputs

Expand Down
26 changes: 26 additions & 0 deletions examples/input/mouse_grab.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use bevy::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_system(grab_mouse)
.run();
}

// This system grab the mouse when you press the left mouse button and release
emersonmx marked this conversation as resolved.
Show resolved Hide resolved
// when press escape key:
emersonmx marked this conversation as resolved.
Show resolved Hide resolved
emersonmx marked this conversation as resolved.
Show resolved Hide resolved
fn grab_mouse(
mut windows: ResMut<Windows>,
mouse: Res<Input<MouseButton>>,
key: Res<Input<KeyCode>>,
) {
let window = windows.get_primary_mut().unwrap();
if mouse.just_pressed(MouseButton::Left) {
window.set_cursor_visibility(false);
window.set_cursor_lock_mode(true);
}
if key.just_pressed(KeyCode::Escape) {
window.set_cursor_visibility(true);
window.set_cursor_lock_mode(false);
}
}