mirror of
https://github.com/EmulatorJS/EmulatorJS.git
synced 2024-09-20 02:46:23 +00:00
push gamepad script
This commit is contained in:
parent
7c70ea37f3
commit
49d2c554ef
3 changed files with 95 additions and 0 deletions
|
@ -422,6 +422,7 @@ class EmulatorJS {
|
||||||
this.createControlSettingMenu();
|
this.createControlSettingMenu();
|
||||||
this.setVirtualGamepad();
|
this.setVirtualGamepad();
|
||||||
this.addEventListener(document, "keydown keyup", this.keyChange.bind(this));
|
this.addEventListener(document, "keydown keyup", this.keyChange.bind(this));
|
||||||
|
//this.gamepad = new GamepadHandler(); //https://github.com/ethanaobrien/Gamepad
|
||||||
//keyboard, etc...
|
//keyboard, etc...
|
||||||
}
|
}
|
||||||
createContextMenu() {
|
createContextMenu() {
|
||||||
|
@ -893,6 +894,7 @@ class EmulatorJS {
|
||||||
this.controlPopup = popupMsg;
|
this.controlPopup = popupMsg;
|
||||||
popup.appendChild(popupMsg);
|
popup.appendChild(popupMsg);
|
||||||
this.controlMenu.appendChild(popup);
|
this.controlMenu.appendChild(popup);
|
||||||
|
|
||||||
}
|
}
|
||||||
defaultControllers = {
|
defaultControllers = {
|
||||||
0: {
|
0: {
|
||||||
|
|
92
src/gamepad.js
Normal file
92
src/gamepad.js
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
class GamepadHandler {
|
||||||
|
gamepads;
|
||||||
|
timeout;
|
||||||
|
listeners;
|
||||||
|
constructor() {
|
||||||
|
this.gamepads = [];
|
||||||
|
this.listeners = {};
|
||||||
|
this.timeout = null;
|
||||||
|
this.loop();
|
||||||
|
}
|
||||||
|
terminate() {
|
||||||
|
window.clearTimeout(this.timeout);
|
||||||
|
}
|
||||||
|
getGamepads() {
|
||||||
|
return navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
|
||||||
|
}
|
||||||
|
loop() {
|
||||||
|
this.updateGamepadState();
|
||||||
|
this.timeout = setTimeout(this.loop.bind(this), 10);
|
||||||
|
}
|
||||||
|
updateGamepadState() {
|
||||||
|
const gamepads = this.getGamepads();
|
||||||
|
gamepads.forEach((gamepad, index) => {
|
||||||
|
if (!gamepad) return;
|
||||||
|
let hasGamepad = false;
|
||||||
|
this.gamepads.forEach((oldGamepad, oldIndex) => {
|
||||||
|
if (oldGamepad.index !== gamepad.index) return;
|
||||||
|
hasGamepad = true;
|
||||||
|
|
||||||
|
oldGamepad.axes.forEach((axis, axisIndex) => {
|
||||||
|
if (gamepad.axes[axisIndex] !== axis) {
|
||||||
|
const axis = ['LEFT_STICK_X', 'LEFT_STICK_Y', 'RIGHT_STICK_X', 'RIGHT_STICK_Y'][axisIndex];
|
||||||
|
if (!axis) return;
|
||||||
|
this.dispatchEvent('axischanged', {axis: axis, value: gamepad.axes[axisIndex], index: gamepad.index, gamepadIndex: gamepad.index});
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
gamepad.buttons.forEach((button, buttonIndex) => {
|
||||||
|
let pressed = oldGamepad.buttons[buttonIndex] === 1.0;
|
||||||
|
if (typeof oldGamepad.buttons[buttonIndex] === "object") {
|
||||||
|
pressed = oldGamepad.buttons[buttonIndex].pressed;
|
||||||
|
}
|
||||||
|
let pressed2 = button === 1.0;
|
||||||
|
if (typeof button === "object") {
|
||||||
|
pressed2 = button.pressed;
|
||||||
|
}
|
||||||
|
if (pressed !== pressed2) {
|
||||||
|
if (pressed2) {
|
||||||
|
this.dispatchEvent('buttondown', {index: buttonIndex, gamepadIndex: gamepad.index});
|
||||||
|
} else {
|
||||||
|
this.dispatchEvent('buttonup', {index: buttonIndex, gamepadIndex: gamepad.index});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
this.gamepads[oldIndex] = gamepads[index];
|
||||||
|
})
|
||||||
|
if (!hasGamepad) {
|
||||||
|
this.gamepads.push(gamepads[index]);
|
||||||
|
this.dispatchEvent('connected', {gamepadIndex: gamepad.index});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (let j=0; j<this.gamepads.length; j++) {
|
||||||
|
if (!this.gamepads[j]) continue;
|
||||||
|
let has = false;
|
||||||
|
for (let i=0; i<gamepads.length; i++) {
|
||||||
|
if (!gamepads[i]) continue;
|
||||||
|
if (this.gamepads[j].index === gamepads[i].index) {
|
||||||
|
has = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!has) {
|
||||||
|
this.dispatchEvent('disconnected', {gamepadIndex: this.gamepads[j].index});
|
||||||
|
this.gamepads.splice(j, 1);
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dispatchEvent(name, arg) {
|
||||||
|
if (typeof this.listeners[name] !== 'function') return;
|
||||||
|
if (!arg) arg={};
|
||||||
|
arg.type = name;
|
||||||
|
this.listeners[name](arg);
|
||||||
|
}
|
||||||
|
on(name, cb) {
|
||||||
|
this.listeners[name.toLowerCase()] = cb;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.GamepadHandler = GamepadHandler;
|
|
@ -36,6 +36,7 @@
|
||||||
if (('undefined' != typeof EJS_DEBUG_XX && true === EJS_DEBUG_XX) || true) {
|
if (('undefined' != typeof EJS_DEBUG_XX && true === EJS_DEBUG_XX) || true) {
|
||||||
await loadScript('emulator.js');
|
await loadScript('emulator.js');
|
||||||
await loadScript('nipplejs.js');
|
await loadScript('nipplejs.js');
|
||||||
|
await loadScript('gamepad.js');
|
||||||
await loadScript('GameManager.js');
|
await loadScript('GameManager.js');
|
||||||
await loadStyle('css/main.css');
|
await loadStyle('css/main.css');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue