Gamepad device events - Web/WASM (#1414)

Add gamepad support for stdweb and web-sys, as well as web-specific gamepad examples. 

* [web] Fix compilation error from device api

* [wasm] Apply device api changes

* [wasm] Format and cleanup

* [wasm32] Implement gamepad connections

* [wasm] Harmonize

* [Test] Made some tests with wasm-pack

* Quick fix instant non supporting Hash trait

* Fix on_received_character

* [web_sys] Split add_event and add_window_event

* [web] split device implementations

* Update tests/web...still does not work

* [tests/web] do not ignore index.html

* [web/web_sys] split canvas and window

* [tests/web] enable stack trace

* [web] fix borrowmut

* [web_sys] fix gamepad registration

* [web] harmonize naming

* [web_sys] create global emitter

* [web] implement gamepad buttons

* [web] implement gamepad axis

* [web] cleanup

* [web] update test

* [web] move tests/web to examples/web

* [web] axis does produce stick event

* [web] Support Stick event

* [web] implement gamepad to stdweb

* [web] rename examples/web to examples/wasm

* [web/web-sys] Move gamepad_manager from backend

* [web/web_sys] implement EventLoop::gamepads

* [web/web_sys] Drain gamepad events

* [web/stdweb] apply web_sys changes

* [web] update web/examples

* [web] move gamepads code to gamepad_manager

* [web] simplify and optimise

* [web] replace EventCode to GamepadAxis and GamepadButton structs

* [web] reuse gamepad events due to chrome issue

* [web] rumble does not work

* [web/stdweb] try debugging

* [web] fix Chrome gamepad not updated

* [web/stdweb] created an example

* [examples] fix paths

* fix warnings

* [web/examples] update comments

* [web/stdweb] add experimental support to vibrate()

* [web] add CR
This commit is contained in:
Christophe Massolin
2020-03-03 15:56:11 +01:00
committed by GitHub
parent 0729074ce3
commit e004bd2bb3
33 changed files with 1518 additions and 214 deletions

View File

@@ -2,6 +2,7 @@ use super::{backend, state::State};
use crate::event::{Event, StartCause, WindowEvent};
use crate::event_loop as root;
use crate::window::WindowId;
use crate::platform_impl::platform::device::gamepad;
use instant::{Duration, Instant};
use std::{
@@ -24,6 +25,7 @@ pub struct Execution<T> {
events: RefCell<VecDeque<Event<T>>>,
id: RefCell<u32>,
redraw_pending: RefCell<HashSet<WindowId>>,
gamepad_manager: RefCell<gamepad::Manager>,
}
struct Runner<T> {
@@ -49,9 +51,14 @@ impl<T: 'static> Shared<T> {
events: RefCell::new(VecDeque::new()),
id: RefCell::new(0),
redraw_pending: RefCell::new(HashSet::new()),
gamepad_manager: RefCell::new(gamepad::Manager::new()),
}))
}
pub fn set_global_window(&self, global_window: super::global::Shared) {
self.0.gamepad_manager.borrow_mut().set_global_window(global_window);
}
// Set the event callback to use for the event loop runner
// This the event callback is a fairly thin layer over the user-provided callback that closes
// over a RootEventLoopWindowTarget reference
@@ -138,6 +145,14 @@ impl<T: 'static> Shared<T> {
&mut control,
);
}
// Collect all global events
let mut gamepad_manager = self.0.gamepad_manager.borrow_mut();
let instance = self.clone();
gamepad_manager.collect_events(move |(handle, event)| {
instance.handle_event(Event::GamepadEvent(handle, event), &mut control);
});
// Every events are cleared
self.handle_event(Event::EventsCleared, &mut control);
self.apply_control_flow(control);
// If the event loop is closed, it has been closed this iteration and now the closing