From 93769a690e34684c7ec9eb09822fb0b92f59bbc2 Mon Sep 17 00:00:00 2001 From: guy-bartkus Date: Wed, 7 Aug 2024 11:38:10 -0700 Subject: [PATCH] Use conversion fn instead of num_enum crate --- Cargo.toml | 3 --- src/event.rs | 17 ++++++++++++++--- src/native/windows.rs | 8 ++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 80aab889..20b204e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,9 +20,6 @@ categories = ["rendering::graphics-api"] # disabled by default log-impl = [] -[dependencies] -num_enum = "0.7.3" - [target.'cfg(target_os = "linux")'.dependencies] libc = "0.2" diff --git a/src/event.rs b/src/event.rs index be6ab284..6c42992a 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,6 +1,4 @@ -use num_enum::TryFromPrimitive; - -#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, TryFromPrimitive)] +#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] #[repr(u8)] pub enum MouseButton { Left = 0, @@ -11,6 +9,19 @@ pub enum MouseButton { Unknown = 255, } +impl MouseButton { + pub fn from_u8(value: u8) -> MouseButton { + match value { + 0 => MouseButton::Left, + 1 => MouseButton::Middle, + 2 => MouseButton::Right, + 3 => MouseButton::MB4, + 4 => MouseButton::MB5, + _ => MouseButton::Unknown, + } + } +} + #[derive(Debug, Copy, Clone)] pub struct Touch { pub id: u32, diff --git a/src/native/windows.rs b/src/native/windows.rs index af864909..4f1bf699 100644 --- a/src/native/windows.rs +++ b/src/native/windows.rs @@ -368,9 +368,9 @@ unsafe extern "system" fn win32_wndproc( let mouse_x = payload.mouse_x; let mouse_y = payload.mouse_y; let xbutton = GET_XBUTTON_WPARAM(wparam) as u8; - let which = MouseButton::try_from(xbutton+2); + let which = MouseButton::from_u8(xbutton+2); - event_handler.mouse_button_down_event(which.unwrap(), mouse_x, mouse_y); + event_handler.mouse_button_down_event(which, mouse_x, mouse_y); } WM_LBUTTONUP => { let mouse_x = payload.mouse_x; @@ -394,9 +394,9 @@ unsafe extern "system" fn win32_wndproc( let mouse_x = payload.mouse_x; let mouse_y = payload.mouse_y; let xbutton = GET_XBUTTON_WPARAM(wparam) as u8; - let which = MouseButton::try_from(xbutton+2); + let which = MouseButton::from_u8(xbutton+2); - event_handler.mouse_button_up_event(which.unwrap(), mouse_x, mouse_y); + event_handler.mouse_button_up_event(which, mouse_x, mouse_y); } WM_MOUSEMOVE => {