From ac5603f2f5b20853ca36f754fadb037976d434ac Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Fri, 2 Feb 2024 21:01:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E4=B9=89=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E6=A0=8F=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/main/custom/toolbar.ts | 70 +++++++++++++++++++++++++++++++++ src/core/main/storage.ts | 19 ++++++++- src/ui/skillTree.vue | 2 +- src/ui/start.vue | 3 ++ src/ui/toolEditor.vue | 8 +++- src/ui/toolbar.vue | 16 +++++++- 6 files changed, 113 insertions(+), 5 deletions(-) diff --git a/src/core/main/custom/toolbar.ts b/src/core/main/custom/toolbar.ts index bb09064..a289aad 100644 --- a/src/core/main/custom/toolbar.ts +++ b/src/core/main/custom/toolbar.ts @@ -15,12 +15,14 @@ import { import { gameKey } from '../init/hotkey'; import { unwarpBinary } from './hotkey'; import { fixedUi } from '../init/ui'; +import { GameStorage } from '../storage'; interface CustomToolbarEvent extends EmitableEvent { add: (item: ValueOf) => void; delete: (item: ValueOf) => void; set: (id: string, data: Partial) => void; emit: (id: string, item: ValueOf) => void; + posChange: (bar: CustomToolbar) => void; } interface ToolbarItemBase { @@ -50,6 +52,14 @@ interface ToolbarItemMap { assistKey: AssistKeyToolbarItem; } +interface ToolbarSaveData { + x: number; + y: number; + w: number; + h: number; + items: ValueOf[]; +} + export type ToolbarItemType = keyof ToolbarItemMap; export type SettableItemData = @@ -82,6 +92,10 @@ interface RegisteredCustomToolInfo { const COM = createToolbarComponents(); const EDITOR = createToolbarEditorComponents(); +const toolbarStorage = new GameStorage>( + GameStorage.fromAuthor('AncTe', 'toolbar') +); + export class CustomToolbar extends EventEmitter { static num: number = 0; static list: CustomToolbar[] = shallowReactive([]); @@ -215,6 +229,7 @@ export class CustomToolbar extends EventEmitter { */ closeAll() { this.showIds.forEach(v => fixedUi.close(v)); + this.showIds = []; } static get(id: string) { @@ -251,6 +266,46 @@ export class CustomToolbar extends EventEmitter { }; this.info[type] = info; } + + static save() { + toolbarStorage.clear(); + this.list.forEach(v => { + const toSave: ToolbarSaveData = { + x: v.x, + y: v.y, + w: v.width, + h: v.height, + items: [] + }; + v.items.forEach(v => { + toSave.items.push(v); + }); + toolbarStorage.setValue(v.id, toSave); + }); + toolbarStorage.write(); + } + + static load() { + toolbarStorage.read(); + for (const [key, value] of Object.entries(toolbarStorage.data)) { + const bar = new CustomToolbar(key); + bar.x = value.x; + bar.y = value.y; + bar.width = value.w; + bar.height = value.h; + for (const item of value.items) { + bar.add(item); + } + } + } + + static showAll(): number[] { + return CustomToolbar.list.map(v => v.show()); + } + + static closeAll() { + this.list.forEach(v => v.closeAll()); + } } CustomToolbar.register( @@ -320,3 +375,18 @@ CustomToolbar.register( }; } ); + +window.addEventListener('unload', () => { + CustomToolbar.save(); +}); +window.addEventListener('blur', () => { + CustomToolbar.save(); +}); + +Mota.require('var', 'loading').once('coreInit', () => { + CustomToolbar.load(); + CustomToolbar.closeAll(); +}); +Mota.require('var', 'hook').on('reset', () => { + CustomToolbar.showAll(); +}); diff --git a/src/core/main/storage.ts b/src/core/main/storage.ts index 05fea91..ba7270c 100644 --- a/src/core/main/storage.ts +++ b/src/core/main/storage.ts @@ -1,4 +1,4 @@ -export class GameStorage { +export class GameStorage { static list: GameStorage[] = []; key: string; @@ -54,6 +54,23 @@ export class GameStorage { return JSON.stringify(this.data); } + clear() { + // @ts-ignore + this.data = {}; + } + + keys() { + return Object.keys(this.data); + } + + values() { + return Object.values(this.data); + } + + entries() { + return Object.entries(this.data); + } + /** * 获取本游戏的存储键 * @param key 存储名称 diff --git a/src/ui/skillTree.vue b/src/ui/skillTree.vue index 09e0633..a6bdb72 100644 --- a/src/ui/skillTree.vue +++ b/src/ui/skillTree.vue @@ -119,7 +119,7 @@ const mdef = ref(core.status.hero.mdef); const skill = computed(() => { update.value; - return skillTree.getSkillFromIndex(selected.value); + return skillTree.getSkillFromIndex(selected.value)!; }); const skills = computed(() => { diff --git a/src/ui/start.vue b/src/ui/start.vue index 2a61038..b63f93c 100644 --- a/src/ui/start.vue +++ b/src/ui/start.vue @@ -72,6 +72,7 @@ import { isMobile } from '../plugin/use'; import { GameUi } from '@/core/main/custom/ui'; import { gameKey } from '@/core/main/init/hotkey'; import { mainUi } from '@/core/main/init/ui'; +import { CustomToolbar } from '@/core/main/custom/toolbar'; const props = defineProps<{ num: number; @@ -325,6 +326,8 @@ onMounted(async () => { showCursor(); await sleep(1200); }); + + CustomToolbar.closeAll(); }); onUnmounted(() => { diff --git a/src/ui/toolEditor.vue b/src/ui/toolEditor.vue index addbb0e..2030876 100644 --- a/src/ui/toolEditor.vue +++ b/src/ui/toolEditor.vue @@ -170,7 +170,7 @@