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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::event::*;
use crate::{EventBuffer, EventStream, Window, WindowContents, Settings};
use futures_util::task::LocalSpawnExt;
use futures_executor::LocalPool;
use mint::Vector2;
use std::cell::RefCell; use std::sync::Arc; use std::future::Future;
use winit::event::{Event as WinitEvent};
use winit::event_loop::{ControlFlow, EventLoop};

pub fn run<F, T>(settings: Settings, app: F) -> !
        where T: 'static + Future<Output = ()>, F: 'static + FnOnce(Window, EventStream) -> T {
    let stream = EventStream::new();
    let buffer = stream.buffer();

    let event_loop = EventLoop::new();
    let window = Arc::new(WindowContents::new(&event_loop, settings));
    let pool = LocalPool::new();
    pool.spawner().spawn_local(app(Window(window.clone()), stream)).expect("Failed to start application");

    do_run(event_loop, window, pool, buffer)
}

#[cfg(feature = "gl")]
use glow::Context;

#[cfg(feature = "gl")]
pub fn run_gl<T, F>(settings: Settings, app: F) -> !
        where T: 'static + Future<Output = ()>, F: 'static + FnOnce(Window, Context, EventStream) -> T {
    let stream = EventStream::new();
    let buffer = stream.buffer();

    let event_loop = EventLoop::new();
    let (window, ctx) = WindowContents::new_gl(&event_loop, settings);
    let window = Arc::new(window);
    let pool = LocalPool::new();
    pool.spawner().spawn_local(app(Window(window.clone()), ctx, stream)).expect("Failed to start application");

    do_run(event_loop, window, pool, buffer)
}

fn do_run(event_loop: EventLoop<()>, window: Arc<WindowContents>, mut pool: LocalPool, buffer: Arc<RefCell<EventBuffer>>) -> ! {
    #[cfg(feature = "gilrs")]
    let mut gilrs = gilrs::Gilrs::new();

    let mut finished = pool.try_run_one();

    event_loop.run(move |event, _, ctrl| {
        match event {
            WinitEvent::NewEvents(winit::event::StartCause::Init) => {
                *ctrl = ControlFlow::Poll;
            }
            WinitEvent::WindowEvent { event, .. } => {
                if let winit::event::WindowEvent::CloseRequested = &event {
                    *ctrl = ControlFlow::Exit;
                }
                if let winit::event::WindowEvent::Resized(size) = &event {
                    window.resize(size);
                }
                if let Some(event) = convert_winit(event) {
                    buffer.borrow_mut().push(event);
                }
            }
            WinitEvent::LoopDestroyed | WinitEvent::EventsCleared => {
                #[cfg(feature = "gilrs")]
                process_gilrs_events(&mut gilrs, &buffer);
                finished = pool.try_run_one()
            }
            _ => ()
        }
        if finished {
            *ctrl = ControlFlow::Exit;
        }
    })
}

#[cfg(feature = "gilrs")]
fn process_gilrs_events(gilrs: &mut Result<gilrs::Gilrs, gilrs::Error>, buffer: &Arc<RefCell<EventBuffer>>) {
    if let Ok(gilrs) = gilrs.as_mut() {
        while let Some(ev) = gilrs.next_event() {
            if let Some(ev) = convert_gilrs(ev) {
                buffer.borrow_mut().push(ev);
            }
        }
    }
}

