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/// Readout of one polled axis pair (left stick by default).
48pub fn stick_pair(state_axes: &HashMap<GamepadAxis, f32>) -> (f32, f32) {
49    let x = state_axes
50        .get(&GamepadAxis::LeftStickX)
51        .copied()
52        .unwrap_or(0.0);
53    let y = state_axes
54        .get(&GamepadAxis::LeftStickY)
55        .copied()
56        .unwrap_or(0.0);
57    (x, y)
58}
59
60pub use repose_core::input::PointerButton as MouseButton;
61pub use repose_core::input::{GamepadAxis as Axis, GamepadButton as PadButton};
62pub use repose_core::shortcuts::KeyChord as Chord;