8000 Implement Into/From<u8>for MouseButton and Into/From<u16> for KeyCode by andybarron · Pull Request #657 · not-fl3/miniquad · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use crate::macros::convertible_enum;

convertible_enum! {
repr = u8,
unknown = Unknown,

#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
#[repr(u8)]
pub enum MouseButton {
Left = 0,
Middle = 1,
Right = 2,
Unknown = 255,
}
}

#[derive(Debug, Copy, Clone)]
pub struct Touch {
Expand All @@ -14,8 +20,11 @@ pub struct Touch {
pub y: f32,
}

convertible_enum! {
repr = u16,
unknown = Unknown,

#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
#[repr(u16)]
/// These keycode values are based off of X11's `keysymdef.h`.
/// Missing keycodes from that list are given the prefix 0x01.
pub enum KeyCode {
Expand Down Expand Up @@ -139,10 +148,11 @@ pub enum KeyCode {
RightAlt = 0xffea,
RightSuper = 0xffec,
Menu = 0xff67,
// Android back button
/// Android back button
Back = 0xff04,
Unknown = 0x01ff,
}
}

#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub struct KeyMods {
Expand Down Expand Up @@ -234,3 +244,33 @@ pub trait EventHandler {
/// can be requested with `ctx.dropped_file_bytes()`.
fn files_dropped_event(&mut self) {}
}

#[test]
fn test_mouse_button_from_u8() {
assert_eq!(MouseButton::from(0), MouseButton::Left);
assert_eq!(MouseButton::from(1), MouseButton::Middle);
assert_eq!(MouseButton::from(2), MouseButton::Right);
assert_eq!(MouseButton::from(3), MouseButton::Unknown);
}

#[test]
fn test_mouse_button_into_u8() {
assert_eq!(u8::from(MouseButton::Left), 0);
assert_eq!(u8::from(MouseButton::Middle), 1);
assert_eq!(u8::from(MouseButton::Right), 2);
assert_eq!(u8::from(MouseButton::Unknown), 255);
}

#[test]
fn test_keycode_from_u16() {
assert_eq!(KeyCode::from(0x0020), KeyCode::Space);
assert_eq!(KeyCode::from(0x0041), KeyCo 8000 de::A);
assert_eq!(KeyCode::from(0x0), KeyCode::Unknown);
}

#[test]
fn test_keycode_into_u16() {
assert_eq!(u16::from(KeyCode::Space), 0x0020);
assert_eq!(u16::from(KeyCode::A), 0x0041);
assert_eq!(u16::from(KeyCode::Unknown), 0x01ff);
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod conf;
mod event;
pub mod fs;
pub mod graphics;
mod macros;
pub mod native;
use std::collections::HashMap;
use std::ops::{Index, IndexMut};
Expand Down
59 changes: 59 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/// This macro accepts a data-less enum definition and generates `From` impls
/// for converting between the enum and its repr type.
///
/// Example usage:
/// ```ignore
/// convertible_enum! {
/// repr = u8,
/// unknown = Unknown,
///
/// #[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
/// pub enum MouseButton {
/// Left = 0,
/// Middle = 1,
/// Right = 2,
/// Unknown = 255,
/// }
/// }
/// ```
macro_rules! convertible_enum {
(
repr = $repr:ident,
unknown = $unknown_variant:ident,
$(#[$outer:meta])*
$vis:vis enum $name:ident {
$(
$(#[$inner:meta])*
$variant:ident = $value:expr
),* $(,)?
}
) => {
$(#[$outer])*
#[repr($repr)]
$vis enum $name {
$(
$(#[$inner])*
$variant = $value,
)*
}

impl From<$repr> for $name {
fn from(value: $repr) -> Self {
match value {
$(
$value => Self::$variant,
)*
_ => Self::$unknown_variant,
}
}
}

impl From<$name> for $repr {
fn from(value: $name) -> Self {
value as $repr
}
}
};
}

pub(crate) use convertible_enum;
Loading
0