1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
pub mod input;

use std::hash::Hash;

use bevy::{prelude::*, utils::HashMap};
use input::{mouse::MouseAxis, events::{InputActionStarted, InputActionContinuing, InputActionFinished}};

use crate::input::events::InputActionActive;

pub trait AutoBinder<K, V>
where
    K: Eq + Hash,
{
    fn bind(&mut self, key: K, value: V) -> &mut Self;
    fn unbind(&mut self, key: K) -> &mut Self;
}

impl<K, V> AutoBinder<K, V> for HashMap<K, V>
where
    K: Eq + Hash,
{
    fn bind(&mut self, key: K, value: V) -> &mut Self {
        self.insert(key, value);
        self
    }

    fn unbind(&mut self, key: K) -> &mut Self {
        self.remove(&key);
        self
    }
}

#[derive(Default, Clone, Resource)]
pub struct InputMapper {
    pub action_value: HashMap<String, f32>,
    pub previous_action_value: HashMap<String, f32>,

    pub keyboard_binding: HashMap<KeyCode, String>,
    pub mouse_button_binding: HashMap<MouseButton, String>,
    pub mouse_axis_binding: HashMap<MouseAxis, String>,
}

#[derive(Default)]
pub struct InputMapperPlugin;

impl Plugin for InputMapperPlugin {
    fn build(&self, app: &mut App) {
        app
            .insert_resource(InputMapper::default())
            .add_event::<InputActionActive>()
            .add_event::<InputActionStarted>()
            .add_event::<InputActionContinuing>()
            .add_event::<InputActionFinished>()
            .add_systems(Update, InputMapper::event_cycle)
            .add_systems(Update, InputMapper::keyboard_key_press_system)
            .add_systems(Update,(InputMapper::mouse_button_press_system, InputMapper::mouse_axis_move_system));
    }
}