feat: 自定义状态栏的显示

This commit is contained in:
AncTe committed 2023-12-18 17:10:28 +08:00
1 parent 56ff2098f7
commit 72d74ec7a6
5 files changed
+108 -24

No files matched your search

+1
View File
@@ -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<typeof import('../plugin/pop').default>;
+42 -7
View File
@@ -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<ToolbarItemMap>) => void;
@@ -77,13 +79,14 @@ export class CustomToolbar extends EventEmitter<CustomToolbarEvent> {
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<CustomToolbarEvent> {
*/
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<CustomToolbarEvent> {
});
// 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<CustomToolbarEvent> {
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<CustomToolbarEvent> {
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'
});
});
+14 -15
View File
@@ -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 (
<span onClick={() => toolbar.emitTool(item.id)}>
<span class="button-text" onClick={() => toolbar.emitTool(item.id)}>
{KeyCodeUtils.toString(item.key)}
</span>
);
@@ -40,30 +39,30 @@ function KeyTool(props: CustomToolbarProps<'hotkey'>) {
function ItemTool(props: CustomToolbarProps<'item'>) {
const { item, toolbar } = props;
return (
<div onClick={() => toolbar.emitTool(item.id)}>
<BoxAnimate id={item.item}></BoxAnimate>
<div
style="display: flex; justify-content: center; width: 50px"
onClick={() => toolbar.emitTool(item.id)}
>
<BoxAnimate
noborder={true}
width={50}
height={50}
id={item.item}
></BoxAnimate>
</div>
);
}
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 (
<span
class="button-text"
// @ts-ignore
active={pressed.value}
onClick={() => toolbar.emitTool(item.id)}
active={pressed}
onClick={() => toolbar.emitTool(item.id).refresh()}
>
{KeyCodeUtils.toString(item.assist)}
</span>