mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-06-28 05:07:59 +08:00
feat: 存档验证函数
This commit is contained in:
parent
f5a12c4a90
commit
bcf3d33a9c
@ -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) {
|
controller.close(instance);
|
||||||
|
res(index);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
controller.close(instance);
|
const validation = validate(index);
|
||||||
res(index);
|
if (validation.valid) {
|
||||||
|
controller.close(instance);
|
||||||
|
res(index);
|
||||||
|
} else {
|
||||||
|
core.drawTip(validation.message);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onExit: () => {
|
onExit: () => {
|
||||||
controller.close(instance);
|
controller.close(instance);
|
||||||
|
Loading…
Reference in New Issue
Block a user