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
mod phase;

pub use phase::TouchPhase;

use crate::error::GameResult;
use crate::window::LogicalPosition;
use std::collections::HashMap;

pub struct Touch {
    positions: HashMap<u64, LogicalPosition>,
}

impl Touch {
    pub(crate) fn new(_: TouchConfig) -> GameResult<Self> {
        Ok(Self {
            positions: HashMap::new(),
        })
    }

    pub(crate) fn handle_event(&mut self, id: u64, phase: TouchPhase, position: LogicalPosition) {
        match phase {
            TouchPhase::Start | TouchPhase::Move => self.positions.insert(id, position),
            TouchPhase::End | TouchPhase::Cancel => self.positions.remove(&id),
        };
    }

    pub(crate) fn clear_states(&mut self) {}

    pub fn touches(&self) -> Vec<u64> {
        let mut touches = Vec::with_capacity(self.positions.len());
        for (id, _) in &self.positions {
            touches.push(*id);
        }
        touches.sort();
        touches
    }

    pub fn position(&self, id: u64) -> Option<LogicalPosition> {
        self.positions.get(&id).map(|position| *position)
    }
}

#[derive(Debug, Clone)]
pub struct TouchConfig {}

impl TouchConfig {
    pub fn new() -> Self {
        Self {}
    }
}