Skip to content

Commit

Permalink
Improved hover detection and release for v11
Browse files Browse the repository at this point in the history
  • Loading branch information
Aioros committed Aug 14, 2024
1 parent c23e1fa commit adfbfd6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
33 changes: 29 additions & 4 deletions src/logic/CanvasTouchPointerEventsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ class CanvasTouchPointerEventsManager extends TouchPointerEventsManager {
})

// New v11 fix (started in v2.2.3): we completely block these events as soon as possible.
// We dispatch a pointermove to the location first, then we dispatch a clone of the original. Except touchstart, that one is gone.
// We dispatch a pointermove to the location first (not for pointerup), then we dispatch a clone of the original. Except touchstart, that one is gone.
if (game.release.generation < 12) {
["pointerdown", "pointerup", "touchstart"].forEach(e => {
["pointerdown", "pointermove", "pointerup", "pointerleave", "touchstart"].forEach(e => {
window.addEventListener(e, evt => {
if (evt.isTrusted && (evt instanceof TouchEvent || ["touch", "pen"].includes(evt.pointerType)) && evt.target === element) {
evt.preventDefault()
evt.stopPropagation()
evt.stopImmediatePropagation()

dispatchModifiedEvent(evt, "pointermove", {button: -1, buttons: 0})
if (["pointerdown", "pointermove"].includes(evt.type)) {
dispatchModifiedEvent(evt, "pointermove", {button: -1, buttons: 0})
}

if (evt.type !== "touchstart") {
if (evt.type !== "touchstart" && !(evt.type == "pointerleave" && evt.pointerType != "pen")) {
dispatchCopy(evt)
}
return false
Expand All @@ -49,6 +51,29 @@ class CanvasTouchPointerEventsManager extends TouchPointerEventsManager {
passive: false,
})
})

// Force hover check on every placeable in the active layer on every pointerdown
document.body.addEventListener("pointerdown", (evt) => {
if (evt.touchvttTrusted || (evt.isTrusted && (evt instanceof TouchEvent || ["touch", "pen"].includes(evt.pointerType)) && evt.target === element)) {
canvas.activeLayer.placeables.forEach(p => {
const mousePos = canvas.mousePosition
if (p.bounds.contains(mousePos.x, mousePos.y)) {
if (!p.hover) {
p._onHoverIn(new PIXI.FederatedEvent("pointerover"), {hoverOutOthers: true})
if (p.mouseInteractionManager.state < MouseInteractionManager.INTERACTION_STATES.HOVER) {
p.mouseInteractionManager.state = MouseInteractionManager.INTERACTION_STATES.HOVER
}
}
} else {
if (p.hover) {
p._onHoverOut(new PIXI.FederatedEvent("pointerout"))
p.mouseInteractionManager.state = MouseInteractionManager.INTERACTION_STATES.NONE
}
}
})
}
}, true)

}

}
Expand Down
4 changes: 2 additions & 2 deletions src/touch-vtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Hooks.once('init', () => {
this.callbacks["longPress"](args[0], args[0].interactionData.origin)
}
}, 1000)
} else if (!["clickRight", "hoverIn", "hoverOut"].includes(event)) {
} else if (!["clickRight"].includes(event)) {
clearTimeout(canvasRightClickTimeout)
clearTimeout(canvasLongPressTimeout)
}
Expand Down Expand Up @@ -193,7 +193,7 @@ Hooks.on('ready', function () {
}

if (getSetting(DEBUG_MODE_SETTING)) {
["pointerdown", "pointermove", "pointerup", "pointercancel", "touchstart", "touchmove", "touchend", "mousedown", "mouseup", "mousemove"].forEach(e => {
["pointerdown", "pointermove", "pointerup", "pointercancel", "pointerleave", "touchstart", "touchmove", "touchend", "mousedown", "mouseup", "mousemove"].forEach(e => {
document.body.addEventListener(e, evt => {
console.log(MODULE_DISPLAY_NAME + ": " + evt.type, evt.pointerType, evt.isTrusted, evt)
}, true)
Expand Down

0 comments on commit adfbfd6

Please sign in to comment.