input refactor

This commit is contained in:
2019-07-28 16:47:50 +02:00
parent c38ffe4638
commit 745402fc0d
2 changed files with 68 additions and 60 deletions

View File

@@ -1,4 +1,4 @@
use winit::{ScanCode, ModifiersState, MouseButton, ElementState, MouseScrollDelta};
use winit::{ScanCode, ModifiersState, MouseButton, ElementState, MouseScrollDelta, Event, WindowEvent, DeviceEvent};
use std::collections::{HashMap, HashSet};
use std::fs;
@@ -7,6 +7,7 @@ use toml;
use serde_derive::{Serialize, Deserialize};
use std::iter::FromIterator;
use crate::config::LogConfig;
#[derive(Debug, Clone)]
pub struct VirtualButton {
@@ -60,7 +61,7 @@ struct InputConfigAxis {
}
#[derive(Debug)]
pub struct InputState {
pub struct InputState<'a> {
pub virtual_buttons: HashMap<String, VirtualButton>,
pub virtual_axes: HashMap<String, VirtualAxis>,
pub mouse_delta_x: f64,
@@ -70,16 +71,17 @@ pub struct InputState {
pressed_mouse_buttons: HashSet<MouseButton>,
analog_wheel_state: f32,
config: InputConfigConfig,
log_config: &'a LogConfig,
}
impl InputState {
pub fn new(toml_path: &str) -> InputState {
impl <'a> InputState<'a> {
pub fn new(toml_path: &str, log_config: &'a LogConfig) -> InputState<'a> {
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(),
input_events: HashSet::new(), pressed_scan_codes: HashSet::new(),
pressed_mouse_buttons: HashSet::new(), analog_wheel_state: 0.0,
config: config.config, mouse_delta_x: 0.0, mouse_delta_y: 0.0 };
config: config.config, mouse_delta_x: 0.0, mouse_delta_y: 0.0, log_config };
config.button.iter().for_each(|bn| {
let modifiers = ModifiersState {
@@ -150,6 +152,55 @@ impl InputState {
return state;
}
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 {
let mods = mods_to_string(&input.modifiers);
if mods.len() > 0 {
println!("Keyboard {:?} {:?} {:?} + {:?}", device_id, input.state, &mods, input.scancode)
} else {
println!("Keyboard {:?} {:?} {:?}", device_id, input.state, input.scancode)
}
}
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 {
let mods = mods_to_string(&modifiers);
if mods.len() > 0 {
println!("Mouse {:?} {:?} {:?} + {:?}", device_id, state, &mods, button)
} else {
println!("Mouse {:?} {:?} {:?}", device_id, state, button)
}
}
self.on_mouse_event(state, button, modifiers);
},
Event::WindowEvent { event: WindowEvent::MouseWheel { device_id, delta, phase, modifiers }, .. } => {
if self.log_config.input {
let mods = mods_to_string(&modifiers);
if mods.len() > 0 {
println!("Scroll {:?} {:?} {:?} + {:?}", device_id, phase, &mods, delta)
} else {
println!("Scroll {:?} {:?} {:?}", device_id, phase, delta)
}
}
self.on_mouse_wheel_event(delta, modifiers);
},
Event::DeviceEvent { device_id, event: DeviceEvent::MouseMotion { delta: (delta_x, delta_y) } } => {
if self.log_config.input {
println!("MouseMotion {:?}, ({:?},{:?})", device_id, delta_x, delta_y);
}
self.mouse_delta_x += *delta_x;
self.mouse_delta_y += *delta_y;
}
_ => {}
}
}
pub fn button_down(self: &Self, button_code: &str) -> bool {
match self.virtual_buttons.get(button_code) {
Some(virtual_button) => {
@@ -315,7 +366,7 @@ impl InputState {
}
}
pub fn mods_to_string(modifiers: &ModifiersState) -> String {
fn mods_to_string(modifiers: &ModifiersState) -> String {
String::from_iter(
vec!["shift", "ctrl", "alt", "logo"].iter()
.zip(vec![modifiers.shift, modifiers.ctrl, modifiers.alt, modifiers.logo])

View File

@@ -1,23 +1,23 @@
use winit::{Event, WindowEvent, DeviceEvent};
use winit::{Event};
use cgmath::{Matrix4, Rad, Vector3, Deg, Quaternion, Rotation3, One, Rotation};
mod vulkan;
use crate::vulkan::{GameData, Game, LinePoint};
mod input;
use crate::input::{InputState, mods_to_string};
use crate::input::{InputState};
mod config;
use crate::config::LogConfig;
struct TestGame {
input: InputState,
struct TestGame<'a> {
input: InputState<'a>,
cam_position: Vector3<f32>,
cam_rotation: Quaternion<f32>,
log_config: LogConfig,
log_config: &'a LogConfig,
}
impl Game for TestGame {
impl Game for TestGame<'_> {
fn validation_layers_enabled(self: &Self) -> bool {
self.log_config.vulkan_validation_layers
}
@@ -71,61 +71,18 @@ impl Game for TestGame {
}
fn on_window_event(self: &mut Self, event: &Event) {
match event {
Event::WindowEvent { event: WindowEvent::KeyboardInput { device_id, input }, .. } => {
if self.log_config.input {
let mods = mods_to_string(&input.modifiers);
if mods.len() > 0 {
println!("Keyboard {:?} {:?} {:?} + {:?}", device_id, input.state, &mods, input.scancode)
} else {
println!("Keyboard {:?} {:?} {:?}", device_id, input.state, input.scancode)
}
}
self.input.on_keyboard_event(input.state, input.scancode, input.modifiers);
},
Event::WindowEvent { event: WindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => {
if self.log_config.input {
let mods = mods_to_string(modifiers);
if mods.len() > 0 {
println!("Mouse {:?} {:?} {:?} + {:?}", device_id, state, &mods, button)
} else {
println!("Mouse {:?} {:?} {:?}", device_id, state, button)
}
}
self.input.on_mouse_event(state, button, modifiers);
},
Event::WindowEvent { event: WindowEvent::MouseWheel { device_id, delta, phase, modifiers }, .. } => {
if self.log_config.input {
let mods = mods_to_string(modifiers);
if mods.len() > 0 {
println!("Scroll {:?} {:?} {:?} + {:?}", device_id, phase, &mods, delta)
} else {
println!("Scroll {:?} {:?} {:?}", device_id, phase, delta)
}
}
self.input.on_mouse_wheel_event(&delta, &modifiers);
},
Event::DeviceEvent { device_id, event: DeviceEvent::MouseMotion { delta: (delta_x, delta_y) } } => {
if self.log_config.input {
println!("MouseMotion {:?}, ({:?},{:?})", device_id, delta_x, delta_y);
}
self.input.mouse_delta_x += *delta_x;
self.input.mouse_delta_y += *delta_y;
}
_ => {}
}
self.input.on_window_event(event);
}
}
fn main() {
let log_config = LogConfig::from_file("config/log.toml");
let mut game = TestGame {
input: InputState::new("config/input.toml"),
input: InputState::new("config/input.toml", &log_config),
cam_rotation: Quaternion::one(),
cam_position: Vector3::new(0.0, 0.0, -10.0),
log_config: LogConfig::from_file("config/log.toml"),
log_config: &log_config,
};
let line_count = 30;