Fix and module update
This commit is contained in:
73
src/input.rs
73
src/input.rs
@@ -1,4 +1,6 @@
|
||||
use winit::{ScanCode, ModifiersState, MouseButton, ElementState, MouseScrollDelta, Event, WindowEvent, DeviceEvent};
|
||||
#![allow(deprecated)]
|
||||
|
||||
use winit::event::{ScanCode, MouseButton, ElementState, MouseScrollDelta, Event, WindowEvent, DeviceEvent, ModifiersState};
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
@@ -81,6 +83,25 @@ pub struct InputState {
|
||||
controller_input: Gilrs,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
|
||||
pub struct KeyboardModifierState {
|
||||
shift: bool,
|
||||
ctrl: bool,
|
||||
alt: bool,
|
||||
logo: bool
|
||||
}
|
||||
|
||||
impl KeyboardModifierState {
|
||||
pub fn from_deprecated_state(old_state: &ModifiersState) -> KeyboardModifierState {
|
||||
KeyboardModifierState {
|
||||
shift: old_state.shift(),
|
||||
ctrl: old_state.ctrl(),
|
||||
alt: old_state.alt(),
|
||||
logo: old_state.logo()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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!");
|
||||
@@ -92,7 +113,7 @@ impl InputState {
|
||||
controller_input: Gilrs::new().unwrap() };
|
||||
|
||||
config.button.iter().for_each(|bn| {
|
||||
let modifiers = ModifiersState {
|
||||
let modifiers = KeyboardModifierState {
|
||||
shift: bn.shift.is_some(),
|
||||
ctrl: bn.ctrl.is_some(),
|
||||
alt: bn.alt.is_some(),
|
||||
@@ -138,7 +159,7 @@ impl InputState {
|
||||
AxisInput::Digital(positive_button, negative_button)
|
||||
},
|
||||
InputConfigAxis { mouse_axis: Some(axis_name), .. } => {
|
||||
let modifiers = ModifiersState {
|
||||
let modifiers = KeyboardModifierState {
|
||||
shift: axis.shift.is_some(),
|
||||
ctrl: axis.ctrl.is_some(),
|
||||
alt: axis.alt.is_some(),
|
||||
@@ -167,11 +188,11 @@ impl InputState {
|
||||
return state;
|
||||
}
|
||||
|
||||
pub fn on_window_event(self: &mut Self, event: &Event) {
|
||||
pub fn on_window_event(self: &mut Self, event: &Event<()>) {
|
||||
match event {
|
||||
Event::WindowEvent { event: WindowEvent::KeyboardInput { device_id, input }, .. } => {
|
||||
Event::WindowEvent { event: WindowEvent::KeyboardInput { device_id, input, .. }, .. } => {
|
||||
if self.log_config.input_events {
|
||||
let mods = mods_to_string(&input.modifiers);
|
||||
let mods = mods_to_string(&KeyboardModifierState::from_deprecated_state(&input.modifiers));
|
||||
if mods.len() > 0 {
|
||||
println!("Keyboard {:?} {:?} {:?} + {:?}", device_id, input.state, &mods, input.scancode)
|
||||
} else {
|
||||
@@ -179,11 +200,11 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
self.on_keyboard_event(input.state, input.scancode, input.modifiers);
|
||||
self.on_keyboard_event(input.state, input.scancode, KeyboardModifierState::from_deprecated_state(&input.modifiers));
|
||||
},
|
||||
Event::WindowEvent { event: WindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => {
|
||||
if self.log_config.input_events {
|
||||
let mods = mods_to_string(&modifiers);
|
||||
let mods = mods_to_string(&KeyboardModifierState::from_deprecated_state(&modifiers));
|
||||
if mods.len() > 0 {
|
||||
println!("Mouse {:?} {:?} {:?} + {:?}", device_id, state, &mods, button)
|
||||
} else {
|
||||
@@ -191,11 +212,11 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
self.on_mouse_event(state, button, modifiers);
|
||||
self.on_mouse_event(state, button, &KeyboardModifierState::from_deprecated_state(&modifiers));
|
||||
},
|
||||
Event::WindowEvent { event: WindowEvent::MouseWheel { device_id, delta, phase, modifiers }, .. } => {
|
||||
if self.log_config.input_events {
|
||||
let mods = mods_to_string(&modifiers);
|
||||
let mods = mods_to_string(&KeyboardModifierState::from_deprecated_state(&modifiers));
|
||||
if mods.len() > 0 {
|
||||
println!("Scroll {:?} {:?} {:?} + {:?}", device_id, phase, &mods, delta)
|
||||
} else {
|
||||
@@ -203,7 +224,7 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
self.on_mouse_wheel_event(delta, modifiers);
|
||||
self.on_mouse_wheel_event(delta, &KeyboardModifierState::from_deprecated_state(&modifiers));
|
||||
},
|
||||
Event::DeviceEvent { device_id, event: DeviceEvent::MouseMotion { delta: (delta_x, delta_y) } } => {
|
||||
if self.log_config.input_events {
|
||||
@@ -269,10 +290,10 @@ impl InputState {
|
||||
axis.axis_inputs.iter().map(|item| {
|
||||
match item {
|
||||
AxisInput::Wheel(modifiers) => {
|
||||
if self.modifiers_are_pressed(*modifiers) { self.analog_wheel_state } else { 0.0 }
|
||||
if self.modifiers_are_pressed(modifiers) { self.analog_wheel_state } else { 0.0 }
|
||||
},
|
||||
AxisInput::MouseMove(direction, modifiers) => {
|
||||
if self.modifiers_are_pressed(*modifiers) {
|
||||
if self.modifiers_are_pressed(modifiers) {
|
||||
match direction {
|
||||
MouseMoveDirection::X => self.mouse_delta_x as f32,
|
||||
MouseMoveDirection::Y => self.mouse_delta_y as f32,
|
||||
@@ -301,11 +322,11 @@ impl InputState {
|
||||
match digital_input {
|
||||
DigitalInput::Keyboard(keyboard_input) => {
|
||||
self.pressed_scan_codes.contains(&keyboard_input.scan_code)
|
||||
&& self.modifiers_are_pressed(keyboard_input.modifiers)
|
||||
&& self.modifiers_are_pressed(&keyboard_input.modifiers)
|
||||
},
|
||||
DigitalInput::Mouse(mouse_input) => {
|
||||
self.pressed_mouse_buttons.contains(&mouse_input.button)
|
||||
&& self.modifiers_are_pressed(mouse_input.modifiers)
|
||||
&& self.modifiers_are_pressed(&mouse_input.modifiers)
|
||||
},
|
||||
DigitalInput::Wheel(wheel_input) => {
|
||||
self.input_events.contains(&DigitalInputEvent::Pressed(DigitalInput::Wheel(wheel_input.clone())))
|
||||
@@ -316,7 +337,7 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
fn modifiers_are_pressed(self: &Self, modifiers: ModifiersState) -> bool {
|
||||
fn modifiers_are_pressed(self: &Self, modifiers: &KeyboardModifierState) -> bool {
|
||||
(!modifiers.ctrl || self.pressed_scan_codes.contains(&29))
|
||||
&& (!modifiers.shift || self.pressed_scan_codes.contains(&42) || self.pressed_scan_codes.contains(&54))
|
||||
&& (!modifiers.alt || self.pressed_scan_codes.contains(&56))
|
||||
@@ -327,7 +348,7 @@ impl InputState {
|
||||
if input.digital_inputs.iter().any(|di| self.digital_input_pressed(di)) { 1.0 } else { 0.0 }
|
||||
}
|
||||
|
||||
pub fn on_keyboard_event(self: &mut Self, state: ElementState, scan_code: ScanCode, modifiers: ModifiersState) {
|
||||
pub fn on_keyboard_event(self: &mut Self, state: ElementState, scan_code: ScanCode, modifiers: KeyboardModifierState) {
|
||||
let input = DigitalInput::Keyboard(KeyboardInput { scan_code, modifiers });
|
||||
match state {
|
||||
ElementState::Pressed => {
|
||||
@@ -341,7 +362,7 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_mouse_event(self: &mut Self, state: &ElementState, button: &MouseButton, modifiers: &ModifiersState) {
|
||||
pub fn on_mouse_event(self: &mut Self, state: &ElementState, button: &MouseButton, modifiers: &KeyboardModifierState) {
|
||||
let input = DigitalInput::Mouse(MouseInput { button: button.clone(), modifiers: modifiers.clone() });
|
||||
match state {
|
||||
ElementState::Pressed => {
|
||||
@@ -355,7 +376,7 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_mouse_wheel_event(self: &mut Self, delta: &MouseScrollDelta, modifiers: &ModifiersState) {
|
||||
pub fn on_mouse_wheel_event(self: &mut Self, delta: &MouseScrollDelta, modifiers: &KeyboardModifierState) {
|
||||
let vertical_direction = match delta {
|
||||
MouseScrollDelta::LineDelta(_x, y) => {
|
||||
if *y > 0.0 { Some(WheelInputDirection::Up) }
|
||||
@@ -412,7 +433,7 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
fn mods_to_string(modifiers: &ModifiersState) -> String {
|
||||
fn mods_to_string(modifiers: &KeyboardModifierState) -> String {
|
||||
String::from_iter(
|
||||
vec!["shift", "ctrl", "alt", "logo"].iter()
|
||||
.zip(vec![modifiers.shift, modifiers.ctrl, modifiers.alt, modifiers.logo])
|
||||
@@ -422,8 +443,8 @@ fn mods_to_string(modifiers: &ModifiersState) -> String {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AxisInput {
|
||||
Wheel(ModifiersState),
|
||||
MouseMove(MouseMoveDirection, ModifiersState),
|
||||
Wheel(KeyboardModifierState),
|
||||
MouseMove(MouseMoveDirection, KeyboardModifierState),
|
||||
Digital(VirtualButton, VirtualButton),
|
||||
Controller(AnalogControllerInput),
|
||||
}
|
||||
@@ -439,19 +460,19 @@ pub enum DigitalInput {
|
||||
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
|
||||
pub struct KeyboardInput {
|
||||
scan_code: ScanCode,
|
||||
modifiers: ModifiersState,
|
||||
modifiers: KeyboardModifierState,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
|
||||
pub struct MouseInput {
|
||||
button: MouseButton,
|
||||
modifiers: ModifiersState,
|
||||
modifiers: KeyboardModifierState,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
|
||||
pub struct WheelInput {
|
||||
direction: WheelInputDirection,
|
||||
modifiers: ModifiersState,
|
||||
modifiers: KeyboardModifierState,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
|
||||
@@ -474,7 +495,7 @@ pub enum MouseMoveDirection {
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct AnalogWheelInput {
|
||||
value: f32,
|
||||
modifiers: ModifiersState,
|
||||
modifiers: KeyboardModifierState,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
|
||||
Reference in New Issue
Block a user