feat: 按键计入本地存储

This commit is contained in:
unanmed 2023-11-20 12:42:55 +08:00
parent 770e38202c
commit cf741cc697
4 changed files with 50 additions and 9 deletions

View File

@ -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<RegisterHotkeyData> {
type HotkeyFunc = (code: KeyCode, ev: KeyboardEvent) => void;
export interface HotkeyJSON {
key: KeyCode;
assist: number;
}
export class Hotkey extends EventEmitter<HotkeyEvent> {
static list: Hotkey[];
@ -135,8 +143,9 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
* @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<HotkeyEvent> {
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<HotkeyEvent> {
func(key, ev);
}
});
this.emit('emit', key, assist, type);
}
/**
@ -214,6 +225,24 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
return this;
}
toJSON() {
const res: Record<string, HotkeyJSON> = {};
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<string, HotkeyJSON> = 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)),

View File

@ -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<Record<string, HotkeyJSON>>(
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]);

View File

@ -47,6 +47,10 @@ export class GameStorage<T> {
}
}
toJSON() {
return JSON.stringify(this.data);
}
/**
*
* @param key

View File

@ -175,7 +175,6 @@ function keyup(e: KeyboardEvent) {
// ban other keys
gameKey.disable();
console.log(gameKey.enabled);
onMounted(() => {
document.addEventListener('keyup', keyup);