Skip to content

Commit

Permalink
small input example improvements (bevyengine#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
cart authored and joshuajbouw committed Oct 24, 2020
1 parent 0c75197 commit 6517da7
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 72 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 6 additions & 10 deletions crates/bevy_input/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,18 @@ pub fn touch_screen_input_system(
mut touch_state: ResMut<Touches>,
touch_input_events: Res<Events<TouchInput>>,
) {
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 {
Expand Down
20 changes: 10 additions & 10 deletions examples/input/gamepad_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ fn main() {
.add_system(connection_system.system())
.add_system(button_system.system())
.add_system(axis_system.system())
.add_resource(Lobby::default())
.init_resource::<GamepadLobby>()
.run();
}

#[derive(Default)]
struct Lobby {
gamepad: HashSet<Gamepad>,
struct GamepadLobby {
gamepads: HashSet<Gamepad>,
gamepad_event_reader: EventReader<GamepadEvent>,
}

fn connection_system(mut lobby: ResMut<Lobby>, gamepad_event: Res<Events<GamepadEvent>>) {
fn connection_system(mut lobby: ResMut<GamepadLobby>, gamepad_event: Res<Events<GamepadEvent>>) {
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);
}
}
}
}

fn button_system(
manager: Res<Lobby>,
lobby: Res<GamepadLobby>,
inputs: Res<Input<GamepadButton>>,
button_axes: Res<Axis<GamepadButton>>,
) {
Expand All @@ -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));
Expand All @@ -80,7 +80,7 @@ fn button_system(
}
}

fn axis_system(manager: Res<Lobby>, axes: Res<Axis<GamepadAxis>>) {
fn axis_system(lobby: Res<GamepadLobby>, axes: Res<Axis<GamepadAxis>>) {
let axis_types = [
GamepadAxisType::LeftStickX,
GamepadAxisType::LeftStickY,
Expand All @@ -91,7 +91,7 @@ fn axis_system(manager: Res<Lobby>, axes: Res<Axis<GamepadAxis>>) {
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) {
Expand Down
3 changes: 1 addition & 2 deletions examples/input/keyboard_input_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use bevy::{input::keyboard::KeyboardInput, prelude::*};
fn main() {
App::build()
.add_default_plugins()
.init_resource::<State>()
.add_system(print_keyboard_event_system.system())
.run();
}
Expand All @@ -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<State>,
mut state: Local<State>,
keyboard_input_events: Res<Events<KeyboardInput>>,
) {
for event in state.event_reader.iter(&keyboard_input_events) {
Expand Down
3 changes: 1 addition & 2 deletions examples/input/mouse_input_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use bevy::{
fn main() {
App::build()
.add_default_plugins()
.init_resource::<State>()
.add_system(print_mouse_events_system.system())
.run();
}
Expand All @@ -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<State>,
mut state: Local<State>,
mouse_button_input_events: Res<Events<MouseButtonInput>>,
mouse_motion_events: Res<Events<MouseMotion>>,
cursor_moved_events: Res<Events<CursorMoved>>,
Expand Down
35 changes: 17 additions & 18 deletions examples/input/touch_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@ fn main() {
}

fn touch_system(touches: Res<Touches>) {
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));
}
}
19 changes: 19 additions & 0 deletions examples/input/touch_input_events.rs
Original file line number Diff line number Diff line change
@@ -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<TouchInput>,
}

fn touch_event_system(mut state: Local<State>, touch_events: Res<Events<TouchInput>>) {
for event in state.event_reader.iter(&touch_events) {
println!("{:?}", event);
}
}
28 changes: 0 additions & 28 deletions examples/input/touch_input_highlevel.rs

This file was deleted.

0 comments on commit 6517da7

Please sign in to comment.