improved connection status reading

This commit is contained in:
e7d 2020-11-25 11:07:05 +01:00
parent d0c08a8b0f
commit 80f6d3bbbe
No known key found for this signature in database
GPG Key ID: F320BE007C0B8881

View File

@ -82,12 +82,13 @@ class Gamepad {
// gamepad help default values // gamepad help default values
this.instructionsTimeout = null; this.instructionsTimeout = null;
this.helpDelay = 12000; this.instructionsDelay = 12000;
// active gamepad default values // active gamepad default values
this.scanDelay = 200; this.scanDelay = 200;
this.debug = false; this.debug = false;
this.index = null; this.index = null;
this.disconnectedIndex = null;
this.type = null; this.type = null;
this.identifier = null; this.identifier = null;
this.lastTimestamp = null; this.lastTimestamp = null;
@ -191,7 +192,7 @@ class Gamepad {
// hide instructions animation if no gamepad is active after X ms // hide instructions animation if no gamepad is active after X ms
this.instructionsTimeout = window.setTimeout(() => { this.instructionsTimeout = window.setTimeout(() => {
this.$instructions.fadeOut(); this.$instructions.fadeOut();
}, this.helpDelay); }, this.instructionsDelay);
} }
/** /**
@ -200,9 +201,6 @@ class Gamepad {
* @param {GamepadEvent} e * @param {GamepadEvent} e
*/ */
onGamepadConnect(e) { onGamepadConnect(e) {
// on gamepad connection, refresh available gamepads list
this.scan();
// refresh gamepad list on help, if displayed // refresh gamepad list on help, if displayed
if (this.helpVisible) this.buildHelpGamepadList(); if (this.helpVisible) this.buildHelpGamepadList();
} }
@ -213,18 +211,14 @@ class Gamepad {
* @param {GamepadEvent} e * @param {GamepadEvent} e
*/ */
onGamepadDisconnect(e) { onGamepadDisconnect(e) {
// display a disconnection indicator for 5 seconds if (e.gamepad.index === this.index) {
this.$gamepad.addClass("disconnected"); // display a disconnection indicator
window.setTimeout(() => { this.$gamepad.addClass("disconnected");
this.$gamepad.removeClass("disconnected"); this.disconnectedIndex = e.gamepad.index;
// remove gamepad from the list and start back scanning
this.disconnect(e.gamepad.index);
this.scan();
// refresh gamepad list on help, if displayed // refresh gamepad list on help, if displayed
if (this.helpVisible) this.buildHelpGamepadList(); if (this.helpVisible) this.buildHelpGamepadList();
}, 5000); }
} }
/** /**
@ -245,7 +239,8 @@ class Gamepad {
switch (e.code) { switch (e.code) {
case "Delete": case "Delete":
case "Escape": case "Escape":
this.disconnect(true); this.clear();
this.displayInstructions();
break; break;
case "KeyB": case "KeyB":
this.changeBackgroundStyle(); this.changeBackgroundStyle();
@ -317,7 +312,9 @@ class Gamepad {
* Builds the help gamepad list * Builds the help gamepad list
*/ */
buildHelpGamepadList() { buildHelpGamepadList() {
console.log("buildHelpGamepadList"); // refresh gamepads information
this.pollGamepads();
const $tbody = []; const $tbody = [];
for (let key = 0; key < this.gamepads.length; key++) { for (let key = 0; key < this.gamepads.length; key++) {
const gamepad = this.gamepads[key]; const gamepad = this.gamepads[key];
@ -377,12 +374,19 @@ class Gamepad {
*/ */
scan() { scan() {
// don't scan if we have an active gamepad // don't scan if we have an active gamepad
if (null !== this.index) return; if (null !== this.index && null === this.disconnectedIndex)
return;
// refresh gamepad information // refresh gamepad information
this.pollGamepads(); this.pollGamepads();
for (let index = 0; index < this.gamepads.length; index++) { for (let index = 0; index < this.gamepads.length; index++) {
if (
null !== this.disconnectedIndex &&
index !== this.disconnectedIndex
)
continue;
const gamepad = this.gamepads[index]; const gamepad = this.gamepads[index];
if (!gamepad) continue; if (!gamepad) continue;
@ -419,6 +423,8 @@ class Gamepad {
// update local references // update local references
this.index = index; this.index = index;
this.disconnectedIndex = null;
this.$gamepad.removeClass("disconnected");
const gamepad = this.getActive(); const gamepad = this.getActive();
// ensure that a gamepad was actually found for this index // ensure that a gamepad was actually found for this index
@ -460,11 +466,25 @@ class Gamepad {
/** /**
* Disconnect the active gamepad * Disconnect the active gamepad
* *
* @param {object} gamepad * @param {int} index
* @param {object} options
*/ */
disconnect(index) { clear() {
// ensure we have an index to remove // ensure we have something to disconnect
if ("undefined" === typeof index) return; if (this.index === null) return;
// clear associated data
this.index = null;
this.disconnectedIndex = null;
this.debug = false;
this.lastTimestamp = null;
this.type = null;
this.identifier = null;
this.colorIndex = null;
this.colorName = null;
this.zoomLevel = 1;
this.$gamepad.empty();
this.clearSettings();
// save statistics // save statistics
if (!!window.ga) { if (!!window.ga) {
@ -475,24 +495,6 @@ class Gamepad {
eventValue: this.identifier, eventValue: this.identifier,
}); });
} }
// if this is the active gamepad
if (true === index || this.index === index) {
// clear associated date
this.index = null;
this.lastTimestamp = null;
this.type = null;
this.identifier = null;
this.colorIndex = null;
this.colorName = null;
this.zoomLevel = 1;
this.$gamepad.empty();
this.updateSettings({ type: undefined, color: undefined });
}
// enqueue a display of the instructions animation
this.displayInstructions();
this.debug = false;
} }
/** /**
@ -547,7 +549,6 @@ class Gamepad {
// enqueue the next refresh // enqueue the next refresh
window.requestAnimationFrame(this.pollStatus.bind(this)); window.requestAnimationFrame(this.pollStatus.bind(this));
// window.setTimeout(this.pollStatus.bind(this), 1000 / 60);
// load latest gamepad data // load latest gamepad data
this.pollGamepads(); this.pollGamepads();
@ -898,6 +899,19 @@ class Gamepad {
return settings; return settings;
} }
/**
* Clear all url settings
*/
clearSettings() {
this.updateSettings({
type: undefined,
color: undefined,
debug: undefined,
triggers: undefined,
zoom: undefined,
});
}
/** /**
* Update url hash with new settings * Update url hash with new settings
* *