2024-05-05 15:30:00 +01:00
|
|
|
import Phaser from "phaser";
|
|
|
|
import {Mode} from "./ui/ui";
|
|
|
|
import {InputsController} from "./inputs-controller";
|
|
|
|
import MessageUiHandler from "./ui/message-ui-handler";
|
|
|
|
import StarterSelectUiHandler from "./ui/starter-select-ui-handler";
|
2024-06-04 00:57:47 +01:00
|
|
|
import {Setting, SettingKeys, settingIndex} from "./system/settings/settings";
|
2024-06-01 13:56:32 +01:00
|
|
|
import SettingsUiHandler from "./ui/settings/settings-ui-handler";
|
2024-06-13 23:44:23 +01:00
|
|
|
import {Button} from "#enums/buttons";
|
2024-06-01 13:56:32 +01:00
|
|
|
import SettingsGamepadUiHandler from "./ui/settings/settings-gamepad-ui-handler";
|
|
|
|
import SettingsKeyboardUiHandler from "#app/ui/settings/settings-keyboard-ui-handler";
|
2024-05-29 13:16:57 +01:00
|
|
|
import BattleScene from "./battle-scene";
|
2024-06-06 15:38:54 +01:00
|
|
|
import SettingsDisplayUiHandler from "./ui/settings/settings-display-ui-handler";
|
|
|
|
import SettingsAudioUiHandler from "./ui/settings/settings-audio-ui-handler";
|
2024-05-05 15:30:00 +01:00
|
|
|
|
2024-05-29 13:16:57 +01:00
|
|
|
type ActionKeys = Record<Button, () => void>;
|
2024-05-05 15:30:00 +01:00
|
|
|
|
|
|
|
export class UiInputs {
|
2024-05-29 13:16:57 +01:00
|
|
|
private scene: BattleScene;
|
|
|
|
private events: Phaser.Events.EventEmitter;
|
2024-05-23 16:03:10 +01:00
|
|
|
private inputsController: InputsController;
|
|
|
|
|
2024-05-29 13:16:57 +01:00
|
|
|
constructor(scene: BattleScene, inputsController: InputsController) {
|
2024-05-23 16:03:10 +01:00
|
|
|
this.scene = scene;
|
|
|
|
this.inputsController = inputsController;
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
init(): void {
|
|
|
|
this.events = this.inputsController.events;
|
|
|
|
this.listenInputs();
|
|
|
|
}
|
|
|
|
|
2024-06-09 02:35:11 +01:00
|
|
|
detectInputMethod(evt): void {
|
|
|
|
if (evt.controller_type === "keyboard") {
|
|
|
|
//if the touch property is present and defined, then this is a simulated keyboard event from the touch screen
|
|
|
|
if (evt.hasOwnProperty("isTouch") && evt.isTouch) {
|
|
|
|
this.scene.inputMethod = "touch";
|
|
|
|
} else {
|
|
|
|
this.scene.inputMethod = "keyboard";
|
|
|
|
}
|
|
|
|
} else if (evt.controller_type === "gamepad") {
|
|
|
|
this.scene.inputMethod = "gamepad";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 16:03:10 +01:00
|
|
|
listenInputs(): void {
|
|
|
|
this.events.on("input_down", (event) => {
|
2024-06-09 02:35:11 +01:00
|
|
|
this.detectInputMethod(event);
|
|
|
|
|
2024-05-23 16:03:10 +01:00
|
|
|
const actions = this.getActionsKeyDown();
|
|
|
|
if (!actions.hasOwnProperty(event.button)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
actions[event.button]();
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.events.on("input_up", (event) => {
|
|
|
|
const actions = this.getActionsKeyUp();
|
|
|
|
if (!actions.hasOwnProperty(event.button)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
actions[event.button]();
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
doVibration(inputSuccess: boolean, vibrationLength: number): void {
|
|
|
|
if (inputSuccess && this.scene.enableVibration && typeof navigator.vibrate !== "undefined") {
|
|
|
|
navigator.vibrate(vibrationLength);
|
2024-05-05 15:30:00 +01:00
|
|
|
}
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getActionsKeyDown(): ActionKeys {
|
2024-05-29 13:16:57 +01:00
|
|
|
const actions: ActionKeys = {
|
|
|
|
[Button.UP]: () => this.buttonDirection(Button.UP),
|
|
|
|
[Button.DOWN]: () => this.buttonDirection(Button.DOWN),
|
|
|
|
[Button.LEFT]: () => this.buttonDirection(Button.LEFT),
|
|
|
|
[Button.RIGHT]: () => this.buttonDirection(Button.RIGHT),
|
|
|
|
[Button.SUBMIT]: () => this.buttonTouch(),
|
2024-05-29 14:33:36 +01:00
|
|
|
[Button.ACTION]: () => this.buttonAb(Button.ACTION),
|
|
|
|
[Button.CANCEL]: () => this.buttonAb(Button.CANCEL),
|
2024-05-29 13:16:57 +01:00
|
|
|
[Button.MENU]: () => this.buttonMenu(),
|
|
|
|
[Button.STATS]: () => this.buttonStats(true),
|
2024-05-29 14:33:36 +01:00
|
|
|
[Button.CYCLE_SHINY]: () => this.buttonCycleOption(Button.CYCLE_SHINY),
|
|
|
|
[Button.CYCLE_FORM]: () => this.buttonCycleOption(Button.CYCLE_FORM),
|
|
|
|
[Button.CYCLE_GENDER]: () => this.buttonCycleOption(Button.CYCLE_GENDER),
|
|
|
|
[Button.CYCLE_ABILITY]: () => this.buttonCycleOption(Button.CYCLE_ABILITY),
|
|
|
|
[Button.CYCLE_NATURE]: () => this.buttonCycleOption(Button.CYCLE_NATURE),
|
2024-06-04 03:18:09 +01:00
|
|
|
[Button.V]: () => this.buttonCycleOption(Button.V),
|
2024-05-29 13:16:57 +01:00
|
|
|
[Button.SPEED_UP]: () => this.buttonSpeedChange(),
|
|
|
|
[Button.SLOW_DOWN]: () => this.buttonSpeedChange(false),
|
|
|
|
};
|
2024-05-23 16:03:10 +01:00
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
getActionsKeyUp(): ActionKeys {
|
2024-05-29 13:16:57 +01:00
|
|
|
const actions: ActionKeys = {
|
|
|
|
[Button.UP]: () => undefined,
|
|
|
|
[Button.DOWN]: () => undefined,
|
|
|
|
[Button.LEFT]: () => undefined,
|
|
|
|
[Button.RIGHT]: () => undefined,
|
|
|
|
[Button.SUBMIT]: () => undefined,
|
|
|
|
[Button.ACTION]: () => undefined,
|
|
|
|
[Button.CANCEL]: () => undefined,
|
|
|
|
[Button.MENU]: () => undefined,
|
|
|
|
[Button.STATS]: () => this.buttonStats(false),
|
|
|
|
[Button.CYCLE_SHINY]: () => undefined,
|
|
|
|
[Button.CYCLE_FORM]: () => undefined,
|
|
|
|
[Button.CYCLE_GENDER]: () => undefined,
|
|
|
|
[Button.CYCLE_ABILITY]: () => undefined,
|
|
|
|
[Button.CYCLE_NATURE]: () => undefined,
|
2024-05-30 01:29:59 +01:00
|
|
|
[Button.V]: () => this.buttonInfo(false),
|
2024-05-29 13:16:57 +01:00
|
|
|
[Button.SPEED_UP]: () => undefined,
|
|
|
|
[Button.SLOW_DOWN]: () => undefined,
|
|
|
|
};
|
2024-05-23 16:03:10 +01:00
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonDirection(direction: Button): void {
|
|
|
|
const inputSuccess = this.scene.ui.processInput(direction);
|
|
|
|
const vibrationLength = 5;
|
|
|
|
this.doVibration(inputSuccess, vibrationLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonAb(button: Button): void {
|
|
|
|
this.scene.ui.processInput(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonTouch(): void {
|
|
|
|
this.scene.ui.processInput(Button.SUBMIT) || this.scene.ui.processInput(Button.ACTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonStats(pressed: boolean = true): void {
|
2024-06-06 02:28:12 +01:00
|
|
|
// allow access to Button.STATS as a toggle for other elements
|
|
|
|
for (const t of this.scene.getInfoToggles(true)) {
|
|
|
|
t.toggleInfo(pressed);
|
|
|
|
}
|
|
|
|
// handle normal pokemon battle ui
|
2024-05-30 01:29:59 +01:00
|
|
|
for (const p of this.scene.getField().filter(p => p?.isActive(true))) {
|
|
|
|
p.toggleStats(pressed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buttonInfo(pressed: boolean = true): void {
|
2024-06-04 03:18:09 +01:00
|
|
|
if (this.scene.showMovesetFlyout ) {
|
|
|
|
for (const p of this.scene.getField().filter(p => p?.isActive(true))) {
|
|
|
|
p.toggleFlyout(pressed);
|
|
|
|
}
|
2024-05-30 01:29:59 +01:00
|
|
|
}
|
|
|
|
|
2024-06-04 03:18:09 +01:00
|
|
|
if (this.scene.showArenaFlyout) {
|
|
|
|
this.scene.ui.processInfoButton(pressed);
|
2024-05-05 15:30:00 +01:00
|
|
|
}
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
2024-05-05 15:30:00 +01:00
|
|
|
|
2024-05-23 16:03:10 +01:00
|
|
|
buttonMenu(): void {
|
|
|
|
if (this.scene.disableMenu) {
|
|
|
|
return;
|
2024-05-05 15:30:00 +01:00
|
|
|
}
|
2024-05-23 16:03:10 +01:00
|
|
|
switch (this.scene.ui?.getMode()) {
|
|
|
|
case Mode.MESSAGE:
|
|
|
|
if (!(this.scene.ui.getHandler() as MessageUiHandler).pendingPrompt) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Mode.TITLE:
|
|
|
|
case Mode.COMMAND:
|
2024-06-11 03:18:07 +01:00
|
|
|
case Mode.MODIFIER_SELECT:
|
2024-05-23 16:03:10 +01:00
|
|
|
this.scene.ui.setOverlayMode(Mode.MENU);
|
|
|
|
break;
|
2024-06-11 03:18:07 +01:00
|
|
|
case Mode.STARTER_SELECT:
|
|
|
|
this.buttonTouch();
|
|
|
|
break;
|
2024-05-23 16:03:10 +01:00
|
|
|
case Mode.MENU:
|
|
|
|
this.scene.ui.revertMode();
|
|
|
|
this.scene.playSound("select");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
2024-05-05 15:30:00 +01:00
|
|
|
}
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
2024-05-05 15:30:00 +01:00
|
|
|
|
2024-05-23 16:03:10 +01:00
|
|
|
buttonCycleOption(button: Button): void {
|
2024-06-06 15:38:54 +01:00
|
|
|
const whitelist = [StarterSelectUiHandler, SettingsUiHandler, SettingsDisplayUiHandler, SettingsAudioUiHandler, SettingsGamepadUiHandler, SettingsKeyboardUiHandler];
|
2024-06-01 13:56:32 +01:00
|
|
|
const uiHandler = this.scene.ui?.getHandler();
|
|
|
|
if (whitelist.some(handler => uiHandler instanceof handler)) {
|
2024-05-23 16:03:10 +01:00
|
|
|
this.scene.ui.processInput(button);
|
2024-05-30 01:29:59 +01:00
|
|
|
} else if (button === Button.V) {
|
|
|
|
this.buttonInfo(true);
|
2024-05-05 15:30:00 +01:00
|
|
|
}
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buttonSpeedChange(up = true): void {
|
2024-06-04 00:57:47 +01:00
|
|
|
const settingGameSpeed = settingIndex(SettingKeys.Game_Speed);
|
|
|
|
if (up && this.scene.gameSpeed < 5) {
|
2024-06-18 15:07:36 +01:00
|
|
|
this.scene.gameData.saveSetting(SettingKeys.Game_Speed, Setting[settingGameSpeed].options.findIndex((item) => item.label === `${this.scene.gameSpeed}x`) + 1);
|
2024-06-04 00:57:47 +01:00
|
|
|
if (this.scene.ui?.getMode() === Mode.SETTINGS) {
|
|
|
|
(this.scene.ui.getHandler() as SettingsUiHandler).show([]);
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
2024-06-04 00:57:47 +01:00
|
|
|
} else if (!up && this.scene.gameSpeed > 1) {
|
2024-06-18 15:07:36 +01:00
|
|
|
this.scene.gameData.saveSetting(SettingKeys.Game_Speed, Math.max(Setting[settingGameSpeed].options.findIndex((item) => item.label === `${this.scene.gameSpeed}x`) - 1, 0));
|
2024-05-23 16:03:10 +01:00
|
|
|
if (this.scene.ui?.getMode() === Mode.SETTINGS) {
|
|
|
|
(this.scene.ui.getHandler() as SettingsUiHandler).show([]);
|
|
|
|
}
|
2024-05-05 15:30:00 +01:00
|
|
|
}
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
2024-05-05 15:30:00 +01:00
|
|
|
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|