Skip to content

Commit

Permalink
fix(phaserx): distinguish left and right click in click$ stream (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs authored Sep 20, 2022
1 parent be86d24 commit 004753d
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/phaserx/src/createInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,31 @@ export function createInput(inputPlugin: Phaser.Input.InputPlugin) {
filterNullish()
);

const pointerdown$: Observable<{ type: "down"; pointer: Phaser.Input.Pointer }> = fromEvent(
const pointerdown$: Observable<{ pointer: Phaser.Input.Pointer; event: MouseEvent }> = fromEvent(
document,
"mousedown"
).pipe(
filter(() => enabled.current),
map(() => ({ type: "down" as const, pointer: inputPlugin.manager?.activePointer })),
map((event) => ({ pointer: inputPlugin.manager?.activePointer, event: event as MouseEvent })),
filterNullish()
);

const pointerup$: Observable<{ type: "up"; pointer: Phaser.Input.Pointer }> = fromEvent(document, "mouseup").pipe(
const pointerup$: Observable<{ pointer: Phaser.Input.Pointer; event: MouseEvent }> = fromEvent(
document,
"mouseup"
).pipe(
filter(() => enabled.current),
map(() => ({ type: "up" as const, pointer: inputPlugin.manager?.activePointer })),
map((event) => ({ pointer: inputPlugin.manager?.activePointer, event: event as MouseEvent })),
filterNullish()
);

// Click stream
const click$ = merge(pointerdown$, pointerup$).pipe(
filter(() => enabled.current),
map<{ type: string; pointer: Phaser.Input.Pointer }, [boolean, number]>(({ type }) => {
return [type === "down", Date.now()];
}), // Map events to whether the left button is down and the current timestamp
map<{ pointer: Phaser.Input.Pointer; event: MouseEvent }, [boolean, number]>(({ event }) => [
event.type === "mousedown" && event.button === 0,
Date.now(),
]), // Map events to whether the left button is down and the current timestamp
bufferCount(2, 1), // Store the last two timestamps
filter(([prev, now]) => prev[0] && !now[0] && now[1] - prev[1] < 250), // Only care if button was pressed before and is not anymore and it happened within 500ms
map(() => inputPlugin.manager?.activePointer), // Return the current pointer
Expand Down

0 comments on commit 004753d

Please sign in to comment.