added quick restart and crosspad grid

This commit is contained in:
Nathan DECHER
2020-04-05 20:58:35 +02:00
parent b12ac2fab9
commit 3a571a9c30
6 changed files with 103 additions and 13 deletions
+35 -4
View File
@@ -1,6 +1,7 @@
let currentInputs={};
let handlers=[];
let config;
let hud;
const toAngleMagnitude=(x, y) => {
return {
@@ -25,6 +26,26 @@ const handleAngleMagnitude=(x, y, threshold=0, fn=null, clearBuffer=false) => {
}
const handleCrosspad=(() => {
const ns='http://www.w3.org/2000/svg';
const cross=document.createElementNS(ns, 'svg');
cross.classList.add('crosspadOverlay');
cross.setAttribute('width', 1000);
cross.setAttribute('height', 1000);
let dr=document.createElementNS(ns, 'line');
dr.setAttribute('x1', 0);
dr.setAttribute('y1', 0);
dr.setAttribute('x2', 1000);
dr.setAttribute('y2', 1000);
dr.setAttribute('stroke', 'black');
cross.appendChild(dr);
let dl=document.createElementNS(ns, 'line');
dl.setAttribute('x1', 1000);
dl.setAttribute('y1', 0);
dl.setAttribute('x2', 0);
dl.setAttribute('y2', 1000);
dl.setAttribute('stroke', 'black');
cross.appendChild(dl);
const fn=e =>
handleAngleMagnitude(
e.touches[0].clientX-window.innerWidth/2,
@@ -35,7 +56,9 @@ const handleCrosspad=(() => {
);
return {
touchstart: fn,
touchmove: fn
touchmove: fn,
init: () => hud.appendChild(cross),
fini: () => hud.removeChild(cross)
};
})();
@@ -118,15 +141,23 @@ const handleEvent=(type, evt) => {
};
const enableHandler=handler => {
if(!handlers.includes(handler)) handlers.push(handler);
if(!handlers.includes(handler)) {
handlers.push(handler);
if(handler.init) handler.init();
}
};
const disableHandler=handler => {
let idx=handlers.indexOf(handler);
if(idx!=-1) handlers.splice(idx, 1);
if(idx!=-1) {
handlers.splice(idx, 1);
if(handler.fini) handler.fini();
}
};
const updateConfig=cfg =>
config=cfg;
const setHud=elem =>
hud=elem;
const clear=() =>
Object
@@ -149,5 +180,5 @@ return module.exports={
touchscreenJoystick: handleJoystick,
touchscreenSwipe: handleSwipe
},
updateConfig
updateConfig, setHud
};