repame-input 0.3.27

sim-side action map, edge state, and contexts for all games
Documentation
//! Physical inputs bound to game actions: leafwing-style map flattened
//! for fixed-tick CPU sims on raw `bevy_ecs` plus `repose` input types.

use std::collections::HashMap;
use std::hash::Hash;

use repame_sim::bevy_ecs::prelude::*;
use repose_core::input::GamepadAxis;

mod aim;
mod binding;
mod haptics;
mod held;
mod keymap;
mod map;
mod motion;
mod remap;
mod snapshot;
mod state;

pub use aim::{AimTracker, StagedClick};
pub use binding::Binding;
pub use haptics::{HapticBus, HapticEffect, HapticSink};
pub use held::reconcile_held;
pub use keymap::{
    Keymap, KeymapCapture, KeymapDevice, KeymapEntry, KeymapRow, chord_for_physical,
    decode_keymap_entry, encode_keymap_entry, glyph_for_physical,
};
pub use map::ActionMap;
pub use motion::{GRAVITY, MotionSample, MotionSnapshot, MotionSource, MotionTracker, SensorKind};
pub use remap::RemapSession;
pub use snapshot::{GamepadState, MouseState, TouchContact, apply_stick, dead_zone};
pub use state::ActionState;

/// Action key. Games use their own enums (`Clone + Eq + Hash`);
/// a `&'static str` works for quick wiring.
pub trait ActionLike: Clone + Eq + Hash + Send + Sync + 'static {}

impl<T: Clone + Eq + Hash + Send + Sync + 'static> ActionLike for T {}

/// Insert an [`ActionState`] built from `map` into the sim world.
pub fn init_state<A: ActionLike + std::fmt::Debug>(sim: &mut repame_sim::Sim, map: ActionMap<A>) {
    sim.world.insert_resource(ActionState::new(map));
}

/// Edge rollover at tick end: register last so edges survive the tick, then clear.
pub fn end_tick_system<A: ActionLike + std::fmt::Debug>(mut state: ResMut<ActionState<A>>) {
    state.end_tick();
}

/// Readout of one polled axis pair (left stick by default).
pub fn stick_pair(state_axes: &HashMap<GamepadAxis, f32>) -> (f32, f32) {
    let x = state_axes
        .get(&GamepadAxis::LeftStickX)
        .copied()
        .unwrap_or(0.0);
    let y = state_axes
        .get(&GamepadAxis::LeftStickY)
        .copied()
        .unwrap_or(0.0);
    (x, y)
}

pub use repose_core::input::PointerButton as MouseButton;
pub use repose_core::input::{GamepadAxis as Axis, GamepadButton as PadButton};
pub use repose_core::shortcuts::KeyChord as Chord;