1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
7pub enum TouchAction {
8 Tap,
10 Swipe {
12 end_x: f32,
14 end_y: f32,
16 duration_ms: u32,
18 },
19 Hold {
21 duration_ms: u32,
23 },
24}
25
26#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct Touch {
29 pub x: f32,
31 pub y: f32,
33 pub action: TouchAction,
35}
36
37impl Touch {
38 #[must_use]
40 pub const fn tap(x: f32, y: f32) -> Self {
41 Self {
42 x,
43 y,
44 action: TouchAction::Tap,
45 }
46 }
47
48 #[must_use]
50 pub const fn swipe(
51 start_x: f32,
52 start_y: f32,
53 end_x: f32,
54 end_y: f32,
55 duration_ms: u32,
56 ) -> Self {
57 Self {
58 x: start_x,
59 y: start_y,
60 action: TouchAction::Swipe {
61 end_x,
62 end_y,
63 duration_ms,
64 },
65 }
66 }
67
68 #[must_use]
70 pub const fn hold(x: f32, y: f32, duration_ms: u32) -> Self {
71 Self {
72 x,
73 y,
74 action: TouchAction::Hold { duration_ms },
75 }
76 }
77}
78
79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
81pub enum InputEvent {
82 Touch {
84 x: f32,
86 y: f32,
88 },
89 KeyPress {
91 key: String,
93 },
94 KeyRelease {
96 key: String,
98 },
99 MouseClick {
101 x: f32,
103 y: f32,
105 },
106 MouseMove {
108 x: f32,
110 y: f32,
112 },
113 GamepadButton {
115 button: u8,
117 pressed: bool,
119 },
120}
121
122impl InputEvent {
123 #[must_use]
125 pub const fn touch(x: f32, y: f32) -> Self {
126 Self::Touch { x, y }
127 }
128
129 #[must_use]
131 pub fn key_press(key: impl Into<String>) -> Self {
132 Self::KeyPress { key: key.into() }
133 }
134
135 #[must_use]
137 pub fn key_release(key: impl Into<String>) -> Self {
138 Self::KeyRelease { key: key.into() }
139 }
140
141 #[must_use]
143 pub const fn mouse_click(x: f32, y: f32) -> Self {
144 Self::MouseClick { x, y }
145 }
146
147 #[must_use]
149 pub const fn mouse_move(x: f32, y: f32) -> Self {
150 Self::MouseMove { x, y }
151 }
152
153 #[must_use]
155 pub const fn gamepad_button(button: u8, pressed: bool) -> Self {
156 Self::GamepadButton { button, pressed }
157 }
158}