From 55899f26ad9a06a9d618a999aa14f5f625922977 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Thu, 30 Jul 2020 23:54:04 -0700 Subject: [PATCH] need to add gamepad events --- Cargo.toml | 1 + src/main.rs | 30 ++++++++++++++++++++---------- src/util/mod.rs | 1 + src/util/tr_event.rs | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/util/tr_event.rs diff --git a/Cargo.toml b/Cargo.toml index 3e795a1b..90379e45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ vulkano-win = "0.19.0" #shade_runner = {version = "0.1.1", git = "https://github.com/MitchellHansen/shade_runner"} shade_runner = {path = "../shade_runner"} +gilrs = "0.7.2" cgmath = "0.17.0" simple-stopwatch="0.1.4" nalgebra = "0.18.0" diff --git a/src/main.rs b/src/main.rs index 2b8e1db5..6b4375a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ use crate::util::timer::Timer; use crate::util::vertex::{TextureVertex3D, VertexTypes}; use crate::vkprocessor::VkProcessor; use std::path::Path; +use gilrs::{Gilrs, Button, Event as GilEvent}; pub mod util; pub mod vkprocessor; @@ -144,17 +145,14 @@ pub fn main() { // while (accumulator_time - step_size) >= step_size { // accumulator_time -= step_size; // } + + let mut gilrs = Gilrs::new().unwrap(); + // Iterate over all connected gamepads + for (_id, gamepad) in gilrs.gamepads() { + println!("{} is {:?}", gamepad.name(), gamepad.power_info()); + } - - - - - - - - - - + let mut active_gamepad = None; // Events loop is borrowed from the surface events_loop.run(move |event, _, control_flow| { @@ -183,6 +181,18 @@ pub fn main() { compu_frame); } + while let Some(GilEvent { id, event, time }) = gilrs.next_event() { + println!("{:?} New event from {}: {:?}", time, id, event); + active_gamepad = Some(id); + } + + // You can also use cached gamepad state + if let Some(gamepad) = active_gamepad.map(|id| gilrs.gamepad(id)) { + if gamepad.is_pressed(Button::South) { + println!("Button South is pressed (XBox - A, PS - X)"); + } + } + } Event::DeviceEvent { event: DeviceEvent::Key(keyboard_input), .. } => { match keyboard_input.virtual_keycode.unwrap() { diff --git a/src/util/mod.rs b/src/util/mod.rs index 8aa17a2e..95019bf9 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; pub mod timer; pub mod vertex; +pub mod tr_event; pub fn load_raw(filename: String) -> (Vec, (u32,u32)) { diff --git a/src/util/tr_event.rs b/src/util/tr_event.rs new file mode 100644 index 00000000..7b4a6e85 --- /dev/null +++ b/src/util/tr_event.rs @@ -0,0 +1,18 @@ +use winit::window::WindowId; +use winit::event::{WindowEvent, DeviceId, DeviceEvent}; + +enum TrEvent { + + // WindowEvent { + // window_id: WindowId, + // event: WindowEvent<'a>, + // }, + + /// Emitted when the OS sends an event to a device. + DeviceEvent { + device_id: DeviceId, + event: DeviceEvent, + }, + + +} \ No newline at end of file