From 205536ce5506f18eb7994291c7404e1705cec3e0 Mon Sep 17 00:00:00 2001 From: Till Date: Wed, 31 Jul 2019 19:29:54 +0200 Subject: [PATCH] print controller input --- config/log.toml | 5 +++-- src/config.rs | 5 +++-- src/input.rs | 22 +++++++++++++--------- src/main.rs | 14 ++++++++------ 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/config/log.toml b/config/log.toml index 893e4a0..21f4239 100644 --- a/config/log.toml +++ b/config/log.toml @@ -1,2 +1,3 @@ -input = false -vulkan_validation_layers = true \ No newline at end of file +input_events = false +vulkan_validation_layers = true +mesh_load_info = true \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index a1e03b8..c5db96f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,10 +2,11 @@ use serde_derive::{Serialize, Deserialize}; use toml; use std::fs; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone, Copy)] pub struct LogConfig { - pub input: bool, + pub input_events: bool, pub vulkan_validation_layers: bool, + pub mesh_load_info: bool, } impl LogConfig { diff --git a/src/input.rs b/src/input.rs index 96a04cc..f586d95 100644 --- a/src/input.rs +++ b/src/input.rs @@ -67,7 +67,7 @@ struct InputConfigAxis { } #[derive(Debug)] -pub struct InputState<'a> { +pub struct InputState { pub virtual_buttons: HashMap, pub virtual_axes: HashMap, pub mouse_delta_x: f64, @@ -77,12 +77,12 @@ pub struct InputState<'a> { pressed_mouse_buttons: HashSet, analog_wheel_state: f32, config: InputConfigConfig, - log_config: &'a LogConfig, + log_config: LogConfig, controller_input: Gilrs, } -impl <'a> InputState<'a> { - pub fn new(toml_path: &str, log_config: &'a LogConfig) -> InputState<'a> { +impl InputState { + pub fn new(toml_path: &str, log_config: LogConfig) -> InputState { let config: InputConfig = toml::from_slice(&fs::read(toml_path).expect("Failed to read input config!")).expect("Failed to parse input config!"); let mut state = InputState { virtual_buttons: HashMap::new(), virtual_axes: HashMap::new(), @@ -170,7 +170,7 @@ impl <'a> InputState<'a> { pub fn on_window_event(self: &mut Self, event: &Event) { match event { Event::WindowEvent { event: WindowEvent::KeyboardInput { device_id, input }, .. } => { - if self.log_config.input { + if self.log_config.input_events { let mods = mods_to_string(&input.modifiers); if mods.len() > 0 { println!("Keyboard {:?} {:?} {:?} + {:?}", device_id, input.state, &mods, input.scancode) @@ -182,7 +182,7 @@ impl <'a> InputState<'a> { self.on_keyboard_event(input.state, input.scancode, input.modifiers); }, Event::WindowEvent { event: WindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => { - if self.log_config.input { + if self.log_config.input_events { let mods = mods_to_string(&modifiers); if mods.len() > 0 { println!("Mouse {:?} {:?} {:?} + {:?}", device_id, state, &mods, button) @@ -194,7 +194,7 @@ impl <'a> InputState<'a> { self.on_mouse_event(state, button, modifiers); }, Event::WindowEvent { event: WindowEvent::MouseWheel { device_id, delta, phase, modifiers }, .. } => { - if self.log_config.input { + if self.log_config.input_events { let mods = mods_to_string(&modifiers); if mods.len() > 0 { println!("Scroll {:?} {:?} {:?} + {:?}", device_id, phase, &mods, delta) @@ -206,7 +206,7 @@ impl <'a> InputState<'a> { self.on_mouse_wheel_event(delta, modifiers); }, Event::DeviceEvent { device_id, event: DeviceEvent::MouseMotion { delta: (delta_x, delta_y) } } => { - if self.log_config.input { + if self.log_config.input_events { println!("MouseMotion {:?}, ({:?},{:?})", device_id, delta_x, delta_y); } self.mouse_delta_x += *delta_x; @@ -385,14 +385,18 @@ impl <'a> InputState<'a> { while let Some(event) = self.controller_input.next_event() { match event.event { EventType::ButtonPressed(button, _) => { + if self.log_config.input_events { println!("ControllerButton Pressed {:?} {:?}", event.id, button); } self.input_events.insert(DigitalInputEvent::Pressed(DigitalInput::Controller(ControllerInput { button }))); }, EventType::ButtonRepeated(_, _) => {}, EventType::ButtonReleased(button, _) => { + if self.log_config.input_events { println!("ControllerButton Released {:?} {:?}", event.id, button); } self.input_events.insert(DigitalInputEvent::Released(DigitalInput::Controller(ControllerInput { button }))); }, EventType::ButtonChanged(_, _, _) => {}, - EventType::AxisChanged(_, _, _) => {}, + EventType::AxisChanged(axis, value, _) => { + if self.log_config.input_events { println!("ControllerAxis {:?} {:?} {:?}", event.id, axis, value); } + }, EventType::Connected => {}, EventType::Disconnected => {}, EventType::Dropped => {}, diff --git a/src/main.rs b/src/main.rs index 01b3046..18a6017 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,23 +12,24 @@ use crate::config::LogConfig; mod mesh; -struct TestGame<'a> { - input: InputState<'a>, +struct TestGame { + input: InputState, cam_position: Vector3, cam_rotation: Quaternion, cube_mesh: Option, cubes: Vec, + log_config: LogConfig, } -impl Game for TestGame<'_> { +impl Game for TestGame { fn on_window_event(self: &mut Self, event: &Event) { self.input.on_window_event(event); } } -impl TestGame<'_> { +impl TestGame { fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) { - self.cube_mesh = Some(renderer.upload_mesh(mesh::load_mesh("models/cube.dae", true).into_iter().nth(0).unwrap())); + self.cube_mesh = Some(renderer.upload_mesh(mesh::load_mesh("models/cube.dae", self.log_config.mesh_load_info).into_iter().nth(0).unwrap())); println!("Game started."); } @@ -89,11 +90,12 @@ fn main() { let log_config = LogConfig::from_file("config/log.toml"); let mut game = TestGame { - input: InputState::new("config/input.toml", &log_config), + input: InputState::new("config/input.toml", log_config), cam_rotation: Quaternion::one(), cam_position: Vector3::new(0.0, 0.0, -10.0), cube_mesh: None, cubes: vec![], + log_config }; let line_count = 30;