Skip to main content

oa/input/
type.rs

1//! Platform-neutral input vocabulary.
2//!
3//! Donor: `oa/ui/event.h` (`UiEvent`, `UiKey`, modifiers, gestures) and the
4//! shared motion-timing preset. These records carry no widget semantics so
5//! `oa::input` stays usable outside the UI layer.
6
7// ── Key ─────────────────────────────────────────────────────────────────────
8// Values match SDL3 scancodes. Donor: `oa/ui/event.h`
9
10/// Platform-neutral keyboard key identifier (SDL3 scancode values).
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
12#[repr(u32)]
13pub enum Key {
14	#[default]
15	Unknown = 0,
16	A = 4,
17	B = 5,
18	C = 6,
19	D = 7,
20	E = 8,
21	F = 9,
22	G = 10,
23	H = 11,
24	I = 12,
25	J = 13,
26	K = 14,
27	L = 15,
28	M = 16,
29	N = 17,
30	O = 18,
31	P = 19,
32	Q = 20,
33	R = 21,
34	S = 22,
35	T = 23,
36	U = 24,
37	V = 25,
38	W = 26,
39	X = 27,
40	Y = 28,
41	Z = 29,
42	Num1 = 30,
43	Num2 = 31,
44	Num3 = 32,
45	Num4 = 33,
46	Num5 = 34,
47	Num6 = 35,
48	Num7 = 36,
49	Num8 = 37,
50	Num9 = 38,
51	Num0 = 39,
52	Return = 40,
53	Escape = 41,
54	Backspace = 42,
55	Tab = 43,
56	Space = 44,
57	Minus = 45,
58	Equals = 46,
59	Comma = 54,
60	Period = 55,
61	Slash = 56,
62	F1 = 58,
63	F2 = 59,
64	F3 = 60,
65	F4 = 61,
66	F5 = 62,
67	F6 = 63,
68	F7 = 64,
69	F8 = 65,
70	F9 = 66,
71	F10 = 67,
72	F11 = 68,
73	F12 = 69,
74	Home = 74,
75	Delete = 76,
76	End = 77,
77	Right = 79,
78	Left = 80,
79	Down = 81,
80	Up = 82,
81	KpEnter = 88,
82	Kp1 = 89,
83	Kp2 = 90,
84	Kp3 = 91,
85	Kp4 = 92,
86	Kp5 = 93,
87	Kp6 = 94,
88	Kp7 = 95,
89	Kp8 = 96,
90	Kp9 = 97,
91	Kp0 = 98,
92}
93
94impl Key {
95	/// Try to convert an SDL3 scancode integer to a `Key`.
96	pub fn new_from_sdl_scancode(scancode: u32) -> Self {
97		match scancode {
98			4 => Self::A,
99			5 => Self::B,
100			6 => Self::C,
101			7 => Self::D,
102			8 => Self::E,
103			9 => Self::F,
104			10 => Self::G,
105			11 => Self::H,
106			12 => Self::I,
107			13 => Self::J,
108			14 => Self::K,
109			15 => Self::L,
110			16 => Self::M,
111			17 => Self::N,
112			18 => Self::O,
113			19 => Self::P,
114			20 => Self::Q,
115			21 => Self::R,
116			22 => Self::S,
117			23 => Self::T,
118			24 => Self::U,
119			25 => Self::V,
120			26 => Self::W,
121			27 => Self::X,
122			28 => Self::Y,
123			29 => Self::Z,
124			30 => Self::Num1,
125			31 => Self::Num2,
126			32 => Self::Num3,
127			33 => Self::Num4,
128			34 => Self::Num5,
129			35 => Self::Num6,
130			36 => Self::Num7,
131			37 => Self::Num8,
132			38 => Self::Num9,
133			39 => Self::Num0,
134			40 => Self::Return,
135			41 => Self::Escape,
136			42 => Self::Backspace,
137			43 => Self::Tab,
138			44 => Self::Space,
139			45 => Self::Minus,
140			46 => Self::Equals,
141			54 => Self::Comma,
142			55 => Self::Period,
143			56 => Self::Slash,
144			58 => Self::F1,
145			59 => Self::F2,
146			60 => Self::F3,
147			61 => Self::F4,
148			62 => Self::F5,
149			63 => Self::F6,
150			64 => Self::F7,
151			65 => Self::F8,
152			66 => Self::F9,
153			67 => Self::F10,
154			68 => Self::F11,
155			69 => Self::F12,
156			74 => Self::Home,
157			76 => Self::Delete,
158			77 => Self::End,
159			79 => Self::Right,
160			80 => Self::Left,
161			81 => Self::Down,
162			82 => Self::Up,
163			88 => Self::KpEnter,
164			89 => Self::Kp1,
165			90 => Self::Kp2,
166			91 => Self::Kp3,
167			92 => Self::Kp4,
168			93 => Self::Kp5,
169			94 => Self::Kp6,
170			95 => Self::Kp7,
171			96 => Self::Kp8,
172			97 => Self::Kp9,
173			98 => Self::Kp0,
174			_ => Self::Unknown,
175		}
176	}
177
178	/// True when key is a navigation arrow.
179	pub fn is_arrow(self) -> bool {
180		matches!(self, Self::Left | Self::Right | Self::Up | Self::Down)
181	}
182}
183
184// ── InputModifiers ───────────────────────────────────────────────────────────────
185
186/// Modifier key bitmask (matches donor `UiModifier*` constants).
187pub type InputModifiers = u32;
188pub const INPUT_MOD_NONE: InputModifiers = 0;
189pub const INPUT_MOD_SHIFT: InputModifiers = 1 << 0;
190pub const INPUT_MOD_CTRL: InputModifiers = 1 << 1;
191pub const INPUT_MOD_ALT: InputModifiers = 1 << 2;
192pub const INPUT_MOD_SUPER: InputModifiers = 1 << 3;
193
194// ── ScrollGesture ───────────────────────────────────────────────────────────
195
196/// Classified scroll gesture — maps to donor `UiScrollGesture`.
197#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
198pub enum ScrollGesture {
199	#[default]
200	None,
201	/// Discrete mouse-wheel notch → zoom-at-cursor.
202	MouseWheel,
203	/// Smooth two-finger trackpad pan.
204	TouchpadPan,
205	/// Ctrl + smooth scroll (Wayland pinch fallback).
206	PinchScroll,
207}
208
209// ── PinchPhase ─────────────────────────────────────────────────────────────
210
211/// Phase of a multi-touch pinch gesture.
212#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
213pub enum PinchPhase {
214	#[default]
215	None,
216	Begin,
217	Update,
218	End,
219}
220
221// ── InputEvent ───────────────────────────────────────────────────────────────────
222
223/// Platform-neutral input event. Donor: `oa/ui/event.h` `UiEvent`.
224#[derive(Clone, Debug, Default)]
225pub struct InputEvent {
226	pub kind: InputEventKind,
227	/// Physical pixel pointer position.
228	pub mouse_x: f32,
229	pub mouse_y: f32,
230	/// Pointer delta from previous position.
231	pub mouse_dx: f32,
232	pub mouse_dy: f32,
233	/// 1 = left, 2 = middle, 3 = right.
234	pub button: i32,
235	pub click_count: i32,
236	/// Smooth scroll deltas (SDL3 wheel.x/y).
237	pub scroll_x: f32,
238	pub scroll_y: f32,
239	/// Accumulated discrete wheel ticks (SDL3 integer_x/y).
240	pub integer_scroll_x: i32,
241	pub integer_scroll_y: i32,
242	/// SDL3 timestamp in nanoseconds (for burst detection).
243	pub timestamp_ns: u64,
244	pub scroll_gesture: ScrollGesture,
245	/// Multi-touch pinch — scale relative to Begin (1.0 = no change).
246	pub gesture_scale: f32,
247	pub pinch_phase: PinchPhase,
248	pub key: Key,
249	pub modifiers: InputModifiers,
250	pub key_repeat: bool,
251	/// Committed UTF-8 text from platform IME.
252	pub text: String,
253	pub text_selection_start: i32,
254	pub text_selection_length: i32,
255	/// Resized window pixel dimensions.
256	pub window_w: i32,
257	pub window_h: i32,
258	/// Gamepad instance id — meaningful only on `Gamepad*` event kinds.
259	pub gamepad_id: u32,
260	/// Standard gamepad button code (`GAMEPAD_BUTTON_*`) — meaningful only
261	/// on `GamepadButton*` event kinds.
262	pub gamepad_button: i32,
263	/// Standard gamepad axis code (`GAMEPAD_AXIS_*`) — meaningful only on
264	/// `GamepadAxisMotion`.
265	pub gamepad_axis: i32,
266	/// Axis reading in SDL's -32768..=32767 range on `GamepadAxisMotion`.
267	pub gamepad_value: i32,
268}
269
270/// Standard gamepad button codes — SDL's `SDL_GAMEPAD_BUTTON_*` numbering.
271pub const GAMEPAD_BUTTON_SOUTH: i32 = 0;
272/// `SDL_GAMEPAD_BUTTON_EAST` — B / Circle.
273pub const GAMEPAD_BUTTON_EAST: i32 = 1;
274/// `SDL_GAMEPAD_BUTTON_WEST` — X / Square.
275pub const GAMEPAD_BUTTON_WEST: i32 = 2;
276/// `SDL_GAMEPAD_BUTTON_NORTH` — Y / Triangle.
277pub const GAMEPAD_BUTTON_NORTH: i32 = 3;
278/// `SDL_GAMEPAD_BUTTON_BACK`.
279pub const GAMEPAD_BUTTON_BACK: i32 = 4;
280/// `SDL_GAMEPAD_BUTTON_GUIDE`.
281pub const GAMEPAD_BUTTON_GUIDE: i32 = 5;
282/// `SDL_GAMEPAD_BUTTON_START`.
283pub const GAMEPAD_BUTTON_START: i32 = 6;
284/// `SDL_GAMEPAD_BUTTON_LEFT_STICK`.
285pub const GAMEPAD_BUTTON_LEFT_STICK: i32 = 7;
286/// `SDL_GAMEPAD_BUTTON_RIGHT_STICK`.
287pub const GAMEPAD_BUTTON_RIGHT_STICK: i32 = 8;
288/// `SDL_GAMEPAD_BUTTON_LEFT_SHOULDER`.
289pub const GAMEPAD_BUTTON_LEFT_SHOULDER: i32 = 9;
290/// `SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER`.
291pub const GAMEPAD_BUTTON_RIGHT_SHOULDER: i32 = 10;
292/// `SDL_GAMEPAD_BUTTON_DPAD_UP`.
293pub const GAMEPAD_BUTTON_DPAD_UP: i32 = 11;
294/// `SDL_GAMEPAD_BUTTON_DPAD_DOWN`.
295pub const GAMEPAD_BUTTON_DPAD_DOWN: i32 = 12;
296/// `SDL_GAMEPAD_BUTTON_DPAD_LEFT`.
297pub const GAMEPAD_BUTTON_DPAD_LEFT: i32 = 13;
298/// `SDL_GAMEPAD_BUTTON_DPAD_RIGHT`.
299pub const GAMEPAD_BUTTON_DPAD_RIGHT: i32 = 14;
300
301/// Standard gamepad axis codes — SDL's `SDL_GAMEPAD_AXIS_*` numbering.
302pub const GAMEPAD_AXIS_LEFT_X: i32 = 0;
303/// `SDL_GAMEPAD_AXIS_LEFTY`.
304pub const GAMEPAD_AXIS_LEFT_Y: i32 = 1;
305/// `SDL_GAMEPAD_AXIS_RIGHTX`.
306pub const GAMEPAD_AXIS_RIGHT_X: i32 = 2;
307/// `SDL_GAMEPAD_AXIS_RIGHTY`.
308pub const GAMEPAD_AXIS_RIGHT_Y: i32 = 3;
309/// `SDL_GAMEPAD_AXIS_LEFT_TRIGGER`.
310pub const GAMEPAD_AXIS_LEFT_TRIGGER: i32 = 4;
311/// `SDL_GAMEPAD_AXIS_RIGHT_TRIGGER`.
312pub const GAMEPAD_AXIS_RIGHT_TRIGGER: i32 = 5;
313
314impl InputEvent {
315	pub fn ctrl(&self) -> bool {
316		self.modifiers & INPUT_MOD_CTRL != 0
317	}
318	pub fn shift(&self) -> bool {
319		self.modifiers & INPUT_MOD_SHIFT != 0
320	}
321	pub fn alt(&self) -> bool {
322		self.modifiers & INPUT_MOD_ALT != 0
323	}
324
325	/// Construct a pointer-move event.
326	pub fn mouse_move(x: f32, y: f32, modifiers: InputModifiers) -> Self {
327		Self {
328			kind: InputEventKind::MouseMove,
329			mouse_x: x,
330			mouse_y: y,
331			modifiers,
332			gesture_scale: 1.0,
333			text_selection_start: -1,
334			text_selection_length: -1,
335			..Default::default()
336		}
337	}
338	/// Construct a pointer-down event.
339	pub fn mouse_down(
340		x: f32,
341		y: f32,
342		button: i32,
343		click_count: i32,
344		modifiers: InputModifiers,
345	) -> Self {
346		Self {
347			kind: InputEventKind::MouseDown,
348			mouse_x: x,
349			mouse_y: y,
350			button,
351			click_count,
352			modifiers,
353			gesture_scale: 1.0,
354			text_selection_start: -1,
355			text_selection_length: -1,
356			..Default::default()
357		}
358	}
359	/// Construct a pointer-up event.
360	pub fn mouse_up(x: f32, y: f32, button: i32, modifiers: InputModifiers) -> Self {
361		Self {
362			kind: InputEventKind::MouseUp,
363			mouse_x: x,
364			mouse_y: y,
365			button,
366			modifiers,
367			gesture_scale: 1.0,
368			text_selection_start: -1,
369			text_selection_length: -1,
370			..Default::default()
371		}
372	}
373	/// Construct a key-down event.
374	pub fn key_down(key: Key, modifiers: InputModifiers, repeat: bool) -> Self {
375		Self {
376			kind: InputEventKind::KeyDown,
377			key,
378			modifiers,
379			key_repeat: repeat,
380			gesture_scale: 1.0,
381			text_selection_start: -1,
382			text_selection_length: -1,
383			..Default::default()
384		}
385	}
386	/// Construct a key-up event.
387	pub fn key_up(key: Key, modifiers: InputModifiers) -> Self {
388		Self {
389			kind: InputEventKind::KeyUp,
390			key,
391			modifiers,
392			gesture_scale: 1.0,
393			text_selection_start: -1,
394			text_selection_length: -1,
395			..Default::default()
396		}
397	}
398}
399
400/// Discriminant for [`InputEvent`]. Donor: `UiEventType`.
401#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
402pub enum InputEventKind {
403	#[default]
404	None,
405	MouseMove,
406	MouseDown,
407	MouseUp,
408	MouseScroll,
409	KeyDown,
410	KeyUp,
411	/// Committed text from platform IME.
412	KeyChar,
413	/// Window pixel-size changed.
414	WindowResize,
415	WindowClose,
416	WindowFocus,
417	WindowBlur,
418	/// Multi-touch pinch.
419	Pinch,
420	/// IME pre-edit text update.
421	TextEditing,
422	MouseEnter,
423	MouseLeave,
424	/// Standard-layout gamepad button down.
425	GamepadButtonDown,
426	/// Standard-layout gamepad button up.
427	GamepadButtonUp,
428	/// Standard-layout gamepad axis motion.
429	GamepadAxisMotion,
430}
431
432// ── InputState ─────────────────────────────────────────────────────────────
433
434/// Stable per-frame pointer and modifier snapshot. Donor: `oa/ui/event.h`.
435#[derive(Clone, Debug, Default)]
436pub struct InputState {
437	pub mouse_x: f32,
438	pub mouse_y: f32,
439	pub mouse_dx: f32,
440	pub mouse_dy: f32,
441	pub l_button: bool,
442	pub m_button: bool,
443	pub r_button: bool,
444	/// True on the exact frame the left button transitioned down.
445	pub l_pressed: bool,
446	/// True on the exact frame the left button transitioned up.
447	pub l_released: bool,
448	pub l_click_count: i32,
449	pub scroll_x: f32,
450	pub scroll_y: f32,
451	pub modifiers: InputModifiers,
452	// ── backwards-compat field aliases ──────────────────────────────────────
453	/// Alias for `l_button` (legacy API used by current widget code).
454	pub pointer_down: bool,
455	pub pointer_x: f32,
456	pub pointer_y: f32,
457	pub shift: bool,
458	pub ctrl: bool,
459}
460
461impl InputState {
462	pub const EMPTY: InputState = InputState {
463		mouse_x: 0.0,
464		mouse_y: 0.0,
465		mouse_dx: 0.0,
466		mouse_dy: 0.0,
467		l_button: false,
468		m_button: false,
469		r_button: false,
470		l_pressed: false,
471		l_released: false,
472		l_click_count: 0,
473		scroll_x: 0.0,
474		scroll_y: 0.0,
475		modifiers: INPUT_MOD_NONE,
476		pointer_down: false,
477		pointer_x: 0.0,
478		pointer_y: 0.0,
479		shift: false,
480		ctrl: false,
481	};
482}
483
484/// Shared donor timing policy for widget and viewport transitions.
485#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
486pub enum MotionSpeed {
487	#[default]
488	Normal,
489	Fast,
490	Fastest,
491	Instant,
492}
493
494impl MotionSpeed {
495	pub const fn duration_ms(self, normal: f32) -> f32 {
496		match self {
497			Self::Normal => normal,
498			Self::Fast => normal * 0.65,
499			Self::Fastest => normal * 0.35,
500			Self::Instant => 0.0,
501		}
502	}
503
504	pub const fn next(self) -> Self {
505		match self {
506			Self::Normal => Self::Fast,
507			Self::Fast => Self::Fastest,
508			Self::Fastest => Self::Instant,
509			Self::Instant => Self::Normal,
510		}
511	}
512}