Crate press_here

Crate press_here 

Source
Expand description

ยง๐Ÿ‘‰ Press Here ๐Ÿ‘ˆ

Crates.io docs.rs

press_here provides simple and modular input handling for the Bevy game engine.

ยงExample

Setup is quick and easy. Define axis/trigger and configure bindings:


fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        // Space key or south button on the gamepad for jump
        .add_trigger::<Jump>((KeyCode::Space, GamepadButton::South))
        .add_axis::<Walk>((
            // A/D keys for left/right movement
            Pair(KeyCode::KeyA, KeyCode::KeyD),
            // ..or left stick X-axis with deadzone
            GamepadAxis::LeftStickX.deadzone(0.1),
        ))
        .add_systems(Update, update)
        .run();
}

struct Jump;
struct Walk;

fn update(jump: Res<Trigger<Jump>>, walk: Res<Axis<Walk>>) {
    if jump.just_pressed() {
        jump_character();
    }

    let walk_value = walk.value();
    if walk_value != 0.0 {
        walk_character(walk_value);
    }
}

ยงSuper modular

Axis and trigger bindings are modular and can be combined to configure complex input responses. Check out the all_bindings example to see all bindings and modfiers in action.

let binding = Smooth::new(
    (
        Pair(KeyCode::KeyA, KeyCode::KeyD),
        Deadzone(GamepadAxis::LeftStickX, 0.1),
    ),
    0.2,
);

The same binding can be defined using the builder pattern:

let binding = (
    Pair(KeyCode::KeyA, KeyCode::KeyD),
    GamepadAxis::LeftStickX.deadzone(0.1),
)
    .smooth(0.2);

ยงNext steps

  • Better documentation.
    • Currently most structs have some comments and the crate is rather straight forward, but better examples in comments could be provided.
  • Binding information.
    • 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.
  • Observer support.
    • Currently you have to set up event triggering yourself. This can be streamlined.
  • Tests
    • 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.
  • Referring to existing axes and triggers.
    • 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>().
  • Schedule configuration.
    • Right now all checks are happending in the PreUpdate schedule. It might be desirable to configure this.
  • Time clock configuration.
    • Currently bindings that rely on time use the Time<Real> clock. If a different clock is desired, we have no way to configure that.
  • ???

ยงBevy compatibility

Bevypress_here
0.170.1

Structsยง

Add
A modifier that adds two axis values together.
And
A combinator that returns true only if both trigger bindings are pressed.
Axis
A resource that holds the current value and bindings for an axis.
AxisVisualizer
A helper struct for visualization of the axis values. It takes in an axis X and an optional axis Y which can be used to visualize axis behaviour using gizmos.
Clamp
Clamps the binding value to a specified range.
Deadzone
A filter that only allows axis values that exceed a certain deadzone threshold.
Divide
A modifier that divides two axis values.
Inputs
A collection of input references for use in input bindings.
Invert
A modifier that inverts the axis value.
MouseWheel
Binds mouse wheel movement as an axis input.
MouseX
Binds the X-axis of mouse movement as an axis input.
MouseY
Binds the Y-axis of mouse movement as an axis input.
Multiply
A modifier that multiplies two axis values together.
Normalize
A filter that normalizes the axis value so that the combined magnitude of the two axes is at most 1.0.
Not
A modifier that inverts the trigger state.
Pair
A pair of axis binding, where the first axis is used for the negative direction and the second axis is used for the positive direction.
RateLimit
Limits the rate of change of an axis value to a maximum delta per second.
Remap
A modifier that remaps the axis value from one range to another.
Smooth
A filter that smooths axis values using exponential smoothing.
Subtract
A modifier that subtracts two axis values.
Transformation
A modifier that applies a custom transformation function to the axis value.
Trigger
A resource that holds the current state and binding for a trigger.
WithCurve
A modifier that applies a curve to the axis value. This is useful for creating non-linear input responses.
WithTriggerBinding
An axis binding that is only active when the given trigger binding is active.

Traitsยง

AppExt
AxisBinding
AxisBindingBuilder
TriggerBinding
TriggerBindingBuilder