1 #[derive(Copy, Clone, Debug)] 2 pub enum EventOption { 3 /// 按键事件 4 Key(KeyEvent), 5 /// 鼠标相对移动事件 6 MouseRelative(MouseRelativeEvent), 7 /// 鼠标按键事件 8 Button(ButtonEvent), 9 /// 窗口位置移动事件 10 WindowMove(WindowMoveEvent), 11 /// 窗口大小改变事件 12 WindowResize(WindowResizeEvent), 13 /// 未知事件 14 Unknown(Event), 15 /// 空事件 16 None, 17 } 18 19 pub const EVENT_NONE: i64 = 0; 20 pub const EVENT_KEY: i64 = 1; 21 pub const EVENT_MOUSE_RELATIVE: i64 = 2; 22 pub const EVENT_BUTTON: i64 = 3; 23 pub const EVENT_MOUSE_UPDATE: i64 = 4; 24 pub const EVENT_WINDOW_MOVE: i64 = 5; 25 pub const EVENT_WINDOW_RESIZE: i64 = 6; 26 27 /// 通用事件 28 #[derive(Copy, Clone, Debug)] 29 pub struct Event { 30 pub code: i64, 31 pub a: i64, 32 pub b: i64, 33 } 34 35 impl Event { new() -> Event36 pub fn new() -> Event { 37 Event { 38 code: 0, 39 a: 0, 40 b: 0, 41 } 42 } 43 to_option(self) -> EventOption44 pub fn to_option(self) -> EventOption { 45 match self.code { 46 EVENT_NONE => EventOption::None, 47 EVENT_KEY => EventOption::Key(KeyEvent::from_event(self)), 48 EVENT_MOUSE_RELATIVE => { 49 EventOption::MouseRelative(MouseRelativeEvent::from_event(self)) 50 } 51 EVENT_BUTTON => EventOption::Button(ButtonEvent::from_event(self)), 52 _ => EventOption::Unknown(self), 53 } 54 } 55 } 56 57 /// 键盘按键事件 58 #[derive(Copy, Clone, Debug)] 59 pub struct KeyEvent { 60 /// 按键字符 61 pub character: char, 62 /// 按键扫描码 63 pub scancode: u8, 64 /// 是否按下 65 pub pressed: bool, 66 } 67 68 impl KeyEvent { 69 /// 转换为Event to_event(&self) -> Event70 pub fn to_event(&self) -> Event { 71 Event { 72 code: EVENT_KEY, 73 a: self.character as i64, 74 b: self.scancode as i64 | (self.pressed as i64) << 8, 75 } 76 } 77 78 /// 从Event转换为KeyEvent from_event(event: Event) -> KeyEvent79 pub fn from_event(event: Event) -> KeyEvent { 80 KeyEvent { 81 character: char::from_u32(event.a as u32).unwrap_or('\0'), 82 scancode: event.b as u8, 83 pressed: event.b & (1 << 8) == (1 << 8), 84 } 85 } 86 } 87 88 /// 鼠标相对移动事件 89 #[derive(Copy, Clone, Debug)] 90 pub struct MouseRelativeEvent { 91 /// x轴向上的相对运动 92 pub dx: i32, 93 /// y轴向上的相对运动 94 pub dy: i32, 95 } 96 97 impl MouseRelativeEvent { 98 /// 转换为Event to_event(&self) -> Event99 pub fn to_event(&self) -> Event { 100 Event { 101 code: EVENT_MOUSE_RELATIVE, 102 a: self.dx as i64, 103 b: self.dy as i64, 104 } 105 } 106 107 /// 从Event转换为MouseRelativeEvent from_event(event: Event) -> MouseRelativeEvent108 pub fn from_event(event: Event) -> MouseRelativeEvent { 109 MouseRelativeEvent { 110 dx: event.a as i32, 111 dy: event.b as i32, 112 } 113 } 114 } 115 116 /// TODO: 按键松开事件 117 /// 鼠标按键事件 118 #[derive(Clone, Copy, Debug)] 119 pub struct ButtonEvent { 120 /// 左键是否按下 121 pub left: bool, 122 /// 右键是否按下 123 pub right: bool, 124 /// 中键是否按下 125 pub middle: bool, 126 } 127 128 impl ButtonEvent { new(byte: u8) -> Self129 pub fn new(byte: u8) -> Self { 130 ButtonEvent { 131 left: byte & (1 << 0) == 1, 132 middle: byte & (1 << 1) == 1, 133 right: byte & (1 << 2) == 1, 134 } 135 } 136 137 /// 转换为Event to_event(&self) -> Event138 pub fn to_event(&self) -> Event { 139 Event { 140 code: EVENT_BUTTON, 141 a: self.left as i64 | (self.middle as i64) << 1 | (self.right as i64) << 2, 142 b: 0, 143 } 144 } 145 146 /// 从Event转换为ButtonEvent from_event(event: Event) -> ButtonEvent147 pub fn from_event(event: Event) -> ButtonEvent { 148 ButtonEvent { 149 left: event.a & (1 << 0) == 1, 150 middle: event.a & (1 << 1) == 1, 151 right: event.a & (1 << 2) == 1, 152 } 153 } 154 } 155 156 /// 鼠标位置更新事件 157 pub struct MouseUpdateEvent { 158 /// 更新后鼠标位置x坐标 159 pub x: i32, 160 /// 更新后鼠标位置y坐标 161 pub y: i32, 162 } 163 164 impl MouseUpdateEvent { 165 /// 转换为Event to_event(&self) -> Event166 pub fn to_event(&self) -> Event { 167 Event { 168 code: EVENT_MOUSE_UPDATE, 169 a: self.x as i64, 170 b: self.y as i64, 171 } 172 } 173 174 /// 从Event转换为MouseUpdateEvent from_event(event: Event) -> MouseUpdateEvent175 pub fn from_event(event: Event) -> MouseUpdateEvent { 176 MouseUpdateEvent { 177 x: event.a as i32, 178 y: event.b as i32, 179 } 180 } 181 } 182 183 /// 窗口位置移动事件 184 #[derive(Copy, Clone, Debug)] 185 pub struct WindowMoveEvent { 186 pub x: i32, 187 pub y: i32, 188 } 189 190 impl WindowMoveEvent { to_event(&self) -> Event191 pub fn to_event(&self) -> Event { 192 Event { 193 code: EVENT_WINDOW_MOVE, 194 a: self.x as i64, 195 b: self.y as i64, 196 } 197 } 198 from_event(event: Event) -> WindowMoveEvent199 pub fn from_event(event: Event) -> WindowMoveEvent { 200 WindowMoveEvent { 201 x: event.a as i32, 202 y: event.b as i32, 203 } 204 } 205 } 206 207 /// 窗口改变大小事件 208 #[derive(Copy, Clone, Debug)] 209 pub struct WindowResizeEvent { 210 pub width: u32, 211 pub height: u32, 212 } 213 214 impl WindowResizeEvent { to_event(&self) -> Event215 pub fn to_event(&self) -> Event { 216 Event { 217 code: EVENT_WINDOW_RESIZE, 218 a: self.width as i64, 219 b: self.height as i64, 220 } 221 } 222 from_event(event: Event) -> WindowResizeEvent223 pub fn from_event(event: Event) -> WindowResizeEvent { 224 WindowResizeEvent { 225 width: event.a as u32, 226 height: event.b as u32, 227 } 228 } 229 } 230