Skip to content

Commit

Permalink
Fix missing click events in mixed touch/mouse scenarios (#4839)
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfooman committed Nov 11, 2022
1 parent a8ef694 commit b232b13
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/framework/input/element-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ class ElementInput {

if (!this._clickedEntities[element.entity.getGuid()]) {
this._fireEvent('click', new ElementTouchEvent(event, element, camera, x, y, touch));
this._clickedEntities[element.entity.getGuid()] = true;
this._clickedEntities[element.entity.getGuid()] = Date.now();
}

}
Expand Down Expand Up @@ -736,15 +736,23 @@ class ElementInput {
if (eventType === 'mouseup' && this._pressedElement) {
// click event
if (this._pressedElement === this._hoveredElement) {
this._pressedElement = null;

// fire click event if it hasn't been fired already by the touchup handler
if (!this._clickedEntities || !this._clickedEntities[this._hoveredElement.entity.getGuid()]) {
// fire click event if it hasn't been fired already by the touchend handler
const guid = this._hoveredElement.entity.getGuid();
// Always fire, if there are no clicked entities
let fireClick = !this._clickedEntities;
// But if there are, we need to check how long ago touchend added a "click brake"
if (this._clickedEntities) {
const lastTouchUp = this._clickedEntities[guid] || 0;
const dt = Date.now() - lastTouchUp;
fireClick = dt > 300;
}
// We do not check another time, so the worst thing that can happen is one ignored click in 300ms.
delete this._clickedEntities[guid];
if (fireClick) {
this._fireEvent('click', new ElementMouseEvent(event, this._hoveredElement, camera, targetX, targetY, this._lastX, this._lastY));
}
} else {
this._pressedElement = null;
}
this._pressedElement = null;
}
}

Expand Down

0 comments on commit b232b13

Please sign in to comment.