Files
winit/src/platform_impl/web/device/gamepad/manager.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

168 lines
5.9 KiB
Rust

use super::utils;
use crate::event::device;
use crate::platform_impl::platform::{backend, device::gamepad, GamepadHandle, event_loop::global};
use std::collections::VecDeque;
pub struct Manager {
pub(crate) gamepads: Vec<backend::gamepad::Gamepad>,
pub(crate) events: VecDeque<(backend::gamepad::Gamepad, device::GamepadEvent)>,
pub(crate) global_window: Option<global::Shared>,
}
impl Manager {
pub fn new() -> Self {
Self {
gamepads: Vec::new(),
events: VecDeque::new(),
global_window: None,
}
}
// Register global window to fetch gamepads.
// Due to Chrome issue, I prefer to use its gamepad list
pub fn set_global_window(&mut self, global_window: global::Shared) {
self.global_window.replace(global_window);
}
// Get an updated raw gamepad and generate a new mapping
pub fn collect_gamepads(&self) -> Option<Vec<backend::gamepad::Gamepad>> {
self.global_window.as_ref().map(|w| w.get_gamepads())
}
// Collect gamepad events (buttons/axes/sticks)
// dispatch to handler and update gamepads
pub fn collect_events<F>(&mut self, mut handler: F)
where
F: 'static + FnMut((device::GamepadHandle, device::GamepadEvent)),
{
let opt_new_gamepads = self.collect_gamepads();
if opt_new_gamepads.is_none() {
return;
}
let new_gamepads = opt_new_gamepads.unwrap();
let old_gamepads = &self.gamepads;
let mut old_index = 0;
let mut new_index = 0;
// Collect events
loop {
match (old_gamepads.get(old_index), new_gamepads.get(new_index)) {
(Some(old), Some(new)) if old.index() == new.index() => {
// Button events
let buttons = old.mapping.buttons().zip(new.mapping.buttons()).enumerate();
for (btn_index, (old_button, new_button)) in buttons {
match (old_button, new_button) {
(false, true) => {
self.events.push_back((new.clone(), utils::gamepad_button(btn_index, true)))
}
(true, false) => {
self.events.push_back((new.clone(), utils::gamepad_button(btn_index, false)))
}
_ => (),
}
}
// Axis events
let axes = old.mapping.axes().zip(new.mapping.axes()).enumerate();
for (axis_index, (old_axis, new_axis)) in axes {
if old_axis != new_axis {
self.events.push_back((new.clone(), utils::gamepad_axis(axis_index, new_axis)))
}
}
// Stick events
let mut old_axes = old.mapping.axes();
let mut new_axes = new.mapping.axes();
let old_left = (old_axes.next(), old_axes.next());
let new_left = (new_axes.next(), new_axes.next());
if old_left != new_left {
if let (Some(x), Some(y)) = (new_left.0, new_left.1) {
self.events.push_back((
new.clone(),
utils::gamepad_stick(0, 1, x, y, device::Side::Left),
));
}
}
let old_right = (old_axes.next(), old_axes.next());
let new_right = (new_axes.next(), new_axes.next());
if old_right != new_right {
if let (Some(x), Some(y)) = (new_right.0, new_right.1) {
self.events.push_back((
new.clone(),
utils::gamepad_stick(2, 3, x, y, device::Side::Right),
));
}
}
// Increment indices
old_index += 1;
new_index += 1;
},
// Connect
(None, Some(new)) => {
self.events.push_back((
new.clone(),
device::GamepadEvent::Added,
));
new_index += 1;
},
// Connect
(Some(old), Some(new)) if old.index > new.index => {
self.events.push_back((
new.clone(),
device::GamepadEvent::Added,
));
new_index += 1;
},
// Disconnect
(Some(old), Some(_new)) => {
self.events.push_back((
old.clone(),
device::GamepadEvent::Removed,
));
old_index += 1;
},
// Disconnect
(Some(old), None) => {
self.events.push_back((
old.clone(),
device::GamepadEvent::Removed,
));
old_index += 1;
},
// Break loop
(None, None) => {
break
}
}
}
// Dispatch events and drain events vec
loop {
if let Some((gamepad, event)) = self.events.pop_front() {
handler((
device::GamepadHandle(GamepadHandle {
id: gamepad.index,
gamepad: gamepad::Shared::Raw(gamepad),
}),
event,
));
} else {
break;
}
}
// Update gamepads
self.gamepads = new_gamepads;
}
}