use repose_core::input::{GamepadAxis, GamepadButton, PointerButton};
use repose_core::shortcuts::KeyChord;
#[derive(Clone, Debug)]
pub enum Binding {
Key(KeyChord),
Mouse(PointerButton),
Pad(GamepadButton),
Axis { axis: GamepadAxis, threshold: f32 },
}
impl PartialEq for Binding {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Binding::Key(a), Binding::Key(b)) => a == b,
(Binding::Mouse(a), Binding::Mouse(b)) => {
std::mem::discriminant(a) == std::mem::discriminant(b)
}
(Binding::Pad(a), Binding::Pad(b)) => a == b,
(
Binding::Axis {
axis: a,
threshold: t,
},
Binding::Axis {
axis: b,
threshold: u,
},
) => a == b && t == u,
_ => false,
}
}
}
impl Binding {
pub fn axis_active(threshold: f32, value: f32) -> bool {
if threshold >= 0.0 {
value >= threshold
} else {
value <= threshold
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn axis_threshold_sign_selects_side() {
assert!(Binding::axis_active(0.5, 0.7));
assert!(!Binding::axis_active(0.5, 0.3));
assert!(Binding::axis_active(-0.5, -0.7));
assert!(!Binding::axis_active(-0.5, -0.3));
assert!(Binding::axis_active(0.0, 0.0));
}
}