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