input: Add support for Axis, Mouse Button, MouseWheel
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m50s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m50s
This commit is contained in:
parent
8c42e7b139
commit
b0f82b0714
9 changed files with 308 additions and 160 deletions
|
@ -2,15 +2,15 @@ use std::collections::HashMap;
|
|||
|
||||
use keyboard_state::KeyboardState;
|
||||
use mouse_state::MouseState;
|
||||
use virtual_binding::VirtualBinding;
|
||||
use virtual_input::VirtualInput;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event::{ElementState, WindowEvent};
|
||||
|
||||
pub mod keyboard_state;
|
||||
pub mod mouse_state;
|
||||
pub mod virtual_binding;
|
||||
pub mod virtual_input;
|
||||
pub mod virtual_state;
|
||||
mod keyboard_state;
|
||||
mod mouse_state;
|
||||
mod virtual_binding;
|
||||
mod virtual_input;
|
||||
mod virtual_state;
|
||||
pub use virtual_binding::{AxisDirection, VirtualBinding};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct InputManager {
|
||||
|
@ -19,17 +19,34 @@ pub struct InputManager {
|
|||
virtual_input: VirtualInput,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for InputManager {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("InputManager")
|
||||
.field("virtual_input", &self.virtual_input)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl InputManager {
|
||||
pub fn new(input_mapping: HashMap<String, Vec<VirtualBinding>>) -> Self {
|
||||
let mut input_manager = InputManager::default();
|
||||
for (value_name, bindings) in input_mapping {
|
||||
input_manager.add_virtual_bindings(value_name, bindings);
|
||||
}
|
||||
input_manager
|
||||
}
|
||||
|
||||
pub fn process_window_event(&mut self, event: &WindowEvent) {
|
||||
match event {
|
||||
WindowEvent::KeyboardInput { event, .. } => {
|
||||
WindowEvent::AxisMotion { axis, value, .. } => {
|
||||
self.virtual_input.update_axis_binding(*axis, *value as f32);
|
||||
}
|
||||
_ => {
|
||||
self.keyboard_state
|
||||
.process_window_event(event, &mut self.virtual_input);
|
||||
self.mouse_state
|
||||
.process_window_event(event, &mut self.virtual_input);
|
||||
}
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
self.mouse_state.process_window_event(position);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,27 +55,41 @@ impl InputManager {
|
|||
self.mouse_state.update(&mut self.virtual_input);
|
||||
}
|
||||
|
||||
pub fn get_mouse_state(&self) -> &MouseState {
|
||||
&self.mouse_state
|
||||
}
|
||||
|
||||
pub fn get_keyboard_state(&self) -> &KeyboardState {
|
||||
&self.keyboard_state
|
||||
}
|
||||
|
||||
pub fn get_virtual_input(&self) -> &VirtualInput {
|
||||
&self.virtual_input
|
||||
}
|
||||
|
||||
pub fn add_virtual_binding(&mut self, value_name: String, binding: VirtualBinding) {
|
||||
self.virtual_input.add_binding(value_name, binding);
|
||||
}
|
||||
|
||||
pub fn add_virtual_bindings(&mut self, value_name: String, bindings: Vec<VirtualBinding>) {
|
||||
self.virtual_input.add_bindings(value_name, bindings);
|
||||
}
|
||||
|
||||
pub fn get_virtual_input_state(&self, value_name: &str) -> f32 {
|
||||
self.virtual_input.get_state(value_name)
|
||||
}
|
||||
|
||||
fn add_virtual_bindings(&mut self, value_name: String, bindings: Vec<VirtualBinding>) {
|
||||
self.virtual_input.add_bindings(value_name, bindings);
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps the old element state to the new element state.
|
||||
/// if is changed, returns Some(new_state), otherwise returns None
|
||||
#[inline]
|
||||
fn process_new_element_state(
|
||||
old: Option<&ElementState>,
|
||||
new: ElementState,
|
||||
) -> Option<ElementState> {
|
||||
match old {
|
||||
Some(old) => match new {
|
||||
ElementState::Pressed => match old {
|
||||
ElementState::Released => Some(ElementState::Pressed),
|
||||
ElementState::Pressed => None,
|
||||
},
|
||||
ElementState::Released => match old {
|
||||
ElementState::Released => None,
|
||||
ElementState::Pressed => Some(ElementState::Released),
|
||||
},
|
||||
},
|
||||
None => match new {
|
||||
ElementState::Pressed => Some(ElementState::Pressed),
|
||||
ElementState::Released => Some(ElementState::Released),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn process_axis_deadzone(value: f32, deadzone: f32) -> f32 {
|
||||
if value.abs() < deadzone { 0.0 } else { value }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue