break up input examples

This commit is contained in:
Carter Anderson 2020-06-04 23:49:36 -07:00
parent 5b6f24d6a2
commit ed561d7f70
5 changed files with 65 additions and 23 deletions

View file

@ -152,12 +152,20 @@ name = "ecs_guide"
path = "examples/ecs/ecs_guide.rs"
[[example]]
name = "input_mouse"
path = "examples/input/input_mouse.rs"
name = "mouse_input"
path = "examples/input/mouse_input.rs"
[[example]]
name = "input_keyboard"
path = "examples/input/input_keyboard.rs"
name = "mouse_input_events"
path = "examples/input/mouse_input_events.rs"
[[example]]
name = "keyboard_input"
path = "examples/input/keyboard_input.rs"
[[example]]
name = "keyboard_input_events"
path = "examples/input/keyboard_input_events.rs"
[[example]]
name = "load_scene"

View file

@ -1,5 +1,7 @@
use bevy::{input::keyboard::KeyCode, prelude::*};
use bevy_input::Input;
use bevy::{
input::{keyboard::KeyCode, Input},
prelude::*,
};
fn main() {
App::build()
@ -9,7 +11,7 @@ fn main() {
.run();
}
/// moves our cube left when the "left" key is pressed. moves it right when the "right" key is pressed
/// This system moves our cube left when the "left" key is pressed and moves it right when the "right" key is pressed
fn move_on_input(
world: &mut SubWorld,
time: Res<Time>,
@ -39,7 +41,7 @@ fn move_on_input(
}
}
/// creates a simple scene
/// Creates a simple scene containing the cube we will be controlling
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResMut<Assets<Mesh>>,
@ -68,7 +70,7 @@ fn setup(
// camera
.add_entity(PerspectiveCameraEntity {
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
)),

View file

@ -0,0 +1,24 @@
use bevy::{input::keyboard::KeyboardInput, prelude::*};
fn main() {
App::build()
.add_default_plugins()
.init_resource::<State>()
.add_system(print_keyboard_event_system.system())
.run();
}
#[derive(Default)]
struct State {
event_reader: EventReader<KeyboardInput>,
}
/// This system prints out all mouse events as they come in
fn print_keyboard_event_system(
mut state: ResMut<State>,
keyboard_input_events: Res<Events<KeyboardInput>>,
) {
for event in state.event_reader.iter(&keyboard_input_events) {
println!("{:?}", event);
}
}

View file

@ -0,0 +1,19 @@
use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_system(mouse_click_system.system())
.run();
}
// This system prints messages when you press or release the left mouse button:
fn mouse_click_system(mouse_button_input: Res<Input<MouseButton>>) {
if mouse_button_input.just_pressed(MouseButton::Left) {
println!("left mouse clicked");
}
if mouse_button_input.just_released(MouseButton::Left) {
println!("left mouse released");
}
}

View file

@ -8,21 +8,10 @@ fn main() {
App::build()
.add_default_plugins()
.init_resource::<State>()
.add_system(mouse_click_system.system())
.add_system(mouse_input_event_system.system())
.add_system(print_mouse_events_system.system())
.run();
}
fn mouse_click_system(mouse_button_input: Res<Input<MouseButton>>) {
if mouse_button_input.just_pressed(MouseButton::Left) {
println!("left mouse clicked");
}
if mouse_button_input.just_released(MouseButton::Left) {
println!("left mouse released");
}
}
#[derive(Default)]
struct State {
mouse_button_event_reader: EventReader<MouseButtonInput>,
@ -30,8 +19,8 @@ struct State {
cursor_moved_event_reader: EventReader<CursorMoved>,
}
/// prints out all mouse events as they come in
fn mouse_input_event_system(
/// This system prints out all mouse events as they come in
fn print_mouse_events_system(
mut state: ResMut<State>,
mouse_button_input_events: Res<Events<MouseButtonInput>>,
mouse_motion_events: Res<Events<MouseMotion>>,