Skip to content

Commit 12bedbd

Browse files
committed
Gamepad plugin: keep a touch's hover events on the controls from the game (#1805)
A touch fires pointerover and pointerenter before its pointerdown, and pointerout and pointerleave after its pointerup. The plugin only kept the down, move, up and cancel from the engine, so the widget under a button got onOver as the button was pressed (the Alex 4 menu arrow jumped to the line under B). The plugin now also captures the four hover events and stops them for a touch that lands on, or lifted from, a control. A mouse hovering over the controls is left alone, as its moves are. Fixes #1805 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JnzwA4xBNBw7cEZzG1bUru
1 parent 99c7c51 commit 12bedbd

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

‎packages/gamepad-plugin/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [1.0.2] - _unreleased_
4+
5+
### Fixed
6+
- A touch on the controls no longer reaches the game as a hover: the `pointerover` / `pointerenter` before it and the `pointerout` / `pointerleave` after it are kept from the engine too, so a widget under a button no longer gets `onOver` as the button is pressed ([#1805](https://github.com/melonjs/melonJS/issues/1805)). A mouse hovering over the controls is unchanged
7+
38
## [1.0.1] - _2026-10-30_
49

510
### Changed

‎packages/gamepad-plugin/src/plugin.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ const POINTER_EVENTS = [
9292
"pointercancel",
9393
] as const;
9494

95+
/**
96+
* The hover events a touch also fires: over and enter just before its
97+
* pointer-down, out and leave just after its pointer-up. The engine listens
98+
* to them too, so a touch on the controls has to be kept from it here as
99+
* well, or the widget under a button lights up as it is pressed (#1805).
100+
*/
101+
const HOVER_EVENTS = [
102+
"pointerover",
103+
"pointerenter",
104+
"pointerout",
105+
"pointerleave",
106+
] as const;
107+
95108
/**
96109
* On-screen touch controls, reported as a gamepad: sticks and buttons drawn
97110
* over the game, read by the engine as a pad in a gamepad slot. Everything a
@@ -392,6 +405,9 @@ export class GamepadPlugin extends plugin.BasePlugin {
392405
passive: false,
393406
});
394407
}
408+
for (const type of HOVER_EVENTS) {
409+
this.target.addEventListener(type, this.onHover, { capture: true });
410+
}
395411
// a finger held on the controls starts a selection, the iOS image
396412
// menu or the context menu, none of which a pointer event can cancel
397413
this.target.addEventListener("touchstart", this.onTouchStart, {
@@ -418,6 +434,10 @@ export class GamepadPlugin extends plugin.BasePlugin {
418434
capture: true,
419435
});
420436
}
437+
for (const type of HOVER_EVENTS) {
438+
this.target?.removeEventListener(type, this.onHover, { capture: true });
439+
}
440+
this.touchesOnControls.clear();
421441
this.target?.removeEventListener("touchstart", this.onTouchStart, {
422442
capture: true,
423443
});
@@ -479,6 +499,7 @@ export class GamepadPlugin extends plugin.BasePlugin {
479499
return;
480500
}
481501
this.pointers.set(e.pointerId, control);
502+
this.touchesOnControls.add(e.pointerId);
482503
if (control instanceof VirtualStick) {
483504
control.grab(e.pointerId, x, y);
484505
} else {
@@ -531,6 +552,43 @@ export class GamepadPlugin extends plugin.BasePlugin {
531552
this.app.repaint();
532553
};
533554

555+
/**
556+
* The pointers whose touch landed on the controls, kept past their
557+
* release for the out and leave that follow it.
558+
*/
559+
private readonly touchesOnControls = new Set<number>();
560+
561+
/**
562+
* Keep a touch's hover events from the game when the touch is the
563+
* controls': over and enter when it lands on one, out and leave once it
564+
* lifts from one. A mouse hovering over the controls is left alone, as
565+
* its moves are.
566+
* @param e - the hover event
567+
*/
568+
private readonly onHover = (e: PointerEvent): void => {
569+
if (!this.ready || e.pointerType === "mouse") {
570+
return;
571+
}
572+
if (e.type === "pointerover" || e.type === "pointerenter") {
573+
if (!this.visible) {
574+
return;
575+
}
576+
const { x, y } = this.toViewport(e);
577+
if (this.touchesOnControls.has(e.pointerId) || this.onControls(x, y)) {
578+
this.touchesOnControls.add(e.pointerId);
579+
e.stopPropagation();
580+
}
581+
return;
582+
}
583+
// pointerout, pointerleave
584+
if (this.touchesOnControls.has(e.pointerId)) {
585+
e.stopPropagation();
586+
if (e.type === "pointerleave") {
587+
this.touchesOnControls.delete(e.pointerId);
588+
}
589+
}
590+
};
591+
534592
/**
535593
* keep an event on the controls from the engine and the page
536594
* @param e - the pointer event

‎packages/gamepad-plugin/tests/gamepad-plugin.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,38 @@ describe("GamepadPlugin", () => {
422422
input.releasePointerEvent("pointerdown", app.viewport, spy);
423423
});
424424

425+
it("keeps a touch's hover from the game too (#1805)", () => {
426+
const enter = vi.fn();
427+
const leave = vi.fn();
428+
input.registerPointerEvent("pointerenter", app.viewport, enter);
429+
input.registerPointerEvent("pointerleave", app.viewport, leave);
430+
431+
// a tap on a button: over and enter before the down, out and leave
432+
// after the up, as a touch fires them
433+
const at = button.center;
434+
const over = pointer("pointerover", 1, at.x, at.y);
435+
pointer("pointerenter", 1, at.x, at.y);
436+
down(1, at);
437+
up(1, at);
438+
pointer("pointerout", 1, at.x, at.y);
439+
pointer("pointerleave", 1, at.x, at.y);
440+
expect(enter).not.toHaveBeenCalled();
441+
expect(leave).not.toHaveBeenCalled();
442+
expect(over.defaultPrevented).toBe(false);
443+
444+
// a tap on the game is the game's
445+
const game = { x: app.viewport.width / 2, y: 20 };
446+
pointer("pointerenter", 2, game.x, game.y);
447+
expect(enter).toHaveBeenCalledOnce();
448+
449+
// and a mouse over a button is left alone, as its moves are
450+
pointer("pointerenter", 3, at.x, at.y, "mouse");
451+
expect(enter).toHaveBeenCalledTimes(2);
452+
453+
input.releasePointerEvent("pointerenter", app.viewport, enter);
454+
input.releasePointerEvent("pointerleave", app.viewport, leave);
455+
});
456+
425457
it("fires a locked key once per tap, however long it is held", () => {
426458
input.bindKey(KEY.X, "jump", true);
427459
down(1, button.center);

0 commit comments

Comments
 (0)