input: Refactor all previous states
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m48s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m48s
This commit is contained in:
parent
b0f82b0714
commit
5b0ab19207
5 changed files with 135 additions and 118 deletions
|
@ -1,12 +1,13 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use keyboard_state::KeyboardState;
|
||||
use mouse_state::MouseState;
|
||||
use cache::{CachedElementState, CachedMovement};
|
||||
use virtual_input::VirtualInput;
|
||||
use winit::event::{ElementState, WindowEvent};
|
||||
use winit::{
|
||||
event::{MouseButton, MouseScrollDelta, WindowEvent},
|
||||
keyboard::PhysicalKey,
|
||||
};
|
||||
|
||||
mod keyboard_state;
|
||||
mod mouse_state;
|
||||
mod cache;
|
||||
mod virtual_binding;
|
||||
mod virtual_input;
|
||||
mod virtual_state;
|
||||
|
@ -14,8 +15,10 @@ pub use virtual_binding::{AxisDirection, VirtualBinding};
|
|||
|
||||
#[derive(Default)]
|
||||
pub struct InputManager {
|
||||
keyboard_state: KeyboardState,
|
||||
mouse_state: MouseState,
|
||||
keys_state: CachedElementState<PhysicalKey>,
|
||||
mouse_buttons_state: CachedElementState<MouseButton>,
|
||||
mouse_position_delta: CachedMovement<glam::Vec2>,
|
||||
mouse_wheel_delta: CachedMovement<glam::Vec2>,
|
||||
virtual_input: VirtualInput,
|
||||
}
|
||||
|
||||
|
@ -41,18 +44,45 @@ impl InputManager {
|
|||
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::KeyboardInput { event, .. } => {
|
||||
let new_key_state = self
|
||||
.keys_state
|
||||
.set_key_state(event.physical_key, event.state);
|
||||
if let Some(new_key_state) = new_key_state {
|
||||
self.virtual_input
|
||||
.update_key_binding(event.physical_key, new_key_state);
|
||||
}
|
||||
}
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
self.mouse_position_delta
|
||||
.set_value(glam::Vec2::new(position.x as f32, position.y as f32));
|
||||
}
|
||||
WindowEvent::MouseInput { button, state, .. } => {
|
||||
let new_mouse_button_state =
|
||||
self.mouse_buttons_state.set_key_state(*button, *state);
|
||||
if let Some(new_mouse_button_state) = new_mouse_button_state {
|
||||
self.virtual_input
|
||||
.update_mouse_button_binding(*button, new_mouse_button_state);
|
||||
}
|
||||
}
|
||||
WindowEvent::MouseWheel { delta, .. } => {
|
||||
self.mouse_wheel_delta += match delta {
|
||||
MouseScrollDelta::PixelDelta(position) => {
|
||||
glam::Vec2::new(position.x as f32, position.y as f32)
|
||||
}
|
||||
MouseScrollDelta::LineDelta(x, y) => glam::Vec2::new(*x as f32, *y as f32),
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates deltas before running update
|
||||
pub fn update(&mut self) {
|
||||
self.mouse_state.update(&mut self.virtual_input);
|
||||
self.virtual_input
|
||||
.update_mouse_move_binding(&self.mouse_position_delta.reset());
|
||||
self.virtual_input
|
||||
.update_mouse_wheel_binding(&self.mouse_wheel_delta.reset());
|
||||
}
|
||||
|
||||
pub fn get_virtual_input_state(&self, value_name: &str) -> f32 {
|
||||
|
@ -63,33 +93,3 @@ impl InputManager {
|
|||
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