From a602f50c2c3d8bc963a4224110041dd37451e7ce Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Sun, 18 Oct 2020 13:20:42 -0700 Subject: [PATCH] small input example improvements (#701) --- Cargo.toml | 4 +-- crates/bevy_input/src/touch.rs | 16 +++++------ examples/input/gamepad_input.rs | 20 +++++++------- examples/input/keyboard_input_events.rs | 3 +-- examples/input/mouse_input_events.rs | 3 +-- examples/input/touch_input.rs | 35 ++++++++++++------------- examples/input/touch_input_events.rs | 19 ++++++++++++++ examples/input/touch_input_highlevel.rs | 28 -------------------- 8 files changed, 56 insertions(+), 72 deletions(-) create mode 100644 examples/input/touch_input_events.rs delete mode 100644 examples/input/touch_input_highlevel.rs diff --git a/Cargo.toml b/Cargo.toml index 580cbeac0d..a38db3beae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -235,8 +235,8 @@ name = "touch_input" path = "examples/input/touch_input.rs" [[example]] -name = "touch_input_highlevel" -path = "examples/input/touch_input_highlevel.rs" +name = "touch_input_events" +path = "examples/input/touch_input_events.rs" [[example]] name = "scene" diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 8a92a10be9..17394bead1 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -98,22 +98,18 @@ pub fn touch_screen_input_system( mut touch_state: ResMut, touch_input_events: Res>, ) { - touch_state.just_pressed.clear(); - - let released_touch_ids: HashSet<_> = touch_state.just_released.iter().cloned().collect(); - let cancelled_touch_ids: HashSet<_> = touch_state.just_released.iter().cloned().collect(); - - touch_state.just_released.clear(); - touch_state.just_cancelled.clear(); - - for released_id in released_touch_ids { + let touch_state = &mut *touch_state; + for released_id in touch_state.just_released.iter() { touch_state.active_touches.remove(&released_id); } - for cancelled_id in cancelled_touch_ids { + for cancelled_id in touch_state.just_cancelled.iter() { touch_state.active_touches.remove(&cancelled_id); } + touch_state.just_pressed.clear(); + touch_state.just_cancelled.clear(); + for event in state.touch_event_reader.iter(&touch_input_events) { let active_touch = touch_state.active_touches.get(&event.id); match event.phase { diff --git a/examples/input/gamepad_input.rs b/examples/input/gamepad_input.rs index dfa146c242..5ddd40b2f8 100644 --- a/examples/input/gamepad_input.rs +++ b/examples/input/gamepad_input.rs @@ -9,25 +9,25 @@ fn main() { .add_system(connection_system.system()) .add_system(button_system.system()) .add_system(axis_system.system()) - .add_resource(Lobby::default()) + .init_resource::() .run(); } #[derive(Default)] -struct Lobby { - gamepad: HashSet, +struct GamepadLobby { + gamepads: HashSet, gamepad_event_reader: EventReader, } -fn connection_system(mut lobby: ResMut, gamepad_event: Res>) { +fn connection_system(mut lobby: ResMut, gamepad_event: Res>) { for event in lobby.gamepad_event_reader.iter(&gamepad_event) { match &event { GamepadEvent(gamepad, GamepadEventType::Connected) => { - lobby.gamepad.insert(*gamepad); + lobby.gamepads.insert(*gamepad); println!("Connected {:?}", gamepad); } GamepadEvent(gamepad, GamepadEventType::Disconnected) => { - lobby.gamepad.remove(gamepad); + lobby.gamepads.remove(gamepad); println!("Disconnected {:?}", gamepad); } } @@ -35,7 +35,7 @@ fn connection_system(mut lobby: ResMut, gamepad_event: Res, + lobby: Res, inputs: Res>, button_axes: Res>, ) { @@ -60,7 +60,7 @@ fn button_system( GamepadButtonType::DPadLeft, GamepadButtonType::DPadRight, ]; - for gamepad in manager.gamepad.iter() { + for gamepad in lobby.gamepads.iter() { for button_type in button_types.iter() { if inputs.just_pressed(GamepadButton(*gamepad, *button_type)) { println!("Pressed {:?}", GamepadButton(*gamepad, *button_type)); @@ -80,7 +80,7 @@ fn button_system( } } -fn axis_system(manager: Res, axes: Res>) { +fn axis_system(lobby: Res, axes: Res>) { let axis_types = [ GamepadAxisType::LeftStickX, GamepadAxisType::LeftStickY, @@ -91,7 +91,7 @@ fn axis_system(manager: Res, axes: Res>) { GamepadAxisType::DPadX, GamepadAxisType::DPadY, ]; - for gamepad in manager.gamepad.iter() { + for gamepad in lobby.gamepads.iter() { for axis_type in axis_types.iter() { if let Some(value) = axes.get(&GamepadAxis(*gamepad, *axis_type)) { if value_check(value) { diff --git a/examples/input/keyboard_input_events.rs b/examples/input/keyboard_input_events.rs index cc41babfc9..a42a8626c5 100644 --- a/examples/input/keyboard_input_events.rs +++ b/examples/input/keyboard_input_events.rs @@ -3,7 +3,6 @@ use bevy::{input::keyboard::KeyboardInput, prelude::*}; fn main() { App::build() .add_default_plugins() - .init_resource::() .add_system(print_keyboard_event_system.system()) .run(); } @@ -15,7 +14,7 @@ struct State { /// This system prints out all keyboard events as they come in fn print_keyboard_event_system( - mut state: ResMut, + mut state: Local, keyboard_input_events: Res>, ) { for event in state.event_reader.iter(&keyboard_input_events) { diff --git a/examples/input/mouse_input_events.rs b/examples/input/mouse_input_events.rs index 32941fccff..4ec827a1d0 100644 --- a/examples/input/mouse_input_events.rs +++ b/examples/input/mouse_input_events.rs @@ -7,7 +7,6 @@ use bevy::{ fn main() { App::build() .add_default_plugins() - .init_resource::() .add_system(print_mouse_events_system.system()) .run(); } @@ -22,7 +21,7 @@ struct State { /// This system prints out all mouse events as they come in fn print_mouse_events_system( - mut state: ResMut, + mut state: Local, mouse_button_input_events: Res>, mouse_motion_events: Res>, cursor_moved_events: Res>, diff --git a/examples/input/touch_input.rs b/examples/input/touch_input.rs index ef923bf439..29eb79748e 100644 --- a/examples/input/touch_input.rs +++ b/examples/input/touch_input.rs @@ -8,28 +8,27 @@ fn main() { } fn touch_system(touches: Res) { - for touch in touches.iter() { + for touch in touches.iter_just_pressed() { println!( - "active touch: {} {} {} {}", - touch.id, touch.position, touch.previous_position, touch.start_position + "just pressed touch with id: {:?}, at: {:?}", + touch.id, touch.position ); + } - if touches.just_pressed(touch.id) { - println!( - "just pressed touch with id: {:?}, at: {:?}", - touch.id, touch.position - ); - } + for touch in touches.iter_just_released() { + println!( + "just released touch with id: {:?}, at: {:?}", + touch.id, touch.position + ); + } - if touches.just_released(touch.id) { - println!( - "just released touch with id: {:?}, at: {:?}", - touch.id, touch.position - ); - } + for touch in touches.iter_just_cancelled() { + println!("cancelled touch with id: {:?}", touch.id); + } - if touches.just_cancelled(touch.id) { - println!("cancelled touch with id: {:?}", touch.id); - } + // you can also iterate all current touches and retrieve their state like this: + for touch in touches.iter() { + println!("active touch: {:?}", touch); + println!(" just_pressed: {}", touches.just_pressed(touch.id)); } } diff --git a/examples/input/touch_input_events.rs b/examples/input/touch_input_events.rs new file mode 100644 index 0000000000..6ec2c73217 --- /dev/null +++ b/examples/input/touch_input_events.rs @@ -0,0 +1,19 @@ +use bevy::{input::touch::*, prelude::*}; + +fn main() { + App::build() + .add_default_plugins() + .add_system(touch_event_system.system()) + .run(); +} + +#[derive(Default)] +struct State { + event_reader: EventReader, +} + +fn touch_event_system(mut state: Local, touch_events: Res>) { + for event in state.event_reader.iter(&touch_events) { + println!("{:?}", event); + } +} diff --git a/examples/input/touch_input_highlevel.rs b/examples/input/touch_input_highlevel.rs deleted file mode 100644 index 1b7fd9c85e..0000000000 --- a/examples/input/touch_input_highlevel.rs +++ /dev/null @@ -1,28 +0,0 @@ -use bevy::{input::touch::*, prelude::*}; - -fn main() { - App::build() - .add_default_plugins() - .add_system(touch_system.system()) - .run(); -} - -fn touch_system(touches: Res) { - for touch in touches.iter_just_pressed() { - println!( - "just pressed touch with id: {:?}, at: {:?}", - touch.id, touch.position - ); - } - - for touch in touches.iter_just_released() { - println!( - "just released touch with id: {:?}, at: {:?}", - touch.id, touch.position - ); - } - - for touch in touches.iter_just_cancelled() { - println!("cancelled touch with id: {:?}", touch.id); - } -}