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
312 changes: 176 additions & 136 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
#[repr(u8)]
pub enum MouseButton {
Left = 0,
Middle = 1,
Right = 2,
Unknown = 255,
use crate::macros::convertible_enum;

convertible_enum! {
repr = u8,
unknown = Unknown,

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

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

#[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 {
Space = 0x0020,
Apostrophe = 0x0027,
Comma = 0x002c,
Minus = 0x002d,
Period = 0x002e,
Slash = 0x002f,
Key0 = 0x0030,
Key1 = 0x0031,
Key2 = 0x0032,
Key3 = 0x0033,
Key4 = 0x0034,
Key5 = 0x0035,
Key6 = 0x0036,
Key7 = 0x0037,
Key8 = 0x0038,
Key9 = 0x0039,
Semicolon = 0x003b,
Equal = 0x003d,
A = 0x0041,
B = 0x0042,
C = 0x0043,
D = 0x0044,
E = 0x0045,
F = 0x0046,
G = 0x0047,
H = 0x0048,
I = 0x0049,
J = 0x004a,
K = 0x004b,
L = 0x004c,
M = 0x004d,
N = 0x004e,
O = 0x004f,
P = 0x0050,
Q = 0x0051,
R = 0x0052,
S = 0x0053,
T = 0x0054,
U = 0x0055,
V = 0x0056,
W = 0x0057,
X = 0x0058,
Y = 0x0059,
Z = 0x005a,
LeftBracket = 0x005b,
Backslash = 0x005c,
RightBracket = 0x005d,
GraveAccent = 0x0060,
World1 = 0x0100,
World2 = 0x0101,
Escape = 0xff1b,
Enter = 0xff0d,
Tab = 0xff09,
Backspace = 0xff08,
Insert = 0xff63,
Delete = 0xffff,
Right = 0xff53,
Left = 0xff51,
Down = 0xff54,
Up = 0xff52,
PageUp = 0xff55,
PageDown = 0xff56,
Home = 0xff50,
End = 0xff57,
CapsLock = 0xffe5,
ScrollLock = 0xff14,
NumLock = 0xff7f,
PrintScreen = 0xfd1d,
Pause = 0xff13,
F1 = 0xffbe,
F2 = 0xffbf,
F3 = 0xffc0,
F4 = 0xffc1,
F5 = 0xffc2,
F6 = 0xffc3,
F7 = 0xffc4,
F8 = 0xffc5,
F9 = 0xffc6,
F10 = 0xffc7,
F11 = 0xffc8,
F12 = 0xffc9,
F13 = 0xffca,
F14 = 0xffcb,
F15 = 0xffcc,
F16 = 0xffcd,
F17 = 0xffce,
F18 = 0xffcf,
F19 = 0xffd0,
F20 = 0xffd1,
F21 = 0xffd2,
F22 = 0xffd3,
F23 = 0xffd4,
F24 = 0xffd5,
F25 = 0xffd6,
Kp0 = 0xffb0,
Kp1 = 0xffb1,
Kp2 = 0xffb2,
Kp3 = 0xffb3,
Kp4 = 0xffb4,
Kp5 = 0xffb5,
Kp6 = 0xffb6,
Kp7 = 0xffb7,
Kp8 = 0xffb8,
Kp9 = 0xffb9,
KpDecimal = 0xffae,
KpDivide = 0xffaf,
KpMultiply = 0xffaa,
KpSubtract = 0xffad,
KpAdd = 0xffab,
KpEnter = 0xff8d,
KpEqual = 0xffbd,
LeftShift = 0xffe1,
LeftControl = 0xffe3,
LeftAlt = 0xffe9,
LeftSuper = 0xffeb,
RightShift = 0xffe2,
RightControl = 0xffe4,
RightAlt = 0xffea,
RightSuper = 0xffec,
Menu = 0xff67,
// Android back button
Back = 0xff04,
Unknown = 0x01ff,
convertible_enum! {
repr = u16,
unknown = Unknown,

#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
/// These keycode values are based off of X11's `keysymdef.h`.
/// Missing keycodes from that list are given the prefix 0x01.
pub enum KeyCode {
Space = 0x0020,
Apostrophe = 0x0027,
Comma = 0x002c,
Minus = 0x002d,
Period = 0x002e,
Slash = 0x002f,
Key0 = 0x0030,
Key1 = 0x0031,
Key2 = 0x0032,
Key3 = 0x0033,
Key4 = 0x0034,
Key5 = 0x0035,
Key6 = 0x0036,
Key7 = 0x0037,
Key8 = 0x0038,
Key9 = 0x0039,
Semicolon = 0x003b,
Equal = 0x003d,
A = 0x0041,
B = 0x0042,
C = 0x0043,
D = 0x0044,
E = 0x0045,
F = 0x0046,
G = 0x0047,
H = 0x0048,
I = 0x0049,
J = 0x004a,
K = 0x004b,
L = 0x004c,
M = 0x004d,
N = 0x004e,
O = 0x004f,
P = 0x0050,
Q = 0x0051,
R = 0x0052,
S = 0x0053,
T = 0x0054,
U = 0x0055,
V = 0x0056,
W = 0x0057,
X = 0x0058,
Y = 0x0059,
Z = 0x005a,
LeftBracket = 0x005b,
Backslash = 0x005c,
RightBracket = 0x005d,
GraveAccent = 0x0060,
World1 = 0x0100,
World2 = 0x0101,
Escape = 0xff1b,
Enter = 0xff0d,
Tab = 0xff09,
Backspace = 0xff08,
Insert = 0xff63,
Delete = 0xffff,
Right = 0xff53,
Left = 0xff51,
Down = 0xff54,
Up = 0xff52,
PageUp = 0xff55,
PageDown = 0xff56,
Home = 0xff50,
End = 0xff57,
CapsLock = 0xffe5,
ScrollLock = 0xff14,
NumLock = 0xff7f,
PrintScreen = 0xfd1d,
Pause = 0xff13,
F1 = 0xffbe,
F2 = 0xffbf,
F3 = 0xffc0,
F4 = 0xffc1,
F5 = 0xffc2,
F6 = 0xffc3,
F7 = 0xffc4,
F8 = 0xffc5,
F9 = 0xffc6,
F10 = 0xffc7,
F11 = 0xffc8,
F12 = 0xffc9,
F13 = 0xffca,
F14 = 0xffcb,
F15 = 0xffcc,
F16 = 0xffcd,
F17 = 0xffce,
F18 = 0xffcf,
F19 = 0xffd0,
F20 = 0xffd1,
F21 = 0xffd2,
F22 = 0xffd3,
F23 = 0xffd4,
F24 = 0xffd5,
F25 = 0xffd6,
Kp0 = 0xffb0,
Kp1 = 0xffb1,
Kp2 = 0xffb2,
Kp3 = 0xffb3,
Kp4 = 0xffb4,
Kp5 = 0xffb5,
Kp6 = 0xffb6,
Kp7 = 0xffb7,
Kp8 = 0xffb8,
Kp9 = 0xffb9,
KpDecimal = 0xffae,
KpDivide = 0xffaf,
KpMultiply = 0xffaa,
KpSubtract = 0xffad,
KpAdd = 0xffab,
KpEnter = 0xff8d,
KpEqual = 0xffbd,
LeftShift = 0xffe1,
LeftControl = 0xffe3,
LeftAlt = 0xffe9,
LeftSuper = 0xffeb,
RightShift = 0xffe2,
RightControl = 0xffe4,
RightAlt = 0xffea,
RightSuper = 0xffec,
Menu = 0xff67,
/// Android back button
Back = 0xff04,
Unknown = 0x01ff,
}
}

#[derive(Debug, Copy, Clone, PartialEq, Default)]
Expand Down Expand Up @@ -177,7 +187,7 @@ pub trait EventHandler {
fn mouse_button_down_event(&mut self, _button: MouseButton, _x: f32, _y: f32) {}
fn mouse_button_up_event(&mut self, _button: MouseButton, _x: f32, _y: f32) {}
fn mouse_leave_event(&mut self) {}
fn mouse_enter_event(&mut self, _button: MouseButton ,_x: f32, _y: f32 ) {}
fn mouse_enter_event(&mut self, _button: MouseButton, _x: f32, _y: f32) {}

fn char_event(&mut self, _character: char, _keymods: KeyMods, _repeat: bool) {}

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), KeyCode::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
Loading
Loading