From 4979a06e90fe3a1a8c06cc56d91eb88db360986e Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Wed, 3 Jun 2020 23:22:32 -0700 Subject: [PATCH] input: fix input example and add cursor move events --- crates/bevy_app/src/event/event.rs | 3 +++ crates/bevy_input/Cargo.toml | 1 + crates/bevy_input/src/mouse.rs | 5 +++-- crates/bevy_window/Cargo.toml | 1 + crates/bevy_window/src/event.rs | 7 +++++++ crates/bevy_window/src/lib.rs | 1 + crates/bevy_winit/Cargo.toml | 3 ++- crates/bevy_winit/src/lib.rs | 17 +++++++++++++++-- examples/input/input_mouse.rs | 11 +++++++++-- 9 files changed, 42 insertions(+), 7 deletions(-) diff --git a/crates/bevy_app/src/event/event.rs b/crates/bevy_app/src/event/event.rs index 10718ab897..bffb8be4ad 100644 --- a/crates/bevy_app/src/event/event.rs +++ b/crates/bevy_app/src/event/event.rs @@ -1,11 +1,13 @@ use legion::prelude::{ResMut, Resources}; use std::marker::PhantomData; +#[derive(Debug)] struct EventInstance { 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 { events_a: Vec>, events_b: Vec>, diff --git a/crates/bevy_input/Cargo.toml b/crates/bevy_input/Cargo.toml index bb13b3bb80..309abe9980 100644 --- a/crates/bevy_input/Cargo.toml +++ b/crates/bevy_input/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" [dependencies] bevy_app = { path = "../bevy_app" } legion = { path = "../bevy_legion" } +glam = "0.8.7" diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index 88b1d2e765..ce0014b606 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -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, +} \ No newline at end of file diff --git a/crates/bevy_window/Cargo.toml b/crates/bevy_window/Cargo.toml index bb2825547b..8d94db199e 100644 --- a/crates/bevy_window/Cargo.toml +++ b/crates/bevy_window/Cargo.toml @@ -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"] } \ No newline at end of file diff --git a/crates/bevy_window/src/event.rs b/crates/bevy_window/src/event.rs index 57d297562f..862d8f0df1 100644 --- a/crates/bevy_window/src/event.rs +++ b/crates/bevy_window/src/event.rs @@ -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, +} \ No newline at end of file diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index f753bd6ac7..d029518b97 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -31,6 +31,7 @@ impl AppPlugin for WindowPlugin { .add_event::() .add_event::() .add_event::() + .add_event::() .init_resource::(); if let Some(ref primary_window_descriptor) = self.primary_window { diff --git a/crates/bevy_winit/Cargo.toml b/crates/bevy_winit/Cargo.toml index 1d6e7c8e90..2a95103f1b 100644 --- a/crates/bevy_winit/Cargo.toml +++ b/crates/bevy_winit/Cargo.toml @@ -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"] } \ No newline at end of file diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 6a8ed96a4f..f8e84979d8 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -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::>().unwrap(); keyboard_input_events.send(converters::convert_keyboard_input(input)); } + WindowEvent::CursorMoved { position, .. } => { + let mut cursor_moved_events = + app.resources.get_mut::>().unwrap(); + let winit_windows = app.resources.get_mut::().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::>().unwrap(); @@ -115,7 +126,9 @@ pub fn winit_runner(mut app: App) { DeviceEvent::MouseMotion { delta } => { let mut mouse_motion_events = app.resources.get_mut::>().unwrap(); - mouse_motion_events.send(MouseMotionInput { delta: *delta }); + mouse_motion_events.send(MouseMotionInput { + delta: Vec2::new(delta.0 as f32, delta.1 as f32), + }); } _ => {} }, diff --git a/examples/input/input_mouse.rs b/examples/input/input_mouse.rs index 0fed75de58..b3845117c7 100644 --- a/examples/input/input_mouse.rs +++ b/examples/input/input_mouse.rs @@ -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, mouse_motion_event_reader: EventReader, + cursor_moved_event_reader: EventReader, } /// prints out mouse events as they come in fn mouse_input_system( mut state: ResMut, - mouse_button_input_events: Com>, - mouse_motion_events: Com>, + mouse_button_input_events: Res>, + mouse_motion_events: Res>, + cursor_moved_events: Res>, ) { 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); + } }