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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use {
    super::KeyBuf,
    std::{fmt::Debug, time::Instant},
    winit::keyboard::KeyCode,
};

/// A binding between key and axis activation values.
#[derive(Clone, Debug)]
pub struct Binding<A> {
    axis: A,
    key: KeyCode,
    multiplier: f32,
    activation: f32,
    activation_time: f32,
}

impl<A> Binding<A> {
    pub const DEFAULT_ACTIVATION_TIME: f32 = 0.15;

    pub fn new(key: KeyCode, axis: A, multiplier: f32) -> Self {
        Self {
            axis,
            key,
            multiplier,
            activation: 0.0,
            activation_time: Self::DEFAULT_ACTIVATION_TIME,
        }
    }

    pub fn with_activation_time(mut self, activation_time: f32) -> Self {
        assert!(activation_time >= 0.0);

        self.activation_time = activation_time;
        self
    }
}

/// A basic key input mapping.
#[derive(Clone, Debug)]
pub struct KeyMap<A> {
    axis: Vec<(A, f32)>,
    bindings: Vec<Binding<A>>,
    last_update: Instant,
}

impl<A> KeyMap<A>
where
    A: Copy + Debug + Ord,
{
    /// Gets the value of this axis, which is between -1.0 and 1.0 inclusive.
    pub fn axis_value(&self, axis: &A) -> f32 {
        let res = self
            .axis
            .binary_search_by(|(a, _)| a.cmp(axis))
            .ok()
            .map(|idx| self.axis[idx].1);

        #[cfg(debug_assertions)]
        if res.is_none() {
            log::warn!("Unrecognized axis: {:#?}", axis);
        }

        res.unwrap_or_default()
    }

    /// Binds a key to an axis.
    pub fn bind(self, key: KeyCode, axis: A, multiplier: f32) -> Self {
        self.binding(Binding::new(key, axis, multiplier))
    }

    /// Binds a key.
    pub fn binding(mut self, binding: Binding<A>) -> Self {
        if let Err(idx) = self.axis.binary_search_by(|(a, _)| a.cmp(&binding.axis)) {
            self.axis.insert(idx, (binding.axis, 0.0));
        };
        self.bindings.push(binding);
        self
    }

    /// Updates the key axis values.
    pub fn update(&mut self, keyboard: &KeyBuf) {
        let now = Instant::now();
        let dt = (now - self.last_update).as_secs_f32().max(1.0 / 60.0);
        self.last_update = now;

        for binding in &mut self.bindings {
            if binding.activation_time > 1e-10 {
                let change = if keyboard.is_pressed(binding.key) {
                    dt
                } else {
                    -dt
                };
                binding.activation =
                    (binding.activation + change / binding.activation_time).clamp(0.0, 1.0);
            } else if keyboard.is_pressed(binding.key) {
                binding.activation = 1.0;
            } else {
                binding.activation = 0.0;
            }

            let axis_idx = self
                .axis
                .binary_search_by(|(a, _)| a.cmp(&binding.axis))
                .unwrap();
            let (_, value) = &mut self.axis[axis_idx];
            *value = (*value + binding.activation * binding.multiplier).clamp(-1.0, 1.0);
        }
    }
}

impl<A> Default for KeyMap<A> {
    fn default() -> Self {
        Self {
            axis: Default::default(),
            bindings: Default::default(),
            last_update: Instant::now(),
        }
    }
}