From 72d74ec7a6c064a4ca74a4314452f606c1eade93 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Mon, 18 Dec 2023 17:10:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E4=B9=89=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=A0=8F=E7=9A=84=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/index.ts | 1 + src/core/main/custom/toolbar.ts | 49 ++++++++++++++++++++++++++----- src/core/main/init/toolbar.tsx | 29 +++++++++---------- src/plugin/utils.ts | 2 +- src/ui/toolbar.vue | 51 ++++++++++++++++++++++++++++++++- 5 files changed, 108 insertions(+), 24 deletions(-) diff --git a/src/core/index.ts b/src/core/index.ts index 07f35d7..607dd9c 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -9,6 +9,7 @@ import { fixedUi, mainUi } from './main/init/ui'; import { GameStorage } from './main/storage'; import { resolvePlugin } from './plugin'; import './main/init/'; +import './main/custom/toolbar'; interface AncTePlugin { pop: ReturnType; diff --git a/src/core/main/custom/toolbar.ts b/src/core/main/custom/toolbar.ts index be075d4..dbfca58 100644 --- a/src/core/main/custom/toolbar.ts +++ b/src/core/main/custom/toolbar.ts @@ -1,10 +1,12 @@ import { EmitableEvent, EventEmitter } from '@/core/common/eventEmitter'; import { KeyCode } from '@/plugin/keyCodes'; import { flipBinary, has } from '@/plugin/utils'; -import { FunctionalComponent, reactive } from 'vue'; +import { FunctionalComponent, nextTick, reactive } from 'vue'; import { createToolbarComponents } from '../init/toolbar'; import { gameKey } from '../init/hotkey'; import { unwarpBinary } from './hotkey'; +import { fixedUi } from '../init/ui'; +import { hook } from '../game'; interface CustomToolbarEvent extends EmitableEvent { add: (item: ValueOf) => void; @@ -77,13 +79,14 @@ export class CustomToolbar extends EventEmitter { x: number = 300; y: number = 300; width: number = 300; - height: number = 100; + height: number = 70; // ----- other assistKey: number = 0; constructor(id: string) { super(); this.id = id; + this.show(); CustomToolbar.list.push(this); } @@ -134,13 +137,12 @@ export class CustomToolbar extends EventEmitter { */ emitTool(id: string) { const item = this.items.find(v => v.id === id); - if (!item) return; + if (!item) return this; this.emit(id); if (item.type === 'hotkey') { // 按键 - const { ctrl, shift, alt } = unwarpBinary( - item.assist | this.assistKey - ); + const assist = item.assist | this.assistKey; + const { ctrl, shift, alt } = unwarpBinary(assist); const ev = new KeyboardEvent('keyup', { ctrlKey: ctrl, shiftKey: shift, @@ -148,7 +150,7 @@ export class CustomToolbar extends EventEmitter { }); // todo: Advanced KeyboardEvent simulate - gameKey.emitKey(item.key, item.assist, 'up', ev); + gameKey.emitKey(item.key, assist, 'up', ev); } else if (item.type === 'item') { // 道具 core.tryUseItem(item.item); @@ -162,6 +164,15 @@ export class CustomToolbar extends EventEmitter { this.assistKey = flipBinary(this.assistKey, 2); } } + return this; + } + + refresh() { + const items = this.items.splice(0); + nextTick(() => { + this.items.push(...items); + }); + // this.items.push(...this.items.splice(0)); } setPos(x?: number, y?: number) { @@ -174,7 +185,31 @@ export class CustomToolbar extends EventEmitter { has(height) && (this.height = height); } + show() { + fixedUi.open('toolbar', { bar: this }); + } + static get(id: string) { return this.list.find(v => v.id === id); } } + +hook.once('reset', () => { + const toolbar = new CustomToolbar('test'); + toolbar.add<'hotkey'>({ + id: 'test1', + type: 'hotkey', + assist: 0, + key: KeyCode.KeyX + }); + toolbar.add<'assistKey'>({ + id: 'test2', + type: 'assistKey', + assist: KeyCode.Ctrl + }); + toolbar.add<'item'>({ + id: 'test3', + type: 'item', + item: 'book' + }); +}); diff --git a/src/core/main/init/toolbar.tsx b/src/core/main/init/toolbar.tsx index 5b6dc83..66165a9 100644 --- a/src/core/main/init/toolbar.tsx +++ b/src/core/main/init/toolbar.tsx @@ -4,7 +4,6 @@ import type { CustomToolbarProps } from '../custom/toolbar'; import BoxAnimate from '@/components/boxAnimate.vue'; -import { onUnmounted, ref } from 'vue'; import { checkAssist } from '../custom/hotkey'; interface Components { @@ -31,7 +30,7 @@ function DefaultTool(props: CustomToolbarProps) { function KeyTool(props: CustomToolbarProps<'hotkey'>) { const { item, toolbar } = props; return ( - toolbar.emitTool(item.id)}> + toolbar.emitTool(item.id)}> {KeyCodeUtils.toString(item.key)} ); @@ -40,30 +39,30 @@ function KeyTool(props: CustomToolbarProps<'hotkey'>) { function ItemTool(props: CustomToolbarProps<'item'>) { const { item, toolbar } = props; return ( -
toolbar.emitTool(item.id)}> - +
toolbar.emitTool(item.id)} + > +
); } function AssistKeyTool(props: CustomToolbarProps<'assistKey'>) { const { item, toolbar } = props; - const pressed = ref(checkAssist(toolbar.assistKey, item.assist)); - const listen = () => { - pressed.value = checkAssist(toolbar.assistKey, item.assist); - }; - toolbar.on('emit', listen); - - onUnmounted(() => { - toolbar.off('emit', listen); - }); + const pressed = checkAssist(toolbar.assistKey, item.assist); return ( toolbar.emitTool(item.id)} + active={pressed} + onClick={() => toolbar.emitTool(item.id).refresh()} > {KeyCodeUtils.toString(item.assist)} diff --git a/src/plugin/utils.ts b/src/plugin/utils.ts index f3c13f4..76ad37f 100644 --- a/src/plugin/utils.ts +++ b/src/plugin/utils.ts @@ -336,6 +336,6 @@ export function getStatusLabel(name: string) { export function flipBinary(num: number, col: number) { const n = 1 << col; - if (num & col) return num & ~n; + if (num & n) return num & ~n; else return num | n; } diff --git a/src/ui/toolbar.vue b/src/ui/toolbar.vue index 1e59e33..a3a6691 100644 --- a/src/ui/toolbar.vue +++ b/src/ui/toolbar.vue @@ -1,5 +1,6 @@