mirror of
https://github.com/motajs/template.git
synced 2026-11-16 17:04:31 +08:00
refactor: 按键系统
This commit is contained in:
1 parent
d5b2a820d8
commit
564f53d82a
15 files changed
+569
-419
No files matched your search
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
HotkeySystem,
|
||||
IHotkeyCommand,
|
||||
IHotkeySystem,
|
||||
KeyCode
|
||||
} from '@motajs/system';
|
||||
import { IClientHotkey, IClientHotkeySave } from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
|
||||
export class ClientHotkey implements IClientHotkey {
|
||||
system: IHotkeySystem = new HotkeySystem();
|
||||
|
||||
/** 按键分组 */
|
||||
private readonly groups: Map<string, IHotkeyCommand[]> = new Map();
|
||||
|
||||
setGroup(group: string, commands: IHotkeyCommand[]): void {
|
||||
if (commands.length > 0) {
|
||||
if (this.groups.has(group)) {
|
||||
logger.warn(208, group);
|
||||
}
|
||||
this.groups.set(group, commands);
|
||||
} else {
|
||||
this.groups.delete(group);
|
||||
}
|
||||
}
|
||||
|
||||
getGroup(group: string): IHotkeyCommand[] {
|
||||
return this.groups.get(group) ?? [];
|
||||
}
|
||||
|
||||
getUngrouped(): Iterable<IHotkeyCommand> {
|
||||
const grouped = new Set([...this.groups.values()].flat());
|
||||
return new Set([...this.system.iterateCommands()]).difference(grouped);
|
||||
}
|
||||
|
||||
saveState(): IClientHotkeySave {
|
||||
const keys: Map<string, (readonly KeyCode[])[]> = new Map();
|
||||
for (const command of this.system.iterateCommands()) {
|
||||
const saveKeys: (readonly KeyCode[])[] = Array.from(
|
||||
{ length: command.length },
|
||||
() => []
|
||||
);
|
||||
for (const [index, key] of command.iterateKeyCombinations()) {
|
||||
saveKeys[index] = key;
|
||||
}
|
||||
keys.set(command.command, saveKeys);
|
||||
}
|
||||
return {
|
||||
keys
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IClientHotkeySave): void {
|
||||
for (const [id, keys] of state.keys) {
|
||||
const command = this.system.getCommand(id);
|
||||
if (!command) {
|
||||
logger.warn(209, id);
|
||||
return;
|
||||
}
|
||||
command.setKeys(keys.length);
|
||||
keys.forEach((key, index) => {
|
||||
command.setKeyCombination(index, key);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './hotkey';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,30 @@
|
||||
import { IHotkeyCommand, IHotkeySystem, KeyCode } from '@motajs/system';
|
||||
import { ISaveableContent } from '@user/data-common';
|
||||
|
||||
export interface IClientHotkeySave {
|
||||
/** 按键指令列表 */
|
||||
readonly keys: ReadonlyMap<string, readonly (readonly KeyCode[])[]>;
|
||||
}
|
||||
|
||||
export interface IClientHotkey extends ISaveableContent<IClientHotkeySave> {
|
||||
/** 按键系统对象 */
|
||||
readonly system: IHotkeySystem;
|
||||
|
||||
/**
|
||||
* 设置指定分组的按键指令列表,若不存在分组则创建,若传入空数组则删除
|
||||
* @param group 按键分组名
|
||||
* @param commands 分组中包含的按键指令名
|
||||
*/
|
||||
setGroup(group: string, commands: IHotkeyCommand[]): void;
|
||||
|
||||
/**
|
||||
* 获取指定按键分组中包含的按键,如果不存在则返回空数组
|
||||
* @param group 按键分组名
|
||||
*/
|
||||
getGroup(group: string): IHotkeyCommand[];
|
||||
|
||||
/**
|
||||
* 获取未分组的按键列表
|
||||
*/
|
||||
getUngrouped(): Iterable<IHotkeyCommand>;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from './components';
|
||||
export * from './elements';
|
||||
export * from './hotkey';
|
||||
export * from './layout';
|
||||
export * from './map';
|
||||
export * from './material';
|
||||
|
||||
@@ -11,6 +11,7 @@ import { ICoreState } from '@user/data-state';
|
||||
import { ISaveSystem } from './save';
|
||||
import { ITextureManager } from './material';
|
||||
import { IMapRenderer, IMapExtensionManager } from './map';
|
||||
import { IClientHotkey } from './hotkey';
|
||||
|
||||
export interface IGameUIPropsBase<
|
||||
T extends UIComponent = UIComponent
|
||||
@@ -47,6 +48,8 @@ export interface IClientBase extends ICoreState {
|
||||
readonly sceneController: IUIController<IGameUIPropsBase>;
|
||||
/** 主界面的 UI 控制器,管理游戏主界面的 UI 显示 */
|
||||
readonly mainUIController: IUIController<IGameUIPropsBase>;
|
||||
/** 渲染端主按键系统 */
|
||||
readonly hotkey: IClientHotkey;
|
||||
}
|
||||
|
||||
export interface IClientBaseExtended {
|
||||
|
||||
Reference in new issue
Block a user