pokerogue/src/ui/form-modal-ui-handler.ts

152 lines
4.4 KiB
TypeScript
Raw Normal View History

import BattleScene from "../battle-scene";
2023-12-30 18:41:25 -05:00
import { ModalConfig, ModalUiHandler } from "./modal-ui-handler";
import { Mode } from "./ui";
import { TextStyle, addTextInputObject, addTextObject } from "./text";
import { WindowVariant, addWindow } from "./ui-theme";
2023-12-30 18:41:25 -05:00
import InputText from "phaser3-rex-plugins/plugins/inputtext";
import * as Utils from "../utils";
import i18next from "i18next";
import {Button} from "#enums/buttons";
2023-12-30 18:41:25 -05:00
export interface FormModalConfig extends ModalConfig {
errorMessage?: string;
}
export abstract class FormModalUiHandler extends ModalUiHandler {
protected editing: boolean;
protected inputContainers: Phaser.GameObjects.Container[];
protected inputs: InputText[];
protected errorMessage: Phaser.GameObjects.Text;
[Refactor] use typescript `strict-null` (#3259) * TS: enable strict-null * fix battle-scene.ts * fix voucher.ts * adapt more files to strict-null * adapt more files to strict-null ( 2) * adapt ability.ts to strict-null * adapt `arena.ts` to strict-null * adapt TagAddedEvent constructor to strict-null * adapt phases.ts.to strict-null * adapt status-effect.ts to strict-null * adapt `account.ts` to strict-null * adapt `configHandler.ts` to strict-null * adapt `ability.ts` to strict-null * adapt `biomes.ts` to strict-null * adapt `challenge.ts` to strict-null * adapt `daily-run.ts` to strict-null * adapt `nature.ts` to strict-null * adapt `pokemon-forms.ts` to strict-null * adapt `tainer-names.ts` to strict-null * adapt `types.ts` to strict-null * adapt `weather.ts` to strict-null * adapt `egg-hatch-phase.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `pokemon-sprite-sparkle-handler.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `game-mode.ts` to strict-null * adapt `utils.ts` to strict-null * adapt `voucher-ui-handler.ts` to strict-null * adapt `src/ui/unavailable-modal-ui-handler.ts` to strict-null * adapt `src/ui/ui.ts` to strict-null * adapt `src/ui/ui-theme.ts` to strict-null * adapt `src/ui/title-ui-handler.ts` to strict-null * adapt `src/ui/time-of-day-widget.ts` to strict-null * adapt `src/ui/text.ts` to strict-null * adapt `src/ui/target-select-ui-handler.ts` to strict-null * adapt `src/ui/settings/settings-keyboard-ui-handler.ts` to strict-null * adapt more files to strict-null (3) * adapt more files to strict-null (4) * adapt more files (mostly tests) to strict-null (5) * adapt more files to strict-null (6) * adapt more files to strict-null (7) * Update `src/data/pokemon-evolutions.ts` for strict-null Partial update `src/data/pokemon-species.ts` for strict-null * adapt more files to strict-null (8) * adapt more files to strict-null (9) * Strict some more nulls (still a few errors remaining) * adapt rest of the files to strict-null (9) * fix tests (check for null instead of undefined) * repalce a lot of `??` with bangs And added TODO notice as usual * fix more tests * all tests pass now * fix broken game-loop after trainer battle add some console.warn for missing cases and falling back to default * remove guessed fallback from utils.rgbHexToRgba * add TODO for this.currentBattle = null * adjust getPokemonById() return to include `null` * fix compilation errors * add test for pokemon.trySetStatus * `chanceMultiplier` shouldn't be optional * allow `null` for currentPhase * adjust hasExpSprite logic for no keymatch found * reduce bang usage in account.updateUserInfo() * fix new strict-null issues after merge * fix `strict-null` issues in dropdown.ts and sand_spit.test.ts * fix egg-gacha * adapt gul_missile.test.ts to strict-null * fix move.ts strict-null * fix i18n.ts strict-null * fix strict-null issues * fix baton_pass test after accidentially breaking it * chore: fix compiler errors * revert accidential changes in baton_pass.test.ts --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2024-08-07 09:23:12 -07:00
protected submitAction: Function | null;
protected tween: Phaser.Tweens.Tween;
2023-12-30 18:41:25 -05:00
constructor(scene: BattleScene, mode: Mode | null = null) {
2023-12-30 18:41:25 -05:00
super(scene, mode);
this.editing = false;
this.inputContainers = [];
this.inputs = [];
}
abstract getFields(): string[];
getHeight(config?: ModalConfig): number {
2024-04-18 19:08:53 -04:00
return 20 * this.getFields().length + (this.getModalTitle() ? 26 : 0) + ((config as FormModalConfig)?.errorMessage ? 12 : 0) + this.getButtonTopMargin() + 28;
2023-12-30 18:41:25 -05:00
}
getReadableErrorMessage(error: string): string {
if (error?.indexOf("connection refused") > -1) {
return "Could not connect to the server";
}
2023-12-30 18:41:25 -05:00
return error;
}
setup(): void {
super.setup();
const fields = this.getFields();
const hasTitle = !!this.getModalTitle();
fields.forEach((field, f) => {
const label = addTextObject(this.scene, 10, (hasTitle ? 31 : 5) + 20 * f, field, TextStyle.TOOLTIP_CONTENT);
this.modalContainer.add(label);
const inputContainer = this.scene.add.container(70, (hasTitle ? 28 : 2) + 20 * f);
inputContainer.setVisible(false);
2023-12-30 18:41:25 -05:00
const inputBg = addWindow(this.scene, 0, 0, 80, 16, false, false, 0, 0, WindowVariant.XTHIN);
const isPassword = field.includes(i18next.t("menu:password")) || field.includes(i18next.t("menu:confirmPassword"));
const input = addTextInputObject(this.scene, 4, -2, 440, 116, TextStyle.TOOLTIP_CONTENT, { type: isPassword ? "password" : "text", maxLength: isPassword ? 64 : 16 });
2023-12-30 18:41:25 -05:00
input.setOrigin(0, 0);
inputContainer.add(inputBg);
inputContainer.add(input);
this.modalContainer.add(inputContainer);
this.inputContainers.push(inputContainer);
this.inputs.push(input);
});
this.errorMessage = addTextObject(this.scene, 10, (hasTitle ? 31 : 5) + 20 * (fields.length - 1) + 16 + this.getButtonTopMargin(), "", TextStyle.TOOLTIP_CONTENT);
this.errorMessage.setColor(this.getTextColor(TextStyle.SUMMARY_PINK));
this.errorMessage.setShadowColor(this.getTextColor(TextStyle.SUMMARY_PINK, true));
2023-12-30 18:41:25 -05:00
this.errorMessage.setVisible(false);
this.modalContainer.add(this.errorMessage);
}
show(args: any[]): boolean {
if (super.show(args)) {
this.inputContainers.map(ic => ic.setVisible(true));
const config = args[0] as FormModalConfig;
this.submitAction = config.buttonActions.length
? config.buttonActions[0]
: null;
if (this.buttonBgs.length) {
this.buttonBgs[0].off("pointerdown");
this.buttonBgs[0].on("pointerdown", () => {
if (this.submitAction) {
2023-12-30 18:41:25 -05:00
this.submitAction();
}
2023-12-30 18:41:25 -05:00
});
}
this.modalContainer.y += 24;
this.modalContainer.setAlpha(0);
this.tween = this.scene.tweens.add({
targets: this.modalContainer,
duration: Utils.fixedInt(1000),
ease: "Sine.easeInOut",
y: "-=24",
alpha: 1
});
2023-12-30 18:41:25 -05:00
return true;
}
return false;
}
processInput(button: Button): boolean {
if (button === Button.SUBMIT && this.submitAction) {
this.submitAction();
return true;
}
return false;
}
sanitizeInputs(): void {
for (const input of this.inputs) {
2023-12-30 18:41:25 -05:00
input.text = input.text.trim();
}
2023-12-30 18:41:25 -05:00
}
updateContainer(config?: ModalConfig): void {
super.updateContainer(config);
this.errorMessage.setText(this.getReadableErrorMessage((config as FormModalConfig)?.errorMessage || ""));
2023-12-30 18:41:25 -05:00
this.errorMessage.setVisible(!!this.errorMessage.text);
}
clear(): void {
super.clear();
this.modalContainer.setVisible(false);
this.inputContainers.map(ic => ic.setVisible(false));
this.submitAction = null;
if (this.tween) {
this.tween.remove();
}
2023-12-30 18:41:25 -05:00
}
}