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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::{
    input::{ButtonState, GamepadAxis, GamepadButton, Key, MouseButton},
    geom::Vector
};
#[cfg(not(target_arch="wasm32"))]
use {
    crate::{
        input::{KEY_LIST, LINES_TO_PIXELS},
        lifecycle::Window,
    },
    glutin::{
        EventsLoop, 
        Event::WindowEvent
    }
};

/// An input event
#[derive(Copy, Clone, Debug)]
pub enum Event {
    /// The application has been closed
    Closed,
    /// The application has gained focus
    Focused,
    /// The application has lost focus
    Unfocused,
    /// A key has changed its button state
    Key(Key, ButtonState),
    /// An alphanumeric character has been entered from the keyboard
    Typed(char),
    /// The mouse has been moved to a position
    MouseMoved(Vector),
    /// The mouse has entered the window
    MouseEntered,
    /// The mouse has exited the window
    MouseExited,
    /// The mouse wheel has been scrolled by a vector
    MouseWheel(Vector),
    /// A mouse button has changed its button state
    MouseButton(MouseButton, ButtonState),
    /// A gamepad axis has changed its state
    GamepadAxis(i32, GamepadAxis, f32),
    /// A gamepad button has changed its state
    GamepadButton(i32, GamepadButton, ButtonState),
    /// A gamepad has been connected
    GamepadConnected(i32),
    /// A gamepad has been disconnected
    GamepadDisconnected(i32)
}

#[cfg(not(target_arch="wasm32"))]
pub(crate) struct EventProvider {
    events_loop: EventsLoop
}

#[cfg(not(target_arch="wasm32"))]
impl EventProvider {
    pub(crate) fn new(events_loop: EventsLoop) -> EventProvider {
        EventProvider { 
            events_loop
        }
    }

    pub(crate) fn generate_events(&mut self, window: &mut Window, events: &mut Vec<Event>) -> bool {
        let mut running = true;
        self.events_loop.poll_events(|event| match event {
            WindowEvent { event, .. } => match event {
                glutin::WindowEvent::CloseRequested => {
                    running = false;
                    events.push(Event::Closed);
                }
                glutin::WindowEvent::KeyboardInput { input: event, .. } => {
                    if let Some(keycode) = event.virtual_keycode {
                        let state = match event.state {
                            glutin::ElementState::Pressed => ButtonState::Pressed,
                            glutin::ElementState::Released => ButtonState::Released
                        };
                        let key = KEY_LIST[keycode as usize];
                        events.push(Event::Key(key, state));
                    }
                }
                glutin::WindowEvent::ReceivedCharacter(character) if !character.is_ascii_control() => {
                    events.push(Event::Typed(character));
                }
                glutin::WindowEvent::CursorMoved { position, .. } => {
                    let position: Vector = position.into();
                    let position = window.project() * (position - window.screen_offset());
                    events.push(Event::MouseMoved(position));
                }
                glutin::WindowEvent::MouseInput { state, button, .. } => {
                    let value = match state {
                        glutin::ElementState::Pressed => ButtonState::Pressed,
                        glutin::ElementState::Released => ButtonState::Released,
                    };
                    let index = match button {
                        glutin::MouseButton::Left => MouseButton::Left,
                        glutin::MouseButton::Right => MouseButton::Right,
                        glutin::MouseButton::Middle => MouseButton::Middle,
                        // Other mouse buttons just mean we should move on to the next glutin event
                        _ => { return; },
                    };
                    events.push(Event::MouseButton(index, value));
                }
                glutin::WindowEvent::MouseWheel { delta, .. } => {
                    let vector = match delta {
                        glutin::MouseScrollDelta::LineDelta(x, y) => Vector::new(x, -y) * LINES_TO_PIXELS,
                        glutin::MouseScrollDelta::PixelDelta(delta) => delta.into()
                    };
                    events.push(Event::MouseWheel(vector));
                }
                glutin::WindowEvent::Resized(size) => {
                    let size: Vector = size.into();
                    // Glutin reports a resize to 0, 0 when minimizing the window
                    if size.x != 0.0 && size.y != 0.0 {
                        window.adjust_size(size);
                    }
                },
                _ => ()
            },
            _ => ()
        });
        running
    }
}