1use repose_core::input::{GamepadAxis, GamepadButton, PointerButton};
4use repose_core::shortcuts::KeyChord;
5
6#[derive(Clone, Debug)]
9pub enum Binding {
10 Key(KeyChord),
12 Mouse(PointerButton),
14 Pad(GamepadButton),
16 Axis { axis: GamepadAxis, threshold: f32 },
20}
21
22impl PartialEq for Binding {
25 fn eq(&self, other: &Self) -> bool {
26 match (self, other) {
27 (Binding::Key(a), Binding::Key(b)) => a == b,
28 (Binding::Mouse(a), Binding::Mouse(b)) => {
29 std::mem::discriminant(a) == std::mem::discriminant(b)
30 }
31 (Binding::Pad(a), Binding::Pad(b)) => a == b,
32 (
33 Binding::Axis {
34 axis: a,
35 threshold: t,
36 },
37 Binding::Axis {
38 axis: b,
39 threshold: u,
40 },
41 ) => a == b && t == u,
42 _ => false,
43 }
44 }
45}
46
47impl Binding {
48 pub fn axis_active(threshold: f32, value: f32) -> bool {
53 if !value.is_finite() {
54 return false;
55 }
56 if threshold == 0.0 {
57 value != 0.0
58 } else if threshold > 0.0 {
59 value >= threshold
60 } else {
61 value <= threshold
62 }
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn axis_threshold_sign_selects_side() {
72 assert!(Binding::axis_active(0.5, 0.7));
73 assert!(!Binding::axis_active(0.5, 0.3));
74 assert!(Binding::axis_active(-0.5, -0.7));
75 assert!(!Binding::axis_active(-0.5, -0.3));
76 assert!(!Binding::axis_active(0.0, 0.0));
77 assert!(Binding::axis_active(0.0, 0.1));
78 assert!(Binding::axis_active(0.0, -0.1));
79 assert!(!Binding::axis_active(0.0, f32::NAN));
80 assert!(!Binding::axis_active(0.5, f32::NAN));
81 assert!(!Binding::axis_active(-0.5, f32::NAN));
82 }
83}