handle many button presses and fix to use targetTouches

This commit is contained in:
Corwin 2024-04-06 02:12:39 +01:00
parent 3a1f1691f9
commit b9a67e6b0f
No known key found for this signature in database

View file

@ -270,11 +270,11 @@
}; };
mobileDpad.addEventListener("touchstart", (evt) => mobileDpad.addEventListener("touchstart", (evt) =>
dpadMovement(evt.touches[0]) dpadMovement(evt.targetTouches[0])
); );
mobileDpad.addEventListener("touchmove", (evt) => mobileDpad.addEventListener("touchmove", (evt) =>
dpadMovement(evt.touches[0]) dpadMovement(evt.targetTouches[0])
); );
mobileDpad.addEventListener("touchend", (evt) => { mobileDpad.addEventListener("touchend", (evt) => {
@ -284,57 +284,59 @@
previouslyPressedButtons = []; previouslyPressedButtons = [];
}); });
let mobileAbAPress = undefined; let mobileAbAPress = new Set();
const mobileAbMovement = (touch) => { const mobileAbMovement = (touches) => {
const target = touch.target.getBoundingClientRect(); const currentTouches = new Set();
for (let touch of touches) {
const target = touch.target.getBoundingClientRect();
const touchPoint = { x: touch.clientX, y: touch.clientY }; const touchPoint = { x: touch.clientX, y: touch.clientY };
const targetArea = { const targetArea = {
x: target.left, x: target.left,
y: target.top, y: target.top,
width: target.width, width: target.width,
height: target.height, height: target.height,
}; };
const relativePosition = { const relativePosition = {
x: touchPoint.x - targetArea.x, x: touchPoint.x - targetArea.x,
y: touchPoint.y - targetArea.y, y: touchPoint.y - targetArea.y,
}; };
const aPress = relativePosition.x > relativePosition.y; const aPress = relativePosition.x > relativePosition.y;
currentTouches.add(aPress ? "A" : "B");
}
if (aPress !== mobileAbAPress) { for (let oldTouch of mobileAbAPress) {
if (mobileAbAPress === true) { if (!currentTouches.has(oldTouch)) {
releaseButton("A"); console.log("Release", oldTouch);
} else if (mobileAbAPress === false) { releaseButton(oldTouch);
releaseButton("B");
} }
} }
if (aPress) {
pressButton("A");
} else {
pressButton("B");
}
mobileAbAPress = aPress; for (let newTouch of currentTouches) {
if (!mobileAbAPress.has(newTouch)) {
console.log("Press", newTouch);
pressButton(newTouch);
}
}
mobileAbAPress = currentTouches;
}; };
mobileAb.addEventListener("touchstart", (evt) => mobileAb.addEventListener("touchstart", (evt) => {
mobileAbMovement(evt.touches[0]) mobileAbMovement(evt.targetTouches);
); });
mobileAb.addEventListener("touchmove", (evt) => mobileAb.addEventListener("touchmove", (evt) => {
mobileAbMovement(evt.touches[0]) mobileAbMovement(evt.targetTouches);
); });
mobileAb.addEventListener("touchend", (evt) => { mobileAb.addEventListener("touchend", (evt) => {
if (mobileAbAPress === true) { mobileAbMovement(evt.targetTouches);
releaseButton("A"); });
} else if (mobileAbAPress === false) {
releaseButton("B");
}
mobileAbAPress = undefined; mobileAb.addEventListener("touchcancel", (evt) => {
mobileAbMovement(evt.targetTouches);
}); });
</script> </script>
</body> </body>