added config manager (closes #18) and fixed crash at win
This commit is contained in:
+96
-42
@@ -1,6 +1,7 @@
|
||||
const config=require('config');
|
||||
|
||||
let currentInputs={};
|
||||
let handlers=[];
|
||||
let config;
|
||||
let hud;
|
||||
|
||||
const toAngleMagnitude=(x, y) => {
|
||||
@@ -10,7 +11,7 @@ const toAngleMagnitude=(x, y) => {
|
||||
};
|
||||
};
|
||||
|
||||
const handleAngleMagnitude=(x, y, threshold=0, fn=null, clearBuffer=false) => {
|
||||
const handleAngleMagnitude=(x, y, threshold=0, fn=null) => {
|
||||
const {angle, magnitude}=toAngleMagnitude(x, y);
|
||||
|
||||
if(magnitude>threshold) {
|
||||
@@ -20,7 +21,6 @@ const handleAngleMagnitude=(x, y, threshold=0, fn=null, clearBuffer=false) => {
|
||||
else if(angle>1.25 && angle<1.75) inputs.left=true;
|
||||
else inputs.down=true;
|
||||
|
||||
if(clearBuffer) inputs.clearBuffer=true;
|
||||
if(fn) fn(angle, magnitude);
|
||||
}
|
||||
}
|
||||
@@ -46,19 +46,42 @@ const handleCrosspad=(() => {
|
||||
dl.setAttribute('stroke', 'black');
|
||||
cross.appendChild(dl);
|
||||
|
||||
let useOverlay=false;
|
||||
let enabled=false;
|
||||
const displayOverlay=() => {
|
||||
if(hud) {
|
||||
if(useOverlay && enabled) hud.appendChild(cross);
|
||||
else hud.removeChild(cross);
|
||||
}
|
||||
};
|
||||
config.watchB('input.touchscreen.crosspad.overlay', (k, v) => {
|
||||
useOverlay=v;
|
||||
displayOverlay();
|
||||
});
|
||||
|
||||
const fn=e =>
|
||||
handleAngleMagnitude(
|
||||
e.touches[0].clientX-window.innerWidth/2,
|
||||
e.touches[0].clientY-window.innerHeight/2,
|
||||
0,
|
||||
null,
|
||||
!config.touchscreen.buffer
|
||||
null
|
||||
);
|
||||
|
||||
const init=() => {
|
||||
useOverlay=config.getB('input.touchscreen.crosspad.overlay');
|
||||
enabled=true;
|
||||
displayOverlay();
|
||||
};
|
||||
const fini=() => {
|
||||
enabled=false;
|
||||
displayOverlay();
|
||||
};
|
||||
|
||||
return {
|
||||
touchstart: fn,
|
||||
touchmove: fn,
|
||||
init: () => hud.appendChild(cross),
|
||||
fini: () => hud.removeChild(cross)
|
||||
init,
|
||||
fini
|
||||
};
|
||||
})();
|
||||
|
||||
@@ -69,8 +92,6 @@ const handleKeyboard={
|
||||
else if(e.key=='ArrowDown') inputs.down=true;
|
||||
else if(e.key=='ArrowLeft') inputs.left=true;
|
||||
else if(e.key=='ArrowRight') inputs.right=true;
|
||||
|
||||
if(!config.keyboard.buffer) inputs.clearBuffer=true;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -79,7 +100,17 @@ const handleJoystick=(() => {
|
||||
x: 0,
|
||||
y: 0
|
||||
};
|
||||
let deadzone;
|
||||
|
||||
const init=() => {
|
||||
deadzone=config.getN('input.touchscreen.joystick.deadzone');
|
||||
};
|
||||
config.watchN('input.touchscreen.joystick.deadzone', (k, v) => {
|
||||
deadzone=v;
|
||||
});
|
||||
|
||||
return {
|
||||
init,
|
||||
touchstart: e => {
|
||||
center.x=e.touches[0].clientX;
|
||||
center.y=e.touches[0].clientY;
|
||||
@@ -88,9 +119,8 @@ const handleJoystick=(() => {
|
||||
handleAngleMagnitude(
|
||||
e.touches[0].clientX-center.x,
|
||||
e.touches[0].clientY-center.y,
|
||||
config.touchscreen.deadzone,
|
||||
null,
|
||||
!config.touchscreen.buffer
|
||||
deadzone,
|
||||
null
|
||||
)
|
||||
}
|
||||
})();
|
||||
@@ -100,38 +130,59 @@ const handleSwipe=(() => {
|
||||
x: 0,
|
||||
y: 0
|
||||
};
|
||||
let deadzone;
|
||||
|
||||
let resetCenter=e => {
|
||||
center.x=e.touches[0].clientX;
|
||||
center.y=e.touches[0].clientY;
|
||||
};
|
||||
|
||||
const init=() => {
|
||||
deadzone=config.getN('input.touchscreen.swipe.deadzone');
|
||||
};
|
||||
config.watchN('input.touchscreen.swipe.deadzone', (k, v) => {
|
||||
deadzone=v;
|
||||
});
|
||||
|
||||
return {
|
||||
init,
|
||||
touchstart: resetCenter,
|
||||
touchmove: e =>
|
||||
handleAngleMagnitude(
|
||||
e.touches[0].clientX-center.x,
|
||||
e.touches[0].clientY-center.y,
|
||||
config.touchscreen.deadzone,
|
||||
() => resetCenter(e),
|
||||
!config.touchscreen.buffer
|
||||
deadzone,
|
||||
() => resetCenter(e)
|
||||
)
|
||||
}
|
||||
})();
|
||||
|
||||
const handleGamepads={
|
||||
frame: () => {
|
||||
const gp=navigator.getGamepads()[0];
|
||||
let inputs=currentInputs;
|
||||
if(!gp || !gp.axes) return;
|
||||
const handleGamepads=(() => {
|
||||
let deadzone;
|
||||
|
||||
handleAngleMagnitude(
|
||||
gp.axes[0],
|
||||
gp.axes[1],
|
||||
config.gamepad.deadzone,
|
||||
null,
|
||||
!config.gamepad.buffer
|
||||
);
|
||||
}
|
||||
};
|
||||
const init=() => {
|
||||
deadzone=config.getN('input.touchscreen.swipe.deadzone');
|
||||
};
|
||||
config.watchN('input.touchscreen.swipe.deadzone', (k, v) => {
|
||||
deadzone=v;
|
||||
});
|
||||
|
||||
return {
|
||||
init,
|
||||
frame: () => {
|
||||
const gp=navigator.getGamepads()[0];
|
||||
let inputs=currentInputs;
|
||||
if(!gp || !gp.axes) return;
|
||||
|
||||
handleAngleMagnitude(
|
||||
gp.axes[0],
|
||||
gp.axes[1],
|
||||
deadzone,
|
||||
null
|
||||
);
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
const handleEvent=(type, evt) => {
|
||||
for(let handler of handlers) {
|
||||
@@ -153,11 +204,22 @@ const disableHandler=handler => {
|
||||
if(handler.fini) handler.fini();
|
||||
}
|
||||
};
|
||||
const linkHandler=(handler, key) => {
|
||||
if(config.getB(key)) enableHandler(handler);
|
||||
config.watchB(key, (k, v) => {
|
||||
if(v) enableHandler(handler);
|
||||
else disableHandler(handler);
|
||||
});
|
||||
};
|
||||
|
||||
const updateConfig=cfg =>
|
||||
config=cfg;
|
||||
const setHud=elem =>
|
||||
hud=elem;
|
||||
const init=({hud: hudElem}) => {
|
||||
hud=hudElem;
|
||||
linkHandler(handleCrosspad, 'input.touchscreen.crosspad.enabled');
|
||||
linkHandler(handleJoystick, 'input.touchscreen.joystick.enabled');
|
||||
linkHandler(handleSwipe, 'input.touchscreen.swipe.enabled');
|
||||
linkHandler(handleGamepads, 'input.gamepad.enabled');
|
||||
linkHandler(handleKeyboard, 'input.keyboard.enabled');
|
||||
};
|
||||
|
||||
const clear=() =>
|
||||
Object
|
||||
@@ -171,14 +233,6 @@ for(let type of ['keydown', 'touchstart', 'touchmove']) {
|
||||
return module.exports={
|
||||
inputs: currentInputs,
|
||||
clear,
|
||||
enableHandler, disableHandler,
|
||||
framefn: handleEvent.bind(null, 'frame'),
|
||||
availableHandlers: {
|
||||
keyboard: handleKeyboard,
|
||||
gamepad: handleGamepads,
|
||||
touchscreenCrosspad: handleCrosspad,
|
||||
touchscreenJoystick: handleJoystick,
|
||||
touchscreenSwipe: handleSwipe
|
||||
},
|
||||
updateConfig, setHud
|
||||
init
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user