pokerogue/src/ui-inputs.ts

217 lines
7.8 KiB
TypeScript
Raw Normal View History

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-03 19:57:47 -04:00
import {Setting, SettingKeys, settingIndex} from "./system/settings/settings";
import SettingsUiHandler from "./ui/settings/settings-ui-handler";
import {Button} from "#enums/buttons";
import SettingsGamepadUiHandler from "./ui/settings/settings-gamepad-ui-handler";
import SettingsKeyboardUiHandler from "#app/ui/settings/settings-keyboard-ui-handler";
2024-05-29 07:16:57 -05:00
import BattleScene from "./battle-scene";
import SettingsDisplayUiHandler from "./ui/settings/settings-display-ui-handler";
import SettingsAudioUiHandler from "./ui/settings/settings-audio-ui-handler";
2024-05-29 07:16:57 -05:00
type ActionKeys = Record<Button, () => void>;
export class UiInputs {
2024-05-29 07:16:57 -05:00
private scene: BattleScene;
private events: Phaser.Events.EventEmitter;
private inputsController: InputsController;
2024-05-29 07:16:57 -05:00
constructor(scene: BattleScene, inputsController: InputsController) {
this.scene = scene;
this.inputsController = inputsController;
this.init();
}
init(): void {
this.events = this.inputsController.events;
this.listenInputs();
}
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";
}
}
listenInputs(): void {
this.events.on("input_down", (event) => {
this.detectInputMethod(event);
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);
}
}
getActionsKeyDown(): ActionKeys {
2024-05-29 07:16:57 -05: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(),
[Button.ACTION]: () => this.buttonAb(Button.ACTION),
[Button.CANCEL]: () => this.buttonAb(Button.CANCEL),
2024-05-29 07:16:57 -05:00
[Button.MENU]: () => this.buttonMenu(),
[Enhancement] Add go filter shortcut (#3345) * update goFilter shortcut * fix filter instruction position * remove unnecessary new lines * update requested changes. add other language entries too * remove unnecessary case code * open dropdown when pressing goFilter shortcut * add missing entry for `fr` * Update src/locales/fr/starter-select-ui-handler.ts Co-authored-by: Lugiad' <adrien.grivel@hotmail.fr> * Update src/locales/de/starter-select-ui-handler.ts Co-authored-by: Jannik Tappert <38758606+CodeTappert@users.noreply.github.com> * update instruction container position and text offset for more longer instruction label. decrease size of fr instruction font * fixed de info font size * Update src/locales/pt_BR/starter-select-ui-handler.ts Co-authored-by: José Ricardo Fleury Oliveira <josefleury@discente.ufg.br> * Update src/locales/ko/starter-select-ui-handler.ts Co-authored-by: Enoch <enoch.jwsong@gmail.com> * Update src/locales/zh_TW/starter-select-ui-handler.ts Co-authored-by: mercurius-00 <80205689+mercurius-00@users.noreply.github.com> * Update src/locales/zh_CN/starter-select-ui-handler.ts Co-authored-by: mercurius-00 <80205689+mercurius-00@users.noreply.github.com> * update font size, starterInfoXPos and starterInfoYOffset in zh. update starterInfoXPos in de * Update src/locales/es/starter-select-ui-handler.ts Co-authored-by: Asdar <asdargmng@gmail.com> --------- Co-authored-by: Lugiad' <adrien.grivel@hotmail.fr> Co-authored-by: Jannik Tappert <38758606+CodeTappert@users.noreply.github.com> Co-authored-by: José Ricardo Fleury Oliveira <josefleury@discente.ufg.br> Co-authored-by: Enoch <enoch.jwsong@gmail.com> Co-authored-by: mercurius-00 <80205689+mercurius-00@users.noreply.github.com> Co-authored-by: Asdar <asdargmng@gmail.com>
2024-08-09 04:34:20 +09:00
[Button.STATS]: () => this.buttonGoToFilter(Button.STATS),
[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),
[Button.V]: () => this.buttonCycleOption(Button.V),
2024-05-29 07:16:57 -05:00
[Button.SPEED_UP]: () => this.buttonSpeedChange(),
[Button.SLOW_DOWN]: () => this.buttonSpeedChange(false),
};
return actions;
}
getActionsKeyUp(): ActionKeys {
2024-05-29 07:16:57 -05: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,
[Button.V]: () => this.buttonInfo(false),
2024-05-29 07:16:57 -05:00
[Button.SPEED_UP]: () => undefined,
[Button.SLOW_DOWN]: () => undefined,
};
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 {
// 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
for (const p of this.scene.getField().filter(p => p?.isActive(true))) {
p.toggleStats(pressed);
}
}
[Enhancement] Add go filter shortcut (#3345) * update goFilter shortcut * fix filter instruction position * remove unnecessary new lines * update requested changes. add other language entries too * remove unnecessary case code * open dropdown when pressing goFilter shortcut * add missing entry for `fr` * Update src/locales/fr/starter-select-ui-handler.ts Co-authored-by: Lugiad' <adrien.grivel@hotmail.fr> * Update src/locales/de/starter-select-ui-handler.ts Co-authored-by: Jannik Tappert <38758606+CodeTappert@users.noreply.github.com> * update instruction container position and text offset for more longer instruction label. decrease size of fr instruction font * fixed de info font size * Update src/locales/pt_BR/starter-select-ui-handler.ts Co-authored-by: José Ricardo Fleury Oliveira <josefleury@discente.ufg.br> * Update src/locales/ko/starter-select-ui-handler.ts Co-authored-by: Enoch <enoch.jwsong@gmail.com> * Update src/locales/zh_TW/starter-select-ui-handler.ts Co-authored-by: mercurius-00 <80205689+mercurius-00@users.noreply.github.com> * Update src/locales/zh_CN/starter-select-ui-handler.ts Co-authored-by: mercurius-00 <80205689+mercurius-00@users.noreply.github.com> * update font size, starterInfoXPos and starterInfoYOffset in zh. update starterInfoXPos in de * Update src/locales/es/starter-select-ui-handler.ts Co-authored-by: Asdar <asdargmng@gmail.com> --------- Co-authored-by: Lugiad' <adrien.grivel@hotmail.fr> Co-authored-by: Jannik Tappert <38758606+CodeTappert@users.noreply.github.com> Co-authored-by: José Ricardo Fleury Oliveira <josefleury@discente.ufg.br> Co-authored-by: Enoch <enoch.jwsong@gmail.com> Co-authored-by: mercurius-00 <80205689+mercurius-00@users.noreply.github.com> Co-authored-by: Asdar <asdargmng@gmail.com>
2024-08-09 04:34:20 +09:00
buttonGoToFilter(button: Button): void {
const whitelist = [StarterSelectUiHandler];
const uiHandler = this.scene.ui?.getHandler();
if (whitelist.some(handler => uiHandler instanceof handler)) {
this.scene.ui.processInput(button);
} else {
this.buttonStats(true);
}
}
buttonInfo(pressed: boolean = true): void {
if (this.scene.showMovesetFlyout ) {
for (const p of this.scene.getField().filter(p => p?.isActive(true))) {
p.toggleFlyout(pressed);
}
}
if (this.scene.showArenaFlyout) {
this.scene.ui.processInfoButton(pressed);
}
}
buttonMenu(): void {
if (this.scene.disableMenu) {
return;
}
switch (this.scene.ui?.getMode()) {
case Mode.MESSAGE:
if (!(this.scene.ui.getHandler() as MessageUiHandler).pendingPrompt) {
return;
}
case Mode.TITLE:
case Mode.COMMAND:
case Mode.MODIFIER_SELECT:
this.scene.ui.setOverlayMode(Mode.MENU);
break;
case Mode.STARTER_SELECT:
this.buttonTouch();
break;
case Mode.MENU:
this.scene.ui.revertMode();
this.scene.playSound("select");
break;
default:
return;
}
}
buttonCycleOption(button: Button): void {
const whitelist = [StarterSelectUiHandler, SettingsUiHandler, SettingsDisplayUiHandler, SettingsAudioUiHandler, SettingsGamepadUiHandler, SettingsKeyboardUiHandler];
const uiHandler = this.scene.ui?.getHandler();
if (whitelist.some(handler => uiHandler instanceof handler)) {
this.scene.ui.processInput(button);
} else if (button === Button.V) {
this.buttonInfo(true);
}
}
buttonSpeedChange(up = true): void {
2024-06-03 19:57:47 -04:00
const settingGameSpeed = settingIndex(SettingKeys.Game_Speed);
if (up && this.scene.gameSpeed < 5) {
2024-06-18 16:07:36 +02:00
this.scene.gameData.saveSetting(SettingKeys.Game_Speed, Setting[settingGameSpeed].options.findIndex((item) => item.label === `${this.scene.gameSpeed}x`) + 1);
2024-06-03 19:57:47 -04:00
if (this.scene.ui?.getMode() === Mode.SETTINGS) {
(this.scene.ui.getHandler() as SettingsUiHandler).show([]);
}
2024-06-03 19:57:47 -04:00
} else if (!up && this.scene.gameSpeed > 1) {
2024-06-18 16:07:36 +02:00
this.scene.gameData.saveSetting(SettingKeys.Game_Speed, Math.max(Setting[settingGameSpeed].options.findIndex((item) => item.label === `${this.scene.gameSpeed}x`) - 1, 0));
if (this.scene.ui?.getMode() === Mode.SETTINGS) {
(this.scene.ui.getHandler() as SettingsUiHandler).show([]);
}
}
}
}