show_image/event/
mod.rs

1//! Event types.
2
3pub use device::*;
4pub use window::*;
5
6pub use winit::event::AxisId;
7pub use winit::event::ButtonId;
8pub use winit::event::DeviceId;
9pub use winit::event::Force;
10pub use winit::event::ModifiersState;
11pub use winit::event::MouseScrollDelta;
12pub use winit::event::ScanCode;
13pub use winit::event::StartCause;
14pub use winit::event::Touch;
15pub use winit::event::TouchPhase;
16pub use winit::event::VirtualKeyCode;
17
18macro_rules! impl_from_variant {
19	($for:ident::$variant:ident($from:ty)) => {
20		impl From<$from> for $for {
21			fn from(other: $from) -> Self {
22				Self::$variant(other)
23			}
24		}
25	};
26}
27
28mod device;
29mod window;
30
31/// Control flow properties for event handlers.
32///
33/// Instances of this struct are passed to event handlers
34/// to allow them to remove themselves and to stop event propagation.
35#[derive(Debug, Default, Clone)]
36pub struct EventHandlerControlFlow {
37	/// Remove the event handler after it returned.
38	pub remove_handler: bool,
39
40	/// Stop propagation of the event to other event handlers.
41	pub stop_propagation: bool,
42}
43
44/// Global event.
45///
46/// This also includes window events for all windows.
47#[derive(Debug, Clone)]
48pub enum Event {
49	/// New events are available for processing.
50	///
51	/// This indicates the start of a new event-processing cycle.
52	NewEvents,
53
54	/// A window event.
55	WindowEvent(WindowEvent),
56
57	/// A device event.
58	DeviceEvent(DeviceEvent),
59
60	/// The application has been suspended.
61	Suspended,
62
63	/// The application has been resumed.
64	Resumed,
65
66	/// All input events have been processed and redraw processing is about to begin.
67	MainEventsCleared,
68
69	/// All open redraw requests have been processed.
70	RedrawEventsCleared,
71
72	/// All windows were closed.
73	///
74	/// This event can be received multiple times if you open a new window after all windows were closed.
75	AllWindowsClosed,
76}
77
78impl_from_variant!(Event::WindowEvent(WindowEvent));
79impl_from_variant!(Event::DeviceEvent(DeviceEvent));
80
81/// Keyboard input.
82#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
83pub struct KeyboardInput {
84	/// Scan code of the physical key.
85	///
86	/// This should not change if the user adjusts the host's keyboard map.
87	/// Use when the physical location of the key is more important than the key's host GUI semantics, such as for movement controls in a first-person game.
88	pub scan_code: ScanCode,
89
90	/// Virtual key code indentifying the semantic meaning of the key.
91	///
92	/// Use this when the semantics of the key are more important than the physical location of the key, such as when implementing appropriate behavior for "page up".
93	pub key_code: Option<VirtualKeyCode>,
94
95	/// State of the key (pressed or released).
96	pub state: ElementState,
97
98	/// Keyboard modifiers that were active at the time of the event.
99	pub modifiers: ModifiersState,
100}
101
102/// OS theme (light or dark).
103#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
104pub enum Theme {
105	/// The theme is a light theme.
106	Light,
107
108	/// The theme is a dark theme.
109	Dark,
110}
111
112impl Theme {
113	/// Check if the theme is light.
114	pub fn is_light(self) -> bool {
115		self == Self::Light
116	}
117
118	/// Check if the theme is dark.
119	pub fn is_dark(self) -> bool {
120		self == Self::Dark
121	}
122}
123
124/// State of a button or key.
125#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
126pub enum ElementState {
127	/// The button or key is pressed.
128	Pressed,
129
130	/// The button or key is released.
131	Released,
132}
133
134impl ElementState {
135	/// Check if the button or key is pressed.
136	pub fn is_pressed(self) -> bool {
137		self == Self::Pressed
138	}
139
140	/// Check if the button or key is released.
141	pub fn is_released(self) -> bool {
142		self == Self::Released
143	}
144}
145
146#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
147/// A mouse button.
148pub enum MouseButton {
149	/// The left mouse button.
150	Left,
151
152	/// The right mouse button.
153	Right,
154
155	/// The middle mouse button (usually triggered by pressing the scroll wheel).
156	Middle,
157
158	/// An other mouse button identified by index.
159	Other(u16),
160}
161
162impl MouseButton {
163	/// Check if the button is the left mouse button.
164	pub fn is_left(self) -> bool {
165		self == Self::Left
166	}
167
168	/// Check if the button is the right mouse button.
169	pub fn is_right(self) -> bool {
170		self == Self::Right
171	}
172
173	/// Check if the button is the middle mouse button.
174	pub fn is_middle(self) -> bool {
175		self == Self::Middle
176	}
177
178	/// Check if the button is a specific other button.
179	pub fn is_other(self, other: u16) -> bool {
180		self == Self::Other(other)
181	}
182}
183
184/// The state of all mouse buttons.
185#[derive(Debug, Clone, Default)]
186pub struct MouseButtonState {
187	/// The set of pressed buttons.
188	buttons: std::collections::BTreeSet<MouseButton>,
189}
190
191impl MouseButtonState {
192	/// Check if a button is pressed.
193	pub fn is_pressed(&self, button: MouseButton) -> bool {
194		self.buttons.get(&button).is_some()
195	}
196
197	/// Iterate over all pressed buttons.
198	pub fn iter_pressed(&self) -> impl Iterator<Item = MouseButton> + '_ {
199		self.buttons.iter().copied()
200	}
201
202	/// Mark a button as pressed or unpressed.
203	pub fn set_pressed(&mut self, button: MouseButton, pressed: bool) {
204		if pressed {
205			self.buttons.insert(button);
206		} else {
207			self.buttons.remove(&button);
208		}
209	}
210}