Skip to main content

jugar_probar/
event.rs

1//! Input event types for testing.
2
3use serde::{Deserialize, Serialize};
4
5/// Touch input action
6#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
7pub enum TouchAction {
8    /// Single tap
9    Tap,
10    /// Swipe gesture
11    Swipe {
12        /// End X coordinate
13        end_x: f32,
14        /// End Y coordinate
15        end_y: f32,
16        /// Duration in milliseconds
17        duration_ms: u32,
18    },
19    /// Hold/long press
20    Hold {
21        /// Duration in milliseconds
22        duration_ms: u32,
23    },
24}
25
26/// Touch input for testing
27#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct Touch {
29    /// X coordinate
30    pub x: f32,
31    /// Y coordinate
32    pub y: f32,
33    /// Touch action type
34    pub action: TouchAction,
35}
36
37impl Touch {
38    /// Create a tap touch event
39    #[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    /// Create a swipe touch event
49    #[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    /// Create a hold touch event
69    #[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/// Input event types
80#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
81pub enum InputEvent {
82    /// Touch event
83    Touch {
84        /// X coordinate
85        x: f32,
86        /// Y coordinate
87        y: f32,
88    },
89    /// Key press event
90    KeyPress {
91        /// Key code
92        key: String,
93    },
94    /// Key release event
95    KeyRelease {
96        /// Key code
97        key: String,
98    },
99    /// Mouse click event
100    MouseClick {
101        /// X coordinate
102        x: f32,
103        /// Y coordinate
104        y: f32,
105    },
106    /// Mouse move event
107    MouseMove {
108        /// X coordinate
109        x: f32,
110        /// Y coordinate
111        y: f32,
112    },
113    /// Gamepad button event
114    GamepadButton {
115        /// Button index
116        button: u8,
117        /// Pressed state
118        pressed: bool,
119    },
120}
121
122impl InputEvent {
123    /// Create a touch event
124    #[must_use]
125    pub const fn touch(x: f32, y: f32) -> Self {
126        Self::Touch { x, y }
127    }
128
129    /// Create a key press event
130    #[must_use]
131    pub fn key_press(key: impl Into<String>) -> Self {
132        Self::KeyPress { key: key.into() }
133    }
134
135    /// Create a key release event
136    #[must_use]
137    pub fn key_release(key: impl Into<String>) -> Self {
138        Self::KeyRelease { key: key.into() }
139    }
140
141    /// Create a mouse click event
142    #[must_use]
143    pub const fn mouse_click(x: f32, y: f32) -> Self {
144        Self::MouseClick { x, y }
145    }
146
147    /// Create a mouse move event
148    #[must_use]
149    pub const fn mouse_move(x: f32, y: f32) -> Self {
150        Self::MouseMove { x, y }
151    }
152
153    /// Create a gamepad button event
154    #[must_use]
155    pub const fn gamepad_button(button: u8, pressed: bool) -> Self {
156        Self::GamepadButton { button, pressed }
157    }
158}