Files
winit/src/platform_impl/web/event_loop/global.rs
T
Christophe Massolin e004bd2bb3 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
2020-03-03 09:56:11 -05:00

82 lines
2.6 KiB
Rust

use super::super::device::{gamepad, GamepadHandle};
use super::backend;
use crate::event::device;
use std::{cell::RefCell, rc::Rc, collections::HashSet};
#[derive(Debug)]
pub struct Window {
raw: RefCell<Option<backend::window::Shared>>,
gamepads: Rc<RefCell<HashSet<i32>>>,
}
#[derive(Debug)]
pub struct Shared(Rc<Window>);
impl Shared {
pub fn new() -> Self {
Self(Rc::new(Window {
raw: RefCell::new(None),
gamepads: Rc::new(RefCell::new(HashSet::new())),
}))
}
// Request window object and listen global events
pub fn register_events(&self) -> Result<(), crate::error::OsError> {
if (*self.0.raw.borrow()).is_none() {
let shared = backend::window::Shared::create()?;
let mut window = shared.0.borrow_mut();
let shared_gamepads = self.0.gamepads.clone();
window.on_gamepad_connected(move |gamepad: backend::gamepad::Gamepad| {
let mut gamepads = shared_gamepads.borrow_mut();
let index = gamepad.index();
if !gamepads.contains(&index) {
gamepads.insert(index);
}
});
let shared_gamepads = self.0.gamepads.clone();
window.on_gamepad_disconnected(move |gamepad: backend::gamepad::Gamepad| {
let mut gamepads = shared_gamepads.borrow_mut();
let index = gamepad.index();
if gamepads.contains(&index) {
gamepads.remove(&index);
}
});
self.0.raw.replace(Some(shared.clone()));
}
Ok(())
}
// Google Chrome create an array of [null, null, null, null].
// To fix that issue, I create my own list of gamepads
// by listening "gamepadconnected" and "gamepaddisconnected"
pub fn get_gamepads(&self) -> Vec<backend::gamepad::Gamepad> {
let gamepads = self.0.gamepads.borrow_mut();
backend::get_gamepads()
.filter(|g| gamepads.contains(&g.index()))
.collect()
}
// Return gamepads handles required for EventLoop::gamepads()
pub fn get_gamepad_handles(&self) -> Vec<device::GamepadHandle> {
self.get_gamepads()
.iter()
.map(|gamepad| {
device::GamepadHandle(GamepadHandle {
id: gamepad.index,
gamepad: gamepad::Shared::Raw(gamepad.clone()),
})
})
.collect()
}
}
impl Clone for Shared {
fn clone(&self) -> Self {
Shared(self.0.clone())
}
}