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
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct StylusAngle<T> {
    pub azimuth: T,
    pub altitude: T,
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub enum TouchPhase {
    Started,
    Moved, // Or pintch changed
    Ended,
    Cancelled,
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Touch {
    pub phase: TouchPhase,
    pub position: glam::Vec2,
    // The angle of the stylus: Apple Pencil
    pub stylus_angle: Option<StylusAngle<f32>>,
    pub pressure: f32,
    // The radius of the contact ellipse along the major axis, in logical pixels.
    pub major_radius: f32,
    // Time interval from the previous touch point
    pub interval: f32,
}

impl Touch {
    pub fn touch_start(position: glam::Vec2) -> Self {
        Self::new(position, TouchPhase::Started)
    }

    pub fn touch_move(position: glam::Vec2) -> Self {
        Self::new(position, TouchPhase::Moved)
    }

    pub fn touch_end(position: glam::Vec2) -> Self {
        Self::new(position, TouchPhase::Ended)
    }

    fn new(position: glam::Vec2, phase: TouchPhase) -> Self {
        Touch {
            position,
            phase,
            stylus_angle: None,
            pressure: 0.0,
            major_radius: 0.0,
            interval: 0.0,
        }
    }
}