diff --git a/packages-user/client-modules/src/action/hotkey.ts b/packages-user/client-modules/src/action/hotkey.ts index 9f74602..3f72896 100644 --- a/packages-user/client-modules/src/action/hotkey.ts +++ b/packages-user/client-modules/src/action/hotkey.ts @@ -3,7 +3,7 @@ import { gameKey, HotkeyJSON } from '@motajs/system-action'; import { hovered, mainUi, tip, openDanmakuPoster } from '@motajs/legacy-ui'; import { GameStorage } from '@motajs/legacy-system'; import { openStatistics } from '../render/ui/statistics'; -import { mainUIController } from '../render'; +import { MAIN_HEIGHT, MAIN_WIDTH, mainUIController, saveSave } from '../render'; export const mainScope = Symbol.for('@key_main'); @@ -488,7 +488,7 @@ gameKey core.openBook(true); }) .realize('save', () => { - core.save(true); + saveSave(mainUIController, [0, 0, MAIN_WIDTH, MAIN_HEIGHT]); }) .realize('load', () => { core.load(true); diff --git a/packages-user/client-modules/src/render/ui/save.tsx b/packages-user/client-modules/src/render/ui/save.tsx index 3c34600..4330aa2 100644 --- a/packages-user/client-modules/src/render/ui/save.tsx +++ b/packages-user/client-modules/src/render/ui/save.tsx @@ -25,9 +25,9 @@ export interface SaveBtnProps extends DefaultProps { export type SaveEmits = { /** 点击存档时触发 */ - emit: (index: number) => void; + emit: (index: number, exist: boolean) => void; /** 删除存档时触发 */ - delete: (index: number) => void; + delete: (index: number, exist: boolean) => void; /** 手动点击退出时触发 */ exit: () => void; }; @@ -289,6 +289,11 @@ export interface SaveValidation { readonly message: string; } +export type SaveValidationFunction = ( + index: number, + exist: boolean +) => SaveValidation; + /** * 打开存读档界面并让用户选择一个存档。如果用户手动关闭了存档界面,返回 -2,否则返回用户选择的存档索引。 * 参数参考 {@link SaveProps},事件不可自定义。 @@ -312,20 +317,20 @@ export interface SaveValidation { export function selectSave( controller: IUIMountable, loc: ElementLocator, - validate?: (index: number) => SaveValidation, + validate?: SaveValidationFunction, props?: SaveProps ) { return new Promise(res => { const instance = controller.open(SaveUI, { loc, ...props, - onEmit: (index: number) => { + onEmit: (index: number, exist: boolean) => { if (!validate) { controller.close(instance); res(index); return; } - const validation = validate(index); + const validation = validate(index, exist); if (validation.valid) { controller.close(instance); res(index); @@ -339,3 +344,31 @@ export function selectSave( }); }); } + +export async function saveSave( + controller: IUIMountable, + loc: ElementLocator, + props?: SaveProps +) { + const validate = (index: number): SaveValidation => { + if (index === -1) { + return { message: '不能存档至自动存档!', valid: false }; + } else { + return { message: '', valid: true }; + } + }; + const index = await selectSave(controller, loc, validate, props); + core.doSL(index, 'save'); +} + +export async function saveLoad( + controller: IUIMountable, + loc: ElementLocator, + props?: SaveProps +) { + const validate = (_: number, exist: boolean): SaveValidation => { + return { message: '无效的存档!', valid: exist }; + }; + const index = await selectSave(controller, loc, validate, props); + core.doSL(index, 'load'); +} diff --git a/packages-user/client-modules/src/render/ui/toolbar.tsx b/packages-user/client-modules/src/render/ui/toolbar.tsx index a47449e..da73a23 100644 --- a/packages-user/client-modules/src/render/ui/toolbar.tsx +++ b/packages-user/client-modules/src/render/ui/toolbar.tsx @@ -21,7 +21,7 @@ import { KeyCode } from '@motajs/client-base'; import { Progress } from '../components/misc'; import { generateBinary } from '@motajs/legacy-common'; import { SetupComponentOptions } from '@motajs/system-ui'; -import { SaveUI } from './save'; +import { saveSave } from './save'; import { mainUIController } from '@user/client-modules'; import { MAIN_WIDTH, MAIN_HEIGHT } from '../shared'; @@ -87,8 +87,8 @@ export const PlayingToolbar = defineComponent< const book = () => core.openBook(true); const tool = () => core.openToolbox(true); const fly = () => core.useFly(true); - const save = async () => { - mainUIController.open(SaveUI, { loc: [0, 0, MAIN_WIDTH, MAIN_HEIGHT] }); + const save = () => { + saveSave(mainUIController, [0, 0, MAIN_WIDTH, MAIN_HEIGHT]); }; const load = () => core.load(true); const equip = () => core.openEquipbox(true); diff --git a/src/types/declaration/control.d.ts b/src/types/declaration/control.d.ts index c0d9fb6..2908190 100644 --- a/src/types/declaration/control.d.ts +++ b/src/types/declaration/control.d.ts @@ -610,7 +610,7 @@ interface Control { /** * 实际进行存读档事件 */ - doSL(id: string, type: SLType): void; + doSL(id: string | number, type: SLType): void; /** * 同步存档到服务器