2025-01-12 15:33:05 -08:00
|
|
|
import type BattleScene from "#app/battle-scene";
|
2024-06-08 00:33:45 +02:00
|
|
|
import pad_xbox360 from "#app/configs/inputs/pad_xbox360";
|
2025-01-12 15:33:05 -08:00
|
|
|
import type { InputsController } from "#app/inputs-controller";
|
2024-06-10 01:15:37 +02:00
|
|
|
import TouchControl from "#app/touch-controls";
|
2025-02-22 22:52:07 -06:00
|
|
|
import { holdOn } from "#test/testUtils/gameManagerUtils";
|
2024-06-08 00:33:45 +02:00
|
|
|
import fs from "fs";
|
2024-08-22 06:49:33 -07:00
|
|
|
import { JSDOM } from "jsdom";
|
|
|
|
import Phaser from "phaser";
|
2024-06-08 00:33:45 +02:00
|
|
|
|
2024-08-07 09:23:12 -07:00
|
|
|
interface LogEntry {
|
|
|
|
type: string;
|
|
|
|
button: any;
|
|
|
|
}
|
2024-06-08 00:33:45 +02:00
|
|
|
|
|
|
|
export default class InputsHandler {
|
|
|
|
private scene: BattleScene;
|
|
|
|
private events: Phaser.Events.EventEmitter;
|
|
|
|
private inputController: InputsController;
|
2024-08-07 09:23:12 -07:00
|
|
|
public log: LogEntry[] = [];
|
|
|
|
public logUp: LogEntry[] = [];
|
2024-06-08 00:33:45 +02:00
|
|
|
private fakePad: Fakepad;
|
|
|
|
private fakeMobile: FakeMobile;
|
|
|
|
|
|
|
|
constructor(scene: BattleScene) {
|
|
|
|
this.scene = scene;
|
|
|
|
this.inputController = this.scene.inputController;
|
|
|
|
this.fakePad = new Fakepad(pad_xbox360);
|
|
|
|
this.fakeMobile = new FakeMobile();
|
2024-08-07 09:23:12 -07:00
|
|
|
this.scene.input.gamepad?.gamepads.push(this.fakePad);
|
2024-06-08 00:33:45 +02:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
2025-02-05 01:56:13 +01:00
|
|
|
pressTouch(button: string, duration: number): Promise<void> {
|
2024-06-08 00:33:45 +02:00
|
|
|
return new Promise(async (resolve) => {
|
|
|
|
this.fakeMobile.touchDown(button);
|
|
|
|
await holdOn(duration);
|
|
|
|
this.fakeMobile.touchUp(button);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-02-05 01:56:13 +01:00
|
|
|
pressGamepadButton(button: number, duration: number): Promise<void> {
|
2024-06-08 00:33:45 +02:00
|
|
|
return new Promise(async (resolve) => {
|
2024-10-04 13:08:31 +08:00
|
|
|
this.scene.input.gamepad?.emit("down", this.fakePad, { index: button });
|
2024-06-08 00:33:45 +02:00
|
|
|
await holdOn(duration);
|
2024-10-04 13:08:31 +08:00
|
|
|
this.scene.input.gamepad?.emit("up", this.fakePad, { index: button });
|
2024-06-08 00:33:45 +02:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-02-05 01:56:13 +01:00
|
|
|
pressKeyboardKey(key: number, duration: number): Promise<void> {
|
2024-06-08 00:33:45 +02:00
|
|
|
return new Promise(async (resolve) => {
|
2024-10-04 13:08:31 +08:00
|
|
|
this.scene.input.keyboard?.emit("keydown", { keyCode: key });
|
2024-06-08 00:33:45 +02:00
|
|
|
await holdOn(duration);
|
2024-10-04 13:08:31 +08:00
|
|
|
this.scene.input.keyboard?.emit("keyup", { keyCode: key });
|
2024-06-08 00:33:45 +02:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
init(): void {
|
2025-01-12 15:33:05 -08:00
|
|
|
const touchControl = new TouchControl();
|
2024-06-10 01:15:37 +02:00
|
|
|
touchControl.deactivatePressedKey(); //test purpose
|
2024-06-08 00:33:45 +02:00
|
|
|
this.events = this.inputController.events;
|
2024-08-07 09:23:12 -07:00
|
|
|
this.scene.input.gamepad?.emit("connected", this.fakePad);
|
2024-06-08 00:33:45 +02:00
|
|
|
this.listenInputs();
|
|
|
|
}
|
|
|
|
|
|
|
|
listenInputs(): void {
|
|
|
|
this.events.on("input_down", (event) => {
|
2024-10-04 13:08:31 +08:00
|
|
|
this.log.push({ type: "input_down", button: event.button });
|
2024-06-08 00:33:45 +02:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.events.on("input_up", (event) => {
|
2024-10-04 13:08:31 +08:00
|
|
|
this.logUp.push({ type: "input_up", button: event.button });
|
2024-06-08 00:33:45 +02:00
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fakepad extends Phaser.Input.Gamepad.Gamepad {
|
|
|
|
public id: string;
|
|
|
|
public index: number;
|
|
|
|
|
|
|
|
constructor(pad) {
|
2024-08-07 09:23:12 -07:00
|
|
|
//@ts-ignore
|
2024-10-04 13:08:31 +08:00
|
|
|
super(undefined, { ...pad, buttons: pad.deviceMapping, axes: []}); //TODO: resolve ts-ignore
|
2024-06-08 00:33:45 +02:00
|
|
|
this.id = "xbox_360_fakepad";
|
|
|
|
this.index = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FakeMobile {
|
|
|
|
constructor() {
|
2025-02-22 22:52:07 -06:00
|
|
|
const fakeMobilePage = fs.readFileSync("././test/testUtils/fakeMobile.html", { encoding: "utf8", flag: "r" });
|
2024-06-08 00:33:45 +02:00
|
|
|
const dom = new JSDOM(fakeMobilePage);
|
|
|
|
Object.defineProperty(window, "document", {
|
|
|
|
value: dom.window.document,
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
touchDown(button: string) {
|
|
|
|
const node = document.querySelector(`[data-key][id='${button}']`);
|
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const event = new Event("touchstart");
|
|
|
|
node.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
touchUp(button: string) {
|
|
|
|
const node = document.querySelector(`[data-key][id='${button}']`);
|
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const event = new Event("touchend");
|
|
|
|
node.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
}
|