switched to es6
This commit is contained in:
parent
5ee5a1f0a3
commit
c12eb0c144
@ -47,7 +47,7 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<th><kbd>D</kbd></th>
|
||||
<td>Toggle debug mode</td>
|
||||
<td>Toggle debug mode for active gamepad</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><kbd>H</kbd></th>
|
||||
@ -63,5 +63,6 @@
|
||||
|
||||
<script src="https://static.e7d.io/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||
<script src="js/gamepad.js"></script>
|
||||
<script src="js/app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
349
js/gamepad.js
349
js/gamepad.js
@ -1,5 +1,5 @@
|
||||
$.urlParam = function(name) {
|
||||
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
|
||||
let results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
|
||||
if (results === null) {
|
||||
return null;
|
||||
} else {
|
||||
@ -7,15 +7,17 @@ $.urlParam = function(name) {
|
||||
}
|
||||
};
|
||||
|
||||
var haveEvents = 'ongamepadconnected' in window;
|
||||
var debug = false;
|
||||
var scanGamepadsDelay = 1000;
|
||||
var gamepads = {};
|
||||
var $gamepad = $('.gamepad');
|
||||
var $nogamepad = $('.no-gamepad');
|
||||
var $debug = $('.debug');
|
||||
var $help = $('.help');
|
||||
var gamepadIdentifiers = {
|
||||
class Gamepad {
|
||||
constructor() {
|
||||
this.haveEvents = 'ongamepadconnected' in window;
|
||||
this.debug = false;
|
||||
this.scanGamepadsDelay = 1000;
|
||||
this.gamepads = {};
|
||||
this.$gamepad = $('.gamepad');
|
||||
this.$nogamepad = $('.no-gamepad');
|
||||
this.$debug = $('.debug');
|
||||
this.$help = $('.help');
|
||||
this.gamepadIdentifiers = {
|
||||
'debug': {
|
||||
'id': /debug/,
|
||||
'colors': []
|
||||
@ -28,193 +30,207 @@ var gamepadIdentifiers = {
|
||||
'id': /xinput|XInput/,
|
||||
'colors': ['black', 'white']
|
||||
}
|
||||
};
|
||||
var gamepadHelpTimeout = null;
|
||||
var gamepadHelpDelay = 5000;
|
||||
var activeGamepadIndex = null;
|
||||
var activeGamepadType = null;
|
||||
var activeGamepadIdentifier = null;
|
||||
var activeGamepadColorIndex = null;
|
||||
var activeGamepadColorName = null;
|
||||
var activeGamepadZoomLevel = 1;
|
||||
var mapping = {
|
||||
};
|
||||
this.gamepadHelpTimeout = null;
|
||||
this.gamepadHelpDelay = 5000;
|
||||
this.activeGamepadIndex = null;
|
||||
this.activeGamepadType = null;
|
||||
this.activeGamepadIdentifier = null;
|
||||
this.activeGamepadColorIndex = null;
|
||||
this.activeGamepadColorName = null;
|
||||
this.activeGamepadZoomLevel = 1;
|
||||
this.mapping = {
|
||||
buttons: [],
|
||||
axes: []
|
||||
};
|
||||
};
|
||||
|
||||
window.addEventListener("gamepadconnected", onGamepadConnect);
|
||||
window.addEventListener("gamepaddisconnected", onGamepadDisconnect);
|
||||
window.addEventListener("keydown", onKeyDown);
|
||||
window.addEventListener("gamepadconnected", this.onGamepadConnect.bind(this));
|
||||
window.addEventListener("gamepaddisconnected", this.onGamepadDisconnect.bind(this));
|
||||
window.addEventListener("keydown", this.onKeyDown.bind(this));
|
||||
|
||||
displayGamepadHelp();
|
||||
function displayGamepadHelp() {
|
||||
gamepadHelpTimeout = window.setTimeout(function() {
|
||||
$nogamepad.fadeIn();
|
||||
}, gamepadHelpDelay);
|
||||
}
|
||||
function hideGamepadHelp() {
|
||||
window.clearTimeout(gamepadHelpTimeout);
|
||||
$nogamepad.hide();
|
||||
}
|
||||
window.setInterval(this.scanGamepads.bind(this), this.scanGamepadsDelay);
|
||||
this.displayGamepadHelp();
|
||||
}
|
||||
|
||||
function onGamepadConnect(e) {
|
||||
addGamepad(e.gamepad);
|
||||
}
|
||||
displayGamepadHelp() {
|
||||
this.gamepadHelpTimeout = window.setTimeout(() => {
|
||||
this.$nogamepad.fadeIn();
|
||||
}, this.gamepadHelpDelay);
|
||||
}
|
||||
hideGamepadHelp() {
|
||||
window.clearTimeout(this.gamepadHelpTimeout);
|
||||
this.$nogamepad.hide();
|
||||
}
|
||||
|
||||
function onGamepadDisconnect(e) {
|
||||
removeGamepad(e.gamepad.index);
|
||||
}
|
||||
onGamepadConnect(e) {
|
||||
this.addGamepad(e.gamepad);
|
||||
}
|
||||
|
||||
function onKeyDown(e) {
|
||||
onGamepadDisconnect(e) {
|
||||
this.removeGamepad(e.gamepad.index);
|
||||
}
|
||||
|
||||
onKeyDown(e) {
|
||||
switch (e.code) {
|
||||
case "Delete":
|
||||
case "Escape":
|
||||
removeGamepad(activeGamepadIndex);
|
||||
this.removeGamepad();
|
||||
break;
|
||||
case "KeyC":
|
||||
changeGamepadColor();
|
||||
this.changeGamepadColor();
|
||||
break;
|
||||
case "KeyD":
|
||||
toggleDebug();
|
||||
this.toggleDebug();
|
||||
break;
|
||||
case "KeyH":
|
||||
toggleHelp();
|
||||
this.toggleHelp();
|
||||
break;
|
||||
case "NumpadAdd":
|
||||
case "Equal":
|
||||
changeZoom("+");
|
||||
this.changeZoom("+");
|
||||
break;
|
||||
case "NumpadSubtract":
|
||||
case "Minus":
|
||||
changeZoom("-");
|
||||
this.changeZoom("-");
|
||||
break;
|
||||
case "Numpad0":
|
||||
case "Digit0":
|
||||
changeZoom("0");
|
||||
this.changeZoom("0");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function getGamepads() {
|
||||
return navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
|
||||
}
|
||||
|
||||
function addGamepad(gamepad) {
|
||||
gamepads[gamepad.index] = gamepad;
|
||||
}
|
||||
|
||||
function removeGamepad(gamepadIndex) {
|
||||
if (gamepadIndex === activeGamepadIndex) {
|
||||
activeGamepadIndex = null;
|
||||
$gamepad.empty();
|
||||
}
|
||||
delete gamepads[gamepadIndex];
|
||||
|
||||
displayGamepadHelp();
|
||||
}
|
||||
getGamepads() {
|
||||
return navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
|
||||
}
|
||||
|
||||
setInterval(scanGamepads, scanGamepadsDelay);
|
||||
function scanGamepads() {
|
||||
if (null !== activeGamepadIndex) {
|
||||
getActiveGamepad() {
|
||||
if (null === this.activeGamepadIndex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.gamepads[this.activeGamepadIndex];
|
||||
}
|
||||
|
||||
addGamepad(gamepad) {
|
||||
this.gamepads[gamepad.index] = gamepad;
|
||||
}
|
||||
|
||||
removeGamepad(gamepadIndex) {
|
||||
if (!gamepadIndex || gamepadIndex === this.activeGamepadIndex) {
|
||||
this.activeGamepadIndex = null;
|
||||
this.$gamepad.empty();
|
||||
}
|
||||
delete this.gamepads[gamepadIndex];
|
||||
|
||||
this.displayGamepadHelp();
|
||||
this.debug = false;
|
||||
}
|
||||
|
||||
scanGamepads() {
|
||||
if (null !== this.activeGamepadIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
gamepads = getGamepads();
|
||||
for (var gamepadIndex = 0; gamepadIndex < gamepads.length; gamepadIndex++) {
|
||||
var gamepad = gamepads[gamepadIndex];
|
||||
this.gamepads = this.getGamepads();
|
||||
for (let gamepadIndex = 0; gamepadIndex < this.gamepads.length; gamepadIndex++) {
|
||||
const gamepad = this.gamepads[gamepadIndex];
|
||||
if (gamepad) {
|
||||
if (gamepad.index in gamepads) {
|
||||
gamepads[gamepad.index] = gamepad;
|
||||
if (gamepad.index in this.gamepads) {
|
||||
this.gamepads[gamepad.index] = gamepad;
|
||||
}
|
||||
|
||||
for (var buttonIndex = 0; buttonIndex < gamepad.buttons.length; buttonIndex++) {
|
||||
let button;
|
||||
for (let buttonIndex = 0; buttonIndex < gamepad.buttons.length; buttonIndex++) {
|
||||
button = gamepad.buttons[buttonIndex];
|
||||
if (null === activeGamepadIndex && button.pressed) {
|
||||
mapGamepad(gamepad.index);
|
||||
if (null === this.activeGamepadIndex && button.pressed) {
|
||||
this.mapGamepad(gamepad.index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function mapGamepad(gamepadIndex) {
|
||||
activeGamepadIndex = gamepadIndex;
|
||||
var gamepad = gamepads[gamepadIndex];
|
||||
mapGamepad(gamepadIndex) {
|
||||
this.removeGamepad();
|
||||
|
||||
var button;
|
||||
var axis;
|
||||
this.activeGamepadIndex = gamepadIndex;
|
||||
const gamepad = this.gamepads[this.activeGamepadIndex];
|
||||
|
||||
hideGamepadHelp();
|
||||
let button;
|
||||
let axis;
|
||||
|
||||
if (debug) {
|
||||
activeGamepadType = 'debug';
|
||||
activeGamepadIdentifier = gamepadIdentifiers[activeGamepadType];
|
||||
activeGamepadColorIndex = 0;
|
||||
this.hideGamepadHelp();
|
||||
|
||||
if (this.debug) {
|
||||
this.activeGamepadType = 'debug';
|
||||
this.activeGamepadIdentifier = this.gamepadIdentifiers[this.activeGamepadType];
|
||||
this.activeGamepadColorIndex = 0;
|
||||
} else {
|
||||
for (var gamepadType in gamepadIdentifiers) {
|
||||
if (gamepadIdentifiers[gamepadType].id.test(gamepad.id)) {
|
||||
activeGamepadType = gamepadType;
|
||||
activeGamepadIdentifier = gamepadIdentifiers[gamepadType];
|
||||
activeGamepadColorIndex = 0;
|
||||
for (let gamepadType in this.gamepadIdentifiers) {
|
||||
if (this.gamepadIdentifiers[gamepadType].id.test(gamepad.id)) {
|
||||
this.activeGamepadType = gamepadType;
|
||||
this.activeGamepadIdentifier = this.gamepadIdentifiers[gamepadType];
|
||||
this.activeGamepadColorIndex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$.ajax(
|
||||
'templates/' + activeGamepadType + '/template.html', {
|
||||
'templates/' + this.activeGamepadType + '/template.html', {
|
||||
async: true
|
||||
}
|
||||
).done(function(template) {
|
||||
$gamepad.html(template);
|
||||
).done((template) => {
|
||||
this.$gamepad.html(template);
|
||||
|
||||
if ($.urlParam('color')) {
|
||||
changeGamepadColor($.urlParam('color'));
|
||||
this.changeGamepadColor($.urlParam('color'));
|
||||
}
|
||||
if ($.urlParam('c')) {
|
||||
changeGamepadColor($.urlParam('c'));
|
||||
this.changeGamepadColor($.urlParam('c'));
|
||||
}
|
||||
|
||||
if ($.urlParam('zoom')) {
|
||||
changeZoom($.urlParam('zoom'));
|
||||
this.changeZoom($.urlParam('zoom'));
|
||||
}
|
||||
if ($.urlParam('z')) {
|
||||
changeZoom($.urlParam('z'));
|
||||
this.changeZoom($.urlParam('z'));
|
||||
}
|
||||
|
||||
mapping.buttons = [];
|
||||
for (var buttonIndex = 0; buttonIndex < gamepad.buttons.length; buttonIndex++) {
|
||||
this.mapping.buttons = [];
|
||||
for (let buttonIndex = 0; buttonIndex < gamepad.buttons.length; buttonIndex++) {
|
||||
button = gamepad.buttons[buttonIndex];
|
||||
mapping.buttons[buttonIndex] = $('[data-button=' + buttonIndex + ']');
|
||||
this.mapping.buttons[buttonIndex] = $('[data-button=' + buttonIndex + ']');
|
||||
}
|
||||
|
||||
mapping.axes = [];
|
||||
for (var axisIndex = 0; axisIndex < gamepad.axes.length; axisIndex++) {
|
||||
this.mapping.axes = [];
|
||||
for (let axisIndex = 0; axisIndex < gamepad.axes.length; axisIndex++) {
|
||||
axis = gamepad.axes[axisIndex];
|
||||
mapping.axes[axisIndex] = $('[data-axis=' + axisIndex + '], [data-axis-x=' + axisIndex + '], [data-axis-y=' + axisIndex + '], [data-axis-z=' + axisIndex + ']');
|
||||
this.mapping.axes[axisIndex] = $('[data-axis=' + axisIndex + '], [data-axis-x=' + axisIndex + '], [data-axis-y=' + axisIndex + '], [data-axis-z=' + axisIndex + ']');
|
||||
}
|
||||
|
||||
updateVisualStatus();
|
||||
this.updateVisualStatus();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function updateVisualStatus() {
|
||||
if (null === activeGamepadIndex) {
|
||||
updateVisualStatus() {
|
||||
if (null === this.activeGamepadIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
gamepads = getGamepads();
|
||||
var activeGamepad = gamepads[activeGamepadIndex];
|
||||
this.gamepads = this.getGamepads();
|
||||
const activeGamepad = this.gamepads[this.activeGamepadIndex];
|
||||
|
||||
if (!activeGamepad) {
|
||||
return;
|
||||
}
|
||||
|
||||
requestAnimationFrame(updateVisualStatus);
|
||||
requestAnimationFrame(this.updateVisualStatus.bind(this));
|
||||
|
||||
var button;
|
||||
var $button;
|
||||
for (var buttonIndex = 0; buttonIndex < activeGamepad.buttons.length; buttonIndex++) {
|
||||
$button = mapping.buttons[buttonIndex];
|
||||
let button;
|
||||
let $button;
|
||||
for (let buttonIndex = 0; buttonIndex < activeGamepad.buttons.length; buttonIndex++) {
|
||||
$button = this.mapping.buttons[buttonIndex];
|
||||
if (!$button) {
|
||||
break;
|
||||
}
|
||||
@ -224,15 +240,15 @@ function updateVisualStatus() {
|
||||
$button.attr('data-pressed', button.pressed);
|
||||
$button.attr('data-value', button.value);
|
||||
|
||||
if ("function" === typeof updateButton) {
|
||||
updateButton($button);
|
||||
if ("function" === typeof this.updateButton) {
|
||||
this.updateButton($button);
|
||||
}
|
||||
}
|
||||
|
||||
var axis;
|
||||
var $axis;
|
||||
for (var axisIndex = 0; axisIndex < activeGamepad.axes.length; axisIndex++) {
|
||||
$axis = mapping.axes[axisIndex];
|
||||
let axis;
|
||||
let $axis;
|
||||
for (let axisIndex = 0; axisIndex < activeGamepad.axes.length; axisIndex++) {
|
||||
$axis = this.mapping.axes[axisIndex];
|
||||
if (!$axis) {
|
||||
break;
|
||||
}
|
||||
@ -252,78 +268,87 @@ function updateVisualStatus() {
|
||||
$axis.attr('data-value-z', axis);
|
||||
}
|
||||
|
||||
if ("function" === typeof updateAxis) {
|
||||
updateAxis($axis);
|
||||
if ("function" === typeof this.updateAxis) {
|
||||
this.updateAxis($axis);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function changeGamepadColor(gamepadColor) {
|
||||
if (! activeGamepadIdentifier) {
|
||||
changeGamepadColor(gamepadColor) {
|
||||
if (!this.activeGamepadIdentifier) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!! gamepadColor) {
|
||||
if (! isNaN(parseInt(gamepadColor))) {
|
||||
activeGamepadColorIndex = gamepadColor;
|
||||
activeGamepadColorName = activeGamepadIdentifier.colors[activeGamepadColorIndex];
|
||||
if (!gamepadColor) {
|
||||
this.activeGamepadColorIndex++;
|
||||
if (this.activeGamepadColorIndex > this.activeGamepadIdentifier.colors.length - 1) {
|
||||
this.activeGamepadColorIndex = 0;
|
||||
}
|
||||
|
||||
this.activeGamepadColorName = this.activeGamepadIdentifier.colors[this.activeGamepadColorIndex];
|
||||
} else {
|
||||
activeGamepadColorName = gamepadColor;
|
||||
activeGamepadColorIndex = 0;
|
||||
for (var gamepadColorName in activeGamepadIdentifier.colors) {
|
||||
if (activeGamepadColorName === gamepadColorName) {
|
||||
if (! isNaN(parseInt(gamepadColor))) {
|
||||
this.activeGamepadColorIndex = gamepadColor;
|
||||
this.activeGamepadColorName = this.activeGamepadIdentifier.colors[this.activeGamepadColorIndex];
|
||||
} else {
|
||||
this.activeGamepadColorName = gamepadColor;
|
||||
this.activeGamepadColorIndex = 0;
|
||||
for (let gamepadColorName in this.activeGamepadIdentifier.colors) {
|
||||
if (this.activeGamepadColorName === gamepadColorName) {
|
||||
break;
|
||||
}
|
||||
activeGamepadColorIndex++;
|
||||
this.activeGamepadColorIndex++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
activeGamepadColorIndex++;
|
||||
if (activeGamepadColorIndex > activeGamepadIdentifier.colors.length - 1) {
|
||||
activeGamepadColorIndex = 0;
|
||||
}
|
||||
|
||||
activeGamepadColorName = activeGamepadIdentifier.colors[activeGamepadColorIndex];
|
||||
this.$gamepad.attr('data-color', this.activeGamepadColorName);
|
||||
}
|
||||
|
||||
$gamepad.attr('data-color', activeGamepadColorName);
|
||||
}
|
||||
|
||||
function changeZoom(zoomLevel) {
|
||||
if (! activeGamepadIdentifier) {
|
||||
changeZoom(zoomLevel) {
|
||||
if (!this.activeGamepadIdentifier) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! zoomLevel) {
|
||||
if (!zoomLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ('0' === zoomLevel) {
|
||||
activeGamepadZoomLevel = 1;
|
||||
this.activeGamepadZoomLevel = 1;
|
||||
}
|
||||
else if ('+' === zoomLevel && activeGamepadZoomLevel < 2) {
|
||||
activeGamepadZoomLevel += 0.1;
|
||||
else if ('+' === zoomLevel && this.activeGamepadZoomLevel < 2) {
|
||||
this.activeGamepadZoomLevel += 0.1;
|
||||
}
|
||||
else if ('-' === zoomLevel && activeGamepadZoomLevel > 0.2) {
|
||||
activeGamepadZoomLevel -= 0.1;
|
||||
else if ('-' === zoomLevel && this.activeGamepadZoomLevel > 0.2) {
|
||||
this.activeGamepadZoomLevel -= 0.1;
|
||||
}
|
||||
else if (! isNaN(zoomLevel = parseFloat(zoomLevel))) {
|
||||
activeGamepadZoomLevel = zoomLevel;
|
||||
this.activeGamepadZoomLevel = zoomLevel;
|
||||
}
|
||||
|
||||
// hack: fix js float issues
|
||||
activeGamepadZoomLevel = +activeGamepadZoomLevel.toFixed(1);
|
||||
this.activeGamepadZoomLevel = +this.activeGamepadZoomLevel.toFixed(1);
|
||||
|
||||
$gamepad.css('transform', 'translate(-50%, -50%) scale(' + activeGamepadZoomLevel + ', ' + activeGamepadZoomLevel + ')');
|
||||
}
|
||||
this.$gamepad.css(
|
||||
'transform',
|
||||
'translate(-50%, -50%) scale(' + this.activeGamepadZoomLevel + ', ' + this.activeGamepadZoomLevel + ')'
|
||||
);
|
||||
}
|
||||
|
||||
function toggleHelp(zoomLevel) {
|
||||
$help.toggleClass('active');
|
||||
}
|
||||
toggleHelp(zoomLevel) {
|
||||
this.$help.toggleClass('active');
|
||||
}
|
||||
|
||||
function toggleDebug() {
|
||||
debug = !debug;
|
||||
toggleDebug() {
|
||||
if (null === this.activeGamepadIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.debug = !this.debug;
|
||||
|
||||
// remap current gamepad
|
||||
mapGamepad(activeGamepadIndex);
|
||||
this.removeGamepad();
|
||||
this.mapGamepad(this.activeGamepadIndex);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user