Skip to content

Commit

Permalink
Add touch index parameter to extractPosition & extractTarget in HTMLM…
Browse files Browse the repository at this point in the history
…anager
  • Loading branch information
AntumDeluge committed Mar 2, 2024
1 parent 55a8d0c commit ff6cf0b
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions srcjs/stendhal/ui/HTMLManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,49 @@ export class HTMLManager {

/**
* Retrieves target element from event.
*
* @param event {any}
* Executed event.
* @param tidx {number}
* Touch index (default: 0).
* @return {EventTarget}
* Translated event target.
*/
extractTarget(event: any): EventTarget {
extractTarget(event: any, tidx=0): EventTarget {
if (event.changedTouches) {
if (["touchmove", "touchend"].indexOf(event.type) > -1) {
// touch events target source element
for (const el of document.elementsFromPoint(event.changedTouches[0].pageX, event.changedTouches[0].pageY)) {
for (const el of document.elementsFromPoint(event.changedTouches[tidx].pageX, event.changedTouches[tidx].pageY)) {
if (!el.classList.contains("notarget")) {
return el;
}
}
}
return event.changedTouches[0].target;
return event.changedTouches[tidx].target;
}
return event.target;
}

extractPosition(event: any): any {
/**
* Normalizes an event object.
*
* @param event {any}
* Executed event.
* @param tidx {number}
* Touch index (default: 0).
* @return {any}
* Normalized event.
*/
extractPosition(event: any, tidx=0): any {
let pos = event;

const canvas = this.extractTarget(event) as HTMLCanvasElement;
if (event.changedTouches) {
pos = {
pageX: Math.round(event.changedTouches[0].pageX),
pageY: Math.round(event.changedTouches[0].pageY),
clientX: Math.round(event.changedTouches[0].clientX),
clientY: Math.round(event.changedTouches[0].clientY),
pageX: Math.round(event.changedTouches[tidx].pageX),
pageY: Math.round(event.changedTouches[tidx].pageY),
clientX: Math.round(event.changedTouches[tidx].clientX),
clientY: Math.round(event.changedTouches[tidx].clientY),
target: canvas
}
if (["touchmove", "touchend"].indexOf(event.type) > -1) {
Expand Down

0 comments on commit ff6cf0b

Please sign in to comment.