From 5909474e21e48de4e91428256a24a69b203c2504 Mon Sep 17 00:00:00 2001 From: Emerson MX Date: Sat, 5 Mar 2022 12:58:18 -0300 Subject: [PATCH 1/4] Add mouse grab example --- Cargo.toml | 4 ++++ examples/README.md | 1 + examples/input/mouse_grab.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 examples/input/mouse_grab.rs diff --git a/Cargo.toml b/Cargo.toml index b14b51b22ef91..783e061c2eaaf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/README.md b/examples/README.md index 340918cd1e443..292394da087d1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 `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 diff --git a/examples/input/mouse_grab.rs b/examples/input/mouse_grab.rs new file mode 100644 index 0000000000000..4156d622216e3 --- /dev/null +++ b/examples/input/mouse_grab.rs @@ -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 +// when press escape key: +fn grab_mouse( + mut windows: ResMut, + mouse: Res>, + key: Res>, +) { + 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); + } +} From 391b475d0df1897ff8165f38d29c49b1409dc068 Mon Sep 17 00:00:00 2001 From: Emerson MX Date: Sat, 5 Mar 2022 17:07:30 -0300 Subject: [PATCH 2/4] Update examples/README.md Co-authored-by: Alice Cecile --- examples/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index 292394da087d1..390d1e43f6f74 100644 --- a/examples/README.md +++ b/examples/README.md @@ -203,7 +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 +`mouse_grab` | [`input/mouse_grab.rs`](./input/mouse_grab.rs) | Demonstrates how to grab the mouse, locking the cursor to the app's screen `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 From 4ea9ac429cc8ddbcffc6f4efc74e29e095cb2d66 Mon Sep 17 00:00:00 2001 From: Emerson MX Date: Tue, 8 Mar 2022 13:54:48 -0300 Subject: [PATCH 3/4] Update examples/input/mouse_grab.rs Co-authored-by: James Liu --- examples/input/mouse_grab.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/input/mouse_grab.rs b/examples/input/mouse_grab.rs index 4156d622216e3..dcb46c898579a 100644 --- a/examples/input/mouse_grab.rs +++ b/examples/input/mouse_grab.rs @@ -7,7 +7,8 @@ fn main() { .run(); } -// This system grab the mouse when you press the left mouse button and release +// This system grabs the mouse when the left mouse button is pressed +// and releases it when the escape key is pressed // when press escape key: fn grab_mouse( mut windows: ResMut, From cfe629c18cdf62edd87fea62e0e90ea99c486efa Mon Sep 17 00:00:00 2001 From: Emerson MX Date: Tue, 8 Mar 2022 14:07:33 -0300 Subject: [PATCH 4/4] Update examples/input/mouse_grab.rs Co-authored-by: Alice Cecile --- examples/input/mouse_grab.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/input/mouse_grab.rs b/examples/input/mouse_grab.rs index dcb46c898579a..7b125d205db48 100644 --- a/examples/input/mouse_grab.rs +++ b/examples/input/mouse_grab.rs @@ -9,7 +9,6 @@ fn main() { // This system grabs the mouse when the left mouse button is pressed // and releases it when the escape key is pressed -// when press escape key: fn grab_mouse( mut windows: ResMut, mouse: Res>,