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
use crate::{ButtonId, ModifierFilterMask, ModifierId};
use std::collections::HashMap;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum InputMapping {
    ScanCodeKey(winit::ScanCode),
    VirtualKey(winit::VirtualKeyCode),
    MouseAxis(u32),
    MouseAxisWithDevice(winit::DeviceId, u32),
    Gamepad(gilrs::ev::Axis),
    GamepadWithDevice(gilrs::GamepadId, gilrs::ev::Axis),
}

pub struct Mapping {
    axis_mapping: HashMap<InputMapping, (ButtonId, ModifierFilterMask, f32)>,
    modifier_mapping: HashMap<InputMapping, ModifierId>,
}

impl Mapping {
    pub fn new() -> Mapping {
        Mapping {
            axis_mapping: HashMap::new(),
            modifier_mapping: HashMap::new(),
        }
    }

    pub fn add_modifier_mapping(&mut self, input_event: InputMapping, modifier_id: ModifierId) {
        if self.modifier_mapping.values().any(|m| *m == modifier_id) {
            log::warn!("Multiple mapping for {:?}", modifier_id);
        }
        self.modifier_mapping.insert(input_event, modifier_id);
    }

    pub fn add_button_mapping(
        &mut self,
        input_event: InputMapping,
        input_modifiers: Option<ModifierFilterMask>,
        button_id: ButtonId,
        sensitivity: f32,
    ) {
        if self.axis_mapping.values().any(|b| b.0 == button_id) {
            log::warn!("Multiple mapping for {:?}", button_id);
        }

        let mask = input_modifiers.unwrap_or_default();
        self.axis_mapping.insert(input_event, (button_id, mask, sensitivity));
    }

    pub fn map_winit_axis_to_modifier(&self, device_id: winit::DeviceId, axis: u32) -> Option<ModifierId> {
        let mapping = &self.modifier_mapping;

        if let Some(res) = mapping.get(&InputMapping::MouseAxisWithDevice(device_id.to_owned(), axis)) {
            return Some(res.to_owned());
        }

        if let Some(res) = mapping.get(&InputMapping::MouseAxis(axis)) {
            return Some(res.to_owned());
        }

        None
    }

    pub fn map_winit_key_to_modifier(&self, key_input: &winit::KeyboardInput) -> Option<ModifierId> {
        let mapping = &self.modifier_mapping;

        if let Some(res) = mapping.get(&InputMapping::ScanCodeKey(key_input.scancode)) {
            return Some(res.to_owned());
        }

        if let Some(vk) = key_input.virtual_keycode {
            if let Some(res) = mapping.get(&InputMapping::VirtualKey(vk)) {
                return Some(res.to_owned());
            }
        }

        None
    }

    pub fn map_winit_axis_to_button(&self, device_id: winit::DeviceId, axis: u32) -> Option<(ButtonId, ModifierFilterMask, f32)> {
        let mapping = &self.axis_mapping;

        if let Some(res) = mapping.get(&InputMapping::MouseAxisWithDevice(device_id.to_owned(), axis)) {
            return Some(res.to_owned());
        }

        if let Some(res) = mapping.get(&InputMapping::MouseAxis(axis)) {
            return Some(res.to_owned());
        }

        None
    }

    pub fn map_winit_key_to_button(&self, key_input: &winit::KeyboardInput) -> Option<(ButtonId, ModifierFilterMask, f32)> {
        let mapping = &self.axis_mapping;

        if let Some(res) = mapping.get(&InputMapping::ScanCodeKey(key_input.scancode)) {
            return Some(res.to_owned());
        }

        if let Some(vk) = key_input.virtual_keycode {
            if let Some(res) = mapping.get(&InputMapping::VirtualKey(vk)) {
                return Some(res.to_owned());
            }
        }

        None
    }

    pub fn map_gil_axis_to_modifier(&self, device_id: gilrs::GamepadId, axis: gilrs::ev::Axis) -> Option<ModifierId> {
        let mapping = &self.modifier_mapping;

        if let Some(res) = mapping.get(&InputMapping::GamepadWithDevice(device_id.to_owned(), axis.to_owned())) {
            return Some(res.to_owned());
        }

        if let Some(res) = mapping.get(&InputMapping::Gamepad(axis.to_owned())) {
            return Some(res.to_owned());
        }

        None
    }

    pub fn map_gil_axis_to_button(
        &self,
        device_id: gilrs::GamepadId,
        axis: gilrs::ev::Axis,
    ) -> Option<(ButtonId, ModifierFilterMask, f32)> {
        let mapping = &self.axis_mapping;

        if let Some(res) = mapping.get(&InputMapping::GamepadWithDevice(device_id.to_owned(), axis.to_owned())) {
            return Some(res.to_owned());
        }

        if let Some(res) = mapping.get(&InputMapping::Gamepad(axis.to_owned())) {
            return Some(res.to_owned());
        }

        None
    }
}