diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index 0208802318..e517bccf32 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -4,7 +4,7 @@ pub mod system; use bevy_app::{AppBuilder, AppPlugin}; use keyboard::KeyboardInput; -use mouse::{MouseButtonInput, MouseMotion}; +use mouse::{MouseButtonInput, MouseMotionInput}; #[derive(Default)] pub struct InputPlugin; @@ -13,6 +13,6 @@ impl AppPlugin for InputPlugin { fn build(&self, app: &mut AppBuilder) { app.add_event::() .add_event::() - .add_event::(); + .add_event::(); } } diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index 580a6b2682..88b1d2e765 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -15,6 +15,6 @@ pub enum MouseButton { } #[derive(Debug, Clone)] -pub struct MouseMotion { +pub struct MouseMotionInput { pub delta: (f64, f64), } diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 03a0b1e344..a9db4f4016 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -4,7 +4,7 @@ pub use winit_windows::*; use bevy_input::{ keyboard::KeyboardInput, - mouse::{MouseButtonInput, MouseMotion}, + mouse::{MouseButtonInput, MouseMotionInput}, }; use bevy_app::{App, AppBuilder, AppExit, AppPlugin, EventReader, Events, GetEventReader}; @@ -114,8 +114,8 @@ pub fn winit_runner(mut app: App) { event::Event::DeviceEvent { ref event, .. } => match event { DeviceEvent::MouseMotion { delta } => { let mut mouse_motion_events = - app.resources.get_mut::>().unwrap(); - mouse_motion_events.send(MouseMotion { delta: *delta }); + app.resources.get_mut::>().unwrap(); + mouse_motion_events.send(MouseMotionInput { delta: *delta }); } _ => {} }, diff --git a/examples/headless.rs b/examples/headless.rs index 09255ac468..3af3bfa04d 100644 --- a/examples/headless.rs +++ b/examples/headless.rs @@ -17,7 +17,7 @@ fn main() { .add_plugin(ScheduleRunnerPlugin { run_mode: RunMode::Once, }) - .add_system(hello_world.system()) + .add_system(hello_world_system.system()) .run(); // this app loops forever at 60 fps @@ -27,10 +27,10 @@ fn main() { wait: Some(Duration::from_secs_f64(1.0 / 60.0)), }, }) - .add_system(hello_world.system()) + .add_system(hello_world_system.system()) .run(); } -pub fn hello_world() { +fn hello_world_system() { println!("hello world"); } diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 1da236e622..09c20d74a1 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -3,10 +3,10 @@ use bevy::prelude::*; fn main() { App::build() .add_default_plugins() - .add_system(hello_world.system()) + .add_system(hello_world_system.system()) .run(); } -pub fn hello_world() { +fn hello_world_system() { println!("hello world"); } \ No newline at end of file diff --git a/examples/input_keyboard.rs b/examples/input_keyboard.rs index ed0fc2c76d..983f7efd2e 100644 --- a/examples/input_keyboard.rs +++ b/examples/input_keyboard.rs @@ -6,53 +6,60 @@ use bevy::{ fn main() { App::build() .add_default_plugins() - .add_system_init(move_on_input_system) + .add_resource_init::() + .add_system(collect_input_system.system()) + .add_system(move_system.system()) .add_startup_system(setup) .run(); } +#[derive(Resource)] +struct State { + event_reader: EventReader, + moving_right: bool, + moving_left: bool, +} + +/// adjusts move state based on keyboard input +fn collect_input_system( + mut state: ResourceMut, + keyboard_input_events: Resource>, +) { + for event in state.event_reader.iter(&keyboard_input_events) { + match event { + KeyboardInput { + virtual_key_code: Some(VirtualKeyCode::Left), + state: element_state, + .. + } => { + state.moving_left = element_state.is_pressed(); + } + KeyboardInput { + virtual_key_code: Some(VirtualKeyCode::Right), + state: element_state, + .. + } => { + state.moving_right = element_state.is_pressed(); + } + _ => {} + } + } +} + /// moves our cube left when the "left" key is pressed. moves it right when the "right" key is pressed -pub fn move_on_input_system(resources: &mut Resources) -> Box { - let mut keyboard_input_event_reader = resources.get_event_reader::(); - let mut moving_left = false; - let mut moving_right = false; - SystemBuilder::new("input_handler") - .read_resource::