euv_engine/input/struct.rs
1use super::*;
2
3/// A zero-sized namespace struct providing static input event extraction methods.
4///
5/// This struct follows the same pattern as `App`, serving as a namespace for
6/// free-standing input utility functions that extract data from DOM events.
7#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
8pub struct Input;
9
10/// Tracks the current state of all input devices (keyboard, mouse, touch)
11/// for a single game frame. The state should be updated by event handlers
12/// and cleared of per-frame data at the end of each frame.
13#[derive(Clone, Data, Debug, New, PartialEq)]
14pub struct InputState {
15 /// Key codes that were pressed during this frame.
16 #[get_mut(pub(crate))]
17 #[new(skip)]
18 pub(crate) keys_pressed: KeyStateSet,
19 /// Key codes that are currently held down.
20 #[get_mut(pub(crate))]
21 #[new(skip)]
22 pub(crate) keys_held: KeyStateSet,
23 /// Key codes that were released during this frame.
24 #[get_mut(pub(crate))]
25 #[new(skip)]
26 pub(crate) keys_released: KeyStateSet,
27 /// Mouse buttons that were pressed during this frame.
28 #[get_mut(pub(crate))]
29 #[new(skip)]
30 pub(crate) mouse_buttons_pressed: HashSet<MouseButton>,
31 /// Mouse buttons that are currently held down.
32 #[get_mut(pub(crate))]
33 #[new(skip)]
34 pub(crate) mouse_buttons_held: HashSet<MouseButton>,
35 /// Mouse buttons that were released during this frame.
36 #[get_mut(pub(crate))]
37 #[new(skip)]
38 pub(crate) mouse_buttons_released: HashSet<MouseButton>,
39 /// The current mouse position in screen coordinates.
40 #[get(type(copy))]
41 #[get_mut(pub(crate))]
42 #[new(skip)]
43 pub(crate) mouse_position: Vector2D,
44 /// The mouse position delta (movement) since the last frame.
45 #[get(type(copy))]
46 #[get_mut(pub(crate))]
47 #[new(skip)]
48 pub(crate) mouse_delta: Vector2D,
49 /// Whether the mouse has moved during this frame.
50 #[get(type(copy))]
51 #[get_mut(pub(crate))]
52 #[new(skip)]
53 pub(crate) mouse_moved: bool,
54 /// Active touch points mapped by identifier to screen position.
55 #[get_mut(pub(crate))]
56 #[new(skip)]
57 pub(crate) touch_points: TouchPointMap,
58 /// Touch point identifiers that started during this frame.
59 #[get_mut(pub(crate))]
60 #[new(skip)]
61 pub(crate) touch_started: HashSet<i32>,
62 /// Touch point identifiers that ended during this frame.
63 #[get_mut(pub(crate))]
64 #[new(skip)]
65 pub(crate) touch_ended: HashSet<i32>,
66}
67
68/// Per-frame and steady-state data for a single gamepad index.
69///
70/// The edge sets follow the same contract as [`InputState`]:
71/// `buttons_pressed` records a button that went down on the frame it
72/// went down and is cleared by [`GamepadState::end_frame`],
73/// `buttons_held` records every button currently down and survives
74/// `end_frame`, and `buttons_released` records a button that was held
75/// and came up on this frame.
76#[derive(Clone, Data, Debug, Default, PartialEq)]
77pub struct GamepadState {
78 /// The browser-reported identifier of the device, e.g. a mapping
79 /// string such as `Xbox 360 Controller (XInput STANDARD GAMEPAD)`.
80 #[get(type(clone))]
81 pub(crate) id: String,
82 /// The current reading of every analog axis, indexed by axis number.
83 /// Values outside the deadzone are stored as reported; the deadzone
84 /// is applied on read by [`GamepadState::axis`].
85 #[get_mut(pub(crate))]
86 pub(crate) axes: Vec<f64>,
87 /// The current pressure of every button, indexed by button number.
88 /// Analog triggers report a continuous value here, digital buttons
89 /// report `1.0` while down and `0.0` while up.
90 #[get_mut(pub(crate))]
91 pub(crate) button_values: Vec<f64>,
92 /// Whether the device is currently connected. A disconnected pad
93 /// keeps its last `id` so callers can still identify it.
94 #[get(type(copy))]
95 pub(crate) connected: bool,
96 /// Button indices that were pressed during this frame.
97 #[get_mut(pub(crate))]
98 pub(crate) buttons_pressed: GamepadButtonSet,
99 /// Button indices that are currently held down.
100 #[get_mut(pub(crate))]
101 pub(crate) buttons_held: GamepadButtonSet,
102 /// Button indices that were released during this frame.
103 #[get_mut(pub(crate))]
104 pub(crate) buttons_released: GamepadButtonSet,
105}
106
107/// Owns the state of every gamepad the page has seen and drives the
108/// per-frame poll.
109///
110/// A poll re-reads `navigator.get_gamepads()` and folds the fresh raw
111/// readings into the stored state, computing this frame's edge sets
112/// from the previous frame's held set. The DOM connect / disconnect
113/// events are honoured too: a device announced between two polls is
114/// already present as a state when the next poll runs, so polling
115/// alone never loses a connect or a disconnect.
116#[derive(Clone, Data, Debug, Default, PartialEq)]
117pub struct GamepadManager {
118 /// Every gamepad state ever observed, keyed by gamepad index.
119 #[get_mut(pub(crate))]
120 pub(crate) states: GamepadStateMap,
121 /// Gamepad indices that were first seen during this frame.
122 #[get_mut(pub(crate))]
123 pub(crate) connected: GamepadIndexSet,
124 /// Gamepad indices that were last seen leaving during this frame.
125 #[get_mut(pub(crate))]
126 pub(crate) disconnected: GamepadIndexSet,
127}