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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
mod clamped;

use crate::bindings::{Axis1dBinding, Axis2dBinding, Axis3dBinding, Binding};
use crate::state::InputState;

pub use clamped::*;

pub trait ActionKind {
    type Output;

    fn get(state: &InputState, binding: &Binding) -> Option<Self::Output>;
    fn reduce(inputs: &[Self::Output]) -> Self::Output;
}

pub struct ButtonAction;

impl ActionKind for ButtonAction {
    type Output = bool;

    fn get(state: &InputState, binding: &Binding) -> Option<Self::Output> {
        let binding = match binding {
            Binding::Button(inner) => inner,
            _ => return None,
        };

        Some(state.is_button_just_down(*binding))
    }

    fn reduce(inputs: &[Self::Output]) -> Self::Output {
        inputs.iter().any(|x| *x)
    }
}

pub struct ButtonHeldAction;

impl ActionKind for ButtonHeldAction {
    type Output = bool;

    fn get(state: &InputState, binding: &Binding) -> Option<Self::Output> {
        let binding = match binding {
            Binding::Button(inner) => inner,
            _ => return None,
        };

        Some(state.is_button_down(*binding))
    }

    fn reduce(inputs: &[Self::Output]) -> Self::Output {
        inputs.iter().any(|x| *x)
    }
}

pub struct Axis1dAction;

impl ActionKind for Axis1dAction {
    type Output = f32;

    fn get(state: &InputState, binding: &Binding) -> Option<Self::Output> {
        let binding = match binding {
            Binding::Axis1d(inner) => inner,
            _ => return None,
        };

        match binding {
            Axis1dBinding::Buttons {
                neg,
                pos,
                sensitivity,
            } => {
                let is_neg = -(state.is_button_down(*neg) as u8 as f32);
                let is_pos = state.is_button_down(*pos) as u8 as f32;

                Some((is_neg + is_pos) * sensitivity)
            }
            Axis1dBinding::Axis { axis, sensitivity } => {
                Some(state.get_axis1d(*axis) * sensitivity)
            }
        }
    }

    fn reduce(inputs: &[Self::Output]) -> Self::Output {
        inputs.iter().sum::<f32>()
    }
}

pub struct Axis2dAction;

impl ActionKind for Axis2dAction {
    type Output = (f32, f32);

    fn get(state: &InputState, binding: &Binding) -> Option<Self::Output> {
        let binding = match binding {
            Binding::Axis2d(inner) => inner,
            _ => return None,
        };

        match binding {
            Axis2dBinding::Individual { x, y } => {
                let x = Axis1dAction::get(state, &Binding::Axis1d(*x))?;
                let y = Axis1dAction::get(state, &Binding::Axis1d(*y))?;

                Some((x, y))
            }
            Axis2dBinding::Axis { axis, sensitivity } => {
                let (x, y) = state.get_axis2d(*axis);

                Some((x * sensitivity, y * sensitivity))
            }
        }
    }

    fn reduce(inputs: &[Self::Output]) -> Self::Output {
        inputs
            .iter()
            .fold((0.0, 0.0), |(ax, ay), (bx, by)| (ax + bx, ay + by))
    }
}

pub struct Axis3dAction;

impl ActionKind for Axis3dAction {
    type Output = (f32, f32, f32);

    fn get(state: &InputState, binding: &Binding) -> Option<Self::Output> {
        let binding = match binding {
            Binding::Axis3d(inner) => inner,
            _ => return None,
        };

        match binding {
            Axis3dBinding::Individual { x, y, z } => {
                let x = Axis1dAction::get(state, &Binding::Axis1d(*x))?;
                let y = Axis1dAction::get(state, &Binding::Axis1d(*y))?;
                let z = Axis1dAction::get(state, &Binding::Axis1d(*z))?;

                Some((x, y, z))
            }
        }
    }

    fn reduce(inputs: &[Self::Output]) -> Self::Output {
        inputs
            .iter()
            .fold((0.0, 0.0, 0.0), |(ax, ay, az), (bx, by, bz)| {
                (ax + bx, ay + by, az + bz)
            })
    }
}