generate settins in URL

This commit is contained in:
e7d 2020-08-19 16:24:55 +02:00
parent 38244961d3
commit dc1a041c35
No known key found for this signature in database
GPG Key ID: F320BE007C0B8881

View File

@ -34,12 +34,27 @@ class Gamepad {
colors: [], colors: [],
}, },
ds4: { ds4: {
id: /054c|0810|2563/, // 054c = Sony vendor code, 0810,2563 = PS-like controllers vendor codes id: /054c|54c|046d|0810|2563/, // 054c = Sony vendor code, 046d,0810,2563 = PS-like controllers vendor codes
name: "DualShock 4", name: "DualShock 4",
colors: ["black", "white", "red", "blue"], colors: ["black", "white", "red", "blue"],
}, },
// gamecube: {
// id: /0079/, // 0079 = Nintendo GameCube vendor code
// name: "GameCube Controller",
// colors: ["black"],
// },
// "joy-con": {
// id: /200e/, // 0079 = Joy-Con specific product code
// name: "Joy-Con (L+R) Controllers",
// colors: ["blue-red"],
// },
// stadia: {
// id: /18d1/, // 18d1 = Google vendor code
// name: "Stadia Controller",
// colors: ["black"],
// },
// "switch-pro": { // "switch-pro": {
// id: /057e/, // 057e = Nintendo vendor code // id: /057e|20d6/, // 057e = Nintendo Switch vendor code, 20d6 = Switch Pro-like vendor code
// name: "Switch Pro Controller", // name: "Switch Pro Controller",
// colors: ["black"], // colors: ["black"],
// }, // },
@ -72,6 +87,9 @@ class Gamepad {
axes: [], axes: [],
}; };
// // read hash
// this.hash = this.readHash();
// listen for gamepad related events // listen for gamepad related events
this.haveEvents = "GamepadEvent" in window; this.haveEvents = "GamepadEvent" in window;
if (this.haveEvents) { if (this.haveEvents) {
@ -108,16 +126,17 @@ class Gamepad {
if (this.params.background) { if (this.params.background) {
for (let i = 0; i < this.backgroundColors.length; i++) { for (let i = 0; i < this.backgroundColors.length; i++) {
if (this.params.background === this.backgroundColors[i]) { if (this.params.background === this.backgroundColors[i]) {
this.index = i; this.backgroundColorIndex = i;
break; break;
} }
} }
if (!this.index) { if (this.backgroundColorIndex) {
return; this.$body.css(
"background",
this.backgroundColors[this.backgroundColorIndex]
);
} }
this.$body.css("background", this.backgroundColors[this.index]);
} }
// by default, enqueue a delayed display of the instructions animation // by default, enqueue a delayed display of the instructions animation
@ -133,11 +152,9 @@ class Gamepad {
let results = new RegExp("[?&]" + name + "(=([^&#]*))?").exec( let results = new RegExp("[?&]" + name + "(=([^&#]*))?").exec(
window.location.search window.location.search
); );
if (results === null) { return results === null
return null; ? null
} : decodeURIComponent(results[2] || true) || true;
return decodeURIComponent(results[2] || true) || true;
} }
/** /**
@ -190,10 +207,10 @@ class Gamepad {
*/ */
onGamepadDisconnect(e) { onGamepadDisconnect(e) {
// on gamepad disconnection, remove it from the list // on gamepad disconnection, remove it from the list
this.$gamepad.addClass('disconnected'); this.$gamepad.addClass("disconnected");
window.setTimeout(() => { window.setTimeout(() => {
this.$gamepad.removeClass('disconnected'); this.$gamepad.removeClass("disconnected");
// remove gamepad from the list and start back scanning // remove gamepad from the list and start back scanning
this.disconnect(e.gamepad.index); this.disconnect(e.gamepad.index);
@ -455,6 +472,9 @@ class Gamepad {
this.$gamepad.empty(); this.$gamepad.empty();
} }
// update navigation hash
this.updateSettings({ player: null });
// enqueue a display of the instructions animation // enqueue a display of the instructions animation
this.displayInstructions(); this.displayInstructions();
this.debug = false; this.debug = false;
@ -617,6 +637,9 @@ class Gamepad {
color: this.textColors[this.backgroundColorIndex], color: this.textColors[this.backgroundColorIndex],
}); });
// update current settings
this.updateSettings({ background: this.backgroundColorName });
// save statistics // save statistics
if (!!window.ga) { if (!!window.ga) {
ga("send", "event", { ga("send", "event", {
@ -669,6 +692,9 @@ class Gamepad {
// update the DOM with the color value // update the DOM with the color value
this.$gamepad.attr("data-color", this.colorName); this.$gamepad.attr("data-color", this.colorName);
// update current settings
this.updateSettings({ color: this.colorName });
// save statistics // save statistics
if (!!window.ga) { if (!!window.ga) {
ga("send", "event", { ga("send", "event", {
@ -791,7 +817,24 @@ class Gamepad {
*/ */
toggleTriggersMeter() { toggleTriggersMeter() {
this.triggersMeter = !this.triggersMeter; this.triggersMeter = !this.triggersMeter;
this.$gamepad[this.triggersMeter ? 'addClass' : 'removeClass']('triggers-meter'); this.$gamepad[this.triggersMeter ? "addClass" : "removeClass"](
"triggers-meter"
);
}
/**
* Update url hash with new settings
* @param {*} newSettings
*/
updateSettings(newSettings) {
const settings = Object.assign(Object.fromEntries(
window.location.search
.replace("?", "")
.split("&")
.map((param) => param.split("="))
), newSettings);
const query = Object.entries(settings).map(([key, value]) => `${key}=${value}`).join('&');
window.history.replaceState({}, document.title, `/?${query}`);
} }
} }