Centralized UI updates

This commit is contained in:
Unrud 2022-02-21 22:30:27 +01:00
parent 42dcafda3d
commit 89131db79b

View file

@ -408,6 +408,7 @@ window.addEventListener("load", function() {
"keyboard": 1 "keyboard": 1
}; };
var ready = false; var ready = false;
var closed = false;
var scenes = document.querySelectorAll("body > .scene"); var scenes = document.querySelectorAll("body > .scene");
var openingScene = document.getElementById("opening"); var openingScene = document.getElementById("opening");
var closedScene = document.getElementById("closed"); var closedScene = document.getElementById("closed");
@ -471,7 +472,21 @@ window.addEventListener("load", function() {
sessionStorage.setItem("keyboard", keyboardTextarea.value); sessionStorage.setItem("keyboard", keyboardTextarea.value);
}; };
showScene(opening); function updateUI() {
if (!ready) {
showScene(closed ? closedScene : openingScene);
} else if (pointerLockElement()) {
showScene(mouseScene);
} else if ((history.state || "").split(":")[0] == "keys") {
showKeys(history.state.substr("keys:".length));
} else if (history.state == "keyboard") {
showKeyboard();
} else {
showScene(padScene);
}
}
updateUI();
var wsURL = new URL("ws", location.href); var wsURL = new URL("ws", location.href);
wsURL.protocol = wsURL.protocol == "http:" ? "ws:" : "wss:"; wsURL.protocol = wsURL.protocol == "http:" ? "ws:" : "wss:";
@ -492,12 +507,13 @@ window.addEventListener("load", function() {
return; return;
} }
ready = true; ready = true;
window.onpopstate(); updateUI();
}; };
ws.onclose = function() { ws.onclose = function() {
ready = false; ready = false;
showScene(closedScene); closed = true;
updateUI();
}; };
document.getElementById("keysbutton").addEventListener("click", function() { document.getElementById("keysbutton").addEventListener("click", function() {
@ -506,11 +522,7 @@ window.addEventListener("load", function() {
document.getElementById("keyboardbutton").addEventListener("click", function() { document.getElementById("keyboardbutton").addEventListener("click", function() {
showKeyboard(); showKeyboard();
}); });
addFullscreenchangeEventListener(function() { addFullscreenchangeEventListener(updateUI);
if (fullscreenElement() && !activeScene.classList.contains("fullscreen")) {
exitFullscreen();
}
});
if (!fullscreenEnabled()) { if (!fullscreenEnabled()) {
fullscreenbutton.classList.add("hidden"); fullscreenbutton.classList.add("hidden");
} }
@ -565,19 +577,7 @@ window.addEventListener("load", function() {
} }
history.back(); history.back();
}); });
window.onpopstate = function() { window.addEventListener("popstate", updateUI);
if (ready) {
if (pointerLockElement()) {
showScene(mouseScene);
} else if ((history.state || "").split(":")[0] == "keys") {
showKeys(history.state.substr("keys:".length));
} else if (history.state == "keyboard") {
showKeyboard();
} else {
showScene(padScene);
}
}
};
document.getElementById("reloadbutton").addEventListener("click", function() { document.getElementById("reloadbutton").addEventListener("click", function() {
location.reload(); location.reload();
}); });
@ -595,12 +595,7 @@ window.addEventListener("load", function() {
handleKeydown(evt); handleKeydown(evt);
} }
}); });
addPointerlockchangeEventListener(function() { addPointerlockchangeEventListener(updateUI);
if (pointerLockElement() && !ready) {
exitPointerLock();
}
window.onpopstate();
});
document.addEventListener("mousedown", function(event) { document.addEventListener("mousedown", function(event) {
if (activeScene != mouseScene && event.buttons == 1 && event.target.classList.contains("touch")) { if (activeScene != mouseScene && event.buttons == 1 && event.target.classList.contains("touch")) {
requestPointerLock(mouseScene); requestPointerLock(mouseScene);