input: fix input example and add cursor move events

This commit is contained in:
Carter Anderson 2020-06-03 23:22:32 -07:00
parent 6eea96366d
commit 4979a06e90
9 changed files with 42 additions and 7 deletions

View file

@ -1,11 +1,13 @@
use legion::prelude::{ResMut, Resources};
use std::marker::PhantomData;
#[derive(Debug)]
struct EventInstance<T> {
pub event_count: usize,
pub event: T,
}
#[derive(Debug)]
enum State {
A,
B,
@ -53,6 +55,7 @@ enum State {
///
/// An alternative call pattern would be to call [Events::update] manually across frames to control when events are cleared. However
/// this complicates consumption
#[derive(Debug)]
pub struct Events<T> {
events_a: Vec<EventInstance<T>>,
events_b: Vec<EventInstance<T>>,

View file

@ -7,3 +7,4 @@ edition = "2018"
[dependencies]
bevy_app = { path = "../bevy_app" }
legion = { path = "../bevy_legion" }
glam = "0.8.7"

View file

@ -1,4 +1,5 @@
use super::keyboard::ElementState;
use glam::Vec2;
#[derive(Debug, Clone)]
pub struct MouseButtonInput {
@ -16,5 +17,5 @@ pub enum MouseButton {
#[derive(Debug, Clone)]
pub struct MouseMotionInput {
pub delta: (f64, f64),
}
pub delta: Vec2,
}

View file

@ -7,4 +7,5 @@ edition = "2018"
[dependencies]
bevy_app = { path = "../bevy_app" }
legion = { path = "../bevy_legion" }
glam = "0.8.7"
uuid = { version = "0.8", features = ["v4", "serde"] }

View file

@ -1,4 +1,5 @@
use super::{WindowDescriptor, WindowId};
use glam::Vec2;
/// A window event that is sent whenever a window has been resized.
#[derive(Debug, Clone)]
@ -35,3 +36,9 @@ pub struct WindowCloseRequested {
pub id: WindowId,
pub is_primary: bool,
}
#[derive(Debug, Clone)]
pub struct CursorMoved {
pub id: WindowId,
pub position: Vec2,
}

View file

@ -31,6 +31,7 @@ impl AppPlugin for WindowPlugin {
.add_event::<WindowCreated>()
.add_event::<WindowCloseRequested>()
.add_event::<CloseWindow>()
.add_event::<CursorMoved>()
.init_resource::<Windows>();
if let Some(ref primary_window_descriptor) = self.primary_window {

View file

@ -10,5 +10,6 @@ bevy_input = { path = "../bevy_input" }
bevy_window = { path = "../bevy_window" }
legion = { path = "../bevy_legion" }
winit = { version = "0.22.1" }
winit = { version = "0.22.2" }
glam = "0.8.7"
log = { version = "0.4", features = ["release_max_level_info"] }

View file

@ -9,8 +9,9 @@ use bevy_input::{
use bevy_app::{App, AppBuilder, AppExit, AppPlugin, EventReader, Events, GetEventReader};
use bevy_window::{
CreateWindow, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows,
CreateWindow, CursorMoved, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows,
};
use glam::Vec2;
use legion::prelude::*;
use winit::{
event,
@ -101,6 +102,16 @@ pub fn winit_runner(mut app: App) {
app.resources.get_mut::<Events<KeyboardInput>>().unwrap();
keyboard_input_events.send(converters::convert_keyboard_input(input));
}
WindowEvent::CursorMoved { position, .. } => {
let mut cursor_moved_events =
app.resources.get_mut::<Events<CursorMoved>>().unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
cursor_moved_events.send(CursorMoved {
id: window_id,
position: Vec2::new(position.x as f32, position.y as f32),
});
}
WindowEvent::MouseInput { state, button, .. } => {
let mut mouse_button_input_events =
app.resources.get_mut::<Events<MouseButtonInput>>().unwrap();
@ -115,7 +126,9 @@ pub fn winit_runner(mut app: App) {
DeviceEvent::MouseMotion { delta } => {
let mut mouse_motion_events =
app.resources.get_mut::<Events<MouseMotionInput>>().unwrap();
mouse_motion_events.send(MouseMotionInput { delta: *delta });
mouse_motion_events.send(MouseMotionInput {
delta: Vec2::new(delta.0 as f32, delta.1 as f32),
});
}
_ => {}
},

View file

@ -2,6 +2,7 @@ use bevy::{
input::mouse::{MouseButtonInput, MouseMotionInput},
prelude::*,
};
use bevy_window::CursorMoved;
fn main() {
App::build()
@ -15,13 +16,15 @@ fn main() {
struct State {
mouse_button_event_reader: EventReader<MouseButtonInput>,
mouse_motion_event_reader: EventReader<MouseMotionInput>,
cursor_moved_event_reader: EventReader<CursorMoved>,
}
/// prints out mouse events as they come in
fn mouse_input_system(
mut state: ResMut<State>,
mouse_button_input_events: Com<Events<MouseButtonInput>>,
mouse_motion_events: Com<Events<MouseMotionInput>>,
mouse_button_input_events: Res<Events<MouseButtonInput>>,
mouse_motion_events: Res<Events<MouseMotionInput>>,
cursor_moved_events: Res<Events<CursorMoved>>,
) {
for event in state
.mouse_button_event_reader
@ -33,4 +36,8 @@ fn mouse_input_system(
for event in state.mouse_motion_event_reader.iter(&mouse_motion_events) {
println!("{:?}", event);
}
for event in state.cursor_moved_event_reader.iter(&cursor_moved_events) {
println!("{:?}", event);
}
}