Skip to content

Commit

Permalink
fix: use "tap" for mobile compatibility (#117)
Browse files Browse the repository at this point in the history
This PR registers "tap" event handlers in addition to the "click"
handlers already installed. This makes the simulator register clicks
correctly on mobile devices.
  • Loading branch information
MegaRedHand authored Feb 9, 2025
1 parent 2cf3a99 commit e6f533f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"scripts": {
"test": "jest",
"start": "webpack serve --open --config webpack.dev.js",
"start-host": "webpack serve --config webpack.dev.js --host 0.0.0.0",
"build": "webpack --config webpack.prod.js",
"format": "prettier . --write",
"lint": "prettier . --check && eslint src/"
Expand Down
9 changes: 6 additions & 3 deletions src/graphics/viewport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Graphics, EventSystem } from "pixi.js";
import { Graphics, EventSystem, FederatedPointerEvent } from "pixi.js";
import * as pixi_viewport from "pixi-viewport";
import { deselectElement } from "../types/viewportManager";

Expand Down Expand Up @@ -47,11 +47,14 @@ export class Viewport extends pixi_viewport.Viewport {
});

// Only deselect if it's a genuine click, not a drag
this.on("click", (event) => {
const onClick = (event: FederatedPointerEvent) => {
if (!this.isDragging && event.target === this) {
deselectElement();
}
});
};
this.on("click", onClick, this);
// NOTE: this is "click" for mobile devices
this.on("tap", onClick, this);
}

clear() {
Expand Down
12 changes: 6 additions & 6 deletions src/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
<div class="wheel-container">
<div class="wheel-label">Speed</div>
<div class="circular-slider">
<input
type="range"
id="speed-wheel"
min="0.5"
max="4"
step="0.1"
<input
type="range"
id="speed-wheel"
min="0.5"
max="4"
step="0.1"
value="1"
>
</div>
Expand Down
7 changes: 3 additions & 4 deletions src/types/devices/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export abstract class Device extends Sprite {

this.on("pointerdown", this.onPointerDown, this);
this.on("click", this.onClick, this);
// NOTE: this is "click" for mobile devices
this.on("tap", this.onClick, this);
}

// Function to add the ID label to the device
Expand Down Expand Up @@ -146,7 +148,7 @@ export abstract class Device extends Sprite {
break;
}
default:
console.warn("Packets type unrecognized");
console.warn("Packet's type unrecognized");
}
}

Expand All @@ -160,7 +162,6 @@ export abstract class Device extends Sprite {
}

onPointerDown(event: FederatedPointerEvent): void {
console.log("Entered onPointerDown");
if (!Device.connectionTarget) {
selectElement(this);
}
Expand All @@ -177,8 +178,6 @@ export abstract class Device extends Sprite {

connectTo(adyacentId: DeviceId): boolean {
// Connects both devices with an edge.
// console.log("Entered connectTo");

const edgeId = this.viewgraph.addEdge(this.id, adyacentId);
if (edgeId) {
const adyacentDevice = this.viewgraph.getDevice(adyacentId);
Expand Down
2 changes: 2 additions & 0 deletions src/types/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class Edge extends Graphics {
this.interactive = true;
this.cursor = "pointer";
this.on("click", () => selectElement(this));
// NOTE: this is "click" for mobile devices
this.on("tap", () => selectElement(this));
}

nodePosition(nodeId: DeviceId): Point | undefined {
Expand Down
2 changes: 2 additions & 0 deletions src/types/packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export class Packet extends Graphics {
this.interactive = true;
this.cursor = "pointer";
this.on("click", this.onClick, this);
// NOTE: this is "click" for mobile devices
this.on("tap", this.onClick, this);
}

onClick(e: FederatedPointerEvent) {
Expand Down

0 comments on commit e6f533f

Please sign in to comment.