bevy_input_bindings 0.0.2

High level, flexible and shareable input binding library for the Bevy game engine
Documentation
use bevy::ecs::component::Component;
use bevy::input::gamepad::{GamepadAxis, GamepadButton};
use bevy::reflect::Reflect;

use super::common::{
    CONTROLLER_DURATION_BEFORE_FIRST_REPEAT, CONTROLLER_DURATION_BETWEEN_REPEAT, OtsUiActions,
    OtsUiValues,
};
use crate::gamepad::binding::GamepadBinding;
use crate::gamepad::plugin::GamepadPlugin;
use crate::gamepad::triggers::{ButtonPressed, Hysteresis, TriggerTrait};

pub type OtsUiGamepadPlugin = GamepadPlugin<OtsUiActions, OtsUiValues, OtsUiGamepadBinding>;

#[derive(Component, Reflect)]
pub struct OtsUiGamepadBinding {
    enabled: bool,
    up: ButtonPressed,
    down: ButtonPressed,
    left: ButtonPressed,
    right: ButtonPressed,
    validate: ButtonPressed,
    back: ButtonPressed,
    stick_up: Hysteresis,
    stick_down: Hysteresis,
    stick_left: Hysteresis,
    stick_right: Hysteresis,
}

impl OtsUiGamepadBinding {
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    pub fn disable(&mut self) {
        self.enabled = false;
    }

    pub fn set_enable(&mut self, enable: bool) {
        self.enabled = enable;
    }
}

impl Default for OtsUiGamepadBinding {
    fn default() -> Self {
        Self {
            enabled: true,
            up: ButtonPressed::new(GamepadButton::DPadUp).with_repeat(
                CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
                CONTROLLER_DURATION_BETWEEN_REPEAT,
            ),
            down: ButtonPressed::new(GamepadButton::DPadDown).with_repeat(
                CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
                CONTROLLER_DURATION_BETWEEN_REPEAT,
            ),
            left: ButtonPressed::new(GamepadButton::DPadLeft).with_repeat(
                CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
                CONTROLLER_DURATION_BETWEEN_REPEAT,
            ),
            right: ButtonPressed::new(GamepadButton::DPadRight).with_repeat(
                CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
                CONTROLLER_DURATION_BETWEEN_REPEAT,
            ),
            validate: ButtonPressed::new(GamepadButton::South),
            back: ButtonPressed::new(GamepadButton::North),
            stick_up: Hysteresis::new_above(GamepadAxis::LeftStickY, 0.5, 0.8).with_repeat(
                CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
                CONTROLLER_DURATION_BETWEEN_REPEAT,
            ),
            stick_down: Hysteresis::new_below(GamepadAxis::LeftStickY, -0.5, -0.8).with_repeat(
                CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
                CONTROLLER_DURATION_BETWEEN_REPEAT,
            ),
            stick_left: Hysteresis::new_below(GamepadAxis::LeftStickX, -0.5, -0.8).with_repeat(
                CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
                CONTROLLER_DURATION_BETWEEN_REPEAT,
            ),
            stick_right: Hysteresis::new_above(GamepadAxis::LeftStickX, 0.5, 0.8).with_repeat(
                CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
                CONTROLLER_DURATION_BETWEEN_REPEAT,
            ),
        }
    }
}

impl GamepadBinding<OtsUiActions, OtsUiValues> for OtsUiGamepadBinding {
    fn get_action_bindings<'a>(&'a mut self) -> Vec<(&'a mut dyn TriggerTrait, OtsUiActions)> {
        // If not enabled, there is not bindings attached
        if !self.enabled {
            return vec![];
        }
        return vec![
            (&mut self.up, OtsUiActions::Up),
            (&mut self.down, OtsUiActions::Down),
            (&mut self.left, OtsUiActions::Left),
            (&mut self.right, OtsUiActions::Right),
            (&mut self.validate, OtsUiActions::Validate),
            (&mut self.back, OtsUiActions::Back),
            (&mut self.stick_up, OtsUiActions::Up),
            (&mut self.stick_down, OtsUiActions::Down),
            (&mut self.stick_left, OtsUiActions::Left),
            (&mut self.stick_right, OtsUiActions::Right),
        ];
    }
}