//! Reimplementation crate of the KeyboardKey and MouseButton structs of raylib, because they do //! not implement `serde::Serialize` and `serde::Deserialize`. If you have a better idea on how to //! handle it, feel free. use raylib::ffi::{KeyboardKey as rlKeyboardKey, MouseButton as rlMouseButton}; use raylib::RaylibHandle; use serde::{Deserialize, Serialize}; use std::mem; /// Enum to abstract the distinction of keyboard keys and mouse keys that raylib does away so the /// user has free reign over what key they use for what purpose. #[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum Button { Mouse(MouseButton), Keyboard(KeyboardKey), } #[repr(u32)] #[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum MouseButton { Left = 0, Right = 1, Middle = 2, } #[repr(u32)] #[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum KeyboardKey { Apostrophe = 39, Comma = 44, Minus = 45, Period = 46, Slash = 47, Zero = 48, One = 49, Two = 50, Three = 51, Four = 52, Five = 53, Six = 54, Seven = 55, Eight = 56, Nine = 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, Space = 32, 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, LeftShift = 340, LeftControl = 341, LeftAlt = 342, LeftSuper = 343, RightShift = 344, RightControl = 345, RightAlt = 346, RightSuper = 347, Menu = 348, LeftBracket = 91, Backslash = 92, RightBracket = 93, Grave = 96, 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, } impl Button { pub fn is_pressed(self, rl: &RaylibHandle, mouse_blocked: bool) -> bool { match self { Self::Mouse(button) => !mouse_blocked && rl.is_mouse_button_pressed(button.into()), Self::Keyboard(key) => rl.is_key_pressed(key.into()), } } } impl From for Button { fn from(button: rlMouseButton) -> Self { Self::Mouse(MouseButton::from(button)) } } impl From for Button { fn from(key: rlKeyboardKey) -> Self { Self::Keyboard(KeyboardKey::from(key)) } } impl From for MouseButton { fn from(button: rlMouseButton) -> Self { unsafe { mem::transmute(button as u32) } } } impl Into for MouseButton { fn into(self) -> rlMouseButton { unsafe { mem::transmute(self as u32) } } } impl From for KeyboardKey { fn from(key: rlKeyboardKey) -> Self { unsafe { mem::transmute(key as u32) } } } impl Into for KeyboardKey { fn into(self) -> rlKeyboardKey { unsafe { mem::transmute(self as u32) } } }