kubi/src/input.rs

74 lines
2.1 KiB
Rust
Raw Normal View History

2023-01-24 20:01:44 -06:00
use glam::{Vec2, DVec2};
use glium::glutin::event::{DeviceEvent, VirtualKeyCode, ElementState};
use hashbrown::HashSet;
2023-01-24 11:58:56 -06:00
use nohash_hasher::BuildNoHashHasher;
2023-01-24 20:01:44 -06:00
use shipyard::{AllStoragesView, Unique, View, IntoIter, UniqueViewMut, Workload, IntoWorkload, UniqueView};
use crate::events::InputDeviceEvent;
2023-01-24 11:58:56 -06:00
2023-01-24 20:01:44 -06:00
#[derive(Unique, Clone, Copy, Default, Debug)]
pub struct Inputs {
pub movement: Vec2,
pub look: Vec2,
pub action_a: bool,
pub action_b: bool,
}
#[derive(Unique, Clone, Default, Debug)]
pub struct RawInputState {
pub keyboard_state: HashSet<VirtualKeyCode, BuildNoHashHasher<u32>>,
pub mouse_delta: DVec2
}
pub fn process_events(
device_events: View<InputDeviceEvent>,
mut input_state: UniqueViewMut<RawInputState>,
) {
input_state.mouse_delta = DVec2::ZERO;
for event in device_events.iter() {
match event.event {
DeviceEvent::MouseMotion { delta } => {
input_state.mouse_delta = DVec2::from(delta);
},
DeviceEvent::Key(input) => {
if let Some(keycode) = input.virtual_keycode {
match input.state {
ElementState::Pressed => input_state.keyboard_state.insert(keycode),
ElementState::Released => input_state.keyboard_state.remove(&keycode),
};
}
},
2023-01-25 21:12:29 -06:00
DeviceEvent::Button { button: _, state: _ } => {
2023-01-24 20:52:54 -06:00
//log::debug!("Button {button} {state:?}");
2023-01-24 20:01:44 -06:00
},
_ => ()
}
}
}
pub fn update_input_states (
raw_inputs: UniqueView<RawInputState>,
mut inputs: UniqueViewMut<Inputs>,
) {
inputs.movement = Vec2::new(
raw_inputs.keyboard_state.contains(&VirtualKeyCode::D) as u32 as f32 -
raw_inputs.keyboard_state.contains(&VirtualKeyCode::A) as u32 as f32,
raw_inputs.keyboard_state.contains(&VirtualKeyCode::W) as u32 as f32 -
raw_inputs.keyboard_state.contains(&VirtualKeyCode::S) as u32 as f32
).normalize_or_zero();
inputs.look = raw_inputs.mouse_delta.as_vec2();
}
pub fn init_input (
storages: AllStoragesView
) {
storages.add_unique(Inputs::default());
storages.add_unique(RawInputState::default());
}
pub fn process_inputs() -> Workload {
(
process_events,
update_input_states
).into_workload()
}