press_here/
lib.rs

1/*!
2# 👉 Press Here 👈
3
4[![Crates.io](https://img.shields.io/crates/v/press_here.svg)](https://crates.io/crates/press_here)
5[![docs.rs](https://img.shields.io/docsrs/press_here/latest)](https://docs.rs/press_here/latest)
6
7`press_here` provides simple and modular input handling for the [Bevy](https://docs.rs/bevy/) game engine.
8
9## Example
10
11Setup is quick and easy. Define axis/trigger and configure bindings:
12
13```no_run
14# use bevy::prelude::*;
15# use press_here::{AppExt, Axis, AxisBindingBuilder, Pair, Trigger};
16
17fn main() {
18    App::new()
19        .add_plugins(DefaultPlugins)
20        // Space key or south button on the gamepad for jump
21        .add_trigger::<Jump>((KeyCode::Space, GamepadButton::South))
22        .add_axis::<Walk>((
23            // A/D keys for left/right movement
24            Pair(KeyCode::KeyA, KeyCode::KeyD),
25            // ..or left stick X-axis with deadzone
26            GamepadAxis::LeftStickX.deadzone(0.1),
27        ))
28        .add_systems(Update, update)
29        .run();
30}
31
32struct Jump;
33struct Walk;
34
35fn update(jump: Res<Trigger<Jump>>, walk: Res<Axis<Walk>>) {
36    if jump.just_pressed() {
37        jump_character();
38    }
39
40    let walk_value = walk.value();
41    if walk_value != 0.0 {
42        walk_character(walk_value);
43    }
44}
45
46# fn jump_character() {}
47# fn walk_character(_value: f32) {}
48```
49
50## Super modular
51
52Axis and trigger bindings are modular and can be combined to configure complex input responses. Check out the [all_bindings](https://github.com/ad-kr/press_here/blob/main/examples/all_bindings.rs) example to see all bindings and modfiers in action.
53
54```no_run
55# use bevy::prelude::*;
56# use press_here::{Deadzone, Pair, Smooth};
57let binding = Smooth::new(
58    (
59        Pair(KeyCode::KeyA, KeyCode::KeyD),
60        Deadzone(GamepadAxis::LeftStickX, 0.1),
61    ),
62    0.2,
63);
64```
65
66The same binding can be defined using the builder pattern:
67
68```no_run
69# use bevy::prelude::*;
70# use press_here::{AxisBindingBuilder, Pair};
71let binding = (
72    Pair(KeyCode::KeyA, KeyCode::KeyD),
73    GamepadAxis::LeftStickX.deadzone(0.1),
74)
75    .smooth(0.2);
76```
77
78## Next steps
79
80- Better documentation.
81  - Currently most structs have some comments and the crate is rather straight forward, but better examples in comments could be provided.
82- Binding information.
83  - Add basic info to each binding so that we can know what bindings are set. Right now we would need to iterate over each binding and downcast its type. Not optimal.
84- Observer support.
85  - Currently you have to set up event triggering yourself. This can be streamlined.
86- Tests
87  - We could write simple unit tests for each binding. `Inputs`-mocking should be made easier. This would also allow us to make create better doc-comments/tests.
88- Referring to existing axes and triggers.
89  - It would be nice to be able to refer to other already defined triggers and axes. This would allow us to do something like `.with_trigger::<SomeTrigger>()`.
90- Schedule configuration.
91  - Right now all checks are happending in the PreUpdate schedule. It might be desirable to configure this.
92- Time clock configuration.
93  - Currently bindings that rely on time use the `Time<Real>` clock. If a different clock is desired, we have no way to configure that.
94- ???
95
96## Bevy compatibility
97
98| Bevy | press_here |
99| ---- | ---------- |
100| 0.17 | 0.1        |
101*/
102mod app;
103mod axis;
104mod inputs;
105mod trigger;
106mod visualizer;
107
108pub use app::AppExt;
109pub use axis::{
110    Axis, AxisBinding, bindings::*, builder::*, combinators::*, filters::*, modifiers::*,
111};
112pub use inputs::Inputs;
113pub use trigger::{Trigger, TriggerBinding, builder::*, combinators::*, modifiers::*};
114#[cfg(feature = "visualizer")]
115pub use visualizer::*;