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
use crate::{Finger, Pos};
use std::collections::HashMap;

/// Describes a physical keyboard and its properties.
#[derive(Clone)]
pub struct Keyboard {
    pub name: String,
    /// how staggered each row is, in units
    pub rowstagger: Vec<f64>,
    /// how staggered each column is, in units
    pub colstagger: Vec<f64>,
    /// number of (cols, rows)
    pub dimensions: [usize; 2],
    /// how tall each key is, in units
    pub keyheight: f64,
    pub fingers: Vec<Vec<Finger>>,
}

impl Keyboard {
    /// Returns the horizontal distance between two keys
    pub fn xdist(&self, a: &Pos, b: &Pos) -> f64 {
        ((self.rowstagger[a.row as usize] + a.col as f64)
            - (self.rowstagger[b.row as usize] + b.col as f64))
            .abs()
    }
    /// Returns the vertical distance between two keys
    pub fn ydist(&self, a: &Pos, b: &Pos) -> f64 {
        ((self.colstagger[a.col as usize] + a.row as f64)
            - (self.colstagger[b.col as usize] + b.row as f64))
            .abs()
    }
}

#[cfg(test)]
mod tests {
    use super::{Keyboard, Pos};
    use std::collections::HashMap;

    #[test]
    fn distance() {
        let matrix = Keyboard {
            name: "Matrix".to_string(),
            rowstagger: vec![0.0, 0.0, 0.0],
            colstagger: vec![0.0, 0.0, 0.0],
            dimensions: [10, 3],
            keyheight: 1.0,
            fingers: vec![vec![]],
        };
        assert_eq!(matrix.xdist(&Pos::new(0, 0), &Pos::new(1, 0)), 1.0);
        // shouldn't have any horizontal distance
        assert_eq!(matrix.xdist(&Pos::new(0, 0), &Pos::new(0, 1)), 0.0);

        assert_eq!(matrix.ydist(&Pos::new(0, 0), &Pos::new(0, 2)), 2.0);
        // shouldn't have any vertical distance
        assert_eq!(matrix.ydist(&Pos::new(0, 0), &Pos::new(2, 0)), 0.0);

        let mut ansi = matrix.clone();
        ansi.rowstagger = vec![-0.25, 0.0, 0.5];
        assert_eq!(ansi.xdist(&Pos::new(0, 0), &Pos::new(1, 0)), 1.0);
        assert_eq!(ansi.xdist(&Pos::new(0, 1), &Pos::new(1, 1)), 1.0);
        assert_eq!(ansi.xdist(&Pos::new(0, 2), &Pos::new(1, 2)), 1.0);

        assert_eq!(ansi.xdist(&Pos::new(0, 0), &Pos::new(1, 1)), 1.25);
        assert_eq!(ansi.xdist(&Pos::new(0, 0), &Pos::new(1, 2)), 1.75);
    }
    #[test]
    fn direction() {
        use crate::{Direction, Finger, FingerKind, Hand};
        let ri = Finger::new(Hand::Right, FingerKind::Index);
        let rm = Finger::new(Hand::Right, FingerKind::Middle);
        let li = Finger::new(Hand::Left, FingerKind::Index);
        assert_eq!(Direction::Inward, Direction::from(rm, ri));
        assert_eq!(Direction::Outward, Direction::from(ri, rm));
        assert_eq!(Direction::None, Direction::from(ri, li));
        assert_eq!(Direction::None, Direction::from(ri, ri));
    }
}