refactor: 所有ui的按键操作

This commit is contained in:
AncTe committed 2023-11-18 22:24:07 +08:00
1 parent 02088aae00
commit 253445eb6e
9 files changed
+191 -226

No files matched your search

+16 -7
View File
@@ -88,13 +88,21 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
* @param func 按键按下时执行的函数
*/
realize(id: string, func: HotkeyFunc) {
const key = this.data[id];
if (!key.func.has(this.scope)) {
throw new Error(
`Cannot access using scope. Call use before calling realize.`
);
const toSet = Object.values(this.data).filter(v => {
return v.id === id || v.id.split('_').slice(0, -1).join('_') === id;
});
if (toSet.length === 0) {
throw new Error(`Realize nonexistent key '${id}'.`);
}
key.func.set(this.scope, func);
for (const key of toSet) {
if (!key.func.has(this.scope)) {
throw new Error(
`Cannot access using scope. Call use before calling realize.`
);
}
key.func.set(this.scope, func);
}
return this;
}
/**
@@ -119,7 +127,7 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
key.func.delete(symbol);
}
spliceBy(this.scopeStack, symbol);
this.scope = this.scopeStack.pop() ?? Symbol();
this.scope = this.scopeStack.at(-1) ?? Symbol();
}
/**
@@ -177,6 +185,7 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
group(id: string, name: string) {
this.grouping = id;
this.groupName[id] = name;
this.groups[id] ??= [];
return this;
}
+5 -3
View File
@@ -160,12 +160,14 @@ gameKey
.register({
id: '@start_up',
name: '上移光标',
defaults: KeyCode.UpArrow
defaults: KeyCode.UpArrow,
type: 'down'
})
.register({
id: '@start_down',
name: '下移光标',
defaults: KeyCode.DownArrow
defaults: KeyCode.DownArrow,
type: 'down'
})
// --------------------
.group('@ui_book', '怪物手册')
@@ -226,7 +228,7 @@ gameKey
type: 'down'
})
.register({
id: '@toolbox_right',
id: '@toolbox_down',
name: '光标下移',
defaults: KeyCode.DownArrow,
type: 'down'
+12 -29
View File
@@ -1,38 +1,21 @@
import * as UI from '@ui/.';
import { GameUi, UiController } from '../custom/ui';
import { Hotkey } from '../custom/hotkey';
import { KeyCode } from '@/plugin/keyCodes';
import { hook } from '../game';
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);
}
});
export const mainUi = new UiController();
mainUi.register(
new GameUi('book', UI.Book, exitKey),
new GameUi('toolbox', UI.Toolbox, exitKey),
new GameUi('equipbox', UI.Equipbox, exitKey),
new GameUi('settings', UI.Settings, exitKey),
new GameUi('desc', UI.Desc, exitKey),
new GameUi('skill', UI.Skill, exitKey),
new GameUi('skillTree', UI.SkillTree, exitKey),
new GameUi('fly', UI.Fly, exitKey),
new GameUi('fixedDetail', UI.FixedDetail, exitKey),
new GameUi('shop', UI.Shop, exitKey),
new GameUi('achievement', UI.Achievement, exitKey),
new GameUi('bgm', UI.BgmList, exitKey)
new GameUi('book', UI.Book),
new GameUi('toolbox', UI.Toolbox),
new GameUi('equipbox', UI.Equipbox),
new GameUi('settings', UI.Settings),
new GameUi('desc', UI.Desc),
new GameUi('skill', UI.Skill),
new GameUi('skillTree', UI.SkillTree),
new GameUi('fly', UI.Fly),
new GameUi('fixedDetail', UI.FixedDetail),
new GameUi('shop', UI.Shop),
new GameUi('achievement', UI.Achievement),
new GameUi('bgm', UI.BgmList)
// todo: 把游戏主 div 加入到 mainUi 里面
);
mainUi.showAll();