feat: 存档验证函数

This commit is contained in:
unanmed 2025-06-17 15:33:18 +08:00
parent f5a12c4a90
commit bcf3d33a9c

View File

@ -19,6 +19,8 @@ export interface SaveProps extends UIComponentProps, DefaultProps {
export type SaveEmits = { export type SaveEmits = {
/** 点击存档时触发 */ /** 点击存档时触发 */
emit: (index: number) => void; emit: (index: number) => void;
/** 删除存档时触发 */
delete: (index: number) => void;
/** 手动点击退出时触发 */ /** 手动点击退出时触发 */
exit: () => void; exit: () => void;
}; };
@ -172,15 +174,22 @@ export const Save = defineComponent<SaveProps, SaveEmits, keyof SaveEmits>(
export const SaveUI = new GameUI('save', Save); export const SaveUI = new GameUI('save', Save);
export interface SaveValidation {
readonly valid: boolean;
readonly message: string;
}
/** /**
* -1 * -2
* {@link SaveProps} * {@link SaveProps}
* *
* 使 * 使
* ```ts * ```ts
* const index = await selectSave(props.controller, [0, 0, 416, 416]); * const index = await selectSave(props.controller, [0, 0, 416, 416]);
* if (index === -1) { * if (index === -2) {
* // 如果用户未选择存档,而是关闭了存档。 * // 如果用户未选择存档,而是关闭了存档。
* } else if (index === -1) {
* // 用户选择了自动存档。
* } else { * } else {
* // 用户选择了一个存档。 * // 用户选择了一个存档。
* } * }
@ -188,24 +197,31 @@ export const SaveUI = new GameUI('save', Save);
* @param controller * @param controller
* @param loc * @param loc
* @param props * @param props
* @returns * @returns
*/ */
export function selectSave( export function selectSave(
controller: IUIMountable, controller: IUIMountable,
loc: ElementLocator, loc: ElementLocator,
validate?: (index: number) => SaveValidation,
props?: SaveProps props?: SaveProps
) { ) {
return new Promise<number>(res => { return new Promise<number>(res => {
const instance = controller.open(SaveUI, { const instance = controller.open(SaveUI, {
loc, loc,
...props, ...props,
onEmit: (index: number, isDelete: boolean) => { onEmit: (index: number) => {
if (index === -1) return; // 自动存档不能用于保存 if (!validate) {
if (isDelete) {
return;
}
controller.close(instance); controller.close(instance);
res(index); res(index);
return;
}
const validation = validate(index);
if (validation.valid) {
controller.close(instance);
res(index);
} else {
core.drawTip(validation.message);
}
}, },
onExit: () => { onExit: () => {
controller.close(instance); controller.close(instance);