ui初始化

This commit is contained in:
unanmed 2023-08-18 11:45:18 +08:00
parent aff4b5a9d0
commit 41e0037bed
4 changed files with 84 additions and 2 deletions

View File

@ -130,6 +130,18 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
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, []);

View File

@ -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<T = any> extends EventEmitter<FocusEvent<T>> {
constructor(react?: boolean) {
super();
this.stack = react ? reactive([]) : [];
this.stack = react ? shallowReactive([]) : [];
}
/**
@ -105,6 +105,7 @@ export class Focus<T = any> extends EventEmitter<FocusEvent<T>> {
this.targets.add(v);
});
this.emit('register', item);
return this;
}
/**
@ -116,6 +117,7 @@ export class Focus<T = any> extends EventEmitter<FocusEvent<T>> {
this.targets.delete(v);
});
this.emit('unregister', item);
return this;
}
}
@ -159,4 +161,22 @@ export class UiController extends Focus<GameUi> {
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);
}
}

46
src/core/main/init/ui.ts Normal file
View File

@ -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);
}
});

View File

@ -287,6 +287,10 @@ export async function triggerFullscreen(full: boolean) {
}
}
/**
* 根据布尔值数组转换成一个二进制数
* @param arr 要转换的布尔值数组
*/
export function generateBinary(arr: boolean[]) {
let num = 0;
arr.forEach((v, i) => {