From 41e0037bed255eeaeccdd54f906c809a4a36cc4b Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Fri, 18 Aug 2023 11:45:18 +0800 Subject: [PATCH] =?UTF-8?q?ui=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/main/custom/hotkey.ts | 12 +++++++++ src/core/main/custom/ui.ts | 24 ++++++++++++++++-- src/core/main/init/ui.ts | 46 ++++++++++++++++++++++++++++++++++ src/plugin/utils.ts | 4 +++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/core/main/init/ui.ts diff --git a/src/core/main/custom/hotkey.ts b/src/core/main/custom/hotkey.ts index eb09041..3f0f92e 100644 --- a/src/core/main/custom/hotkey.ts +++ b/src/core/main/custom/hotkey.ts @@ -130,6 +130,18 @@ export class Hotkey extends EventEmitter { return this; } + /** + * 从一个按键控制器继承按键信息 + * @param hotkey 被继承的按键 + * @param cover 继承时是否覆盖同id的按键 + */ + extend(hotkey: Hotkey, cover: boolean) { + Object.values(hotkey.list).forEach(v => { + if (v.id in this.list && !cover) return; + this.register(v.id, v.name, v); + }); + } + private ensureKey(key: KeyCode) { if (!this.keyMap.has(key)) { this.keyMap.set(key, []); diff --git a/src/core/main/custom/ui.ts b/src/core/main/custom/ui.ts index 815d68f..55e7c99 100644 --- a/src/core/main/custom/ui.ts +++ b/src/core/main/custom/ui.ts @@ -1,4 +1,4 @@ -import { Component, reactive } from 'vue'; +import { Component, shallowReactive } from 'vue'; import { EmitableEvent, EventEmitter } from '../../common/eventEmitter'; import { KeyCode } from '../../../plugin/keyCodes'; import { Hotkey } from './hotkey'; @@ -21,7 +21,7 @@ export class Focus extends EventEmitter> { constructor(react?: boolean) { super(); - this.stack = react ? reactive([]) : []; + this.stack = react ? shallowReactive([]) : []; } /** @@ -105,6 +105,7 @@ export class Focus extends EventEmitter> { this.targets.add(v); }); this.emit('register', item); + return this; } /** @@ -116,6 +117,7 @@ export class Focus extends EventEmitter> { this.targets.delete(v); }); this.emit('unregister', item); + return this; } } @@ -159,4 +161,22 @@ export class UiController extends Focus { emitKey(key: KeyCode, e: KeyboardEvent) { this.focused?.hotkey?.emitKey(key, e); } + + /** + * 根据id获取到ui + * @param id ui的id + */ + get(id: string) { + return [...this.targets.values()].find(v => v.id === id); + } + + /** + * 关闭一个ui,注意在其之后的ui都会同时关闭掉 + * @param id 要关闭的ui的id + */ + close(id: string) { + const ui = this.stack.find(v => v.id === id); + if (!ui) return; + this.splice(ui); + } } diff --git a/src/core/main/init/ui.ts b/src/core/main/init/ui.ts new file mode 100644 index 0000000..3076455 --- /dev/null +++ b/src/core/main/init/ui.ts @@ -0,0 +1,46 @@ +import Book from '../../../ui/book.vue'; +import Toolbox from '../../../ui/toolbox.vue'; +import Equipbox from '../../../ui/equipbox.vue'; +import Settings from '../../../ui/settings.vue'; +import Desc from '../../../ui/desc.vue'; +import Skill from '../../../ui/skill.vue'; +import SkillTree from '../../../ui/skillTree.vue'; +import Fly from '../../../ui/fly.vue'; +import FixedDetail from '../../../ui/fixedDetail.vue'; +import Shop from '../../../ui/shop.vue'; +import Achievement from '../../../ui/achievement.vue'; +import Bgm from '../../../ui/bgmList.vue'; +import { GameUi, UiController } from '../custom/ui'; +import { Hotkey } from '../custom/hotkey'; +import { KeyCode } from '../../../plugin/keyCodes'; + +export const mainUi = new UiController(); +mainUi.register( + new GameUi('book', Book), + new GameUi('toolbox', Toolbox), + new GameUi('equipbox', Equipbox), + new GameUi('settings', Settings), + new GameUi('desc', Desc), + new GameUi('skill', Skill), + new GameUi('skillTree', SkillTree), + new GameUi('fly', Fly), + new GameUi('fixedDetail', FixedDetail), + new GameUi('shop', Shop), + new GameUi('achievement', Achievement), + new GameUi('bgm', Bgm) +); + +export const exitKey = new Hotkey('exitKey'); +exitKey + .register('exit1', '退出', { + defaults: KeyCode.KeyX, + func: () => { + if (mainUi.focused) mainUi.splice(mainUi.focused); + } + }) + .register('exit2', '退出', { + defaults: KeyCode.Escape, + func: () => { + if (mainUi.focused) mainUi.splice(mainUi.focused); + } + }); diff --git a/src/plugin/utils.ts b/src/plugin/utils.ts index 941f9a6..ada4279 100644 --- a/src/plugin/utils.ts +++ b/src/plugin/utils.ts @@ -287,6 +287,10 @@ export async function triggerFullscreen(full: boolean) { } } +/** + * 根据布尔值数组转换成一个二进制数 + * @param arr 要转换的布尔值数组 + */ export function generateBinary(arr: boolean[]) { let num = 0; arr.forEach((v, i) => {