reaction 0.2.0

Universal low-latency input handling for game engines
Documentation
use reaction::mapping::action::InputAction;
use reaction::mapping::binding::{BindingConfig, InputBinding};
use reaction::mapping::resolver::InputMap;
use reaction::prelude::*;

#[test]
fn test_deadzone_rescaling() {
    let mut map = InputMap::new();
    let mut input = AdvancedInputState::new();

    // Disable internal gamepad deadzone to test InputMap rescaling
    input.gamepads.get_mut(GamepadId::One).deadzone = 0.0;

    // Binding with 0.2 deadzone
    let binding = BindingConfig::new(InputBinding::GamepadAxis(GamepadAxis::LeftStickX))
        .with_deadzone(0.2)
        .unwrap();
    map.bind(InputAction::MoveX, binding).unwrap();

    // 1. Below deadzone -> 0.0
    input
        .gamepads
        .get_mut(GamepadId::One)
        .set_axis(GamepadAxis::LeftStickX, 0.1);
    assert_eq!(
        map.resolve(InputAction::MoveX, &input, Some(GamepadId::One)),
        0.0
    );

    // 2. Exactly at deadzone -> 0.0
    input
        .gamepads
        .get_mut(GamepadId::One)
        .set_axis(GamepadAxis::LeftStickX, 0.2);
    assert_eq!(
        map.resolve(InputAction::MoveX, &input, Some(GamepadId::One)),
        0.0
    );

    // 3. Just above deadzone -> small value
    input
        .gamepads
        .get_mut(GamepadId::One)
        .set_axis(GamepadAxis::LeftStickX, 0.3);
    let val = map.resolve(InputAction::MoveX, &input, Some(GamepadId::One));
    // Formula: (0.3 - 0.2) / (1.0 - 0.2) = 0.1 / 0.8 = 0.125
    assert!((val - 0.125).abs() < 0.001);

    // 4. Halfway point
    input
        .gamepads
        .get_mut(GamepadId::One)
        .set_axis(GamepadAxis::LeftStickX, 0.6);
    let val = map.resolve(InputAction::MoveX, &input, Some(GamepadId::One));
    // Formula: (0.6 - 0.2) / (1.0 - 0.2) = 0.4 / 0.8 = 0.5
    assert!((val - 0.5).abs() < 0.001);

    // 5. Max point -> 1.0
    input
        .gamepads
        .get_mut(GamepadId::One)
        .set_axis(GamepadAxis::LeftStickX, 1.0);
    assert_eq!(
        map.resolve(InputAction::MoveX, &input, Some(GamepadId::One)),
        1.0
    );

    // 6. Negative values
    input
        .gamepads
        .get_mut(GamepadId::One)
        .set_axis(GamepadAxis::LeftStickX, -0.6);
    let val = map.resolve(InputAction::MoveX, &input, Some(GamepadId::One));
    assert!((val - (-0.5)).abs() < 0.001);
}