mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 08:11:33 +11:00
handle multiple dpad touches
This commit is contained in:
parent
b9a67e6b0f
commit
3461052193
|
@ -222,46 +222,53 @@
|
||||||
addSimpleButton(mobileStart, "Start");
|
addSimpleButton(mobileStart, "Start");
|
||||||
addSimpleButton(mobileSelect, "Select");
|
addSimpleButton(mobileSelect, "Select");
|
||||||
|
|
||||||
let previouslyPressedButtons = [];
|
let previouslyPressedButtons = new Set();
|
||||||
|
|
||||||
const dpadMovement = (touch) => {
|
const dpadMovement = (touch) => {
|
||||||
const target = touch.target.getBoundingClientRect();
|
const currentlyPressed = 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 touchedBox = {
|
const touchedBox = {
|
||||||
x: Math.floor(relativePosition.x / (targetArea.width / 3)),
|
x: Math.floor(relativePosition.x / (targetArea.width / 3)),
|
||||||
y: Math.floor(relativePosition.y / (targetArea.height / 3)),
|
y: Math.floor(relativePosition.y / (targetArea.height / 3)),
|
||||||
};
|
};
|
||||||
|
|
||||||
const buttonBoxMapping = [
|
const buttonBoxMapping = [
|
||||||
[["Up", "Left"], ["Up"], ["Up", "Right"]],
|
[["Up", "Left"], ["Up"], ["Up", "Right"]],
|
||||||
[["Left"], [], ["Right"]],
|
[["Left"], [], ["Right"]],
|
||||||
[["Down", "Left"], ["Down"], ["Down", "Right"]],
|
[["Down", "Left"], ["Down"], ["Down", "Right"]],
|
||||||
];
|
];
|
||||||
|
|
||||||
const buttonsToPress =
|
const buttonsToPress =
|
||||||
(buttonBoxMapping[touchedBox.y] ?? [])[touchedBox.x] ?? [];
|
(buttonBoxMapping[touchedBox.y] ?? [])[touchedBox.x] ?? [];
|
||||||
|
|
||||||
|
for (let button of buttonsToPress) {
|
||||||
|
currentlyPressed.add(button);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (let oldButton of previouslyPressedButtons) {
|
for (let oldButton of previouslyPressedButtons) {
|
||||||
if (!buttonsToPress.includes(oldButton)) {
|
if (!buttonsToPress.has(oldButton)) {
|
||||||
releaseButton(oldButton);
|
releaseButton(oldButton);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let newButton of buttonsToPress) {
|
for (let newButton of buttonsToPress) {
|
||||||
if (!previouslyPressedButtons.includes(newButton)) {
|
if (!previouslyPressedButtons.has(newButton)) {
|
||||||
pressButton(newButton);
|
pressButton(newButton);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,18 +277,19 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
mobileDpad.addEventListener("touchstart", (evt) =>
|
mobileDpad.addEventListener("touchstart", (evt) =>
|
||||||
dpadMovement(evt.targetTouches[0])
|
dpadMovement(evt.targetTouches)
|
||||||
);
|
);
|
||||||
|
|
||||||
mobileDpad.addEventListener("touchmove", (evt) =>
|
mobileDpad.addEventListener("touchmove", (evt) =>
|
||||||
dpadMovement(evt.targetTouches[0])
|
dpadMovement(evt.targetTouches)
|
||||||
);
|
);
|
||||||
|
|
||||||
mobileDpad.addEventListener("touchend", (evt) => {
|
mobileDpad.addEventListener("touchend", (evt) => {
|
||||||
for (let oldButton of previouslyPressedButtons) {
|
dpadMovement(evt.targetTouches);
|
||||||
releaseButton(oldButton);
|
});
|
||||||
}
|
|
||||||
previouslyPressedButtons = [];
|
mobileDpad.addEventListener("touchcancel", (evt) => {
|
||||||
|
dpadMovement(evt.targetTouches);
|
||||||
});
|
});
|
||||||
|
|
||||||
let mobileAbAPress = new Set();
|
let mobileAbAPress = new Set();
|
||||||
|
|
Loading…
Reference in a new issue