Skip to main content

repame_input/
lib.rs

1//! Physical inputs (keys, mouse buttons, pad buttons, axes) bound to
2//! game actions. (using leafwing `InputMap`/`ActionState` as ref., flattened for
3//! fixed-tick CPU sims on raw `bevy_ecs` + `repose` input types.)
4
5use std::collections::HashMap;
6use std::hash::Hash;
7
8use repame_sim::bevy_ecs::prelude::*;
9use repose_core::input::GamepadAxis;
10
11mod binding;
12mod map;
13mod state;
14
15pub use binding::Binding;
16pub use map::ActionMap;
17pub use state::ActionState;
18
19/// Action key bound. Games use their own enums (must be `Clone + Eq +
20/// `Hash`); a `&'static str` works for quick wiring.
21pub trait ActionLike: Clone + Eq + Hash + Send + Sync + 'static {}
22
23impl<T: Clone + Eq + Hash + Send + Sync + 'static> ActionLike for T {}
24
25/// Insert an [`ActionState`] built from `map` into the sim world.
26pub fn init_state<A: ActionLike + std::fmt::Debug>(sim: &mut repame_sim::Sim, map: ActionMap<A>) {
27    sim.world.insert_resource(ActionState::new(map));
28}
29
30/// Edge rollover at the end of each tick: register LAST in the sim
31/// schedule so `just_pressed`/`just_released` survive from compose-fed
32/// events through the tick's systems, then clear.
33pub fn end_tick_system<A: ActionLike + std::fmt::Debug>(mut state: ResMut<ActionState<A>>) {
34    state.end_tick();
35}
36
37/// Readout of one polled axis pair (left stick by default).
38pub fn stick_pair(state_axes: &HashMap<GamepadAxis, f32>) -> (f32, f32) {
39    let x = state_axes
40        .get(&GamepadAxis::LeftStickX)
41        .copied()
42        .unwrap_or(0.0);
43    let y = state_axes
44        .get(&GamepadAxis::LeftStickY)
45        .copied()
46        .unwrap_or(0.0);
47    (x, y)
48}
49
50pub use repose_core::input::PointerButton as MouseButton;
51pub use repose_core::input::{GamepadAxis as Axis, GamepadButton as PadButton};
52pub use repose_core::shortcuts::KeyChord as Chord;