-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
executable file
·53 lines (44 loc) · 1.81 KB
/
mod.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
use conrod::glium;
pub struct EventLoop {
ui_needs_update: bool,
last_update: std::time::Instant,
}
impl EventLoop {
pub fn new() -> Self {
EventLoop { last_update: std::time::Instant::now(),
ui_needs_update: true,
}
}
/// Produce an iterator yielding all available events.
pub fn next(&mut self, events_loop: &mut glium::glutin::EventsLoop) ->
Vec<glium::glutin::Event> {
// We don't want to loop any faster than 60 FPS, so wait until it has been at least 16ms
// since the last yield.
let last_update = self.last_update;
let sixteen_ms = std::time::Duration::from_millis(16);
let duration_since_last_update = std::time::Instant::now().duration_since(last_update);
if duration_since_last_update < sixteen_ms {
std::thread::sleep(sixteen_ms - duration_since_last_update);
}
// Collect all pending events.
let mut events = Vec::new();
events_loop.poll_events(|event| events.push(event));
// If there are no events and the UI does not need updating, wait
// for the next event.
if events.is_empty() && !self.ui_needs_update {
events_loop.run_forever(|event| { events.push(event);
glium::glutin::ControlFlow::Break });
}
self.ui_needs_update = false;
self.last_update = std::time::Instant::now();
events
}
/// Notifies the event loop that the `Ui` requires another update whether
/// or not there are any pending events.
///
/// This is primarily used on the occasion that some part of the UI is
/// still animating and requires further updates to do so.
pub fn needs_update(&mut self) {
self.ui_needs_update = true;
}
}