mirror of
https://github.com/bevyengine/bevy
synced 2024-11-25 06:00:20 +00:00
small input example improvements (#701)
This commit is contained in:
parent
5acebed731
commit
a602f50c2c
8 changed files with 56 additions and 72 deletions
|
@ -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"
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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::<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);
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ fn connection_system(mut lobby: ResMut<Lobby>, gamepad_event: Res<Events<Gamepad
|
|||
}
|
||||
|
||||
fn button_system(
|
||||
manager: Res<Lobby>,
|
||||
lobby: Res<GamepadLobby>,
|
||||
inputs: Res<Input<GamepadButton>>,
|
||||
button_axes: Res<Axis<GamepadButton>>,
|
||||
) {
|
||||
|
@ -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<Lobby>, axes: Res<Axis<GamepadAxis>>) {
|
||||
fn axis_system(lobby: Res<GamepadLobby>, axes: Res<Axis<GamepadAxis>>) {
|
||||
let axis_types = [
|
||||
GamepadAxisType::LeftStickX,
|
||||
GamepadAxisType::LeftStickY,
|
||||
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
@ -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) {
|
||||
|
|
|
@ -7,7 +7,6 @@ use bevy::{
|
|||
fn main() {
|
||||
App::build()
|
||||
.add_default_plugins()
|
||||
.init_resource::<State>()
|
||||
.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<State>,
|
||||
mut state: Local<State>,
|
||||
mouse_button_input_events: Res<Events<MouseButtonInput>>,
|
||||
mouse_motion_events: Res<Events<MouseMotion>>,
|
||||
cursor_moved_events: Res<Events<CursorMoved>>,
|
||||
|
|
|
@ -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
examples/input/touch_input_events.rs
Normal file
19
examples/input/touch_input_events.rs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<Touches>) {
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue