bevy_controls 1.0.0

Bevy controls library
Documentation
use crate::analog::analog_inputs::AnalogInputs;
use bevy::prelude::*;

#[derive(Component, Clone, Debug, Reflect)]
pub struct AnalogControl<TContext, TAction> {
    pub action: TAction,
    pub contexts: Vec<TContext>,
    pub(crate) value: f32,
    pub(crate) button_controlled: bool,
    pub mappings: Vec<AnalogInputs>,
}

impl<TContext, TAction> AnalogControl<TContext, TAction> {
    pub fn new(action: TAction, contexts: Vec<TContext>, mappings: Vec<AnalogInputs>) -> Self {
        Self {
            action,
            contexts,
            mappings,
            value: 0.,
            button_controlled: false,
        }
    }

    pub fn read(&self) -> f32 {
        self.value
    }

    pub(crate) fn analog_input(&mut self, value: f32) {
        self.value = value;
        self.button_controlled = false;
    }

    pub(crate) fn button_pressed(&mut self, value: f32) {
        if self.button_controlled {
            self.value += value;
        } else {
            self.value = value;
        }
        self.button_controlled = true;
    }

    pub(crate) fn button_released(&mut self, value: f32) {
        if !self.button_controlled {
            return;
        }

        self.value -= value;
    }
}