show_image/event/device.rs
1use super::AxisId;
2use super::ButtonId;
3use super::DeviceId;
4use super::ElementState;
5use super::KeyboardInput;
6use super::MouseScrollDelta;
7
8/// Raw hardware events that are not associated with any particular window.
9///
10/// Useful for interactions that diverge significantly from a conventional 2D GUI, such as 3D camera or first-person game controls.
11/// Many physical actions, such as mouse movement, can produce both device and window events.
12/// Because window events typically arise from virtual devices (corresponding to GUI cursors and keyboard focus) the device IDs may not match.
13///
14/// Note that these events are delivered regardless of input focus.
15#[derive(Debug, Clone)]
16pub enum DeviceEvent {
17 /// A new device was added.
18 Added(DeviceAddedEvent),
19
20 /// A device was removed.
21 Removed(DeviceRemovedEvent),
22
23 /// Change in physical position of a pointing device.
24 MouseMotion(DeviceMouseMotionEvent),
25
26 /// The scroll-wheel of a mouse was moved.
27 MouseWheel(DeviceMouseWheelEvent),
28
29 /// Motion on some analog axis.
30 Motion(DeviceMotionEvent),
31
32 /// A button on a device was pressed or released.
33 Button(DeviceButtonEvent),
34
35 /// A device generated keyboard input.
36 KeyboardInput(DeviceKeyboardInputEvent),
37
38 /// A device generated text input.
39 TextInput(DeviceTextInputEvent),
40}
41
42#[derive(Debug, Clone)]
43/// A new device was added.
44pub struct DeviceAddedEvent {
45 /// The ID of the device.
46 pub device_id: DeviceId,
47}
48
49/// A device was removed.
50#[derive(Debug, Clone)]
51pub struct DeviceRemovedEvent {
52 /// The ID of the device.
53 pub device_id: DeviceId,
54}
55
56/// The physical position of a pointing device was moved.
57///
58/// This represents raw, unfiltered physical motion.
59/// Not to be confused with [`WindowMouseMoveEvent`][super::WindowMouseMoveEvent].
60#[derive(Debug, Clone)]
61pub struct DeviceMouseMotionEvent {
62 /// The ID of the device.
63 pub device_id: DeviceId,
64
65 /// The relative motion.
66 pub delta: glam::Vec2,
67}
68
69/// The scroll-wheel of a mouse was moved.
70#[derive(Debug, Clone)]
71pub struct DeviceMouseWheelEvent {
72 /// The ID of the device.
73 pub device_id: DeviceId,
74
75 /// The scroll delta.
76 pub delta: MouseScrollDelta,
77}
78
79/// An analog axis of a device was moved.
80///
81/// This event will be reported for all arbitrary input devices that winit supports on this platform, including mouse devices.
82/// If the device is a mouse device then this will be reported alongside the [`DeviceMouseMotionEvent`].
83#[derive(Debug, Clone)]
84pub struct DeviceMotionEvent {
85 /// The ID of the device.
86 pub device_id: DeviceId,
87
88 /// The axis that was moved.
89 pub axis: AxisId,
90
91 /// The value by which the axis was moved.
92 pub value: f64,
93}
94
95/// A button on a device was pressed or released.
96#[derive(Debug, Clone)]
97pub struct DeviceButtonEvent {
98 /// The ID of the device.
99 pub device_id: DeviceId,
100
101 /// The button that was pressed or released.
102 pub button: ButtonId,
103
104 /// The new state of the button (pressed or released).
105 pub state: ElementState,
106}
107
108/// A device generated keyboard input.
109#[derive(Debug, Clone)]
110pub struct DeviceKeyboardInputEvent {
111 /// The ID of the device.
112 pub device_id: DeviceId,
113
114 /// The keyboard input.
115 pub input: KeyboardInput,
116}
117
118/// A device generated text input.
119#[derive(Debug, Clone)]
120pub struct DeviceTextInputEvent {
121 /// The ID of the device.
122 pub device_id: DeviceId,
123
124 /// The unicode codepoint that was generated.
125 pub codepoint: char,
126}
127
128impl_from_variant!(DeviceEvent::Added(DeviceAddedEvent));
129impl_from_variant!(DeviceEvent::Removed(DeviceRemovedEvent));
130impl_from_variant!(DeviceEvent::MouseMotion(DeviceMouseMotionEvent));
131impl_from_variant!(DeviceEvent::MouseWheel(DeviceMouseWheelEvent));
132impl_from_variant!(DeviceEvent::Motion(DeviceMotionEvent));
133impl_from_variant!(DeviceEvent::Button(DeviceButtonEvent));
134impl_from_variant!(DeviceEvent::KeyboardInput(DeviceKeyboardInputEvent));
135impl_from_variant!(DeviceEvent::TextInput(DeviceTextInputEvent));