mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-01-31 23:29:27 +08:00
feat: 按键计入本地存储
This commit is contained in:
parent
770e38202c
commit
cf741cc697
@ -1,8 +1,11 @@
|
|||||||
import { KeyCode } from '@/plugin/keyCodes';
|
import { KeyCode } from '@/plugin/keyCodes';
|
||||||
import { deleteWith, spliceBy } from '@/plugin/utils';
|
import { deleteWith, generateBinary, spliceBy } from '@/plugin/utils';
|
||||||
import { EmitableEvent, EventEmitter } from '../../common/eventEmitter';
|
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';
|
type KeyEmitType = 'down' | 'up';
|
||||||
|
|
||||||
@ -27,6 +30,11 @@ interface HotkeyData extends Required<RegisterHotkeyData> {
|
|||||||
|
|
||||||
type HotkeyFunc = (code: KeyCode, ev: KeyboardEvent) => void;
|
type HotkeyFunc = (code: KeyCode, ev: KeyboardEvent) => void;
|
||||||
|
|
||||||
|
export interface HotkeyJSON {
|
||||||
|
key: KeyCode;
|
||||||
|
assist: number;
|
||||||
|
}
|
||||||
|
|
||||||
export class Hotkey extends EventEmitter<HotkeyEvent> {
|
export class Hotkey extends EventEmitter<HotkeyEvent> {
|
||||||
static list: Hotkey[];
|
static list: Hotkey[];
|
||||||
|
|
||||||
@ -135,8 +143,9 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
|
|||||||
* @param id 要设置的按键的id
|
* @param id 要设置的按键的id
|
||||||
* @param key 要设置成的按键
|
* @param key 要设置成的按键
|
||||||
* @param assist 辅助按键,三位二进制数据,从低到高依次为`ctrl` `shift` `alt`
|
* @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 { ctrl, shift, alt } = this.unwarpBinary(assist);
|
||||||
const data = this.data[id];
|
const data = this.data[id];
|
||||||
const before = this.keyMap.get(data.key)!;
|
const before = this.keyMap.get(data.key)!;
|
||||||
@ -148,6 +157,7 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
|
|||||||
data.ctrl = ctrl;
|
data.ctrl = ctrl;
|
||||||
data.shift = shift;
|
data.shift = shift;
|
||||||
data.alt = alt;
|
data.alt = alt;
|
||||||
|
if (emit) this.emit('set', id, key, assist);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -177,6 +187,7 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
|
|||||||
func(key, ev);
|
func(key, ev);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.emit('emit', key, assist, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -214,6 +225,24 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
|
|||||||
return this;
|
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 {
|
private unwarpBinary(bin: number): AssistHoykey {
|
||||||
return {
|
return {
|
||||||
ctrl: !!(bin & (1 << 0)),
|
ctrl: !!(bin & (1 << 0)),
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import { KeyCode } from '@/plugin/keyCodes';
|
import { KeyCode } from '@/plugin/keyCodes';
|
||||||
import { Hotkey } from '../custom/hotkey';
|
import { Hotkey, HotkeyJSON } from '../custom/hotkey';
|
||||||
import { generateBinary, keycode } from '@/plugin/utils';
|
import { generateBinary, keycode } from '@/plugin/utils';
|
||||||
import { hovered } from './fixed';
|
import { hovered } from './fixed';
|
||||||
import { hasMarkedEnemy, markEnemy, unmarkEnemy } from '@/plugin/mark';
|
import { hasMarkedEnemy, markEnemy, unmarkEnemy } from '@/plugin/mark';
|
||||||
import { mainUi } from './ui';
|
import { mainUi } from './ui';
|
||||||
|
import { GameStorage } from '../storage';
|
||||||
|
|
||||||
export const mainScope = Symbol.for('@key_main');
|
export const mainScope = Symbol.for('@key_main');
|
||||||
export const gameKey = new Hotkey('gameKey', '游戏按键');
|
export const gameKey = new Hotkey('gameKey', '游戏按键');
|
||||||
@ -15,10 +16,7 @@ gameKey
|
|||||||
.register({
|
.register({
|
||||||
id: 'book',
|
id: 'book',
|
||||||
name: '怪物手册',
|
name: '怪物手册',
|
||||||
defaults: KeyCode.KeyX,
|
defaults: KeyCode.KeyX
|
||||||
ctrl: true,
|
|
||||||
shift: true,
|
|
||||||
alt: true
|
|
||||||
})
|
})
|
||||||
.register({
|
.register({
|
||||||
id: 'save',
|
id: 'save',
|
||||||
@ -422,6 +420,17 @@ gameKey
|
|||||||
core.actions._clickGameInfo_openComments();
|
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
|
// ----- Listening
|
||||||
document.addEventListener('keyup', e => {
|
document.addEventListener('keyup', e => {
|
||||||
const assist = generateBinary([e.ctrlKey, e.shiftKey, e.altKey]);
|
const assist = generateBinary([e.ctrlKey, e.shiftKey, e.altKey]);
|
||||||
|
@ -47,6 +47,10 @@ export class GameStorage<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return JSON.stringify(this.data);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取本游戏的存储键
|
* 获取本游戏的存储键
|
||||||
* @param key 存储名称
|
* @param key 存储名称
|
||||||
|
@ -175,7 +175,6 @@ function keyup(e: KeyboardEvent) {
|
|||||||
|
|
||||||
// ban other keys
|
// ban other keys
|
||||||
gameKey.disable();
|
gameKey.disable();
|
||||||
console.log(gameKey.enabled);
|
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
document.addEventListener('keyup', keyup);
|
document.addEventListener('keyup', keyup);
|
||||||
|
Loading…
Reference in New Issue
Block a user