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
use glium::backend::glutin_backend::GlutinFacade;
use glium::glutin::{Event, VirtualKeyCode};

/// Every event receiver has to return a response for each event received.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum EventResponse {
    /// The event was not handled at all
    NotHandled,
    /// The event was handled but should be forwarded to other receivers, too
    Continue,
    /// The event was handled and should *not* be forwarded to other receivers
    Break,
    /// In response to the event, the program should terminate
    Quit,
}

pub struct EventManager {
    facade: GlutinFacade,
}

impl EventManager {
    pub fn new(facade: GlutinFacade) -> Self {
        EventManager { facade: facade }
    }

    pub fn poll_events(&self, mut handlers: Vec<&mut EventHandler>) -> EventResponse {

        for ev in self.facade.poll_events() {
            for i in 0..handlers.len() {
                let response = handlers[i].handle_event(&ev);
                match response {
                    EventResponse::NotHandled |
                    EventResponse::Continue => (),
                    EventResponse::Break => break,
                    EventResponse::Quit => return EventResponse::Quit,
                }
            }
        }
        // Just for the sake of return value
        EventResponse::NotHandled
    }
}

pub trait EventHandler {
    fn handle_event(&mut self, e: &Event) -> EventResponse;
}

/// Handler that handles the closing of the window
pub struct CloseHandler;

/// handle_event function of CloseHandler

/// Windows can be closed by:

/// *clicking the 'X' on the upper edge of the window

/// *pressing 'Escape'
impl EventHandler for CloseHandler {
    fn handle_event(&mut self, e: &Event) -> EventResponse {
        match *e {
            Event::Closed => EventResponse::Quit,
            Event::KeyboardInput(_, _, Some(VirtualKeyCode::Escape)) => EventResponse::Quit,
            _ => EventResponse::NotHandled,
        }
    }
}