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