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::collections::{HashMap, HashSet};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -7,6 +7,7 @@ use toml;
|
|||||||
|
|
||||||
use serde_derive::{Serialize, Deserialize};
|
use serde_derive::{Serialize, Deserialize};
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
|
use crate::config::LogConfig;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct VirtualButton {
|
pub struct VirtualButton {
|
||||||
@@ -60,7 +61,7 @@ struct InputConfigAxis {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct InputState {
|
pub struct InputState<'a> {
|
||||||
pub virtual_buttons: HashMap<String, VirtualButton>,
|
pub virtual_buttons: HashMap<String, VirtualButton>,
|
||||||
pub virtual_axes: HashMap<String, VirtualAxis>,
|
pub virtual_axes: HashMap<String, VirtualAxis>,
|
||||||
pub mouse_delta_x: f64,
|
pub mouse_delta_x: f64,
|
||||||
@@ -70,16 +71,17 @@ pub struct InputState {
|
|||||||
pressed_mouse_buttons: HashSet<MouseButton>,
|
pressed_mouse_buttons: HashSet<MouseButton>,
|
||||||
analog_wheel_state: f32,
|
analog_wheel_state: f32,
|
||||||
config: InputConfigConfig,
|
config: InputConfigConfig,
|
||||||
|
log_config: &'a LogConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputState {
|
impl <'a> InputState<'a> {
|
||||||
pub fn new(toml_path: &str) -> InputState {
|
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 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(),
|
let mut state = InputState { virtual_buttons: HashMap::new(), virtual_axes: HashMap::new(),
|
||||||
input_events: HashSet::new(), pressed_scan_codes: HashSet::new(),
|
input_events: HashSet::new(), pressed_scan_codes: HashSet::new(),
|
||||||
pressed_mouse_buttons: HashSet::new(), analog_wheel_state: 0.0,
|
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| {
|
config.button.iter().for_each(|bn| {
|
||||||
let modifiers = ModifiersState {
|
let modifiers = ModifiersState {
|
||||||
@@ -150,6 +152,55 @@ impl InputState {
|
|||||||
return state;
|
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 {
|
pub fn button_down(self: &Self, button_code: &str) -> bool {
|
||||||
match self.virtual_buttons.get(button_code) {
|
match self.virtual_buttons.get(button_code) {
|
||||||
Some(virtual_button) => {
|
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(
|
String::from_iter(
|
||||||
vec!["shift", "ctrl", "alt", "logo"].iter()
|
vec!["shift", "ctrl", "alt", "logo"].iter()
|
||||||
.zip(vec![modifiers.shift, modifiers.ctrl, modifiers.alt, modifiers.logo])
|
.zip(vec![modifiers.shift, modifiers.ctrl, modifiers.alt, modifiers.logo])
|
||||||
|
|||||||
65
src/main.rs
65
src/main.rs
@@ -1,23 +1,23 @@
|
|||||||
use winit::{Event, WindowEvent, DeviceEvent};
|
use winit::{Event};
|
||||||
use cgmath::{Matrix4, Rad, Vector3, Deg, Quaternion, Rotation3, One, Rotation};
|
use cgmath::{Matrix4, Rad, Vector3, Deg, Quaternion, Rotation3, One, Rotation};
|
||||||
|
|
||||||
mod vulkan;
|
mod vulkan;
|
||||||
use crate::vulkan::{GameData, Game, LinePoint};
|
use crate::vulkan::{GameData, Game, LinePoint};
|
||||||
|
|
||||||
mod input;
|
mod input;
|
||||||
use crate::input::{InputState, mods_to_string};
|
use crate::input::{InputState};
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
use crate::config::LogConfig;
|
use crate::config::LogConfig;
|
||||||
|
|
||||||
struct TestGame {
|
struct TestGame<'a> {
|
||||||
input: InputState,
|
input: InputState<'a>,
|
||||||
cam_position: Vector3<f32>,
|
cam_position: Vector3<f32>,
|
||||||
cam_rotation: Quaternion<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 {
|
fn validation_layers_enabled(self: &Self) -> bool {
|
||||||
self.log_config.vulkan_validation_layers
|
self.log_config.vulkan_validation_layers
|
||||||
}
|
}
|
||||||
@@ -71,61 +71,18 @@ impl Game for TestGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn on_window_event(self: &mut Self, event: &Event) {
|
fn on_window_event(self: &mut Self, event: &Event) {
|
||||||
match event {
|
self.input.on_window_event(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;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let log_config = LogConfig::from_file("config/log.toml");
|
||||||
|
|
||||||
let mut game = TestGame {
|
let mut game = TestGame {
|
||||||
input: InputState::new("config/input.toml"),
|
input: InputState::new("config/input.toml", &log_config),
|
||||||
cam_rotation: Quaternion::one(),
|
cam_rotation: Quaternion::one(),
|
||||||
cam_position: Vector3::new(0.0, 0.0, -10.0),
|
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;
|
let line_count = 30;
|
||||||
|
|||||||
Reference in New Issue
Block a user