diff --git a/src/core/main/custom/hotkey.ts b/src/core/main/custom/hotkey.ts index 6e90514..7795561 100644 --- a/src/core/main/custom/hotkey.ts +++ b/src/core/main/custom/hotkey.ts @@ -1,8 +1,11 @@ import { KeyCode } from '@/plugin/keyCodes'; -import { deleteWith, spliceBy } from '@/plugin/utils'; +import { deleteWith, generateBinary, spliceBy } from '@/plugin/utils'; import { EmitableEvent, EventEmitter } from '../../common/eventEmitter'; -interface HotkeyEvent extends EmitableEvent {} +interface HotkeyEvent extends EmitableEvent { + set: (id: string, key: KeyCode, assist: number) => void; + emit: (key: KeyCode, assist: number, type: KeyEmitType) => void; +} type KeyEmitType = 'down' | 'up'; @@ -27,6 +30,11 @@ interface HotkeyData extends Required { type HotkeyFunc = (code: KeyCode, ev: KeyboardEvent) => void; +export interface HotkeyJSON { + key: KeyCode; + assist: number; +} + export class Hotkey extends EventEmitter { static list: Hotkey[]; @@ -135,8 +143,9 @@ export class Hotkey extends EventEmitter { * @param id 要设置的按键的id * @param key 要设置成的按键 * @param assist 辅助按键,三位二进制数据,从低到高依次为`ctrl` `shift` `alt` + * @param emit 是否触发set事件,当且仅当从fromJSON方法调用时为false */ - set(id: string, key: KeyCode, assist: number) { + set(id: string, key: KeyCode, assist: number, emit: boolean = true) { const { ctrl, shift, alt } = this.unwarpBinary(assist); const data = this.data[id]; const before = this.keyMap.get(data.key)!; @@ -148,6 +157,7 @@ export class Hotkey extends EventEmitter { data.ctrl = ctrl; data.shift = shift; data.alt = alt; + if (emit) this.emit('set', id, key, assist); } /** @@ -177,6 +187,7 @@ export class Hotkey extends EventEmitter { func(key, ev); } }); + this.emit('emit', key, assist, type); } /** @@ -214,6 +225,24 @@ export class Hotkey extends EventEmitter { return this; } + toJSON() { + const res: Record = {}; + for (const [key, data] of Object.entries(this.data)) { + res[key] = { + key: data.key, + assist: generateBinary([data.ctrl, data.shift, data.alt]) + }; + } + return JSON.stringify(res); + } + + fromJSON(data: string) { + const json: Record = JSON.parse(data); + for (const [key, data] of Object.entries(json)) { + this.set(key, data.key, data.assist, false); + } + } + private unwarpBinary(bin: number): AssistHoykey { return { ctrl: !!(bin & (1 << 0)), diff --git a/src/core/main/init/hotkey.ts b/src/core/main/init/hotkey.ts index 630fe89..f3fe249 100644 --- a/src/core/main/init/hotkey.ts +++ b/src/core/main/init/hotkey.ts @@ -1,9 +1,10 @@ import { KeyCode } from '@/plugin/keyCodes'; -import { Hotkey } from '../custom/hotkey'; +import { Hotkey, HotkeyJSON } from '../custom/hotkey'; import { generateBinary, keycode } from '@/plugin/utils'; import { hovered } from './fixed'; import { hasMarkedEnemy, markEnemy, unmarkEnemy } from '@/plugin/mark'; import { mainUi } from './ui'; +import { GameStorage } from '../storage'; export const mainScope = Symbol.for('@key_main'); export const gameKey = new Hotkey('gameKey', '游戏按键'); @@ -15,10 +16,7 @@ gameKey .register({ id: 'book', name: '怪物手册', - defaults: KeyCode.KeyX, - ctrl: true, - shift: true, - alt: true + defaults: KeyCode.KeyX }) .register({ id: 'save', @@ -422,6 +420,17 @@ gameKey core.actions._clickGameInfo_openComments(); }); +// ----- Storage +const keyStorage = new GameStorage>( + GameStorage.fromAuthor('AncTe', 'gameKey') +); +keyStorage.data = {}; +keyStorage.read(); +gameKey.on('set', (id, key, assist) => { + keyStorage.setValue(id, { key, assist }); +}); +gameKey.fromJSON(keyStorage.toJSON()); + // ----- Listening document.addEventListener('keyup', e => { const assist = generateBinary([e.ctrlKey, e.shiftKey, e.altKey]); diff --git a/src/core/main/storage.ts b/src/core/main/storage.ts index 3be1650..4cee9ab 100644 --- a/src/core/main/storage.ts +++ b/src/core/main/storage.ts @@ -47,6 +47,10 @@ export class GameStorage { } } + toJSON() { + return JSON.stringify(this.data); + } + /** * 获取本游戏的存储键 * @param key 存储名称 diff --git a/src/ui/hotkey.vue b/src/ui/hotkey.vue index 87eb85a..54926e1 100644 --- a/src/ui/hotkey.vue +++ b/src/ui/hotkey.vue @@ -175,7 +175,6 @@ function keyup(e: KeyboardEvent) { // ban other keys gameKey.disable(); -console.log(gameKey.enabled); onMounted(() => { document.addEventListener('keyup', keyup);