summaryrefslogtreecommitdiff
path: root/src/button_manager.rs
blob: 9915460d2a85aa87b7ba654214ce67e5f3fa87a9 (plain) (blame)
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
#[derive(Debug)]
pub struct ButtonManager<T = usize>(Vec<(f64, f64, T)>);

impl<T> Default for ButtonManager<T> {
    fn default() -> Self {
        Self(Vec::new())
    }
}

impl<T> ButtonManager<T> {
    pub fn push(&mut self, x_offset: f64, width: f64, elem: T) {
        self.0.push((x_offset, width, elem));
    }

    pub fn clear(&mut self) {
        self.0.clear()
    }

    pub fn click(&self, x: f64) -> Option<&T> {
        self.0
            .iter()
            .find(|(x_off, w, _)| x >= *x_off && x <= *x_off + *w)
            .map(|(_, _, e)| e)
    }

    pub fn is_between(&self, x: f64) -> bool {
        let mut left = false;
        let mut right = false;
        for &(x_off, w, _) in &self.0 {
            left |= x_off <= x;
            right |= x_off + w >= x;
        }
        left && right
    }
}