Skip to content

Commit

Permalink
Use conversion fn instead of num_enum crate
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-bartkus committed Aug 7, 2024
1 parent 48bf2e0 commit 93769a6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
17 changes: 14 additions & 3 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/native/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 => {
Expand Down

0 comments on commit 93769a6

Please sign in to comment.