bevy_controls 1.0.0

Bevy controls library
Documentation
use bevy::{
    input::{gamepad::GamepadButtonChangedEvent, keyboard::KeyboardInput, mouse::MouseButtonInput},
    prelude::*,
    state::state::FreelyMutableState,
};
use std::marker::PhantomData;

use crate::button::{button_control::ButtonControl, buttons::Buttons};

pub struct ButtonPlugin<TContext, TAction>(PhantomData<TContext>, PhantomData<TAction>);

impl<TC, TA> Default for ButtonPlugin<TC, TA> {
    fn default() -> Self {
        Self(Default::default(), Default::default())
    }
}

impl<TContext: FreelyMutableState + FromWorld, TAction: Sync + Send + 'static> Plugin
    for ButtonPlugin<TContext, TAction>
{
    fn build(&self, app: &mut App) {
        app.init_state::<TContext>()
            .add_systems(Update, Self::check_controls);
    }
}

impl<TContext: States, TAction: Sync + Send + 'static> ButtonPlugin<TContext, TAction> {
    fn check_controls(
        mut controls: Query<&mut ButtonControl<TContext, TAction>>,
        context: Res<State<TContext>>,
        mut keyboard_input: MessageReader<KeyboardInput>,
        mut mouse_button_input: MessageReader<MouseButtonInput>,
        mut gamepad_button_input: MessageReader<GamepadButtonChangedEvent>,
    ) {
        let mut active_controls = controls
            .iter_mut()
            .filter(|c| c.contexts.contains(&context.get()))
            .collect::<Vec<_>>();

        let inputs = keyboard_input
            .read()
            .map(|k| (Buttons::Keyboard(k.key_code), k.state.is_pressed()))
            .chain(
                mouse_button_input
                    .read()
                    .map(|m| (Buttons::Mouse(m.button), m.state.is_pressed())),
            )
            .chain(
                gamepad_button_input
                    .read()
                    .map(|g| (Buttons::Gamepad(g.button), g.state.is_pressed())),
            );

        for (buttons, is_pressed) in inputs {
            let interacted_controls = active_controls
                .iter_mut()
                .filter(|c| c.mappings.contains(&buttons));

            for control in interacted_controls {
                control.state = is_pressed;
                control.read = false;
            }
        }
    }
}