Skip to main content

ohos_xcomponent_binding/events/
touch_event.rs

1use ohos_xcomponent_sys::{OH_NativeXComponent_TouchEvent, OH_NativeXComponent_TouchPoint};
2
3use crate::{TouchEvent, TouchPointTool};
4
5#[derive(Debug, Clone)]
6pub struct TouchEventData {
7    pub id: i32,
8    pub screen_x: f32,
9    pub screen_y: f32,
10    pub x: f32,
11    pub y: f32,
12    pub event_type: TouchEvent,
13    pub size: f64,
14    pub force: f32,
15    pub device_id: i64,
16    pub timestamp: i64,
17    pub touch_points: Vec<TouchPointData>,
18    pub num_points: u32,
19}
20
21impl Default for TouchEventData {
22    fn default() -> Self {
23        Self {
24            id: 0,
25            screen_x: 0.0,
26            screen_y: 0.0,
27            x: 0.0,
28            y: 0.0,
29            event_type: TouchEvent::Unknown,
30            size: 0.0,
31            force: 0.0,
32            device_id: 0,
33            timestamp: 0,
34            touch_points: vec![TouchPointData::default(); 10],
35            num_points: 0,
36        }
37    }
38}
39
40impl From<TouchEventData> for OH_NativeXComponent_TouchEvent {
41    fn from(value: TouchEventData) -> Self {
42        let default_value = OH_NativeXComponent_TouchPoint {
43            id: 0,
44            screenX: 0.0,
45            screenY: 0.0,
46            x: 0.0,
47            y: 0.0,
48            type_: 0,
49            size: 0.0,
50            force: 0.0,
51            timeStamp: 0,
52            isPressed: false,
53        };
54        let mut touch_points = [default_value; 10];
55
56        for (i, point) in value.touch_points.iter().take(10).enumerate() {
57            touch_points[i] = point.clone().into();
58        }
59
60        OH_NativeXComponent_TouchEvent {
61            id: value.id,
62            screenX: value.screen_x,
63            screenY: value.screen_y,
64            x: value.x,
65            y: value.y,
66            type_: value.event_type.into(),
67            size: value.size,
68            force: value.force,
69            deviceId: value.device_id,
70            timeStamp: value.timestamp,
71            touchPoints: touch_points,
72            numPoints: value.num_points,
73        }
74    }
75}
76
77impl From<OH_NativeXComponent_TouchEvent> for TouchEventData {
78    fn from(value: OH_NativeXComponent_TouchEvent) -> Self {
79        let mut touch_points = Vec::with_capacity(value.numPoints as usize);
80        for point in value.touchPoints.iter().take(value.numPoints as usize) {
81            touch_points.push(TouchPointData {
82                id: point.id,
83                screen_x: point.screenX,
84                screen_y: point.screenY,
85                x: point.x,
86                y: point.y,
87                event_type: point.type_.into(),
88                size: point.size,
89                force: point.force,
90                timestamp: point.timeStamp,
91                is_pressed: point.isPressed,
92                event_tool_type: TouchPointTool::Unknown,
93            });
94        }
95
96        Self {
97            id: value.id,
98            screen_x: value.screenX,
99            screen_y: value.screenY,
100            x: value.x,
101            y: value.y,
102            event_type: value.type_.into(),
103            size: value.size,
104            force: value.force,
105            device_id: value.deviceId,
106            timestamp: value.timeStamp,
107            touch_points,
108            num_points: value.numPoints,
109        }
110    }
111}
112
113#[derive(Debug, Clone)]
114pub struct TouchPointData {
115    pub id: i32,
116    pub screen_x: f32,
117    pub screen_y: f32,
118    pub x: f32,
119    pub y: f32,
120    pub event_type: TouchEvent,
121    pub size: f64,
122    pub force: f32,
123    pub timestamp: i64,
124    pub is_pressed: bool,
125    pub event_tool_type: TouchPointTool,
126}
127
128impl Default for TouchPointData {
129    fn default() -> Self {
130        Self {
131            id: 0,
132            screen_x: 0.0,
133            screen_y: 0.0,
134            x: 0.0,
135            y: 0.0,
136            event_type: TouchEvent::Unknown,
137            size: 0.0,
138            force: 0.0,
139            timestamp: 0,
140            is_pressed: false,
141            event_tool_type: TouchPointTool::Unknown,
142        }
143    }
144}
145
146impl From<TouchPointData> for OH_NativeXComponent_TouchPoint {
147    fn from(value: TouchPointData) -> Self {
148        OH_NativeXComponent_TouchPoint {
149            id: value.id,
150            screenX: value.screen_x,
151            screenY: value.screen_y,
152            x: value.x,
153            y: value.y,
154            type_: value.event_type.into(),
155            size: value.size,
156            force: value.force,
157            timeStamp: value.timestamp,
158            isPressed: value.is_pressed,
159        }
160    }
161}