use glam::Vec2;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Action(pub u16);
#[derive(Clone, Copy, Debug, Default)]
struct State {
down: bool,
pressed: bool,
released: bool,
}
#[derive(Debug, Default)]
pub struct Input {
states: Vec<State>,
pointer_delta: Vec2,
reset_pending: bool,
}
impl Input {
pub fn routed<'a>(&'a self, blocked: &'a [Action], block_motion: bool) -> InputView<'a> {
InputView {
input: self,
blocked,
block_motion,
}
}
pub fn with_capacity(actions: usize) -> Self {
Self {
states: vec![State::default(); actions],
pointer_delta: Vec2::ZERO,
reset_pending: false,
}
}
pub fn set(&mut self, action: Action, down: bool) {
let index = usize::from(action.0);
if index >= self.states.len() {
self.states.resize(index + 1, State::default());
}
let state = &mut self.states[index];
state.pressed |= down && !state.down;
state.released |= !down && state.down;
state.down = down;
}
pub fn down(&self, action: Action) -> bool {
self.states
.get(usize::from(action.0))
.is_some_and(|s| s.down)
}
pub fn pressed(&self, action: Action) -> bool {
self.states
.get(usize::from(action.0))
.is_some_and(|s| s.pressed)
}
pub fn released(&self, action: Action) -> bool {
self.states
.get(usize::from(action.0))
.is_some_and(|s| s.released)
}
pub fn axis(&self, negative: Action, positive: Action) -> f32 {
f32::from(self.down(positive)) - f32::from(self.down(negative))
}
pub fn add_pointer_delta(&mut self, delta: Vec2) {
assert!(delta.is_finite());
self.pointer_delta += delta;
}
pub fn pointer_delta(&self) -> Vec2 {
self.pointer_delta
}
pub fn reset_pending(&self) -> bool {
self.reset_pending
}
pub fn consume_edges(&mut self) {
for state in &mut self.states {
state.pressed = false;
state.released = false;
}
self.pointer_delta = Vec2::ZERO;
self.reset_pending = false;
}
pub fn release_all(&mut self) {
self.reset_pending = true;
for state in &mut self.states {
state.released |= state.down;
state.down = false;
}
self.pointer_delta = Vec2::ZERO;
}
}
#[derive(Clone, Copy, Debug)]
pub struct InputView<'a> {
input: &'a Input,
blocked: &'a [Action],
block_motion: bool,
}
impl InputView<'_> {
pub fn down(&self, action: Action) -> bool {
!self.blocked.contains(&action) && self.input.down(action)
}
pub fn pressed(&self, action: Action) -> bool {
!self.blocked.contains(&action) && self.input.pressed(action)
}
pub fn released(&self, action: Action) -> bool {
!self.blocked.contains(&action) && self.input.released(action)
}
pub fn axis(&self, negative: Action, positive: Action) -> f32 {
f32::from(self.down(positive)) - f32::from(self.down(negative))
}
pub fn pointer_delta(&self) -> Vec2 {
if self.block_motion {
Vec2::ZERO
} else {
self.input.pointer_delta()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn transitions_survive_frames_but_only_fire_on_one_tick() {
let mut input = Input::default();
input.set(Action(2), true);
input.set(Action(2), true); assert!(input.pressed(Action(2)));
input.consume_edges();
assert!(!input.pressed(Action(2)));
assert!(input.down(Action(2)));
input.set(Action(2), false);
assert!(input.released(Action(2)));
}
#[test]
fn quick_tap_keeps_both_edges() {
let mut input = Input::default();
input.set(Action(0), true);
input.set(Action(0), false);
assert!(input.pressed(Action(0)) && input.released(Action(0)));
assert!(!input.down(Action(0)));
}
#[test]
fn unknown_actions_and_focus_loss_are_safe() {
let mut input = Input::default();
assert_eq!(input.axis(Action(0), Action(1)), 0.0);
input.set(Action(1), true);
input.release_all();
assert!(!input.down(Action(1)));
assert!(input.released(Action(1)));
}
#[test]
fn relative_motion_accumulates_until_one_tick_and_is_not_repeated_during_catch_up() {
let mut input = Input::with_capacity(1);
input.set(Action(0), true);
input.add_pointer_delta(Vec2::new(4.0, -2.0));
input.add_pointer_delta(Vec2::new(3.0, 1.0));
assert_eq!(input.pointer_delta(), Vec2::new(7.0, -1.0));
input.consume_edges();
assert_eq!(input.pointer_delta(), Vec2::ZERO);
assert!(input.down(Action(0)));
input.consume_edges();
assert_eq!(input.pointer_delta(), Vec2::ZERO);
input.add_pointer_delta(Vec2::new(-2.0, 3.0));
input.release_all();
assert_eq!(input.pointer_delta(), Vec2::ZERO);
}
}