handle multiple dpad touches

This commit is contained in:
Corwin 2024-04-06 02:14:15 +01:00
parent b9a67e6b0f
commit 3461052193
No known key found for this signature in database

View file

@ -222,9 +222,11 @@
addSimpleButton(mobileStart, "Start");
addSimpleButton(mobileSelect, "Select");
let previouslyPressedButtons = [];
let previouslyPressedButtons = new Set();
const dpadMovement = (touch) => {
const currentlyPressed = new Set();
for (let touch of touches) {
const target = touch.target.getBoundingClientRect();
const touchPoint = { x: touch.clientX, y: touch.clientY };
@ -254,14 +256,19 @@
const buttonsToPress =
(buttonBoxMapping[touchedBox.y] ?? [])[touchedBox.x] ?? [];
for (let button of buttonsToPress) {
currentlyPressed.add(button);
}
}
for (let oldButton of previouslyPressedButtons) {
if (!buttonsToPress.includes(oldButton)) {
if (!buttonsToPress.has(oldButton)) {
releaseButton(oldButton);
}
}
for (let newButton of buttonsToPress) {
if (!previouslyPressedButtons.includes(newButton)) {
if (!previouslyPressedButtons.has(newButton)) {
pressButton(newButton);
}
}
@ -270,18 +277,19 @@
};
mobileDpad.addEventListener("touchstart", (evt) =>
dpadMovement(evt.targetTouches[0])
dpadMovement(evt.targetTouches)
);
mobileDpad.addEventListener("touchmove", (evt) =>
dpadMovement(evt.targetTouches[0])
dpadMovement(evt.targetTouches)
);
mobileDpad.addEventListener("touchend", (evt) => {
for (let oldButton of previouslyPressedButtons) {
releaseButton(oldButton);
}
previouslyPressedButtons = [];
dpadMovement(evt.targetTouches);
});
mobileDpad.addEventListener("touchcancel", (evt) => {
dpadMovement(evt.targetTouches);
});
let mobileAbAPress = new Set();