xref: /StarryEngine/starry_client/src/base/event.rs (revision bee61dca287acb4b9fd6d747ba3f687aebacab90)
1 #[derive(Copy, Clone, Debug)]
2 pub enum EventOption {
3     /// 按键事件
4     Key(KeyEvent),
5     /// 鼠标相对移动事件
6     MouseRelative(MouseRelativeEvent),
7     /// 鼠标按键事件
8     Button(ButtonEvent),
9     /// 空事件
10     None,
11     /// 未知事件
12     Unknown,
13 }
14 
15 /// TODO: 整理
16 pub const EVENT_NONE: i64 = 0;
17 pub const EVENT_KEY: i64 = 1;
18 pub const EVENT_MOUSE_RELATIVE: i64 = 2;
19 pub const EVENT_BUTTON: i64 = 3;
20 pub const EVENT_MOUSE_UPDATE: i64 = 4;
21 
22 /// 通用事件
23 #[derive(Copy, Clone, Debug)]
24 pub struct Event {
25     pub code: i64,
26     pub a: i64,
27     pub b: i64,
28 }
29 
30 impl Event {
31     pub fn new() -> Event {
32         Event {
33             code: 0,
34             a: 0,
35             b: 0,
36         }
37     }
38 
39     pub fn to_option(self) -> EventOption {
40         match self.code {
41             EVENT_NONE => EventOption::None,
42             EVENT_KEY => EventOption::Key(KeyEvent::from_event(self)),
43             EVENT_MOUSE_RELATIVE => EventOption::MouseRelative(MouseRelativeEvent::from_event(self)),
44             EVENT_BUTTON => EventOption::Button(ButtonEvent::from_event(self)),
45             _ => EventOption::Unknown,
46         }
47     }
48 }
49 
50 /// 键盘按键事件
51 #[derive(Copy, Clone, Debug)]
52 pub struct KeyEvent {
53     /// 按键字符
54     pub character: char,
55     /// 按键扫描码
56     pub scancode: u8,
57     /// 是否按下
58     pub pressed: bool,
59 }
60 
61 impl KeyEvent {
62     /// 转换为Event
63     pub fn to_event(&self) -> Event {
64         Event {
65             code: EVENT_KEY,
66             a: self.character as i64,
67             b: self.scancode as i64 | (self.pressed as i64) << 8,
68         }
69     }
70 
71     /// 从Event转换为KeyEvent
72     pub fn from_event(event: Event) -> KeyEvent {
73         KeyEvent {
74             character: char::from_u32(event.a as u32).unwrap_or('\0'),
75             scancode: event.b as u8,
76             pressed: event.b & (1 << 8) == (1 << 8),
77         }
78     }
79 }
80 
81 /// 鼠标相对移动事件
82 #[derive(Copy, Clone, Debug)]
83 pub struct MouseRelativeEvent {
84     /// x轴向上的相对运动
85     pub dx: i32,
86     /// y轴向上的相对运动
87     pub dy: i32,
88 }
89 
90 impl MouseRelativeEvent {
91     /// 转换为Event
92     pub fn to_event(&self) -> Event {
93         Event {
94             code: EVENT_MOUSE_RELATIVE,
95             a: self.dx as i64,
96             b: self.dy as i64,
97         }
98     }
99 
100     /// 从Event转换为MouseRelativeEvent
101     pub fn from_event(event: Event) -> MouseRelativeEvent {
102         MouseRelativeEvent {
103             dx: event.a as i32,
104             dy: event.b as i32,
105         }
106     }
107 }
108 
109 /// TODO: 按键松开事件
110 /// 鼠标按键事件
111 #[derive(Clone, Copy, Debug)]
112 pub struct ButtonEvent {
113     /// 左键是否按下
114     pub left: bool,
115     /// 右键是否按下
116     pub right: bool,
117     /// 中键是否按下
118     pub middle: bool,
119 }
120 
121 impl ButtonEvent {
122     pub fn new(byte: u8) -> Self {
123         ButtonEvent {
124             left: byte & (1 << 0) == 1,
125             middle: byte & (1 << 1) == 1,
126             right: byte & (1 << 2) == 1,
127         }
128     }
129 
130     /// 转换为Event
131     pub fn to_event(&self) -> Event {
132         Event {
133             code: EVENT_BUTTON,
134             a: self.left as i64 | (self.middle as i64) << 1 | (self.right as i64) << 2,
135             b: 0
136         }
137     }
138 
139     /// 从Event转换为ButtonEvent
140     pub fn from_event(event: Event) -> ButtonEvent {
141         ButtonEvent {
142             left: event.a & (1 << 0)  == 1,
143             middle: event.a & (1 << 1) == 1,
144             right: event.a & (1 << 2) == 1
145         }
146     }
147 }
148 
149 /// 鼠标位置更新事件
150 pub struct MouseUpdateEvent {
151     /// 更新后鼠标位置x坐标
152     pub x: i32,
153     /// 更新后鼠标位置y坐标
154     pub y: i32,
155 }
156 
157 impl MouseUpdateEvent {
158     /// 转换为Event
159     pub fn to_event(&self) -> Event {
160         Event {
161             code: EVENT_MOUSE_UPDATE,
162             a: self.x as i64,
163             b: self.y as i64,
164         }
165     }
166 
167     /// 从Event转换为MouseUpdateEvent
168     pub fn from_event(event: Event) -> MouseUpdateEvent {
169         MouseUpdateEvent {
170             x: event.a as i32,
171             y: event.b as i32,
172         }
173     }
174 }