mirror of
https://github.com/rust-windowing/winit.git
synced 2026-06-27 07:03:15 -04:00
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:
committed by
GitHub
parent
0729074ce3
commit
e004bd2bb3
82
src/platform_impl/web/stdweb/gamepad.rs
Normal file
82
src/platform_impl/web/stdweb/gamepad.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use std::{cmp::PartialEq};
|
||||
use crate::platform_impl::platform::device;
|
||||
use super::utils;
|
||||
use stdweb::js;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Gamepad {
|
||||
pub(crate) index: i32,
|
||||
pub(crate) raw: stdweb::web::Gamepad,
|
||||
pub(crate) mapping: device::gamepad::Mapping,
|
||||
}
|
||||
|
||||
impl Gamepad {
|
||||
pub fn new(raw: stdweb::web::Gamepad) -> Self {
|
||||
let mapping = utils::create_mapping(&raw);
|
||||
|
||||
Self {
|
||||
index: raw.index(),
|
||||
raw,
|
||||
mapping,
|
||||
}
|
||||
}
|
||||
|
||||
// An integer that is auto-incremented to be unique for each device
|
||||
// currently connected to the system.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/index
|
||||
pub fn index(&self) -> i32 {
|
||||
self.raw.index()
|
||||
}
|
||||
|
||||
// A string containing some information about the controller.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/id
|
||||
pub fn id(&self) -> String {
|
||||
self.raw.id()
|
||||
}
|
||||
|
||||
// A boolean indicating whether the gamepad is still connected to the system.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/connected
|
||||
pub fn connected(&self) -> bool {
|
||||
self.raw.connected()
|
||||
}
|
||||
|
||||
// EXPERIMENTAL
|
||||
#[allow(dead_code)]
|
||||
pub fn vibrate(&self, value: f64, duration: f64) {
|
||||
let index = self.index;
|
||||
js! {
|
||||
const gamepads = navigator.getGamepads();
|
||||
let gamepad = null;
|
||||
for (let i = 0; i < gamepads.length; i++) {
|
||||
if (gamepads[i] && gamepads[i].index == @{index}) {
|
||||
gamepad = gamepads[i];
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!gamepad || !gamepad.hapticActuators) return;
|
||||
for (let i = 0; i < gamepad.hapticActuators.length; i++) {
|
||||
const actuator = gamepad.hapticActuators[i];
|
||||
if (actuator && actuator.type === "vibration") {
|
||||
actuator.pulse(@{value}, @{duration});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Gamepad {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
index: self.index,
|
||||
raw: self.raw.clone(),
|
||||
mapping: self.mapping.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Gamepad {
|
||||
#[inline(always)]
|
||||
fn eq(&self, othr: &Self) -> bool {
|
||||
self.raw.index() == othr.raw.index()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user