switched to es6

This commit is contained in:
e7d 2017-05-14 10:08:03 +02:00
parent 5ee5a1f0a3
commit c12eb0c144
3 changed files with 330 additions and 303 deletions

View File

@ -47,7 +47,7 @@
</tr> </tr>
<tr> <tr>
<th><kbd>D</kbd></th> <th><kbd>D</kbd></th>
<td>Toggle debug mode</td> <td>Toggle debug mode for active gamepad</td>
</tr> </tr>
<tr> <tr>
<th><kbd>H</kbd></th> <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="https://static.e7d.io/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="js/gamepad.js"></script> <script src="js/gamepad.js"></script>
<script src="js/app.js"></script>
</body> </body>
</html> </html>

1
js/app.js Normal file
View File

@ -0,0 +1 @@
window.gamepad = new Gamepad();

View File

@ -1,5 +1,5 @@
$.urlParam = function(name) { $.urlParam = function(name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href); let results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results === null) { if (results === null) {
return null; return null;
} else { } else {
@ -7,15 +7,17 @@ $.urlParam = function(name) {
} }
}; };
var haveEvents = 'ongamepadconnected' in window; class Gamepad {
var debug = false; constructor() {
var scanGamepadsDelay = 1000; this.haveEvents = 'ongamepadconnected' in window;
var gamepads = {}; this.debug = false;
var $gamepad = $('.gamepad'); this.scanGamepadsDelay = 1000;
var $nogamepad = $('.no-gamepad'); this.gamepads = {};
var $debug = $('.debug'); this.$gamepad = $('.gamepad');
var $help = $('.help'); this.$nogamepad = $('.no-gamepad');
var gamepadIdentifiers = { this.$debug = $('.debug');
this.$help = $('.help');
this.gamepadIdentifiers = {
'debug': { 'debug': {
'id': /debug/, 'id': /debug/,
'colors': [] 'colors': []
@ -29,192 +31,206 @@ var gamepadIdentifiers = {
'colors': ['black', 'white'] 'colors': ['black', 'white']
} }
}; };
var gamepadHelpTimeout = null; this.gamepadHelpTimeout = null;
var gamepadHelpDelay = 5000; this.gamepadHelpDelay = 5000;
var activeGamepadIndex = null; this.activeGamepadIndex = null;
var activeGamepadType = null; this.activeGamepadType = null;
var activeGamepadIdentifier = null; this.activeGamepadIdentifier = null;
var activeGamepadColorIndex = null; this.activeGamepadColorIndex = null;
var activeGamepadColorName = null; this.activeGamepadColorName = null;
var activeGamepadZoomLevel = 1; this.activeGamepadZoomLevel = 1;
var mapping = { this.mapping = {
buttons: [], buttons: [],
axes: [] axes: []
}; };
window.addEventListener("gamepadconnected", onGamepadConnect); window.addEventListener("gamepadconnected", this.onGamepadConnect.bind(this));
window.addEventListener("gamepaddisconnected", onGamepadDisconnect); window.addEventListener("gamepaddisconnected", this.onGamepadDisconnect.bind(this));
window.addEventListener("keydown", onKeyDown); window.addEventListener("keydown", this.onKeyDown.bind(this));
displayGamepadHelp(); window.setInterval(this.scanGamepads.bind(this), this.scanGamepadsDelay);
function displayGamepadHelp() { this.displayGamepadHelp();
gamepadHelpTimeout = window.setTimeout(function() {
$nogamepad.fadeIn();
}, gamepadHelpDelay);
}
function hideGamepadHelp() {
window.clearTimeout(gamepadHelpTimeout);
$nogamepad.hide();
} }
function onGamepadConnect(e) { displayGamepadHelp() {
addGamepad(e.gamepad); this.gamepadHelpTimeout = window.setTimeout(() => {
this.$nogamepad.fadeIn();
}, this.gamepadHelpDelay);
}
hideGamepadHelp() {
window.clearTimeout(this.gamepadHelpTimeout);
this.$nogamepad.hide();
} }
function onGamepadDisconnect(e) { onGamepadConnect(e) {
removeGamepad(e.gamepad.index); this.addGamepad(e.gamepad);
} }
function onKeyDown(e) { onGamepadDisconnect(e) {
this.removeGamepad(e.gamepad.index);
}
onKeyDown(e) {
switch (e.code) { switch (e.code) {
case "Delete": case "Delete":
case "Escape": case "Escape":
removeGamepad(activeGamepadIndex); this.removeGamepad();
break; break;
case "KeyC": case "KeyC":
changeGamepadColor(); this.changeGamepadColor();
break; break;
case "KeyD": case "KeyD":
toggleDebug(); this.toggleDebug();
break; break;
case "KeyH": case "KeyH":
toggleHelp(); this.toggleHelp();
break; break;
case "NumpadAdd": case "NumpadAdd":
case "Equal": case "Equal":
changeZoom("+"); this.changeZoom("+");
break; break;
case "NumpadSubtract": case "NumpadSubtract":
case "Minus": case "Minus":
changeZoom("-"); this.changeZoom("-");
break; break;
case "Numpad0": case "Numpad0":
case "Digit0": case "Digit0":
changeZoom("0"); this.changeZoom("0");
break; break;
} }
} }
function getGamepads() { getGamepads() {
return navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []); return navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
} }
function addGamepad(gamepad) { getActiveGamepad() {
gamepads[gamepad.index] = gamepad; if (null === this.activeGamepadIndex) {
return null;
} }
function removeGamepad(gamepadIndex) { return this.gamepads[this.activeGamepadIndex];
if (gamepadIndex === activeGamepadIndex) {
activeGamepadIndex = null;
$gamepad.empty();
}
delete gamepads[gamepadIndex];
displayGamepadHelp();
} }
setInterval(scanGamepads, scanGamepadsDelay); addGamepad(gamepad) {
function scanGamepads() { this.gamepads[gamepad.index] = gamepad;
if (null !== activeGamepadIndex) { }
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; return;
} }
gamepads = getGamepads(); this.gamepads = this.getGamepads();
for (var gamepadIndex = 0; gamepadIndex < gamepads.length; gamepadIndex++) { for (let gamepadIndex = 0; gamepadIndex < this.gamepads.length; gamepadIndex++) {
var gamepad = gamepads[gamepadIndex]; const gamepad = this.gamepads[gamepadIndex];
if (gamepad) { if (gamepad) {
if (gamepad.index in gamepads) { if (gamepad.index in this.gamepads) {
gamepads[gamepad.index] = gamepad; 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]; button = gamepad.buttons[buttonIndex];
if (null === activeGamepadIndex && button.pressed) { if (null === this.activeGamepadIndex && button.pressed) {
mapGamepad(gamepad.index); this.mapGamepad(gamepad.index);
} }
} }
} }
} }
} }
function mapGamepad(gamepadIndex) { mapGamepad(gamepadIndex) {
activeGamepadIndex = gamepadIndex; this.removeGamepad();
var gamepad = gamepads[gamepadIndex];
var button; this.activeGamepadIndex = gamepadIndex;
var axis; const gamepad = this.gamepads[this.activeGamepadIndex];
hideGamepadHelp(); let button;
let axis;
if (debug) { this.hideGamepadHelp();
activeGamepadType = 'debug';
activeGamepadIdentifier = gamepadIdentifiers[activeGamepadType]; if (this.debug) {
activeGamepadColorIndex = 0; this.activeGamepadType = 'debug';
this.activeGamepadIdentifier = this.gamepadIdentifiers[this.activeGamepadType];
this.activeGamepadColorIndex = 0;
} else { } else {
for (var gamepadType in gamepadIdentifiers) { for (let gamepadType in this.gamepadIdentifiers) {
if (gamepadIdentifiers[gamepadType].id.test(gamepad.id)) { if (this.gamepadIdentifiers[gamepadType].id.test(gamepad.id)) {
activeGamepadType = gamepadType; this.activeGamepadType = gamepadType;
activeGamepadIdentifier = gamepadIdentifiers[gamepadType]; this.activeGamepadIdentifier = this.gamepadIdentifiers[gamepadType];
activeGamepadColorIndex = 0; this.activeGamepadColorIndex = 0;
} }
} }
} }
$.ajax( $.ajax(
'templates/' + activeGamepadType + '/template.html', { 'templates/' + this.activeGamepadType + '/template.html', {
async: true async: true
} }
).done(function(template) { ).done((template) => {
$gamepad.html(template); this.$gamepad.html(template);
if ($.urlParam('color')) { if ($.urlParam('color')) {
changeGamepadColor($.urlParam('color')); this.changeGamepadColor($.urlParam('color'));
} }
if ($.urlParam('c')) { if ($.urlParam('c')) {
changeGamepadColor($.urlParam('c')); this.changeGamepadColor($.urlParam('c'));
} }
if ($.urlParam('zoom')) { if ($.urlParam('zoom')) {
changeZoom($.urlParam('zoom')); this.changeZoom($.urlParam('zoom'));
} }
if ($.urlParam('z')) { if ($.urlParam('z')) {
changeZoom($.urlParam('z')); this.changeZoom($.urlParam('z'));
} }
mapping.buttons = []; this.mapping.buttons = [];
for (var buttonIndex = 0; buttonIndex < gamepad.buttons.length; buttonIndex++) { for (let buttonIndex = 0; buttonIndex < gamepad.buttons.length; buttonIndex++) {
button = gamepad.buttons[buttonIndex]; button = gamepad.buttons[buttonIndex];
mapping.buttons[buttonIndex] = $('[data-button=' + buttonIndex + ']'); this.mapping.buttons[buttonIndex] = $('[data-button=' + buttonIndex + ']');
} }
mapping.axes = []; this.mapping.axes = [];
for (var axisIndex = 0; axisIndex < gamepad.axes.length; axisIndex++) { for (let axisIndex = 0; axisIndex < gamepad.axes.length; axisIndex++) {
axis = gamepad.axes[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() { updateVisualStatus() {
if (null === activeGamepadIndex) { if (null === this.activeGamepadIndex) {
return; return;
} }
gamepads = getGamepads(); this.gamepads = this.getGamepads();
var activeGamepad = gamepads[activeGamepadIndex]; const activeGamepad = this.gamepads[this.activeGamepadIndex];
if (!activeGamepad) { if (!activeGamepad) {
return; return;
} }
requestAnimationFrame(updateVisualStatus); requestAnimationFrame(this.updateVisualStatus.bind(this));
var button; let button;
var $button; let $button;
for (var buttonIndex = 0; buttonIndex < activeGamepad.buttons.length; buttonIndex++) { for (let buttonIndex = 0; buttonIndex < activeGamepad.buttons.length; buttonIndex++) {
$button = mapping.buttons[buttonIndex]; $button = this.mapping.buttons[buttonIndex];
if (!$button) { if (!$button) {
break; break;
} }
@ -224,15 +240,15 @@ function updateVisualStatus() {
$button.attr('data-pressed', button.pressed); $button.attr('data-pressed', button.pressed);
$button.attr('data-value', button.value); $button.attr('data-value', button.value);
if ("function" === typeof updateButton) { if ("function" === typeof this.updateButton) {
updateButton($button); this.updateButton($button);
} }
} }
var axis; let axis;
var $axis; let $axis;
for (var axisIndex = 0; axisIndex < activeGamepad.axes.length; axisIndex++) { for (let axisIndex = 0; axisIndex < activeGamepad.axes.length; axisIndex++) {
$axis = mapping.axes[axisIndex]; $axis = this.mapping.axes[axisIndex];
if (!$axis) { if (!$axis) {
break; break;
} }
@ -252,45 +268,45 @@ function updateVisualStatus() {
$axis.attr('data-value-z', axis); $axis.attr('data-value-z', axis);
} }
if ("function" === typeof updateAxis) { if ("function" === typeof this.updateAxis) {
updateAxis($axis); this.updateAxis($axis);
} }
} }
} }
function changeGamepadColor(gamepadColor) { changeGamepadColor(gamepadColor) {
if (! activeGamepadIdentifier) { if (!this.activeGamepadIdentifier) {
return; return;
} }
if (!! gamepadColor) { if (!gamepadColor) {
if (! isNaN(parseInt(gamepadColor))) { this.activeGamepadColorIndex++;
activeGamepadColorIndex = gamepadColor; if (this.activeGamepadColorIndex > this.activeGamepadIdentifier.colors.length - 1) {
activeGamepadColorName = activeGamepadIdentifier.colors[activeGamepadColorIndex]; this.activeGamepadColorIndex = 0;
}
this.activeGamepadColorName = this.activeGamepadIdentifier.colors[this.activeGamepadColorIndex];
} else { } else {
activeGamepadColorName = gamepadColor; if (! isNaN(parseInt(gamepadColor))) {
activeGamepadColorIndex = 0; this.activeGamepadColorIndex = gamepadColor;
for (var gamepadColorName in activeGamepadIdentifier.colors) { this.activeGamepadColorName = this.activeGamepadIdentifier.colors[this.activeGamepadColorIndex];
if (activeGamepadColorName === gamepadColorName) { } else {
this.activeGamepadColorName = gamepadColor;
this.activeGamepadColorIndex = 0;
for (let gamepadColorName in this.activeGamepadIdentifier.colors) {
if (this.activeGamepadColorName === gamepadColorName) {
break; 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); changeZoom(zoomLevel) {
} if (!this.activeGamepadIdentifier) {
function changeZoom(zoomLevel) {
if (! activeGamepadIdentifier) {
return; return;
} }
@ -299,31 +315,40 @@ function changeZoom(zoomLevel) {
} }
if ('0' === zoomLevel) { if ('0' === zoomLevel) {
activeGamepadZoomLevel = 1; this.activeGamepadZoomLevel = 1;
} }
else if ('+' === zoomLevel && activeGamepadZoomLevel < 2) { else if ('+' === zoomLevel && this.activeGamepadZoomLevel < 2) {
activeGamepadZoomLevel += 0.1; this.activeGamepadZoomLevel += 0.1;
} }
else if ('-' === zoomLevel && activeGamepadZoomLevel > 0.2) { else if ('-' === zoomLevel && this.activeGamepadZoomLevel > 0.2) {
activeGamepadZoomLevel -= 0.1; this.activeGamepadZoomLevel -= 0.1;
} }
else if (! isNaN(zoomLevel = parseFloat(zoomLevel))) { else if (! isNaN(zoomLevel = parseFloat(zoomLevel))) {
activeGamepadZoomLevel = zoomLevel; this.activeGamepadZoomLevel = zoomLevel;
} }
// hack: fix js float issues // 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) { toggleHelp(zoomLevel) {
$help.toggleClass('active'); this.$help.toggleClass('active');
} }
function toggleDebug() { toggleDebug() {
debug = !debug; if (null === this.activeGamepadIndex) {
return;
}
this.debug = !this.debug;
// remap current gamepad // remap current gamepad
mapGamepad(activeGamepadIndex); this.removeGamepad();
this.mapGamepad(this.activeGamepadIndex);
}
} }