Organize code in namespaces

This commit is contained in:
Unrud 2022-02-22 21:53:16 +01:00
parent 9432869bce
commit 190b2763bd

View file

@ -56,31 +56,18 @@ var KEY_RETURN = 17;
var ws = null;
var config = null;
var touchMoved = false;
var touchStart = 0;
var touchLastEnd = 0;
var touchReleasedCount = 0;
var ongoingTouches = [];
var moveXSum = 0;
var moveYSum = 0;
var scrollXSum = 0;
var scrollYSum = 0;
var dragging = false;
var draggingTimeout = null;
var scrollFinish = false;
var scrolling = false;
var mouseButtons = 0;
var updateTimeoutActive = false;
var util = (function() {
var util = {};
function fullscreenEnabled() {
util.fullscreenEnabled = function() {
return (document.fullscreenEnabled ||
document.webkitFullscreenEnabled ||
document.mozFullScreenEnabled ||
document.msFullscreenEnabled ||
false);
}
}
function requestFullscreen(element, options) {
util.requestFullscreen = function(element, options) {
if (element.requestFullscreen) {
element.requestFullscreen(options);
} else if (element.webkitRequestFullscreen) {
@ -90,9 +77,9 @@ function requestFullscreen(element, options) {
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen(options);
}
}
}
function exitFullscreen() {
util.exitFullscreen = function() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
@ -102,55 +89,142 @@ function exitFullscreen() {
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
}
function fullscreenElement() {
util.fullscreenElement = function() {
return (document.fullscreenElement ||
document.webkitFullscreenElement ||
document.mozFullScreenElement ||
document.msFullscreenElement ||
null);
}
}
function addFullscreenchangeEventListener(listener) {
util.addFullscreenchangeEventListener = function(listener) {
if ("onfullscreenchange" in document) {
document.addEventListener("fullscreenchange", listener);
} else if ("onwebkitfullscreenchange" in document) {
document.addEventListener("webkitfullscreenchange", listener);
}
}
}
function requestPointerLock(element) {
util.requestPointerLock = function(element) {
if (element.requestPointerLock) {
element.requestPointerLock();
} else if (element.mozRequestPointerLock) {
element.mozRequestPointerLock();
}
}
}
function exitPointerLock() {
util.exitPointerLock = function() {
if (document.exitPointerLock) {
document.exitPointerLock();
} else if (document.mozExitPointerLock) {
document.mozExitPointerLock();
}
}
}
function pointerLockElement() {
util.pointerLockElement = function() {
return (document.pointerLockElement ||
document.mozPointerLockElement ||
null);
}
}
function addPointerlockchangeEventListener(listener) {
util.addPointerlockchangeEventListener = function(listener) {
if ("onpointerlockchange" in document) {
document.addEventListener("pointerlockchange", listener);
} else if ("onmozpointerlockchange" in document) {
document.addEventListener("mozpointerlockchange", listener);
}
}
}
function copyTouch(touch, timeStamp) {
return util;
})();
var controller = (function() {
var controller = {};
var moveXSum = 0;
var moveYSum = 0;
var scrollHSum = 0;
var scrollVSum = 0;
var scrolling = false;
var scrollFinish = false;
var updateTimoueActive = false;
function startUpdate(fromTimeout) {
if (updateTimoueActive && !fromTimeout) {
return;
}
var finished = true;
var xInt = Math.trunc(moveXSum);
var yInt = Math.trunc(moveYSum);
if (xInt != 0 || yInt != 0) {
ws.send("m" + xInt + ";" + yInt);
moveXSum -= xInt;
moveYSum -= yInt;
finished = false;
}
var hInt = Math.trunc(scrollHSum);
var vInt = Math.trunc(scrollVSum);
if (hInt != 0 || vInt != 0) {
ws.send((scrollFinish ? "S" : "s") + hInt + ";" + vInt);
scrollHSum -= hInt;
scrollVSum -= vInt;
scrolling = !scrollFinish;
scrollFinish = false;
finished = false;
} else if (scrollFinish && scrolling) {
ws.send("S");
scrolling = false;
scrollFinish = false;
}
updateTimoueActive = !finished && config.updateRate > 0
if (updateTimoueActive) {
setTimeout(startUpdate, 1000/config.updateRate, true);
}
}
controller.pointerMove = function(deltaX, deltaY) {
moveXSum += deltaX;
moveYSum += deltaY;
startUpdate();
}
controller.pointerScroll = function(deltaHorizontal, deltaVertical, finish) {
scrollHSum += deltaHorizontal;
scrollVSum += deltaVertical;
scrollFinish |= finish;
startUpdate();
}
controller.pointerButton = function(button, press) {
ws.send("b" + button + ";" + (press ? 1 : 0));
}
controller.keyboardKey = function(key) {
ws.send("k" + key);
}
controller.keyboardText = function(text) {
ws.send("t" + text);
}
return controller;
})();
var touchpad = (function() {
var touchpad = {};
var moved = false;
var startTimeStamp = 0;
var lastEndTimeStamp = 0;
var releasedCount = 0;
var ongoingTouches = [];
var dragging = false;
var draggingTimeout = null;
function copyTouch(touch, timeStamp) {
return {
identifier: touch.identifier,
pageX: touch.pageX,
@ -159,18 +233,18 @@ function copyTouch(touch, timeStamp) {
pageYStart: touch.pageY,
timeStamp: timeStamp
};
}
}
function ongoingTouchIndexById(idToFind) {
function ongoingTouchIndexById(idToFind) {
for (var i = 0; i < ongoingTouches.length; i += 1) {
if (ongoingTouches[i].identifier == idToFind) {
return i;
}
}
return -1;
}
}
function calculatePointerAccelerationMult(speed) {
function calculateAccelerationMult(speed) {
for (var i = 0; i < POINTER_ACCELERATION.length; i += 1) {
var s2 = POINTER_ACCELERATION[i][0];
var a2 = POINTER_ACCELERATION[i][1];
@ -188,65 +262,18 @@ function calculatePointerAccelerationMult(speed) {
return POINTER_ACCELERATION[POINTER_ACCELERATION.length - 1][1];
}
return 1;
}
}
function onDraggingTimeout() {
function onDraggingTimeout() {
draggingTimeout = null;
ws.send("b" + POINTER_BUTTON_LEFT + ";0");
}
function startUpdate(fromTimeout) {
if (updateTimeoutActive && !fromTimeout) {
return;
controller.pointerButton(POINTER_BUTTON_LEFT, false)
}
var updateFinished = true;
var xInt, yInt;
xInt = Math.trunc(moveXSum);
yInt = Math.trunc(moveYSum);
if (xInt != 0 || yInt != 0) {
ws.send("m" + xInt + ";" + yInt);
moveXSum -= xInt;
moveYSum -= yInt;
updateFinished = false;
}
xInt = Math.trunc(scrollXSum);
yInt = Math.trunc(scrollYSum);
if (xInt != 0 || yInt != 0) {
ws.send((scrollFinish ? "S" : "s") + xInt + ";" + yInt);
scrollXSum -= xInt;
scrollYSum -= yInt;
scrolling = !scrollFinish;
scrollFinish = false;
updateFinished = false;
} else if (scrollFinish && scrolling) {
ws.send("S");
scrolling = false;
scrollFinish = false;
}
updateTimeoutActive = !updateFinished && config.updateRate > 0
if (updateTimeoutActive) {
setTimeout(startUpdate, 1000/config.updateRate, true);
}
}
function updateMove(x, y) {
moveXSum += x;
moveYSum += y;
startUpdate();
}
function updateScroll(x, y, finish) {
scrollXSum += x;
scrollYSum += y;
scrollFinish |= finish;
startUpdate();
}
function handleTouchstart(evt) {
touchpad.handleTouchstart = function(evt) {
// Might get called multiple times for the same touches
if (ongoingTouches.length == 0) {
touchStart = evt.timeStamp;
touchMoved = false;
startTimeStamp = evt.timeStamp;
moved = false;
}
var touches = evt.changedTouches;
for (var i = 0; i < touches.length; i += 1) {
@ -261,17 +288,17 @@ function handleTouchstart(evt) {
} else {
ongoingTouches[idx] = touch;
}
touchLastEnd = 0;
lastEndTimeStamp = 0;
if (draggingTimeout != null) {
clearTimeout(draggingTimeout);
draggingTimeout = null;
dragging = true;
}
updateScroll(0, 0, true);
controller.pointerScroll(0, 0, true);
}
}
}
function handleTouchend(evt) {
touchpad.handleTouchend = touchpad.handleTouchcancel = function(evt) {
var touches = evt.changedTouches;
for (var i = 0; i < touches.length; i += 1) {
var idx = ongoingTouchIndexById(touches[i].identifier);
@ -280,39 +307,39 @@ function handleTouchend(evt) {
}
evt.preventDefault();
ongoingTouches.splice(idx, 1);
touchReleasedCount += 1;
touchLastEnd = evt.timeStamp;
updateScroll(0, 0, true);
releasedCount += 1;
lastEndTimeStamp = evt.timeStamp;
controller.pointerScroll(0, 0, true);
}
if (touchReleasedCount > TOUCH_MOVE_THRESHOLD.length) {
touchMoved = true;
if (releasedCount > TOUCH_MOVE_THRESHOLD.length) {
moved = true;
}
if (ongoingTouches.length == 0 && touchReleasedCount >= 1) {
if (ongoingTouches.length == 0 && releasedCount >= 1) {
if (dragging) {
dragging = false;
ws.send("b" + POINTER_BUTTON_LEFT + ";0");
controller.pointerButton(POINTER_BUTTON_LEFT, false)
}
if (!touchMoved && evt.timeStamp - touchStart < TOUCH_TIMEOUT) {
if (!moved && evt.timeStamp - startTimeStamp < TOUCH_TIMEOUT) {
var button = 0;
if (touchReleasedCount == 1) {
if (releasedCount == 1) {
button = POINTER_BUTTON_LEFT;
} else if (touchReleasedCount == 2) {
} else if (releasedCount == 2) {
button = POINTER_BUTTON_RIGHT;
} else if (touchReleasedCount == 3) {
} else if (releasedCount == 3) {
button = POINTER_BUTTON_MIDDLE;
}
ws.send("b" + button + ";1");
controller.pointerButton(button, true)
if (button == POINTER_BUTTON_LEFT) {
draggingTimeout = setTimeout(onDraggingTimeout, TOUCH_TIMEOUT);
} else {
ws.send("b" + button + ";0");
controller.pointerButton(button, false)
}
}
touchReleasedCount = 0;
releasedCount = 0;
}
}
}
function handleTouchmove(evt) {
touchpad.handleTouchmove = function(evt) {
var sumX = 0;
var sumY = 0;
var touches = evt.changedTouches;
@ -322,34 +349,40 @@ function handleTouchmove(evt) {
continue;
}
evt.preventDefault();
if (!touchMoved) {
if (!moved) {
var dist = Math.sqrt(Math.pow(touches[i].pageX - ongoingTouches[idx].pageXStart, 2) +
Math.pow(touches[i].pageY - ongoingTouches[idx].pageYStart, 2));
if (ongoingTouches.length > TOUCH_MOVE_THRESHOLD.length ||
dist > TOUCH_MOVE_THRESHOLD[ongoingTouches.length - 1] ||
evt.timeStamp - touchStart >= TOUCH_TIMEOUT) {
touchMoved = true;
evt.timeStamp - startTimeStamp >= TOUCH_TIMEOUT) {
moved = true;
}
}
var dx = touches[i].pageX - ongoingTouches[idx].pageX;
var dy = touches[i].pageY - ongoingTouches[idx].pageY;
var timeDelta = evt.timeStamp - ongoingTouches[idx].timeStamp;
sumX += dx * calculatePointerAccelerationMult(Math.abs(dx) / timeDelta * 1000);
sumY += dy * calculatePointerAccelerationMult(Math.abs(dy) / timeDelta * 1000);
sumX += dx * calculateAccelerationMult(Math.abs(dx) / timeDelta * 1000);
sumY += dy * calculateAccelerationMult(Math.abs(dy) / timeDelta * 1000);
ongoingTouches[idx].pageX = touches[i].pageX;
ongoingTouches[idx].pageY = touches[i].pageY;
ongoingTouches[idx].timeStamp = evt.timeStamp;
}
if (touchMoved && evt.timeStamp - touchLastEnd >= TOUCH_TIMEOUT) {
if (moved && evt.timeStamp - lastEndTimeStamp >= TOUCH_TIMEOUT) {
if (ongoingTouches.length == 1 || dragging) {
updateMove(sumX*config.moveSpeed, sumY*config.moveSpeed);
controller.pointerMove(sumX*config.moveSpeed, sumY*config.moveSpeed);
} else if (ongoingTouches.length == 2) {
updateScroll(-sumX*config.scrollSpeed, -sumY*config.scrollSpeed, false);
controller.pointerScroll(-sumX*config.scrollSpeed, -sumY*config.scrollSpeed, false);
}
}
}
}
function handleKeydown(evt) {
return touchpad;
})();
var keyboard = (function() {
var keyboard = {};
keyboard.handleKeydown = function(evt) {
if (evt.ctrlKey || evt.altKey || evt.isComposing) {
return;
}
@ -378,43 +411,50 @@ function handleKeydown(evt) {
if (key != null) {
if (!evt.shiftKey) {
evt.preventDefault();
ws.send("k" + key);
controller.keyboardKey(key);
}
} else if (evt.key.length == 1) {
evt.preventDefault();
ws.send("t" + evt.key);
controller.keyboardText(evt.key);
}
}
}
function updateMouseButtons(buttons) {
return keyboard;
})();
var mouse = (function() {
var mouse = {};
var buttons = 0;
function updateButtons(newButtons) {
for (var button = 0; button < 3; button += 1) {
var flag = 1 << button;
if ((buttons&flag) != (mouseButtons&flag)) {
ws.send("b" + button + ";" + (buttons&flag ? 1 : 0));
if ((newButtons&flag) != (buttons&flag)) {
controller.pointerButton(button, newButtons&flag)
}
}
mouseButtons = buttons;
}
buttons = newButtons;
}
function handleMousedown(evt) {
updateMouseButtons(evt.buttons);
}
mouse.handleMousedown = mouse.handleMouseup = function(evt) {
updateButtons(evt.buttons);
}
function handleMouseup(evt) {
updateMouseButtons(evt.buttons);
}
mouse.handleMousemove = function(evt) {
controller.pointerMove(evt.movementX*config.mouseMoveSpeed, evt.movementY*config.mouseMoveSpeed);
}
function handleMousemove(evt) {
updateMove(evt.movementX*config.mouseMoveSpeed, evt.movementY*config.mouseMoveSpeed);
}
function handleWheel(evt) {
mouse.handleWheel = function(evt) {
if (evt.deltaMode == WheelEvent.DOM_DELTA_PIXEL) {
updateScroll(evt.deltaX*config.mouseScrollSpeed, evt.deltaY*config.mouseScrollSpeed, true);
controller.pointerScroll(evt.deltaX*config.mouseScrollSpeed, evt.deltaY*config.mouseScrollSpeed, true);
} else if (evt.deltaMode == WheelEvent.DOM_DELTA_LINE) {
updateScroll(evt.deltaX*20*config.mouseScrollSpeed, evt.deltaY*20*config.mouseScrollSpeed, true);
controller.pointerScroll(evt.deltaX*20*config.mouseScrollSpeed, evt.deltaY*20*config.mouseScrollSpeed, true);
}
}
}
return mouse;
})();
function challengeResponse(message) {
var shaObj = new jsSHA("SHA-256", "TEXT");
@ -445,11 +485,11 @@ window.addEventListener("load", function() {
function showScene(scene) {
activeScene = scene;
if (fullscreenElement() && !scene.classList.contains("fullscreen")) {
exitFullscreen();
if (util.fullscreenElement() && !scene.classList.contains("fullscreen")) {
util.exitFullscreen();
}
if (pointerLockElement() && activeScene != mouseScene) {
exitPointerLock();
if (util.pointerLockElement() && activeScene != mouseScene) {
util.exitPointerLock();
}
keyboardTextarea.value = "";
scenes.forEach(function(otherScene) {
@ -496,7 +536,7 @@ window.addEventListener("load", function() {
function updateUI() {
if (!ready) {
showScene(closed ? closedScene : openingScene);
} else if (pointerLockElement()) {
} else if (util.pointerLockElement()) {
showScene(mouseScene);
} else if ((history.state || "").split(":")[0] == "keys") {
showKeys(history.state.substr("keys:".length));
@ -543,15 +583,17 @@ window.addEventListener("load", function() {
document.getElementById("keyboardbutton").addEventListener("click", function() {
showKeyboard();
});
addFullscreenchangeEventListener(updateUI);
if (!fullscreenEnabled()) {
util.addFullscreenchangeEventListener(function() {
updateUI();
});
if (!util.fullscreenEnabled()) {
fullscreenbutton.classList.add("hidden");
}
fullscreenbutton.addEventListener("click", function() {
if (fullscreenElement()) {
exitFullscreen();
if (util.fullscreenElement()) {
util.exitFullscreen();
} else {
requestFullscreen(document.documentElement, {navigationUI: "hide"});
util.requestFullscreen(document.documentElement, {navigationUI: "hide"});
}
});
document.getElementById("switchbutton").addEventListener("click", function() {
@ -584,7 +626,7 @@ window.addEventListener("load", function() {
{id: "downbutton", key: KEY_DOWN}
].forEach(function(o) {
document.getElementById(o.id).addEventListener("click", function() {
ws.send("k" + o.key);
controller.keyboardKey(o.key);
});
});
document.getElementById("keyboardkeysbutton").addEventListener("click", function() {
@ -593,13 +635,15 @@ window.addEventListener("load", function() {
document.getElementById("sendbutton").addEventListener("click", function() {
if (keyboardTextarea.value) {
// normalize line endings
ws.send("t" + keyboardTextarea.value.replace(/\r\n?/g, "\n"));
controller.keyboardText(keyboardTextarea.value.replace(/\r\n?/g, "\n"));
keyboardTextarea.value = "";
keyboardTextarea.oninput();
}
history.back();
});
window.addEventListener("popstate", updateUI);
window.addEventListener("popstate", function() {
updateUI();
});
document.getElementById("reloadbutton").addEventListener("click", function() {
location.reload();
});
@ -608,19 +652,21 @@ window.addEventListener("load", function() {
history.back();
});
});
document.addEventListener("touchstart", handleTouchstart);
document.addEventListener("touchend", handleTouchend);
document.addEventListener("touchcancel", handleTouchend);
document.addEventListener("touchmove", handleTouchmove);
document.addEventListener("touchstart", touchpad.handleTouchstart);
document.addEventListener("touchend", touchpad.handleTouchend);
document.addEventListener("touchcancel", touchpad.handleTouchcancel);
document.addEventListener("touchmove", touchpad.handleTouchmove);
document.addEventListener("keydown", function(evt) {
if (activeScene && activeScene.classList.contains("key")) {
handleKeydown(evt);
keyboard.handleKeydown(evt);
}
});
addPointerlockchangeEventListener(updateUI);
util.addPointerlockchangeEventListener(function() {
updateUI();
});
document.addEventListener("mousedown", function(event) {
if (activeScene != mouseScene && event.buttons == 1 && event.target.classList.contains("touch")) {
requestPointerLock(mouseScene);
util.requestPointerLock(mouseScene);
}
});
["touchstart", "touchend", "touchcancel", "touchmove"].forEach(function(type) {
@ -628,8 +674,8 @@ window.addEventListener("load", function() {
evt.preventDefault();
});
});
mouseScene.addEventListener("mousedown", handleMousedown);
mouseScene.addEventListener("mouseup", handleMouseup);
mouseScene.addEventListener("mousemove", handleMousemove);
mouseScene.addEventListener("wheel", handleWheel);
mouseScene.addEventListener("mousedown", mouse.handleMousedown);
mouseScene.addEventListener("mouseup", mouse.handleMouseup);
mouseScene.addEventListener("mousemove", mouse.handleMousemove);
mouseScene.addEventListener("wheel", mouse.handleWheel);
});