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
//! Defines binding structure used for saving and loading input settings.

use std::borrow::Borrow;
use std::hash::Hash;

use fnv::FnvHashMap as HashMap;
use smallvec::SmallVec;

use super::{Axis, Button};

/// Used for saving and loading input settings.
#[derive(Derivative, Serialize, Deserialize, Clone)]
#[derivative(Default(bound = ""))]
pub struct Bindings<AX, AC>
where
    AX: Hash + Eq,
    AC: Hash + Eq,
{
    pub(super) axes: HashMap<AX, Axis>,
    pub(super) actions: HashMap<AC, SmallVec<[Button; 4]>>,
}

impl<AX, AC> Bindings<AX, AC>
where
    AX: Hash + Eq,
    AC: Hash + Eq,
{
    /// Creates a new empty Bindings structure
    pub fn new() -> Self {
        Default::default()
    }
}

impl<AX, AC> Bindings<AX, AC>
where
    AX: Hash + Eq + Clone,
    AC: Hash + Eq + Clone,
{
    /// Assign an axis to an ID value
    ///
    /// This will insert a new axis if no entry for this id exists.
    /// If one does exist this will replace the axis at that id and return it.
    pub fn insert_axis<A: Into<AX>>(&mut self, id: A, axis: Axis) -> Option<Axis> {
        self.axes.insert(id.into(), axis)
    }

    /// Removes an axis, this will return the removed axis if successful.
    pub fn remove_axis<A: Hash + Eq + ?Sized>(&mut self, id: &A) -> Option<Axis>
    where
        AX: Borrow<A>,
    {
        self.axes.remove(id)
    }

    /// Returns a reference to an axis.
    pub fn axis<A: Hash + Eq + ?Sized>(&mut self, id: &A) -> Option<&Axis>
    where
        AX: Borrow<A>,
    {
        self.axes.get(id)
    }

    /// Gets a list of all axes
    pub fn axes(&self) -> Vec<AX> {
        self.axes.keys().cloned().collect::<Vec<AX>>()
    }

    /// Add a button to an action.
    ///
    /// This will insert a new binding between this action and the button.
    pub fn insert_action_binding<A>(&mut self, id: A, binding: Button)
    where
        A: Hash + Eq + Into<AC>,
        AC: Borrow<A>,
    {
        let mut make_new = false;
        match self.actions.get_mut(&id) {
            Some(action_bindings) => if action_bindings.iter().all(|&b| b != binding) {
                action_bindings.push(binding);
            },
            None => {
                make_new = true;
            }
        }
        if make_new {
            let mut bindings = SmallVec::new();
            bindings.push(binding);
            self.actions.insert(id.into(), bindings);
        }
    }

    /// Removes an action binding that was assigned previously.
    pub fn remove_action_binding<T: Hash + Eq + ?Sized>(&mut self, id: &T, binding: Button)
    where
        AC: Borrow<T>,
    {
        let mut kill_it = false;
        if let Some(action_bindings) = self.actions.get_mut(id) {
            let index = action_bindings.iter().position(|&b| b == binding);
            if let Some(index) = index {
                action_bindings.swap_remove(index);
            }
            kill_it = action_bindings.is_empty();
        }
        if kill_it {
            self.actions.remove(id);
        }
    }

    /// Returns an action's bindings.
    pub fn action_bindings<T: Hash + Eq + ?Sized>(&self, id: &T) -> Option<&[Button]>
    where
        AC: Borrow<T>,
    {
        self.actions.get(id).map(|a| &**a)
    }

    /// Gets a list of all action bindings
    pub fn actions(&self) -> Vec<AC> {
        self.actions.keys().cloned().collect::<Vec<AC>>()
    }
}