-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy patheventloop.rs
78 lines (66 loc) · 2.16 KB
/
eventloop.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::cell::{RefCell};
use std::collections::{HashMap};
extern "C" {
fn event_loop_new() -> u32;
fn event_loop_raf(id: u32);
fn event_loop_shutdown(id: u32) -> bool;
}
const EVENT_DESTROYED: u32 = 0;
const EVENT_ANIMATION_FRAME: u32 = 1;
const EVENT_MOUSE_MOVE: u32 = 2;
const EVENT_KEY_DOWN: u32 = 3;
const EVENT_KEY_UP: u32 = 4;
#[derive(Copy,Clone)]
pub enum Event {
Destroyed,
AnimationFrame,
MouseMove { x: u32, y: u32 },
KeyDown { code: u32, chr: Option<char>, flags: u32 },
KeyUp { code: u32, chr: Option<char>, flags: u32 },
}
#[no_mangle]
pub extern "C"
fn event_loop_cb(id: u32, msg: u32, p0: u32, p1: u32, p2: u32) {
EVENTLOOPS.with(|el| {
let mut el = el.borrow_mut();
let mut fake_event_loop = EventLoop { id: id };
let event = match msg {
EVENT_DESTROYED => {
if let Some(mut cb) = el.remove(&id) {
cb(Event::Destroyed, &mut fake_event_loop);
}
return;
},
EVENT_ANIMATION_FRAME => Event::AnimationFrame,
EVENT_MOUSE_MOVE => Event::MouseMove { x: p0, y: p1 },
EVENT_KEY_DOWN => Event::KeyDown { code: p0, chr: ::std::char::from_u32(p1), flags: p2 },
EVENT_KEY_UP => Event::KeyUp { code: p0, chr: ::std::char::from_u32(p1), flags: p2 },
_ => return,
};
if let Some(cb) = el.get_mut(&id) {
cb(event, &mut fake_event_loop);
}
});
}
thread_local! {
static EVENTLOOPS: RefCell<HashMap<u32, EventLoopCb>> = RefCell::new(HashMap::new());
}
pub type EventLoopCb = Box<FnMut(Event, &mut EventLoop)>;
pub struct EventLoop {
id: u32,
}
impl EventLoop {
pub fn new(cb: EventLoopCb) -> EventLoop {
let id = unsafe { event_loop_new() };
EVENTLOOPS.with(|el| {
el.borrow_mut().insert(id, cb);
});
EventLoop { id: id }
}
pub fn request_animation_frame(&mut self) {
unsafe { event_loop_raf(self.id); }
}
pub fn shutdown(&mut self) {
unsafe { event_loop_shutdown(self.id); }
}
}