#[path = "input/control.rs"]
mod control;
use control::cancelled_controls;
pub use control::{InputCancellationReason, InputControl};
#[path = "input/pointer.rs"]
mod pointer;
use pointer::{ALL_MOUSE_BUTTONS, mouse_button_index};
pub use pointer::{DuplicateMouseBinding, MouseButton, PointerSample, PointerSampleError};
#[path = "input/motion.rs"]
mod motion;
pub use motion::{RelativePointerMotion, RelativePointerMotionError};
#[path = "input/scroll.rs"]
mod scroll;
pub use scroll::{FrameInputEvent, PointerScrollEvent, ScrollDelta, ScrollDeltaError, ScrollUnit};
#[path = "input/capture.rs"]
mod capture;
pub use capture::{PointerCapture, PointerCaptureStatus};
#[cfg(test)]
#[path = "input/cancellation_tests.rs"]
mod cancellation_tests;
#[cfg(test)]
#[path = "input/pointer_tests.rs"]
mod pointer_tests;
#[cfg(test)]
#[path = "input/scroll_tests.rs"]
mod scroll_tests;
use std::{
collections::{HashMap, HashSet},
error::Error,
fmt,
hash::Hash,
};
use bevy_ecs::{
prelude::{Res, Resource},
system::SystemParam,
};
use sim_engine::Vec2;
use crate::identity::{ApplicationId, TransitionIntentToken, WorldGeneration};
pub const DEFAULT_INPUT_EVENT_LIMIT: usize = 1_024;
pub trait Action: Copy + Eq + Hash + Send + Sync + 'static {}
impl<T> Action for T where T: Copy + Eq + Hash + Send + Sync + 'static {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DigitalAxis2d<A: Action> {
negative_x: A,
positive_x: A,
negative_y: A,
positive_y: A,
}
impl<A: Action> DigitalAxis2d<A> {
pub const fn new(negative_x: A, positive_x: A, negative_y: A, positive_y: A) -> Self {
Self {
negative_x,
positive_x,
negative_y,
positive_y,
}
}
pub const fn negative_x(self) -> A {
self.negative_x
}
pub const fn positive_x(self) -> A {
self.positive_x
}
pub const fn negative_y(self) -> A {
self.negative_y
}
pub const fn positive_y(self) -> A {
self.positive_y
}
}
#[path = "input/keyboard.rs"]
mod keyboard;
pub use keyboard::PhysicalKeyCode;
pub(crate) use keyboard::{ALL_PHYSICAL_KEYS, SUPPORTED_PHYSICAL_KEY_COUNT, physical_key_index};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ButtonState {
Pressed,
Released,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum InputEvent {
Key {
key: PhysicalKeyCode,
state: ButtonState,
},
PointerMoved {
sample: PointerSample,
},
RelativePointerMotion {
motion: RelativePointerMotion,
},
MouseWheel {
delta: ScrollDelta,
},
MouseButton {
button: MouseButton,
state: ButtonState,
},
PointerLeft,
FocusLost,
}
impl InputEvent {
pub const fn key(key: PhysicalKeyCode, state: ButtonState) -> Self {
Self::Key { key, state }
}
pub const fn pointer_moved(sample: PointerSample) -> Self {
Self::PointerMoved { sample }
}
pub const fn relative_pointer_motion(motion: RelativePointerMotion) -> Self {
Self::RelativePointerMotion { motion }
}
pub const fn mouse_wheel(delta: ScrollDelta) -> Self {
Self::MouseWheel { delta }
}
pub const fn mouse_button(button: MouseButton, state: ButtonState) -> Self {
Self::MouseButton { button, state }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DuplicateKeyBinding {
key: PhysicalKeyCode,
}
impl DuplicateKeyBinding {
pub const fn key(self) -> PhysicalKeyCode {
self.key
}
}
impl fmt::Display for DuplicateKeyBinding {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "physical key {:?} is already bound", self.key)
}
}
impl Error for DuplicateKeyBinding {}
#[derive(Debug, Clone)]
pub(crate) struct ActionBindings<A: Action> {
by_key: [Option<A>; SUPPORTED_PHYSICAL_KEY_COUNT],
by_mouse: [Option<A>; ALL_MOUSE_BUTTONS.len()],
}
impl<A: Action> Default for ActionBindings<A> {
fn default() -> Self {
Self {
by_key: [None; SUPPORTED_PHYSICAL_KEY_COUNT],
by_mouse: [None; ALL_MOUSE_BUTTONS.len()],
}
}
}
impl<A: Action> ActionBindings<A> {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn bind_mouse_button(
&mut self,
button: MouseButton,
action: A,
) -> Result<&mut Self, DuplicateMouseBinding> {
let slot = &mut self.by_mouse[mouse_button_index(button)];
if slot.is_some() {
return Err(DuplicateMouseBinding { button });
}
*slot = Some(action);
Ok(self)
}
fn mouse_action_for(&self, button: MouseButton) -> Option<A> {
self.by_mouse[mouse_button_index(button)]
}
fn control_action_for(&self, control: InputControl) -> Option<A> {
match control {
InputControl::Key(key) => self.action_for(key),
InputControl::MouseButton(button) => self.mouse_action_for(button),
}
}
pub(crate) fn bind(
&mut self,
key: PhysicalKeyCode,
action: A,
) -> Result<&mut Self, DuplicateKeyBinding> {
self.bind_all([(key, action)])
}
pub(crate) fn bind_wasd(
&mut self,
axis: DigitalAxis2d<A>,
) -> Result<&mut Self, DuplicateKeyBinding> {
self.bind_all([
(PhysicalKeyCode::KeyW, axis.positive_y()),
(PhysicalKeyCode::KeyA, axis.negative_x()),
(PhysicalKeyCode::KeyS, axis.negative_y()),
(PhysicalKeyCode::KeyD, axis.positive_x()),
])
}
pub(crate) fn bind_arrows(
&mut self,
axis: DigitalAxis2d<A>,
) -> Result<&mut Self, DuplicateKeyBinding> {
self.bind_all([
(PhysicalKeyCode::ArrowLeft, axis.negative_x()),
(PhysicalKeyCode::ArrowRight, axis.positive_x()),
(PhysicalKeyCode::ArrowDown, axis.negative_y()),
(PhysicalKeyCode::ArrowUp, axis.positive_y()),
])
}
pub(crate) fn bind_wasd_and_arrows(
&mut self,
axis: DigitalAxis2d<A>,
) -> Result<&mut Self, DuplicateKeyBinding> {
self.bind_all([
(PhysicalKeyCode::KeyW, axis.positive_y()),
(PhysicalKeyCode::KeyA, axis.negative_x()),
(PhysicalKeyCode::KeyS, axis.negative_y()),
(PhysicalKeyCode::KeyD, axis.positive_x()),
(PhysicalKeyCode::ArrowLeft, axis.negative_x()),
(PhysicalKeyCode::ArrowRight, axis.positive_x()),
(PhysicalKeyCode::ArrowDown, axis.negative_y()),
(PhysicalKeyCode::ArrowUp, axis.positive_y()),
])
}
fn bind_all<const N: usize>(
&mut self,
bindings: [(PhysicalKeyCode, A); N],
) -> Result<&mut Self, DuplicateKeyBinding> {
for (key, _) in &bindings {
if self.action_for(*key).is_some() {
return Err(DuplicateKeyBinding { key: *key });
}
}
for (key, action) in bindings {
self.by_key[physical_key_index(key)] = Some(action);
}
Ok(self)
}
fn action_for(&self, key: PhysicalKeyCode) -> Option<A> {
self.by_key[physical_key_index(key)]
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputCollectionError {
RelativeMotion(RelativePointerMotionError),
ZeroEventLimit,
StorageAllocationFailed {
limit: usize,
},
EventLimitExceeded {
limit: usize,
received: usize,
},
FrameEdgeLimitExceeded {
limit: usize,
incoming: usize,
},
RetainedFixedEdgeLimitExceeded {
limit: usize,
retained: usize,
incoming: usize,
},
OccurrenceIdentityExhausted,
}
impl fmt::Display for InputCollectionError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RelativeMotion(error) => {
write!(formatter, "relative input collection failed: {error}")
}
Self::ZeroEventLimit => {
formatter.write_str("the input event and retained-edge limit must be positive")
}
Self::StorageAllocationFailed { limit } => write!(
formatter,
"failed to reserve bounded input storage for the configured limit of {limit}"
),
Self::EventLimitExceeded { limit, received } => write!(
formatter,
"input frame contains {received} physical events, exceeding the limit of {limit}"
),
Self::FrameEdgeLimitExceeded { limit, incoming } => write!(
formatter,
"input frame generates {incoming} logical edges, exceeding the limit of {limit}"
),
Self::RetainedFixedEdgeLimitExceeded {
limit,
retained,
incoming,
} => write!(
formatter,
"fixed input retains {retained} edges and cannot accept {incoming} more without exceeding the limit of {limit}"
),
Self::OccurrenceIdentityExhausted => {
formatter.write_str("input occurrence identity space is exhausted")
}
}
}
}
impl Error for InputCollectionError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ActionEdge<A: Action> {
action: A,
state: ButtonState,
intent: TransitionIntentToken,
pointer: Option<PointerSample>,
control: InputControl,
cancellation: Option<InputCancellationReason>,
ordinal: usize,
}
impl<A: Action> ActionEdge<A> {
pub const fn control(self) -> InputControl {
self.control
}
pub const fn cancellation_reason(self) -> Option<InputCancellationReason> {
self.cancellation
}
pub const fn is_cancelled(self) -> bool {
self.cancellation.is_some()
}
pub const fn pointer(self) -> Option<PointerSample> {
self.pointer
}
pub const fn action(self) -> A {
self.action
}
pub const fn state(self) -> ButtonState {
self.state
}
pub const fn intent(self) -> TransitionIntentToken {
self.intent
}
}
#[derive(Debug, Clone, Resource)]
pub(crate) struct FrameInputState<A: Action> {
held: HashSet<A>,
edges: Vec<ActionEdge<A>>,
scrolls: Vec<PointerScrollEvent>,
pointer: Option<PointerSample>,
relative_motion: RelativePointerMotion,
focus_lost: bool,
}
impl<A: Action> FrameInputState<A> {
pub fn held(&self, action: A) -> bool {
self.held.contains(&action)
}
fn digital_axis(&self, axis: DigitalAxis2d<A>) -> Vec2 {
sample_digital_axis(axis, |action| self.held(action))
}
pub fn pressed(&self, action: A) -> impl Iterator<Item = ActionEdge<A>> + '_ {
self.edges
.iter()
.copied()
.filter(move |edge| edge.action == action && edge.state == ButtonState::Pressed)
}
pub fn released(&self, action: A) -> impl Iterator<Item = ActionEdge<A>> + '_ {
self.edges
.iter()
.copied()
.filter(move |edge| edge.action == action && edge.state == ButtonState::Released)
}
pub(crate) fn empty() -> Self {
Self {
held: HashSet::new(),
edges: Vec::new(),
scrolls: Vec::new(),
pointer: None,
relative_motion: RelativePointerMotion::ZERO,
focus_lost: false,
}
}
pub(crate) fn clear_reusing_storage(&mut self) {
self.held.clear();
self.edges.clear();
self.scrolls.clear();
self.pointer = None;
self.relative_motion = RelativePointerMotion::ZERO;
self.focus_lost = false;
}
fn replace_from(&mut self, held_action_counts: &HashMap<A, usize>, edges: &[ActionEdge<A>]) {
replace_snapshot(&mut self.held, &mut self.edges, held_action_counts, edges);
}
}
#[derive(SystemParam)]
pub struct FrameInput<'w, A: Action> {
state: Res<'w, FrameInputState<A>>,
}
impl<A: Action> FrameInput<'_, A> {
pub fn scroll_events(&self) -> impl Iterator<Item = PointerScrollEvent> + '_ {
self.state.scrolls.iter().copied()
}
pub fn events(&self) -> impl Iterator<Item = FrameInputEvent<A>> + '_ {
scroll::ordered_events(&self.state.edges, &self.state.scrolls)
}
pub fn relative_motion(&self) -> RelativePointerMotion {
self.state.relative_motion
}
pub fn focus_lost(&self) -> bool {
self.state.focus_lost
}
pub fn pointer(&self) -> Option<PointerSample> {
self.state.pointer
}
pub fn held(&self, action: A) -> bool {
self.state.held(action)
}
pub fn has_press_occurrence(&self, action: A) -> bool {
self.pressed(action).next().is_some()
}
pub fn has_release_occurrence(&self, action: A) -> bool {
self.released(action).next().is_some()
}
pub fn digital_axis(&self, axis: DigitalAxis2d<A>) -> Vec2 {
self.state.digital_axis(axis)
}
pub fn normalized_digital_axis(&self, axis: DigitalAxis2d<A>) -> Vec2 {
self.digital_axis(axis).normalized()
}
pub fn pressed(&self, action: A) -> impl Iterator<Item = ActionEdge<A>> + '_ {
self.state.pressed(action)
}
pub fn edges(&self) -> impl Iterator<Item = ActionEdge<A>> + '_ {
self.state.edges.iter().copied()
}
pub fn released(&self, action: A) -> impl Iterator<Item = ActionEdge<A>> + '_ {
self.state.released(action)
}
}
#[derive(Debug, Clone, Resource)]
pub(crate) struct FixedInputState<A: Action> {
held: HashSet<A>,
edges: Vec<ActionEdge<A>>,
pointer: Option<PointerSample>,
focus_lost: bool,
}
impl<A: Action> FixedInputState<A> {
pub fn held(&self, action: A) -> bool {
self.held.contains(&action)
}
fn digital_axis(&self, axis: DigitalAxis2d<A>) -> Vec2 {
sample_digital_axis(axis, |action| self.held(action))
}
pub fn pressed(&self, action: A) -> impl Iterator<Item = ActionEdge<A>> + '_ {
self.edges
.iter()
.copied()
.filter(move |edge| edge.action == action && edge.state == ButtonState::Pressed)
}
pub fn released(&self, action: A) -> impl Iterator<Item = ActionEdge<A>> + '_ {
self.edges
.iter()
.copied()
.filter(move |edge| edge.action == action && edge.state == ButtonState::Released)
}
pub(crate) fn empty() -> Self {
Self {
held: HashSet::new(),
edges: Vec::new(),
pointer: None,
focus_lost: false,
}
}
pub(crate) fn clear_reusing_storage(&mut self) {
self.held.clear();
self.edges.clear();
self.pointer = None;
self.focus_lost = false;
}
fn replace_from(&mut self, held_action_counts: &HashMap<A, usize>, edges: &[ActionEdge<A>]) {
replace_snapshot(&mut self.held, &mut self.edges, held_action_counts, edges);
}
}
#[derive(SystemParam)]
pub struct FixedInput<'w, A: Action> {
state: Res<'w, FixedInputState<A>>,
}
impl<A: Action> FixedInput<'_, A> {
pub fn focus_lost(&self) -> bool {
self.state.focus_lost
}
pub fn pointer(&self) -> Option<PointerSample> {
self.state.pointer
}
pub fn held(&self, action: A) -> bool {
self.state.held(action)
}
pub fn has_press_occurrence(&self, action: A) -> bool {
self.pressed(action).next().is_some()
}
pub fn has_release_occurrence(&self, action: A) -> bool {
self.released(action).next().is_some()
}
pub fn digital_axis(&self, axis: DigitalAxis2d<A>) -> Vec2 {
self.state.digital_axis(axis)
}
pub fn normalized_digital_axis(&self, axis: DigitalAxis2d<A>) -> Vec2 {
self.digital_axis(axis).normalized()
}
pub fn pressed(&self, action: A) -> impl Iterator<Item = ActionEdge<A>> + '_ {
self.state.pressed(action)
}
pub fn released(&self, action: A) -> impl Iterator<Item = ActionEdge<A>> + '_ {
self.state.released(action)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct InputCollectionReport {
pub(crate) physical_events: usize,
pub(crate) logical_edges: usize,
pub(crate) suppressed_repeats: usize,
pub(crate) unmapped_events: usize,
}
#[derive(Clone, Copy)]
struct EdgeDelivery {
paused: bool,
application: ApplicationId,
origin: WorldGeneration,
frame: u64,
ordinal: usize,
}
#[derive(Debug)]
pub(crate) struct InputState<A: Action> {
bindings: ActionBindings<A>,
event_limit: usize,
held_keys: [bool; SUPPORTED_PHYSICAL_KEY_COUNT],
held_mouse: [bool; ALL_MOUSE_BUTTONS.len()],
pointer: Option<PointerSample>,
relative_motion: RelativePointerMotion,
focus_lost: bool,
held_action_counts: HashMap<A, usize>,
frame_edges: Vec<ActionEdge<A>>,
frame_scrolls: Vec<PointerScrollEvent>,
fixed_edges: Vec<ActionEdge<A>>,
next_occurrence: u64,
}
impl<A: Action> InputState<A> {
pub(crate) fn new(
bindings: ActionBindings<A>,
event_limit: usize,
) -> Result<Self, InputCollectionError> {
if event_limit == 0 {
return Err(InputCollectionError::ZeroEventLimit);
}
let mut held_action_counts = HashMap::new();
let mut frame_edges = Vec::new();
let mut frame_scrolls = Vec::new();
let mut fixed_edges = Vec::new();
let held_capacity = SUPPORTED_PHYSICAL_KEY_COUNT + ALL_MOUSE_BUTTONS.len();
held_action_counts
.try_reserve(held_capacity)
.map_err(|_| InputCollectionError::StorageAllocationFailed { limit: event_limit })?;
frame_edges
.try_reserve(event_limit)
.map_err(|_| InputCollectionError::StorageAllocationFailed { limit: event_limit })?;
frame_scrolls
.try_reserve(event_limit)
.map_err(|_| InputCollectionError::StorageAllocationFailed { limit: event_limit })?;
fixed_edges
.try_reserve(event_limit)
.map_err(|_| InputCollectionError::StorageAllocationFailed { limit: event_limit })?;
Ok(Self {
bindings,
event_limit,
held_keys: [false; SUPPORTED_PHYSICAL_KEY_COUNT],
held_mouse: [false; ALL_MOUSE_BUTTONS.len()],
pointer: None,
relative_motion: RelativePointerMotion::ZERO,
focus_lost: false,
held_action_counts,
frame_edges,
frame_scrolls,
fixed_edges,
next_occurrence: 0,
})
}
pub(crate) fn collect_frame(
&mut self,
events: &[InputEvent],
paused: bool,
application: ApplicationId,
origin: WorldGeneration,
frame: u64,
) -> Result<InputCollectionReport, InputCollectionError> {
if events.len() > self.event_limit {
return Err(InputCollectionError::EventLimitExceeded {
limit: self.event_limit,
received: events.len(),
});
}
let (relative_motion, focus_lost) =
motion::frame_motion(events).map_err(InputCollectionError::RelativeMotion)?;
let report = self.preflight_frame(events, paused)?;
let logical_edge_count = u64::try_from(report.logical_edges)
.map_err(|_| InputCollectionError::OccurrenceIdentityExhausted)?;
if self
.next_occurrence
.checked_add(logical_edge_count)
.is_none()
{
return Err(InputCollectionError::OccurrenceIdentityExhausted);
}
self.frame_edges.clear();
self.frame_scrolls.clear();
self.relative_motion = relative_motion;
self.focus_lost = focus_lost;
let mut delivery = EdgeDelivery {
paused,
application,
origin,
frame,
ordinal: 0,
};
for (ordinal, event) in events.iter().enumerate() {
delivery.ordinal = ordinal;
match *event {
InputEvent::Key { key, state } => {
self.record_edge(InputControl::Key(key), state, None, delivery);
}
InputEvent::MouseButton { button, state } => {
self.record_edge(InputControl::MouseButton(button), state, None, delivery);
}
InputEvent::PointerMoved { sample } => self.pointer = Some(sample),
InputEvent::RelativePointerMotion { .. } => {}
InputEvent::MouseWheel { delta } => {
if !focus_lost {
self.frame_scrolls.push(PointerScrollEvent {
delta,
pointer: self.pointer,
ordinal,
});
}
}
InputEvent::PointerLeft | InputEvent::FocusLost => {
let reason = cancellation_reason(*event);
self.pointer = None;
for control in cancelled_controls(reason) {
self.record_edge(control, ButtonState::Released, Some(reason), delivery);
}
}
}
}
Ok(report)
}
fn preflight_frame(
&self,
events: &[InputEvent],
paused: bool,
) -> Result<InputCollectionReport, InputCollectionError> {
let mut held_keys = self.held_keys;
let mut held_mouse = self.held_mouse;
let mut report = InputCollectionReport {
physical_events: events.len(),
logical_edges: 0,
suppressed_repeats: 0,
unmapped_events: 0,
};
for event in events {
match *event {
InputEvent::Key { key, state } => {
preflight_button(
self.bindings.action_for(key),
&mut held_keys[physical_key_index(key)],
state,
&mut report,
);
}
InputEvent::MouseButton { button, state } => {
preflight_button(
self.bindings.mouse_action_for(button),
&mut held_mouse[mouse_button_index(button)],
state,
&mut report,
);
}
InputEvent::PointerMoved { .. }
| InputEvent::RelativePointerMotion { .. }
| InputEvent::MouseWheel { .. } => {}
InputEvent::PointerLeft | InputEvent::FocusLost => {
for control in cancelled_controls(cancellation_reason(*event)) {
let held = match control {
InputControl::Key(key) => &mut held_keys[physical_key_index(key)],
InputControl::MouseButton(button) => {
&mut held_mouse[mouse_button_index(button)]
}
};
if self.bindings.control_action_for(control).is_some()
&& change_button(held, ButtonState::Released)
{
report.logical_edges += 1;
}
}
}
}
}
if report.logical_edges > self.event_limit {
return Err(InputCollectionError::FrameEdgeLimitExceeded {
limit: self.event_limit,
incoming: report.logical_edges,
});
}
if !paused && report.logical_edges > self.event_limit.saturating_sub(self.fixed_edges.len())
{
return Err(InputCollectionError::RetainedFixedEdgeLimitExceeded {
limit: self.event_limit,
retained: self.fixed_edges.len(),
incoming: report.logical_edges,
});
}
Ok(report)
}
#[cfg(test)]
pub(crate) fn frame_input(&self) -> FrameInputState<A> {
let mut snapshot = FrameInputState::empty();
self.copy_frame_snapshot_into(&mut snapshot);
snapshot
}
#[cfg(test)]
pub(crate) fn fixed_input(&self) -> FixedInputState<A> {
let mut snapshot = FixedInputState::empty();
self.copy_fixed_snapshot_into(&mut snapshot);
snapshot
}
pub(crate) fn copy_frame_snapshot_into(&self, target: &mut FrameInputState<A>) {
target.replace_from(&self.held_action_counts, &self.frame_edges);
target.scrolls.clear();
target.scrolls.extend_from_slice(&self.frame_scrolls);
target.pointer = self.pointer;
target.relative_motion = self.relative_motion;
target.focus_lost = self.focus_lost;
}
pub(crate) fn copy_fixed_snapshot_into(&self, target: &mut FixedInputState<A>) {
target.replace_from(&self.held_action_counts, &self.fixed_edges);
target.pointer = self.pointer;
target.focus_lost = self.focus_lost;
}
pub(crate) fn consume_fixed_delivery(&mut self) {
self.fixed_edges.clear();
}
pub(crate) fn discard_fixed_edges(&mut self) {
self.fixed_edges.clear();
}
pub(crate) fn consume_token_occurrence(&mut self, token: TransitionIntentToken) -> bool {
let was_live = self.token_is_live(token);
self.frame_edges.retain(|edge| edge.intent != token);
self.fixed_edges.retain(|edge| edge.intent != token);
was_live
}
pub(crate) fn token_is_live(&self, token: TransitionIntentToken) -> bool {
self.frame_edges
.iter()
.chain(&self.fixed_edges)
.any(|edge| edge.intent == token)
}
pub(crate) fn token_is_live_in_frame(&self, token: TransitionIntentToken) -> bool {
self.frame_edges.iter().any(|edge| edge.intent == token)
}
pub(crate) fn token_is_live_in_fixed(&self, token: TransitionIntentToken) -> bool {
self.fixed_edges.iter().any(|edge| edge.intent == token)
}
pub(crate) fn clear_world_edges(&mut self) {
self.frame_edges.clear();
self.frame_scrolls.clear();
self.fixed_edges.clear();
self.relative_motion = RelativePointerMotion::ZERO;
self.focus_lost = false;
}
pub(crate) fn end_frame(&mut self) {
self.frame_edges.clear();
self.frame_scrolls.clear();
self.relative_motion = RelativePointerMotion::ZERO;
self.focus_lost = false;
}
fn record_edge(
&mut self,
control: InputControl,
state: ButtonState,
cancellation: Option<InputCancellationReason>,
delivery: EdgeDelivery,
) {
let Some(action) = self.bindings.control_action_for(control) else {
return;
};
let held = match control {
InputControl::Key(key) => &mut self.held_keys[physical_key_index(key)],
InputControl::MouseButton(button) => &mut self.held_mouse[mouse_button_index(button)],
};
if !change_button(held, state) {
return;
}
let pointer = match (control, cancellation) {
(InputControl::MouseButton(_), None) => self.pointer,
_ => None,
};
self.update_held_action(action, state);
let occurrence = self.next_occurrence;
self.next_occurrence += 1;
let edge = ActionEdge {
action,
state,
pointer,
control,
cancellation,
ordinal: delivery.ordinal,
intent: TransitionIntentToken::input(
delivery.application,
delivery.origin,
delivery.frame,
occurrence,
),
};
self.frame_edges.push(edge);
if !delivery.paused {
self.fixed_edges.push(edge);
}
}
fn update_held_action(&mut self, action: A, state: ButtonState) {
match state {
ButtonState::Pressed => {
let count = self.held_action_counts.entry(action).or_insert(0);
*count += 1;
}
ButtonState::Released => {
if let Some(count) = self.held_action_counts.get_mut(&action) {
*count -= 1;
if *count == 0 {
self.held_action_counts.remove(&action);
}
}
}
}
}
}
fn cancellation_reason(event: InputEvent) -> InputCancellationReason {
if matches!(event, InputEvent::FocusLost) {
InputCancellationReason::FocusLost
} else {
InputCancellationReason::PointerLeft
}
}
fn change_button(held: &mut bool, state: ButtonState) -> bool {
let next = state == ButtonState::Pressed;
let changed = *held != next;
*held = next;
changed
}
fn preflight_button<A: Action>(
action: Option<A>,
held: &mut bool,
state: ButtonState,
report: &mut InputCollectionReport,
) {
if action.is_none() {
report.unmapped_events += 1;
} else if change_button(held, state) {
report.logical_edges += 1;
} else {
report.suppressed_repeats += 1;
}
}
fn replace_snapshot<A: Action>(
target_held: &mut HashSet<A>,
target_edges: &mut Vec<ActionEdge<A>>,
held_action_counts: &HashMap<A, usize>,
source_edges: &[ActionEdge<A>],
) {
target_held.clear();
target_held.extend(held_action_counts.keys().copied());
target_edges.clear();
target_edges.extend_from_slice(source_edges);
}
fn sample_digital_axis<A: Action>(axis: DigitalAxis2d<A>, mut held: impl FnMut(A) -> bool) -> Vec2 {
let x = f32::from(held(axis.positive_x)) - f32::from(held(axis.negative_x));
let y = f32::from(held(axis.positive_y)) - f32::from(held(axis.negative_y));
Vec2::new(x, y)
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum TestAction {
Move,
Enter,
Fire,
Left,
Right,
Down,
Up,
}
const TEST_AXIS: DigitalAxis2d<TestAction> = DigitalAxis2d::new(
TestAction::Left,
TestAction::Right,
TestAction::Down,
TestAction::Up,
);
fn identity() -> (ApplicationId, WorldGeneration) {
let application = ApplicationId::from_raw(7);
(application, WorldGeneration::new(application, 3))
}
fn bindings() -> ActionBindings<TestAction> {
let mut bindings = ActionBindings::new();
assert!(
bindings
.bind(PhysicalKeyCode::KeyW, TestAction::Move)
.is_ok()
);
assert!(
bindings
.bind(PhysicalKeyCode::KeyA, TestAction::Move)
.is_ok()
);
assert!(
bindings
.bind(PhysicalKeyCode::Enter, TestAction::Enter)
.is_ok()
);
assert!(
bindings
.bind(PhysicalKeyCode::Space, TestAction::Fire)
.is_ok()
);
bindings
}
fn state(limit: usize) -> InputState<TestAction> {
let result = InputState::new(bindings(), limit);
let Ok(state) = result else {
panic!("test input state should be valid");
};
state
}
#[test]
fn duplicate_physical_key_is_rejected_without_replacement() {
let mut bindings = bindings();
let duplicate = bindings.bind(PhysicalKeyCode::KeyW, TestAction::Enter);
assert_eq!(
duplicate.map(|_| ()),
Err(DuplicateKeyBinding {
key: PhysicalKeyCode::KeyW,
})
);
assert_eq!(
bindings.action_for(PhysicalKeyCode::KeyW),
Some(TestAction::Move)
);
}
#[test]
fn physical_key_catalog_is_unique_and_round_trips_every_index() {
assert_eq!(SUPPORTED_PHYSICAL_KEY_COUNT, 108);
let mut unique = HashSet::new();
for (index, key) in ALL_PHYSICAL_KEYS.into_iter().enumerate() {
assert!(unique.insert(key), "duplicate catalog key {key:?}");
assert_eq!(physical_key_index(key), index);
assert_eq!(ALL_PHYSICAL_KEYS[physical_key_index(key)], key);
}
}
#[test]
fn wasd_binding_maps_axis_slots_and_remains_compatible_with_manual_keys() {
let mut bindings = ActionBindings::new();
assert!(
bindings
.bind(PhysicalKeyCode::Enter, TestAction::Enter)
.is_ok()
);
assert!(bindings.bind_wasd(TEST_AXIS).is_ok());
assert!(
bindings
.bind(PhysicalKeyCode::Space, TestAction::Fire)
.is_ok()
);
assert_eq!(
bindings.action_for(PhysicalKeyCode::KeyW),
Some(TestAction::Up)
);
assert_eq!(
bindings.action_for(PhysicalKeyCode::KeyA),
Some(TestAction::Left)
);
assert_eq!(
bindings.action_for(PhysicalKeyCode::KeyS),
Some(TestAction::Down)
);
assert_eq!(
bindings.action_for(PhysicalKeyCode::KeyD),
Some(TestAction::Right)
);
assert_eq!(
bindings.action_for(PhysicalKeyCode::Enter),
Some(TestAction::Enter)
);
assert_eq!(
bindings.action_for(PhysicalKeyCode::Space),
Some(TestAction::Fire)
);
}
#[test]
fn wasd_binding_conflicts_are_atomic_for_every_key() {
let preset_keys = [
PhysicalKeyCode::KeyW,
PhysicalKeyCode::KeyA,
PhysicalKeyCode::KeyS,
PhysicalKeyCode::KeyD,
];
for occupied in preset_keys {
let mut bindings = ActionBindings::new();
assert!(bindings.bind(occupied, TestAction::Move).is_ok());
assert_eq!(
bindings.bind_wasd(TEST_AXIS).map(|_| ()),
Err(DuplicateKeyBinding { key: occupied })
);
for key in preset_keys {
let expected = (key == occupied).then_some(TestAction::Move);
assert_eq!(bindings.action_for(key), expected, "occupied={occupied:?}");
}
}
}
#[test]
fn wasd_binding_uses_deterministic_conflict_order_and_allows_repeated_actions() {
let mut conflicted = ActionBindings::new();
assert!(
conflicted
.bind(PhysicalKeyCode::KeyS, TestAction::Fire)
.is_ok()
);
assert!(
conflicted
.bind(PhysicalKeyCode::KeyW, TestAction::Move)
.is_ok()
);
assert_eq!(
conflicted.bind_wasd(TEST_AXIS).map(|_| ()),
Err(DuplicateKeyBinding {
key: PhysicalKeyCode::KeyW,
})
);
let repeated = DigitalAxis2d::new(
TestAction::Move,
TestAction::Move,
TestAction::Move,
TestAction::Move,
);
let mut bindings = ActionBindings::new();
assert!(bindings.bind_wasd(repeated).is_ok());
for key in [
PhysicalKeyCode::KeyW,
PhysicalKeyCode::KeyA,
PhysicalKeyCode::KeyS,
PhysicalKeyCode::KeyD,
] {
assert_eq!(bindings.action_for(key), Some(TestAction::Move));
}
}
#[test]
fn arrow_binding_maps_axis_slots_and_coexists_with_wasd_and_manual_escape() {
let mut bindings = ActionBindings::new();
assert!(bindings.bind_wasd(TEST_AXIS).is_ok());
assert!(bindings.bind_arrows(TEST_AXIS).is_ok());
assert!(
bindings
.bind(PhysicalKeyCode::Escape, TestAction::Enter)
.is_ok()
);
for (key, expected) in [
(PhysicalKeyCode::ArrowLeft, TestAction::Left),
(PhysicalKeyCode::ArrowRight, TestAction::Right),
(PhysicalKeyCode::ArrowDown, TestAction::Down),
(PhysicalKeyCode::ArrowUp, TestAction::Up),
] {
assert_eq!(bindings.action_for(key), Some(expected));
}
assert_eq!(
bindings.action_for(PhysicalKeyCode::Escape),
Some(TestAction::Enter)
);
}
#[test]
fn arrow_binding_conflicts_are_atomic_and_use_axis_slot_order() {
let arrow_keys = [
PhysicalKeyCode::ArrowLeft,
PhysicalKeyCode::ArrowRight,
PhysicalKeyCode::ArrowDown,
PhysicalKeyCode::ArrowUp,
];
for occupied in arrow_keys {
let mut bindings = ActionBindings::new();
assert!(bindings.bind(occupied, TestAction::Move).is_ok());
assert_eq!(
bindings.bind_arrows(TEST_AXIS).map(|_| ()),
Err(DuplicateKeyBinding { key: occupied })
);
for key in arrow_keys {
let expected = (key == occupied).then_some(TestAction::Move);
assert_eq!(bindings.action_for(key), expected, "occupied={occupied:?}");
}
}
let mut two_conflicts = ActionBindings::new();
assert!(
two_conflicts
.bind(PhysicalKeyCode::ArrowUp, TestAction::Fire)
.is_ok()
);
assert!(
two_conflicts
.bind(PhysicalKeyCode::ArrowRight, TestAction::Move)
.is_ok()
);
assert_eq!(
two_conflicts.bind_arrows(TEST_AXIS).map(|_| ()),
Err(DuplicateKeyBinding {
key: PhysicalKeyCode::ArrowRight,
})
);
let repeated = DigitalAxis2d::new(
TestAction::Move,
TestAction::Move,
TestAction::Move,
TestAction::Move,
);
let mut bindings = ActionBindings::new();
assert!(bindings.bind_arrows(repeated).is_ok());
for key in arrow_keys {
assert_eq!(bindings.action_for(key), Some(TestAction::Move));
}
}
#[test]
fn combined_directional_binding_maps_every_slot_and_preserves_manual_keys() {
let mut bindings = ActionBindings::new();
assert!(
bindings
.bind(PhysicalKeyCode::Escape, TestAction::Enter)
.is_ok()
);
assert!(bindings.bind_wasd_and_arrows(TEST_AXIS).is_ok());
for (key, expected) in [
(PhysicalKeyCode::KeyW, TestAction::Up),
(PhysicalKeyCode::KeyA, TestAction::Left),
(PhysicalKeyCode::KeyS, TestAction::Down),
(PhysicalKeyCode::KeyD, TestAction::Right),
(PhysicalKeyCode::ArrowLeft, TestAction::Left),
(PhysicalKeyCode::ArrowRight, TestAction::Right),
(PhysicalKeyCode::ArrowDown, TestAction::Down),
(PhysicalKeyCode::ArrowUp, TestAction::Up),
] {
assert_eq!(bindings.action_for(key), Some(expected));
}
assert_eq!(
bindings.action_for(PhysicalKeyCode::Escape),
Some(TestAction::Enter)
);
assert_eq!(bindings.action_for(PhysicalKeyCode::Enter), None);
assert_eq!(bindings.action_for(PhysicalKeyCode::Space), None);
let before_repeat = ALL_PHYSICAL_KEYS.map(|key| bindings.action_for(key));
assert_eq!(
bindings.bind_wasd_and_arrows(TEST_AXIS).map(|_| ()),
Err(DuplicateKeyBinding {
key: PhysicalKeyCode::KeyW,
})
);
assert_eq!(
ALL_PHYSICAL_KEYS.map(|key| bindings.action_for(key)),
before_repeat
);
}
#[test]
fn combined_directional_binding_conflicts_are_atomic_for_every_key() {
let preset_keys = [
PhysicalKeyCode::KeyW,
PhysicalKeyCode::KeyA,
PhysicalKeyCode::KeyS,
PhysicalKeyCode::KeyD,
PhysicalKeyCode::ArrowLeft,
PhysicalKeyCode::ArrowRight,
PhysicalKeyCode::ArrowDown,
PhysicalKeyCode::ArrowUp,
];
for occupied in preset_keys {
let mut bindings = ActionBindings::new();
assert!(bindings.bind(occupied, TestAction::Move).is_ok());
assert!(
bindings
.bind(PhysicalKeyCode::Enter, TestAction::Enter)
.is_ok()
);
let before = ALL_PHYSICAL_KEYS.map(|key| bindings.action_for(key));
assert_eq!(
bindings.bind_wasd_and_arrows(TEST_AXIS).map(|_| ()),
Err(DuplicateKeyBinding { key: occupied })
);
assert_eq!(
ALL_PHYSICAL_KEYS.map(|key| bindings.action_for(key)),
before,
"occupied={occupied:?}"
);
}
}
#[test]
fn combined_directional_binding_uses_one_order_and_allows_repeated_actions() {
let preset_keys = [
PhysicalKeyCode::KeyW,
PhysicalKeyCode::KeyA,
PhysicalKeyCode::KeyS,
PhysicalKeyCode::KeyD,
PhysicalKeyCode::ArrowLeft,
PhysicalKeyCode::ArrowRight,
PhysicalKeyCode::ArrowDown,
PhysicalKeyCode::ArrowUp,
];
for first_index in 0..preset_keys.len() {
for second_index in (first_index + 1)..preset_keys.len() {
let mut conflicted = ActionBindings::new();
assert!(
conflicted
.bind(preset_keys[second_index], TestAction::Fire)
.is_ok()
);
assert!(
conflicted
.bind(preset_keys[first_index], TestAction::Move)
.is_ok()
);
let before = ALL_PHYSICAL_KEYS.map(|key| conflicted.action_for(key));
assert_eq!(
conflicted.bind_wasd_and_arrows(TEST_AXIS).map(|_| ()),
Err(DuplicateKeyBinding {
key: preset_keys[first_index],
})
);
assert_eq!(
ALL_PHYSICAL_KEYS.map(|key| conflicted.action_for(key)),
before
);
}
}
let repeated = DigitalAxis2d::new(
TestAction::Move,
TestAction::Move,
TestAction::Move,
TestAction::Move,
);
let mut bindings = ActionBindings::new();
assert!(bindings.bind_wasd_and_arrows(repeated).is_ok());
for key in preset_keys {
assert_eq!(bindings.action_for(key), Some(TestAction::Move));
}
}
#[test]
fn digital_axis_descriptor_is_const_copy_and_preserves_slot_order() {
const COPY: DigitalAxis2d<TestAction> = TEST_AXIS;
assert_eq!(COPY.negative_x(), TestAction::Left);
assert_eq!(COPY.positive_x(), TestAction::Right);
assert_eq!(COPY.negative_y(), TestAction::Down);
assert_eq!(COPY.positive_y(), TestAction::Up);
}
#[test]
fn frame_and_fixed_digital_axes_cover_all_held_combinations() {
let actions = [
TestAction::Left,
TestAction::Right,
TestAction::Down,
TestAction::Up,
];
for mask in 0_u8..16 {
let held: HashSet<_> = actions
.into_iter()
.enumerate()
.filter_map(|(index, action)| (mask & (1 << index) != 0).then_some(action))
.collect();
let frame = FrameInputState {
held: held.clone(),
..FrameInputState::empty()
};
let fixed = FixedInputState {
held,
..FixedInputState::empty()
};
let expected = Vec2::new(
f32::from(mask & 0b0010 != 0) - f32::from(mask & 0b0001 != 0),
f32::from(mask & 0b1000 != 0) - f32::from(mask & 0b0100 != 0),
);
assert_eq!(frame.digital_axis(TEST_AXIS), expected, "mask {mask:04b}");
assert_eq!(fixed.digital_axis(TEST_AXIS), expected, "mask {mask:04b}");
}
let diagonal = FrameInputState {
held: HashSet::from([TestAction::Right, TestAction::Up]),
..FrameInputState::empty()
}
.digital_axis(TEST_AXIS);
assert_eq!(diagonal, Vec2::ONE);
assert!((diagonal.normalized().length() - 1.0).abs() < 0.000_1);
}
#[test]
fn repeated_actions_follow_independent_slot_arithmetic() {
let held = |action| action == TestAction::Move;
let cancelled = DigitalAxis2d::new(
TestAction::Move,
TestAction::Move,
TestAction::Enter,
TestAction::Fire,
);
let diagonal = DigitalAxis2d::new(
TestAction::Enter,
TestAction::Move,
TestAction::Fire,
TestAction::Move,
);
assert_eq!(sample_digital_axis(cancelled, held), Vec2::ZERO);
assert_eq!(sample_digital_axis(diagonal, held), Vec2::ONE);
}
#[test]
fn digital_axis_does_not_consume_press_or_release_edges() {
let (application, generation) = identity();
let mut input = state(4);
let events = [
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed),
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Released),
];
assert!(
input
.collect_frame(&events, false, application, generation, 1)
.is_ok()
);
let axis = DigitalAxis2d::new(
TestAction::Enter,
TestAction::Move,
TestAction::Fire,
TestAction::Fire,
);
let frame = input.frame_input();
let fixed = input.fixed_input();
assert_eq!(frame.digital_axis(axis), Vec2::ZERO);
assert_eq!(fixed.digital_axis(axis), Vec2::ZERO);
assert_eq!(frame.pressed(TestAction::Move).count(), 1);
assert_eq!(frame.released(TestAction::Move).count(), 1);
assert_eq!(fixed.pressed(TestAction::Move).count(), 1);
assert_eq!(fixed.released(TestAction::Move).count(), 1);
assert_eq!(frame.digital_axis(axis), Vec2::ZERO);
assert_eq!(fixed.digital_axis(axis), Vec2::ZERO);
}
#[test]
fn space_produces_typed_press_and_release_edges() {
let (application, generation) = identity();
let mut input = state(2);
assert!(
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::Space,
ButtonState::Pressed,
)],
false,
application,
generation,
1,
)
.is_ok()
);
assert!(input.frame_input().held(TestAction::Fire));
assert_eq!(input.frame_input().pressed(TestAction::Fire).count(), 1);
input.end_frame();
assert!(
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::Space,
ButtonState::Released,
)],
false,
application,
generation,
2,
)
.is_ok()
);
assert!(!input.frame_input().held(TestAction::Fire));
assert_eq!(input.frame_input().released(TestAction::Fire).count(), 1);
}
#[test]
fn unreservable_input_limit_returns_a_typed_creation_error() {
let result = InputState::new(bindings(), usize::MAX);
assert!(matches!(
result,
Err(InputCollectionError::StorageAllocationFailed { limit: usize::MAX })
));
}
#[test]
fn overflow_rejects_the_frame_before_mutating_input() {
let (application, generation) = identity();
let mut input = state(1);
assert!(
input
.collect_frame(
&[InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed,)],
false,
application,
generation,
1,
)
.is_ok()
);
let before_frame = input.frame_input();
let before_fixed = input.fixed_input();
let result = input.collect_frame(
&[
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Released),
InputEvent::key(PhysicalKeyCode::Enter, ButtonState::Pressed),
],
false,
application,
generation,
2,
);
assert_eq!(
result,
Err(InputCollectionError::EventLimitExceeded {
limit: 1,
received: 2,
})
);
assert_eq!(
before_frame.pressed(TestAction::Move).count(),
input.frame_input().pressed(TestAction::Move).count()
);
assert_eq!(
before_fixed.pressed(TestAction::Move).count(),
input.fixed_input().pressed(TestAction::Move).count()
);
assert!(input.frame_input().held(TestAction::Move));
}
#[test]
fn retained_fixed_edge_overflow_is_atomic_without_fixed_delivery() {
let (application, generation) = identity();
let mut input = state(2);
assert!(
input
.collect_frame(
&[InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed,)],
false,
application,
generation,
1,
)
.is_ok()
);
input.end_frame();
assert!(
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::KeyW,
ButtonState::Released,
)],
false,
application,
generation,
2,
)
.is_ok()
);
input.end_frame();
let occurrence_before = input.next_occurrence;
let result = input.collect_frame(
&[InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed)],
false,
application,
generation,
3,
);
assert_eq!(
result,
Err(InputCollectionError::RetainedFixedEdgeLimitExceeded {
limit: 2,
retained: 2,
incoming: 1,
})
);
assert_eq!(input.next_occurrence, occurrence_before);
assert!(!input.frame_input().held(TestAction::Move));
assert_eq!(input.frame_input().pressed(TestAction::Move).count(), 0);
assert_eq!(input.fixed_input().pressed(TestAction::Move).count(), 1);
assert_eq!(input.fixed_input().released(TestAction::Move).count(), 1);
}
#[test]
fn consuming_fixed_delivery_releases_the_retained_edge_budget() {
let (application, generation) = identity();
let mut input = state(1);
assert!(
input
.collect_frame(
&[InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed,)],
false,
application,
generation,
1,
)
.is_ok()
);
input.end_frame();
input.consume_fixed_delivery();
assert!(
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::KeyW,
ButtonState::Released,
)],
false,
application,
generation,
2,
)
.is_ok()
);
assert_eq!(input.fixed_input().released(TestAction::Move).count(), 1);
}
#[test]
fn repeated_physical_states_do_not_create_edges() {
let (application, generation) = identity();
let mut input = state(8);
let events = [
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed),
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed),
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Released),
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Released),
];
let report = input.collect_frame(&events, false, application, generation, 1);
let Ok(report) = report else {
panic!("bounded input should be accepted");
};
assert_eq!(report.logical_edges, 2);
assert_eq!(report.suppressed_repeats, 2);
assert_eq!(input.frame_input().pressed(TestAction::Move).count(), 1);
assert_eq!(input.frame_input().released(TestAction::Move).count(), 1);
assert!(!input.frame_input().held(TestAction::Move));
}
#[test]
fn every_supported_key_suppresses_repeated_physical_states() {
let (application, generation) = identity();
let new_keys = ALL_PHYSICAL_KEYS;
let mut bindings = ActionBindings::new();
for key in new_keys {
assert!(bindings.bind(key, TestAction::Move).is_ok());
}
let mut input = InputState::new(bindings, new_keys.len() * 4).unwrap();
let mut events = Vec::new();
for key in new_keys {
events.extend([
InputEvent::key(key, ButtonState::Pressed),
InputEvent::key(key, ButtonState::Pressed),
InputEvent::key(key, ButtonState::Released),
InputEvent::key(key, ButtonState::Released),
]);
}
let report = input
.collect_frame(&events, false, application, generation, 1)
.unwrap();
assert_eq!(report.logical_edges, new_keys.len() * 2);
assert_eq!(report.suppressed_repeats, new_keys.len() * 2);
assert!(!input.frame_input().held(TestAction::Move));
}
#[test]
fn wasd_and_arrow_edges_keep_shared_action_held_until_both_release() {
let (application, generation) = identity();
let mut bindings = ActionBindings::new();
assert!(
bindings
.bind(PhysicalKeyCode::KeyW, TestAction::Move)
.is_ok()
);
assert!(
bindings
.bind(PhysicalKeyCode::ArrowUp, TestAction::Move)
.is_ok()
);
assert!(
bindings
.bind(PhysicalKeyCode::Escape, TestAction::Enter)
.is_ok()
);
let mut input = InputState::new(bindings, 8).unwrap();
let first = [
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed),
InputEvent::key(PhysicalKeyCode::ArrowUp, ButtonState::Pressed),
InputEvent::key(PhysicalKeyCode::Escape, ButtonState::Pressed),
];
input
.collect_frame(&first, false, application, generation, 1)
.unwrap();
let frame = input.frame_input();
let move_edges: Vec<_> = frame.pressed(TestAction::Move).collect();
assert_eq!(move_edges.len(), 2);
assert_ne!(move_edges[0].intent(), move_edges[1].intent());
assert_eq!(
frame.pressed(TestAction::Enter).next(),
input.fixed_input().pressed(TestAction::Enter).next()
);
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::KeyW,
ButtonState::Released,
)],
false,
application,
generation,
2,
)
.unwrap();
assert!(input.frame_input().held(TestAction::Move));
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::ArrowUp,
ButtonState::Released,
)],
false,
application,
generation,
3,
)
.unwrap();
assert!(!input.frame_input().held(TestAction::Move));
}
#[test]
fn two_keys_for_one_action_keep_held_state_until_both_release() {
let (application, generation) = identity();
let mut input = state(8);
assert!(
input
.collect_frame(
&[
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed),
InputEvent::key(PhysicalKeyCode::KeyA, ButtonState::Pressed),
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Released),
],
false,
application,
generation,
1,
)
.is_ok()
);
let axis = DigitalAxis2d::new(
TestAction::Enter,
TestAction::Move,
TestAction::Fire,
TestAction::Fire,
);
assert!(input.frame_input().held(TestAction::Move));
assert_eq!(input.frame_input().digital_axis(axis), Vec2::X);
assert!(
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::KeyA,
ButtonState::Released,
)],
false,
application,
generation,
2,
)
.is_ok()
);
assert!(!input.frame_input().held(TestAction::Move));
assert_eq!(input.frame_input().digital_axis(axis), Vec2::ZERO);
}
#[test]
fn fixed_edges_persist_until_one_tick_consumes_them() {
let (application, generation) = identity();
let mut input = state(8);
assert!(
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::Enter,
ButtonState::Pressed,
)],
false,
application,
generation,
1,
)
.is_ok()
);
input.end_frame();
assert!(
input
.collect_frame(&[], false, application, generation, 2)
.is_ok()
);
assert_eq!(input.frame_input().pressed(TestAction::Enter).count(), 0);
assert_eq!(input.fixed_input().pressed(TestAction::Enter).count(), 1);
input.consume_fixed_delivery();
assert_eq!(input.fixed_input().pressed(TestAction::Enter).count(), 0);
}
#[test]
fn paused_events_update_frame_and_held_but_not_fixed_edges() {
let (application, generation) = identity();
let mut input = state(8);
assert!(
input
.collect_frame(
&[InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed,)],
true,
application,
generation,
1,
)
.is_ok()
);
assert!(input.frame_input().held(TestAction::Move));
assert_eq!(input.frame_input().pressed(TestAction::Move).count(), 1);
assert_eq!(input.fixed_input().pressed(TestAction::Move).count(), 0);
}
#[test]
fn frame_and_fixed_views_share_one_causal_token() {
let (application, generation) = identity();
let mut input = state(8);
assert!(
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::Enter,
ButtonState::Pressed,
)],
false,
application,
generation,
1,
)
.is_ok()
);
let frame_token = input.frame_input().pressed(TestAction::Enter).next();
let fixed_token = input.fixed_input().pressed(TestAction::Enter).next();
assert_eq!(frame_token, fixed_token);
}
#[test]
fn reusable_snapshots_preserve_data_tokens_and_capacity() {
let (application, generation) = identity();
let mut input = state(8);
let events = [
InputEvent::key(PhysicalKeyCode::KeyW, ButtonState::Pressed),
InputEvent::key(PhysicalKeyCode::Enter, ButtonState::Pressed),
];
assert!(
input
.collect_frame(&events, false, application, generation, 1)
.is_ok()
);
let expected_frame_edges = input.frame_edges.clone();
let expected_fixed_edges = input.fixed_edges.clone();
let mut frame = FrameInputState::empty();
let mut fixed = FixedInputState::empty();
input.copy_frame_snapshot_into(&mut frame);
input.copy_fixed_snapshot_into(&mut fixed);
assert_eq!(frame.edges, expected_frame_edges);
assert_eq!(fixed.edges, expected_fixed_edges);
assert_eq!(input.frame_edges, expected_frame_edges);
assert_eq!(input.fixed_edges, expected_fixed_edges);
assert!(frame.held(TestAction::Move));
assert!(frame.held(TestAction::Enter));
assert!(fixed.held(TestAction::Move));
assert!(fixed.held(TestAction::Enter));
let frame_held_capacity = frame.held.capacity();
let frame_edge_capacity = frame.edges.capacity();
let fixed_held_capacity = fixed.held.capacity();
let fixed_edge_capacity = fixed.edges.capacity();
frame.clear_reusing_storage();
fixed.clear_reusing_storage();
assert!(frame.held.is_empty());
assert!(frame.edges.is_empty());
assert!(fixed.held.is_empty());
assert!(fixed.edges.is_empty());
assert_eq!(frame.held.capacity(), frame_held_capacity);
assert_eq!(frame.edges.capacity(), frame_edge_capacity);
assert_eq!(fixed.held.capacity(), fixed_held_capacity);
assert_eq!(fixed.edges.capacity(), fixed_edge_capacity);
input.end_frame();
input.consume_fixed_delivery();
assert!(
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::KeyW,
ButtonState::Released,
)],
false,
application,
generation,
2,
)
.is_ok()
);
input.copy_frame_snapshot_into(&mut frame);
input.copy_fixed_snapshot_into(&mut fixed);
assert_eq!(frame.edges, input.frame_edges);
assert_eq!(fixed.edges, input.fixed_edges);
assert_eq!(frame.held.capacity(), frame_held_capacity);
assert_eq!(frame.edges.capacity(), frame_edge_capacity);
assert_eq!(fixed.held.capacity(), fixed_held_capacity);
assert_eq!(fixed.edges.capacity(), fixed_edge_capacity);
}
#[test]
fn using_a_token_consumes_its_occurrence_in_both_views() {
let (application, generation) = identity();
let mut input = state(8);
assert!(
input
.collect_frame(
&[InputEvent::key(
PhysicalKeyCode::Enter,
ButtonState::Pressed,
)],
false,
application,
generation,
1,
)
.is_ok()
);
let edge = input.frame_input().pressed(TestAction::Enter).next();
let Some(edge) = edge else {
panic!("press edge should exist");
};
assert!(input.consume_token_occurrence(edge.intent()));
assert!(!input.token_is_live(edge.intent()));
assert_eq!(input.frame_input().pressed(TestAction::Enter).count(), 0);
assert_eq!(input.fixed_input().pressed(TestAction::Enter).count(), 0);
}
}