Leafwing-Studios / leafwing-input-manager

A straightforward stateful input manager for the Bevy game engine.
Apache License 2.0
727 stars 112 forks source link

Analog trigger values are only updated when the button is pressed #650

Open Cedev opened 1 month ago

Cedev commented 1 month ago

Version

54a361b

Operating system & version

Debian 12

What you did

Added a throttle button using the right analog trigger on a gamepad, and with a custom Axislike that reads the same thing from the CentralInputStore.

Into an input map for this Actionlike

#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug, Reflect)]
pub enum PlayerAction {
    Throttle,
    #[actionlike(Axis)]
    CustomThrottle,
}

impl PlayerAction {
    pub fn default_input_map() -> InputMap<PlayerAction> {
        let mut input_map = InputMap::default();

        input_map.insert(Self::Throttle, GamepadButtonType::RightTrigger2);
        input_map.insert_axis(Self::CustomThrottle, GamepadButtonAxes::TRIGGERS);

        return input_map;
    }
}
return input_map;

Read the value from both the button and the custom axislike, while printing out all of the GamepadEvents that are received

let throttle = action_state.button_value(&PlayerAction::Throttle);
if throttle != 0.0 {println!("Throttle {:?}", throttle);}

let custom_throttle = action_state.value(&PlayerAction::CustomThrottle);
if custom_throttle != 0.0 {println!("CustomThrottle {:?}", custom_throttle);}

The custom Axislike reads from the CentralInputStore.

#[serde_typetag]
impl Axislike for GamepadButtonAxes {
    fn value(&self, input_store: &updating::CentralInputStore, gamepad: Gamepad) -> f32 {
        let up_value = match self.positive {
            Some(button) => input_store.button_value(&GamepadButton::new(gamepad, button)),
            None => 0.0
        };
        let down_value  = match self.negative {
            Some(button) => input_store.button_value(&GamepadButton::new(gamepad, button)),
            None => 0.0
        };
        return up_value - down_value;
    }
}

What you expected to happen

The throttle value to change over the entire range from 0.0 to 1.0 as the analog stick was depressed

What actually happened

Although GamepadEvents are emitted smoothly over depressing the analog stick, reading the value from the button only ever returns 1.0 (or 0.0) and reading the value from the CentralInputStore only returns high values over about 0.75 (or 0.0).

Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.050980393
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.0627451
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.07450981
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.08627451
...
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.3647059
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.38039216
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.4
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.42352942
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.44313726
...
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.7019608
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.7254902
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.74509805
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.7607843
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.78431374
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.79607844
Throttle 1.0
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.80784315
CustomThrottle 0.827451
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.827451
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8392157
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8509804
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8666667
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8784314
Throttle 1.0
CustomThrottle 0.9019608
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8901961
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.9019608
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.9137255
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.93333334
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.9490196
Throttle 1.0
CustomThrottle 0.9490196
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 1.0
Throttle 1.0
CustomThrottle 1.0
Throttle 1.0
CustomThrottle 1.0
...

Additional information

The UpdatableInput implementation for GamepadButton only records the axis values for buttons that are either pressed or just_released

https://github.com/Leafwing-Studios/leafwing-input-manager/blob/54a361bde6f548e33b4a0491c03d2fa759f5c6cd/src/user_input/gamepad.rs#L570-L587

I don't know why ActionState<>::button_value and ActionState<>::clamped_button_value only return 1.0 or 0.0 even though they are documented as returning analog values in examples/axis_input.rs:

https://github.com/Leafwing-Studios/leafwing-input-manager/blob/54a361bde6f548e33b4a0491c03d2fa759f5c6cd/examples/axis_inputs.rs#L63-L65

alice-i-cecile commented 1 month ago

649 was just merged: is this still an issue for you after those changes?

Cedev commented 1 month ago

After

~/.cargo/git/checkouts/leafwing-input-manager-85f1d55581648fda/54a361b$ git log
commit 54a361bde6f548e33b4a0491c03d2fa759f5c6cd (HEAD -> master)
Author: Shute <sinoctis@163.com>
Date:   Wed Oct 16 17:49:52 2024 +0100

    Add `value` for button-like inputs (#649)

Sorry, should have used the git hash. I guess I shouldn't trust Cargo.toml after cargo add --git.