From 9bf27f58e3292da9194262641b6f5d6788b0a742 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Sat, 18 Nov 2023 23:03:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8C=89=E9=94=AE=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/main/custom/hotkey.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/core/main/custom/hotkey.ts b/src/core/main/custom/hotkey.ts index 6bec68b..6e90514 100644 --- a/src/core/main/custom/hotkey.ts +++ b/src/core/main/custom/hotkey.ts @@ -1,8 +1,6 @@ import { KeyCode } from '@/plugin/keyCodes'; -import { getLocFromMouseLoc } from '@/plugin/ui/fixed'; -import { deleteWith, generateBinary, has, spliceBy, tip } from '@/plugin/utils'; +import { deleteWith, spliceBy } from '@/plugin/utils'; import { EmitableEvent, EventEmitter } from '../../common/eventEmitter'; -import { GameStorage } from '../storage'; interface HotkeyEvent extends EmitableEvent {} @@ -45,6 +43,7 @@ export class Hotkey extends EventEmitter { none: [] }; enabled: boolean = false; + conditionMap: Map boolean> = new Map(); private scope: symbol = Symbol(); private scopeStack: symbol[] = []; @@ -57,7 +56,7 @@ export class Hotkey extends EventEmitter { } /** - * 注册一个按键 + * 注册一个按键,id可以包含数字后缀,可以显示为同一个按键操作拥有多个按键可以触发 * @param data 要注册的按键信息 */ register(data: RegisterHotkeyData) { @@ -84,7 +83,7 @@ export class Hotkey extends EventEmitter { /** * 实现一个按键按下时的操作 - * @param id 要实现的按键id + * @param id 要实现的按键id,可以不包含数字后缀 * @param func 按键按下时执行的函数 */ realize(id: string, func: HotkeyFunc) { @@ -113,6 +112,7 @@ export class Hotkey extends EventEmitter { spliceBy(this.scopeStack, symbol); this.scopeStack.push(symbol); this.scope = symbol; + this.conditionMap.set(symbol, () => true); for (const key of Object.values(this.data)) { key.func.set(symbol, () => {}); } @@ -162,6 +162,8 @@ export class Hotkey extends EventEmitter { ev: KeyboardEvent ) { if (!this.enabled) return; + const when = this.conditionMap.get(this.scope)!; + if (!when()) return; const toEmit = this.keyMap.get(key); if (!toEmit) return; const { ctrl, shift, alt } = this.unwarpBinary(assist); @@ -203,6 +205,15 @@ export class Hotkey extends EventEmitter { this.enabled = false; } + /** + * 在当前作用域下,满足什么条件时触发按键 + * @param fn 条件函数 + */ + when(fn: () => boolean) { + this.conditionMap.set(this.scope, fn); + return this; + } + private unwarpBinary(bin: number): AssistHoykey { return { ctrl: !!(bin & (1 << 0)),