use std::path::PathBuf;
#[derive(Debug)]
#[non_exhaustive]
pub enum Event {
Close,
KeyDown(Key),
KeyUp(Key),
KeyRepeat(Key),
RefreshWindow,
ResizeWindow(i32, i32),
MoveWindow(i32, i32),
TypeChar(char),
Scroll(f64, f64),
DropFiles(Vec<PathBuf>),
MoveMouse(f64, f64),
Focus,
Unfocus,
CursorEnter,
CursorExit,
MouseDown(MouseButton),
MouseUp(MouseButton),
Other(glfw::WindowEvent),
}
impl Event {
#[inline]
#[must_use]
pub fn from_glfw(glfw_event: glfw::WindowEvent) -> Event {
#[expect(
clippy::wildcard_enum_match_arm,
reason = "if more WindowEvent variants are added to glfw, we still want to ignore them (for now at least)"
)]
match glfw_event {
glfw::WindowEvent::Close => Event::Close,
glfw::WindowEvent::Refresh => Event::RefreshWindow,
glfw::WindowEvent::FramebufferSize(width, height) => Event::ResizeWindow(width, height),
glfw::WindowEvent::Pos(x, y) => Event::MoveWindow(x, y),
glfw::WindowEvent::Char(ch) => Event::TypeChar(ch),
glfw::WindowEvent::Scroll(scroll_x, scroll_y) => Event::Scroll(scroll_x, scroll_y),
glfw::WindowEvent::FileDrop(file_bufs) => Event::DropFiles(file_bufs),
glfw::WindowEvent::CursorPos(x, y) => Event::MoveMouse(x, y),
glfw::WindowEvent::Focus(is_focused) => {
if is_focused {
Event::Focus
} else {
Event::Unfocus
}
}
glfw::WindowEvent::CursorEnter(just_entered) => {
if just_entered {
Event::CursorEnter
} else {
Event::CursorExit
}
}
glfw::WindowEvent::MouseButton(glfw_mouse_button, action, _) => {
let mouse_button = MouseButton::from_glfw(glfw_mouse_button);
match action {
glfw::Action::Press => Event::MouseDown(mouse_button),
glfw::Action::Release => Event::MouseUp(mouse_button),
glfw::Action::Repeat => Event::Other(glfw_event),
}
}
glfw::WindowEvent::Key(glfw_key, _, glfw_action, _) => {
let key = unsafe { Key::from_glfw(glfw_key) };
match glfw_action {
glfw::Action::Press => Event::KeyDown(key),
glfw::Action::Release => Event::KeyUp(key),
glfw::Action::Repeat => Event::KeyRepeat(key),
}
}
_ => Event::Other(glfw_event),
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum MouseButton {
Left,
Middle,
Right,
Other,
}
impl MouseButton {
#[inline]
#[expect(
clippy::single_call_fn,
reason = "Separating this internal, unsafe behaviour helps keep the code more concise. Also, this is likely to cause memory bugs, so is a single unsafe function."
)]
const fn from_glfw(glfw_mouse_button: glfw::MouseButton) -> MouseButton {
#[expect(
clippy::wildcard_enum_match_arm,
reason = "if more variants are added, we still want to ignore them"
)]
match glfw_mouse_button {
glfw::MouseButton::Button1 => MouseButton::Left,
glfw::MouseButton::Button2 => MouseButton::Right,
glfw::MouseButton::Button3 => MouseButton::Middle,
_ => MouseButton::Other,
}
}
}
#[repr(usize)]
#[derive(Debug)]
#[non_exhaustive]
pub enum Key {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
Period = 46,
Slash = 47,
Num0 = 48,
Num1 = 49,
Num2 = 50,
Num3 = 51,
Num4 = 52,
Num5 = 53,
Num6 = 54,
Num7 = 55,
Num8 = 56,
Num9 = 57,
Semicolon = 59,
Equal = 61,
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
LeftBracket = 91,
Backslash = 92,
RightBracket = 93,
GraveAccent = 96,
World1 = 161,
World2 = 162,
Escape = 256,
Enter = 257,
Tab = 258,
Backspace = 259,
Insert = 260,
Delete = 261,
Right = 262,
Left = 263,
Down = 264,
Up = 265,
PageUp = 266,
PageDown = 267,
Home = 268,
End = 269,
CapsLock = 280,
ScrollLock = 281,
NumLock = 282,
PrintScreen = 283,
Pause = 284,
F1 = 290,
F2 = 291,
F3 = 292,
F4 = 293,
F5 = 294,
F6 = 295,
F7 = 296,
F8 = 297,
F9 = 298,
F10 = 299,
F11 = 300,
F12 = 301,
F13 = 302,
F14 = 303,
F15 = 304,
F16 = 305,
F17 = 306,
F18 = 307,
F19 = 308,
F20 = 309,
F21 = 310,
F22 = 311,
F23 = 312,
F24 = 313,
F25 = 314,
Keypad0 = 320,
Keypad1 = 321,
Keypad2 = 322,
Keypad3 = 323,
Keypad4 = 324,
Keypad5 = 325,
Keypad6 = 326,
Keypad7 = 327,
Keypad8 = 328,
Keypad9 = 329,
KeypadDecimal = 330,
KeypadDivide = 331,
KeypadMultiply = 332,
KeypadSubtract = 333,
KeypadAdd = 334,
KeypadEnter = 335,
KeypadEqual = 336,
LeftShift = 340,
LeftControl = 341,
LeftAlt = 342,
LeftSuper = 343,
RightShift = 344,
RightControl = 345,
RightAlt = 346,
RightSuper = 347,
Menu = 348,
}
impl Key {
#[expect(
clippy::single_call_fn,
reason = "Separating this internal, unsafe behaviour helps keep the code more concise. Also, this is likely to cause memory bugs, so is a single unsafe function."
)]
const unsafe fn from_glfw(glfw_key: glfw::Key) -> Key {
use core::mem;
#[expect(
clippy::as_conversions,
reason = "can't find a way other than `as` to convert an enum variant to its respective integer"
)]
unsafe {
mem::transmute(glfw_key as usize)
}
}
}