Skip to main content

repame_input/
lib.rs

1//! Physical inputs bound to game actions: leafwing-style map flattened
2//! for fixed-tick CPU sims on raw `bevy_ecs` plus `repose` input types.
3
4use std::collections::HashMap;
5use std::hash::Hash;
6
7use repame_sim::bevy_ecs::prelude::*;
8use repose_core::input::GamepadAxis;
9
10mod aim;
11mod binding;
12mod held;
13mod keymap;
14mod map;
15mod remap;
16mod snapshot;
17mod state;
18
19pub use aim::{AimTracker, StagedClick};
20pub use binding::Binding;
21pub use held::reconcile_held;
22pub use keymap::{
23    Keymap, KeymapCapture, KeymapDevice, KeymapEntry, KeymapRow, chord_for_physical,
24    decode_keymap_entry, encode_keymap_entry, glyph_for_physical,
25};
26pub use map::ActionMap;
27pub use remap::RemapSession;
28pub use snapshot::{GamepadState, MouseState, TouchContact, apply_stick, dead_zone};
29pub use state::ActionState;
30
31/// Action key. Games use their own enums (`Clone + Eq + Hash`);
32/// a `&'static str` works for quick wiring.
33pub trait ActionLike: Clone + Eq + Hash + Send + Sync + 'static {}
34
35impl<T: Clone + Eq + Hash + Send + Sync + 'static> ActionLike for T {}
36
37/// Insert an [`ActionState`] built from `map` into the sim world.
38pub fn init_state<A: ActionLike + std::fmt::Debug>(sim: &mut repame_sim::Sim, map: ActionMap<A>) {
39    sim.world.insert_resource(ActionState::new(map));
40}
41
42/// Edge rollover at tick end: register last so edges survive the tick, then clear.
43pub fn end_tick_system<A: ActionLike + std::fmt::Debug>(mut state: ResMut<ActionState<A>>) {
44    state.end_tick();
45}
46
47/// Full input-tick closeout: edges AND consumption clear, so mocked
48/// pulses and consumed levels never leak into the next tick.
49pub fn end_input_tick_system<A: ActionLike + std::fmt::Debug>(
50    mut state: ResMut<ActionState<A>>,
51) {
52    state.end_tick();
53}
54
55/// Readout of one polled axis pair (left stick by default).
56pub fn stick_pair(state_axes: &HashMap<GamepadAxis, f32>) -> (f32, f32) {
57    let x = state_axes
58        .get(&GamepadAxis::LeftStickX)
59        .copied()
60        .unwrap_or(0.0);
61    let y = state_axes
62        .get(&GamepadAxis::LeftStickY)
63        .copied()
64        .unwrap_or(0.0);
65    (x, y)
66}
67
68pub use repose_core::input::PointerButton as MouseButton;
69pub use repose_core::input::{GamepadAxis as Axis, GamepadButton as PadButton};
70pub use repose_core::shortcuts::KeyChord as Chord;