丰富ui控制器的api

This commit is contained in:
unanmed 2023-08-25 13:44:26 +08:00
parent a12aa293c3
commit 41f0394332
6 changed files with 52 additions and 9 deletions

View File

@ -3,7 +3,9 @@ import { SoundController } from './audio/sound';
import { EventEmitter } from './common/eventEmitter';
import { loading, readyAllResource } from './loader/load';
import { ResourceStore, ResourceType } from './loader/resource';
import { UiController } from './main/custom/ui';
import { GameEvent, hook } from './main/game';
import { fixedUi, mainUi } from './main/init/ui';
import { GameStorage } from './main/storage';
import { resolvePlugin } from './plugin';
@ -44,6 +46,10 @@ export interface AncTe {
hook: EventEmitter<GameEvent>;
storage: GameStorage<any>[];
};
ui: {
main: UiController;
fixed: UiController;
};
}
function ready() {
@ -57,6 +63,10 @@ function ready() {
game: {
hook,
storage: GameStorage.list
},
ui: {
main: mainUi,
fixed: fixedUi
}
};

View File

@ -31,12 +31,14 @@ type RegisterData = Omit<HotkeyData, 'id' | 'key' | 'name'>;
export class Hotkey extends EventEmitter<HotkeyEvent> {
keyMap: Map<KeyCode, HotkeyData[]> = new Map();
list: Record<string, HotkeyData> = {};
storage: GameStorage<Record<string, KeyCode>>;
storage?: GameStorage<Record<string, KeyCode>>;
groups: Record<string, GroupInfo> = {};
constructor(id: string) {
constructor(id: string, storage: boolean = true) {
super();
this.storage = new GameStorage(GameStorage.fromAncTe(id));
if (storage) {
this.storage = new GameStorage(GameStorage.fromAuthor('AncTe', id));
}
}
/**
@ -47,7 +49,7 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
const key = {
id,
name,
key: this.storage.getValue(id, data.defaults),
key: this.storage?.getValue(id, data.defaults) ?? data.defaults,
...data
};
this.ensureKey(key.key).push(key);

View File

@ -185,6 +185,7 @@ export class GameUi extends EventEmitter<GameUiEvent> {
export class UiController extends Focus<GameUi> {
static list: UiController[] = [];
list: Record<string, GameUi> = {};
constructor(equal?: boolean) {
super(true, equal);
@ -219,7 +220,7 @@ export class UiController extends Focus<GameUi> {
* @param id ui的id
*/
get(id: string) {
return [...this.targets.values()].find(v => v.id === id);
return this.list[id];
}
/**
@ -227,8 +228,36 @@ export class UiController extends Focus<GameUi> {
* @param id ui的id
*/
close(id: string) {
const ui = this.stack.find(v => v.id === id);
const ui = this.get(id);
if (!ui) return;
this.splice(ui);
}
/**
* ui
* @param id ui的id
*/
open(id: string) {
const ui = this.get(id);
if (!ui) return;
this.add(ui);
}
override register(...item: GameUi[]): this {
super.register(...item);
item.forEach(v => {
this.list[v.id] = v;
});
return this;
}
override unregister(...item: GameUi[]): this {
super.unregister(...item);
item.forEach(v => {
delete this.list[v.id];
});
return this;
}
}

View File

@ -61,3 +61,5 @@ fixedUi.register(
new GameUi('chapter', Chapter),
new GameUi('completeAchi', CompleteAchi)
);
mainUi.focus(mainUi.get('start'), true);

View File

@ -403,7 +403,7 @@ interface SettingStorage {
}
const storage = new GameStorage<SettingStorage>(
GameStorage.fromAncTe('setting')
GameStorage.fromAuthor('AncTe', 'setting')
);
loading.once('coreInit', () => {

View File

@ -55,8 +55,8 @@ export class GameStorage<T> {
return `HumanBreak_${key}`;
}
static fromAncTe(key: string) {
return `AncTe@${key}`;
static fromAuthor(author: string, key: string) {
return `${author}@${key}`;
}
}