input refactor
This commit is contained in:
63
src/input.rs
63
src/input.rs
@@ -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])
|
||||
|
||||
Reference in New Issue
Block a user