use glam::Vec2;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Action(pub u16);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Axis(pub u16);
#[derive(Clone, Copy, Debug, Default)]
struct State {
down: bool,
pressed: bool,
released: bool,
}
#[derive(Debug, Default)]
pub struct Input {
states: Vec<State>,
axes: Vec<f32>,
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,
blocked_axes: &[],
block_motion,
}
}
pub fn with_capacity(actions: usize) -> Self {
Self {
states: vec![State::default(); actions],
axes: Vec::new(),
pointer_delta: Vec2::ZERO,
reset_pending: false,
}
}
pub fn with_capacities(actions: usize, axes: usize) -> Self {
Self {
axes: vec![0.0; axes],
..Self::with_capacity(actions)
}
}
pub fn set_axis(&mut self, axis: Axis, value: f32) {
let index = usize::from(axis.0);
if index >= self.axes.len() {
self.axes.resize(index + 1, 0.0);
}
self.axes[index] = if value.is_finite() {
value.clamp(-1.0, 1.0)
} else {
0.0
};
}
pub fn value(&self, axis: Axis) -> f32 {
self.axes.get(usize::from(axis.0)).copied().unwrap_or(0.0)
}
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;
self.axes.fill(0.0);
}
}
#[derive(Clone, Copy, Debug)]
pub struct InputView<'a> {
input: &'a Input,
blocked: &'a [Action],
blocked_axes: &'a [Axis],
block_motion: bool,
}
impl<'a> InputView<'a> {
pub fn with_blocked_axes(mut self, blocked: &'a [Axis]) -> Self {
self.blocked_axes = blocked;
self
}
pub fn value(&self, axis: Axis) -> f32 {
if self.blocked_axes.contains(&axis) {
0.0
} else {
self.input.value(axis)
}
}
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 analog_values_persist_across_ticks_route_and_reset_to_neutral() {
let movement = Axis(0);
let look = Axis(1);
let mut input = Input::with_capacities(1, 2);
assert_eq!(input.value(Axis(99)), 0.0);
input.set_axis(movement, 0.4);
input.set_axis(look, -0.8);
input.set(Action(0), true);
input.add_pointer_delta(Vec2::ONE);
let axes = [movement, look];
let view = input.routed(&[Action(0)], true).with_blocked_axes(&axes);
assert_eq!(view.value(movement), 0.0);
assert_eq!(view.value(look), 0.0);
assert!(!view.pressed(Action(0)));
assert_eq!(input.value(movement), 0.4);
input.consume_edges();
for _ in 0..3 {
assert_eq!(input.value(movement), 0.4);
assert_eq!(input.routed(&[], false).value(look), -0.8);
assert!(!input.pressed(Action(0)));
assert_eq!(input.pointer_delta(), Vec2::ZERO);
input.consume_edges();
}
input.set_axis(movement, -0.2); assert_eq!(input.value(movement), -0.2);
input.release_all();
assert_eq!(input.value(movement), 0.0);
assert_eq!(input.value(look), 0.0);
assert!(input.released(Action(0)));
}
#[test]
fn analog_samples_are_bounded_and_nonfinite_values_are_neutral() {
let mut input = Input::default();
for (raw, expected) in [
(5.0, 1.0),
(-5.0, -1.0),
(f32::NAN, 0.0),
(f32::INFINITY, 0.0),
(f32::NEG_INFINITY, 0.0),
] {
input.set_axis(Axis(2), raw);
assert_eq!(input.value(Axis(2)), expected);
}
}
#[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);
}
}