fn convert_winit(event: winit::event::WindowEvent) -> Option<Event> {
    use winit::event::WindowEvent::*;
    Some(match event {
        Resized(ls) => Event::Resized(ls_to_vec(ls)),
        ReceivedCharacter(c) => Event::ReceivedCharacter(c),
        Focused(f) => Event::Focused(f),
        KeyboardInput { input: winit::event::KeyboardInput {
            state, virtual_keycode: Some(key), modifiers, ..
        }, .. } => Event::KeyboardInput {
            key: key.into(),
            modifiers: modifiers.into(),
            state: state.into(),
        },
        CursorMoved { device_id, position, modifiers } => Event::MouseMoved {
            pointer: Pointer(device_id),
            position: lp_to_vec(position),
            modifiers: modifiers.into(),
        },
        CursorEntered { device_id } => Event::MouseEntered {
            pointer: Pointer(device_id),
        },
        CursorLeft { device_id } => Event::MouseLeft {
            pointer: Pointer(device_id),
        },
        MouseWheel { device_id, delta, modifiers, .. } => Event::MouseWheel {
            pointer: Pointer(device_id),
            delta: delta.into(),
            modifiers: modifiers.into()
        },
        MouseInput { device_id, button, state, modifiers, ..} => Event::MouseInput {
            pointer: Pointer(device_id),
            state: state.into(),
            button: button.into(),
            modifiers: modifiers.into(),
        },
        _ => return None
    })
}

#[cfg(feature = "gilrs")]
fn convert_gilrs(event: gilrs::Event) -> Option<Event> {
    use gilrs::ev::EventType::*;
    let gilrs::Event { id, event, .. } = event;
    let event = match event {
        ButtonPressed(btn, _) => convert_gilrs_button(btn)
            .map(|button| GamepadEvent::Button {
                button,
                state: ElementState::Pressed
            }),
        ButtonRepeated(_, _) => None,
        ButtonReleased(btn, _) => convert_gilrs_button(btn)
            .map(|button| GamepadEvent::Button {
                button,
                state: ElementState::Released
            }),
        ButtonChanged(_, _, _) => None,
        AxisChanged(axis, value, _) => convert_gilrs_axis(axis)
            .map(|axis| GamepadEvent::Axis {
                axis,
                value
            }),
        Connected => Some(GamepadEvent::Connected),
        Disconnected => Some(GamepadEvent::Disconnected),
        Dropped => None,
    };
    event.map(|event| Event::GamepadEvent {
        id: GamepadId(id),
        event,
    })
}

#[cfg(feature = "gilrs")]
fn convert_gilrs_button(event: gilrs::ev::Button) -> Option<GamepadButton> {
    use gilrs::ev::Button::*;
    Some(match event {
        South => GamepadButton::South,
        East => GamepadButton::East,
        North => GamepadButton::North,
        West => GamepadButton::West,
        LeftTrigger => GamepadButton::LeftShoulder,
        LeftTrigger2 => GamepadButton::LeftShoulder,
        RightTrigger => GamepadButton::RightShoulder,
        RightTrigger2 => GamepadButton::RightTrigger,
        Select => GamepadButton::Select,
        Start => GamepadButton::Start,
        LeftThumb => GamepadButton::LeftStick,
        RightThumb => GamepadButton::RightStick,
        DPadUp => GamepadButton::DPadUp,
        DPadDown => GamepadButton::DPadDown,
        DPadLeft => GamepadButton::DPadLeft,
        DPadRight => GamepadButton::DPadRight,

        C | Z | Unknown | Mode => return None,
    })
}

#[cfg(feature = "gilrs")]
fn convert_gilrs_axis(axis: gilrs::ev::Axis) -> Option<GamepadAxis> {
    use gilrs::ev::Axis::*;

    Some(match axis {
        LeftStickX => GamepadAxis::LeftStickX,
        LeftStickY => GamepadAxis::LeftStickY,
        RightStickX => GamepadAxis::RightStickX,
        RightStickY => GamepadAxis::RightStickY,

        LeftZ | RightZ | DPadX | DPadY | Unknown => return None,
    })
}

fn ls_to_vec(ls: winit::dpi::LogicalSize) -> Vector2<f32> {
    Vector2 {
        x: ls.width as f32,
        y: ls.height as f32
    }
}

fn lp_to_vec(ls: winit::dpi::LogicalPosition) -> Vector2<f32> {
    Vector2 {
        x: ls.x as f32,
        y: ls.y as f32,
    }
}