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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::sync::mpsc::Receiver;
use std::rc::Rc;
use std::cell::RefCell;
use glfw;

/// An event.
pub struct Event<'a> {
    /// The event timestamp.
    pub timestamp: f64,
    /// The exact glfw event value. This can be modified to fool the other event handlers.
    pub value:     glfw::WindowEvent,
    /// Set this to `true` to prevent the window or the camera from handling the event.
    pub inhibited: bool,
    inhibitor:     &'a RefCell<Vec<glfw::WindowEvent>>
}

impl<'a> Drop for Event<'a> {
    #[inline]
    fn drop(&mut self) {
        if !self.inhibited {
            self.inhibitor.borrow_mut().push(self.value.clone())
        }
    }
}

impl<'a> Event<'a> {
    #[inline]
    fn new(timestamp: f64,
           value:     glfw::WindowEvent,
           inhibitor: &RefCell<Vec<glfw::WindowEvent>>)
           -> Event {
        Event {
            timestamp: timestamp,
            value:     value,
            inhibited: false,
            inhibitor: inhibitor
        }
    }
}

/// An iterator through events.
pub struct Events<'a> {
    stream:    glfw::FlushedMessages<'a, (f64, glfw::WindowEvent)>,
    inhibitor: &'a RefCell<Vec<glfw::WindowEvent>>
}

impl<'a> Events<'a> {
    #[inline]
    fn new(stream:    glfw::FlushedMessages<'a, (f64, glfw::WindowEvent)>,
           inhibitor: &'a RefCell<Vec<glfw::WindowEvent>>)
           -> Events<'a> {
        Events {
            stream:    stream,
            inhibitor: inhibitor
        }
    }
}


impl<'a> Iterator for Events<'a> {
    type Item = Event<'a>;

    #[inline]
    fn next(&mut self) -> Option<Event<'a>> {
        match self.stream.next() {
            None         => None,
            Some((t, e)) => Some(Event::new(t, e, self.inhibitor))
        }
    }
}

/// A stand-alone object that provides an iterator though glfw events.
///
/// It is not lifetime-bound to the main window.
pub struct EventManager {
    events:    Rc<Receiver<(f64, glfw::WindowEvent)>>,
    inhibitor: Rc<RefCell<Vec<glfw::WindowEvent>>>
}

impl EventManager {
    /// Creates a new event manager.
    #[inline]
    pub fn new(events:    Rc<Receiver<(f64, glfw::WindowEvent)>>,
               inhibitor: Rc<RefCell<Vec<glfw::WindowEvent>>>)
               -> EventManager {
        EventManager {
            events:    events,
            inhibitor: inhibitor
        }
    }

    /// Gets an iterator to the glfw events already collected.
    #[inline]
    pub fn iter(&mut self) -> Events {
        Events::new(glfw::flush_messages(&*self.events), &*self.inhibitor)
    }
}