mirror of
https://github.com/unanmed/HumanBreak.git
synced 2026-11-29 05:24:30 +08:00
feat: 自定义状态栏的编辑
This commit is contained in:
1 parent
1e341fb63e
commit
ad2cb0b757
8 files changed
+714
-94
No files matched your search
@@ -93,8 +93,8 @@ export class Keyboard extends EventEmitter<VirtualKeyboardEvent> {
|
||||
* @param assist 辅助按键
|
||||
*/
|
||||
withAssist(assist: number) {
|
||||
this.assist = assist;
|
||||
const symbol = this.createScope();
|
||||
this.assist = assist;
|
||||
return symbol;
|
||||
}
|
||||
|
||||
|
||||
+164
-45
@@ -1,6 +1,6 @@
|
||||
import { EmitableEvent, EventEmitter } from '@/core/common/eventEmitter';
|
||||
import { KeyCode } from '@/plugin/keyCodes';
|
||||
import { flipBinary, has } from '@/plugin/utils';
|
||||
import { deleteWith, flipBinary, has } from '@/plugin/utils';
|
||||
import {
|
||||
FunctionalComponent,
|
||||
markRaw,
|
||||
@@ -8,7 +8,10 @@ import {
|
||||
reactive,
|
||||
shallowReactive
|
||||
} from 'vue';
|
||||
import { createToolbarComponents } from '../init/toolbar';
|
||||
import {
|
||||
createToolbarComponents,
|
||||
createToolbarEditorComponents
|
||||
} from '../init/toolbar';
|
||||
import { gameKey } from '../init/hotkey';
|
||||
import { unwarpBinary } from './hotkey';
|
||||
import { fixedUi } from '../init/ui';
|
||||
@@ -24,7 +27,6 @@ interface CustomToolbarEvent extends EmitableEvent {
|
||||
interface ToolbarItemBase<T extends ToolbarItemType> {
|
||||
type: T;
|
||||
id: string;
|
||||
com: CustomToolbarComponent<T>;
|
||||
}
|
||||
|
||||
// 快捷键
|
||||
@@ -49,7 +51,7 @@ interface ToolbarItemMap {
|
||||
assistKey: AssistKeyToolbarItem;
|
||||
}
|
||||
|
||||
type ToolbarItemType = keyof ToolbarItemMap;
|
||||
export type ToolbarItemType = keyof ToolbarItemMap;
|
||||
|
||||
export type SettableItemData<T extends ToolbarItemType = ToolbarItemType> =
|
||||
Omit<ToolbarItemMap[T], 'id' | 'type'>;
|
||||
@@ -64,19 +66,27 @@ export type CustomToolbarComponent<
|
||||
T extends ToolbarItemType = ToolbarItemType
|
||||
> = FunctionalComponent<CustomToolbarProps<T>>;
|
||||
|
||||
const COM = createToolbarComponents();
|
||||
type ToolItemEmitFn<T extends ToolbarItemType> = (
|
||||
this: CustomToolbar,
|
||||
id: string,
|
||||
item: ToolbarItemMap[T]
|
||||
) => boolean;
|
||||
|
||||
const comMap: {
|
||||
[P in ToolbarItemType]: CustomToolbarComponent<P>;
|
||||
} = {
|
||||
hotkey: COM.KeyTool,
|
||||
item: COM.ItemTool,
|
||||
assistKey: COM.AssistKeyTool
|
||||
};
|
||||
interface RegisteredCustomToolInfo {
|
||||
name: string;
|
||||
onEmit: ToolItemEmitFn<ToolbarItemType>;
|
||||
show: CustomToolbarComponent;
|
||||
editor: CustomToolbarComponent;
|
||||
onCreate: (item: any) => ToolbarItemBase<ToolbarItemType>;
|
||||
}
|
||||
|
||||
const COM = createToolbarComponents();
|
||||
const EDITOR = createToolbarEditorComponents();
|
||||
|
||||
export class CustomToolbar extends EventEmitter<CustomToolbarEvent> {
|
||||
static num: number = 0;
|
||||
static list: CustomToolbar[] = shallowReactive([]);
|
||||
static info: Record<string, RegisteredCustomToolInfo> = {};
|
||||
|
||||
items: ValueOf<ToolbarItemMap>[] = reactive([]);
|
||||
num: number = CustomToolbar.num++;
|
||||
@@ -88,6 +98,7 @@ export class CustomToolbar extends EventEmitter<CustomToolbarEvent> {
|
||||
height: number = 70;
|
||||
// ----- other
|
||||
assistKey: number = 0;
|
||||
showIds: number[] = [];
|
||||
|
||||
constructor(id: string) {
|
||||
super();
|
||||
@@ -100,14 +111,15 @@ export class CustomToolbar extends EventEmitter<CustomToolbarEvent> {
|
||||
* 添加一个自定义项
|
||||
* @param item 要添加的自定义工具栏项
|
||||
*/
|
||||
add<K extends ToolbarItemType>(item: Omit<ToolbarItemMap[K], 'com'>) {
|
||||
// @ts-ignore
|
||||
const data: ToolbarItemMap[K] = {
|
||||
com: markRaw(comMap[item.type]),
|
||||
...item
|
||||
} as ToolbarItemMap[K];
|
||||
this.items.push(data);
|
||||
this.emit('add', data);
|
||||
add<K extends ToolbarItemType>(item: ToolbarItemMap[K]) {
|
||||
const index = this.items.findIndex(v => v.id === item.id);
|
||||
if (index !== -1) {
|
||||
console.warn(`添加了id重复的自定义工具,已将其覆盖`);
|
||||
this.items[index] = item;
|
||||
} else {
|
||||
this.items.push(item);
|
||||
}
|
||||
this.emit('add', item);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -148,34 +160,21 @@ export class CustomToolbar extends EventEmitter<CustomToolbarEvent> {
|
||||
const item = this.items.find(v => v.id === id);
|
||||
if (!item) return this;
|
||||
this.emit('emit', id, item);
|
||||
if (item.type === 'hotkey') {
|
||||
// 按键
|
||||
const assist = item.assist | this.assistKey;
|
||||
const { ctrl, shift, alt } = unwarpBinary(assist);
|
||||
const ev = new KeyboardEvent('keyup', {
|
||||
ctrlKey: ctrl,
|
||||
shiftKey: shift,
|
||||
altKey: alt
|
||||
});
|
||||
|
||||
// todo: Advanced KeyboardEvent simulate
|
||||
gameKey.emitKey(item.key, assist, 'up', ev);
|
||||
} else if (item.type === 'item') {
|
||||
// 道具
|
||||
core.tryUseItem(item.item);
|
||||
} else if (item.type === 'assistKey') {
|
||||
// 辅助按键
|
||||
if (item.assist === KeyCode.Ctrl) {
|
||||
this.assistKey = flipBinary(this.assistKey, 0);
|
||||
} else if (item.assist === KeyCode.Shift) {
|
||||
this.assistKey = flipBinary(this.assistKey, 1);
|
||||
} else if (item.assist === KeyCode.Alt) {
|
||||
this.assistKey = flipBinary(this.assistKey, 2);
|
||||
}
|
||||
const info = CustomToolbar.info[item.type];
|
||||
if (!info) {
|
||||
console.warn(`触发了未知的自定义工具类型:'${item.type}'`);
|
||||
return this;
|
||||
}
|
||||
const success = info.onEmit.call(this, id, item);
|
||||
if (!success) {
|
||||
console.warn(`触发自定义工具失败,id:'${id}',type:${item.type}`);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制刷新这个自定义工具栏的所有显示
|
||||
*/
|
||||
refresh() {
|
||||
const items = this.items.splice(0);
|
||||
nextTick(() => {
|
||||
@@ -194,15 +193,135 @@ export class CustomToolbar extends EventEmitter<CustomToolbarEvent> {
|
||||
has(height) && (this.height = height);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示这个自定义工具栏,可以显示多个,且内容互通
|
||||
*/
|
||||
show() {
|
||||
fixedUi.open('toolbar', { bar: this });
|
||||
const id = fixedUi.open('toolbar', { bar: this });
|
||||
this.showIds.push(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭一个以此实例为基础显示的自定义工具栏
|
||||
* @param id 要关闭的id
|
||||
*/
|
||||
close(id: number) {
|
||||
fixedUi.close(id);
|
||||
deleteWith(this.showIds, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭这个自定义工具栏的所有显示
|
||||
*/
|
||||
closeAll() {
|
||||
this.showIds.forEach(v => fixedUi.close(v));
|
||||
}
|
||||
|
||||
static get(id: string) {
|
||||
return this.list.find(v => v.id === id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册一类自定义工具
|
||||
* @param type 要注册的自定义工具类型
|
||||
* @param name 该类型的中文名
|
||||
* @param onEmit 当触发这个自定义工具的时候执行的函数
|
||||
* @param show 这个自定义工具在自定义工具栏的显示组件
|
||||
* @param editor 这个自定义工具在编辑时编辑组件
|
||||
* @param onCreate 当这个自定义工具在编辑器中被添加时,执行的初始化脚本
|
||||
*/
|
||||
static register<K extends ToolbarItemType>(
|
||||
type: K,
|
||||
name: string,
|
||||
onEmit: ToolItemEmitFn<K>,
|
||||
show: CustomToolbarComponent<K>,
|
||||
editor: CustomToolbarComponent<K>,
|
||||
onCreate: (item: any) => ToolbarItemMap[K]
|
||||
) {
|
||||
if (type in this.info) {
|
||||
console.warn(`已存在名为'${type}'的自定义工具类型,已将其覆盖!`);
|
||||
}
|
||||
const info: RegisteredCustomToolInfo = {
|
||||
name,
|
||||
onEmit: onEmit as ToolItemEmitFn<ToolbarItemType>,
|
||||
show: show as CustomToolbarComponent,
|
||||
editor: editor as CustomToolbarComponent,
|
||||
// @ts-ignore
|
||||
onCreate
|
||||
};
|
||||
this.info[type] = info;
|
||||
}
|
||||
}
|
||||
|
||||
CustomToolbar.register(
|
||||
'hotkey',
|
||||
'快捷键',
|
||||
function (id, item) {
|
||||
// 按键
|
||||
const assist = item.assist | this.assistKey;
|
||||
const { ctrl, shift, alt } = unwarpBinary(assist);
|
||||
const ev = new KeyboardEvent('keyup', {
|
||||
ctrlKey: ctrl,
|
||||
shiftKey: shift,
|
||||
altKey: alt
|
||||
});
|
||||
|
||||
// todo: Advanced KeyboardEvent simulate
|
||||
gameKey.emitKey(item.key, assist, 'up', ev);
|
||||
return true;
|
||||
},
|
||||
COM.KeyTool,
|
||||
EDITOR.KeyTool,
|
||||
item => {
|
||||
return {
|
||||
key: KeyCode.Unknown,
|
||||
assist: 0,
|
||||
...item
|
||||
};
|
||||
}
|
||||
);
|
||||
CustomToolbar.register(
|
||||
'item',
|
||||
'使用道具',
|
||||
function (id, item) {
|
||||
// 道具
|
||||
core.tryUseItem(item.item);
|
||||
return true;
|
||||
},
|
||||
COM.ItemTool,
|
||||
EDITOR.ItemTool,
|
||||
item => {
|
||||
return {
|
||||
item: 'book',
|
||||
...item
|
||||
};
|
||||
}
|
||||
);
|
||||
CustomToolbar.register(
|
||||
'assistKey',
|
||||
'辅助按键',
|
||||
function (id, item) {
|
||||
// 辅助按键
|
||||
if (item.assist === KeyCode.Ctrl) {
|
||||
this.assistKey = flipBinary(this.assistKey, 0);
|
||||
} else if (item.assist === KeyCode.Shift) {
|
||||
this.assistKey = flipBinary(this.assistKey, 1);
|
||||
} else if (item.assist === KeyCode.Alt) {
|
||||
this.assistKey = flipBinary(this.assistKey, 2);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
COM.AssistKeyTool,
|
||||
EDITOR.AssistKeyTool,
|
||||
item => {
|
||||
return {
|
||||
assist: KeyCode.Ctrl,
|
||||
...item
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
hook.once('reset', () => {
|
||||
const toolbar = new CustomToolbar('test');
|
||||
toolbar
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { KeyCodeUtils } from '@/plugin/keyCodes';
|
||||
import { KeyCode, KeyCodeUtils } from '@/plugin/keyCodes';
|
||||
import type {
|
||||
CustomToolbarComponent,
|
||||
CustomToolbarProps
|
||||
} from '../custom/toolbar';
|
||||
import BoxAnimate from '@/components/boxAnimate.vue';
|
||||
import { checkAssist } from '../custom/hotkey';
|
||||
import { getVitualKeyOnce } from '@/plugin/utils';
|
||||
import { cloneDeep } from 'lodash-es';
|
||||
import { Select, SelectOption } from 'ant-design-vue';
|
||||
|
||||
// todo: 新增更改设置的ToolItem
|
||||
|
||||
@@ -25,6 +28,16 @@ export function createToolbarComponents() {
|
||||
return com;
|
||||
}
|
||||
|
||||
export function createToolbarEditorComponents() {
|
||||
const com: Components = {
|
||||
DefaultTool: DefaultToolEditor,
|
||||
KeyTool: KeyToolEdtior,
|
||||
ItemTool: ItemToolEditor,
|
||||
AssistKeyTool: AssistKeyToolEditor
|
||||
};
|
||||
return com;
|
||||
}
|
||||
|
||||
function DefaultTool(props: CustomToolbarProps) {
|
||||
return <span>未知工具</span>;
|
||||
}
|
||||
@@ -70,3 +83,123 @@ function AssistKeyTool(props: CustomToolbarProps<'assistKey'>) {
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
function DefaultToolEditor(props: CustomToolbarProps) {
|
||||
return <span></span>;
|
||||
}
|
||||
|
||||
function KeyToolEdtior(props: CustomToolbarProps<'hotkey'>) {
|
||||
const { item, toolbar } = props;
|
||||
|
||||
const getKey = async () => {
|
||||
const { key, assist } = await getVitualKeyOnce(false, item.assist);
|
||||
toolbar.set<'hotkey'>(item.id, {
|
||||
key,
|
||||
assist
|
||||
});
|
||||
};
|
||||
|
||||
const unwarpAssist = (assist: number) => {
|
||||
let res = '';
|
||||
if (assist & (1 << 0)) {
|
||||
res += 'Ctrl + ';
|
||||
}
|
||||
if (assist & (1 << 1)) {
|
||||
res += 'Shift + ';
|
||||
}
|
||||
if (assist & (1 << 2)) {
|
||||
res += 'Alt + ';
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
const getKeyShow = (key: KeyCode, assist: number) => {
|
||||
return (
|
||||
unwarpAssist(assist) +
|
||||
(key === KeyCode.Unknown ? '' : KeyCodeUtils.toString(key))
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
style="
|
||||
display: flex; flex-direction: row; justify-content: space-between;
|
||||
align-items: center; padding: 0 5%; margin: 1%
|
||||
"
|
||||
>
|
||||
<span>触发按键</span>
|
||||
<span
|
||||
style="background-color: #000; width: 50%; text-align: end; padding: 0 5%"
|
||||
onClick={getKey}
|
||||
>
|
||||
{getKeyShow(item.key, item.assist)}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ItemToolEditor(props: CustomToolbarProps<'item'>) {
|
||||
const { item, toolbar } = props;
|
||||
|
||||
const items = cloneDeep(core.status.hero.items.constants);
|
||||
Object.assign(items, core.status.hero.items.tools);
|
||||
|
||||
return (
|
||||
<div
|
||||
style="
|
||||
display: flex; flex-direction: row; justify-content: space-between;
|
||||
align-items: center; padding: 0 5%; margin: 1%
|
||||
"
|
||||
>
|
||||
<span>使用道具</span>
|
||||
<Select
|
||||
style="width: 180px; font-size: 80%; height: 100%; background-color: #000"
|
||||
value={item.item}
|
||||
onChange={value =>
|
||||
toolbar.set<'item'>(item.id, {
|
||||
item: value as ItemIdOf<'tools' | 'constants'>
|
||||
})
|
||||
}
|
||||
>
|
||||
{Object.entries(items).map(v => {
|
||||
return (
|
||||
<SelectOption value={v[0]}>
|
||||
{
|
||||
core.material.items[v[0] as AllIdsOf<'items'>]
|
||||
.name
|
||||
}
|
||||
</SelectOption>
|
||||
);
|
||||
})}
|
||||
</Select>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function AssistKeyToolEditor(props: CustomToolbarProps<'assistKey'>) {
|
||||
const { item, toolbar } = props;
|
||||
|
||||
return (
|
||||
<div
|
||||
style="
|
||||
display: flex; flex-direction: row; justify-content: space-between;
|
||||
align-items: center; padding: 0 5%; margin: 1%
|
||||
"
|
||||
>
|
||||
<span>辅助按键</span>
|
||||
<Select
|
||||
style="width: 180px; font-size: 80%; height: 100%; background-color: #000"
|
||||
value={item.assist}
|
||||
onChange={value =>
|
||||
toolbar.set<'assistKey'>(item.id, {
|
||||
assist: value as KeyCode.Ctrl
|
||||
})
|
||||
}
|
||||
>
|
||||
<SelectOption value={KeyCode.Ctrl}>Ctrl</SelectOption>
|
||||
<SelectOption value={KeyCode.Shift}>Shift</SelectOption>
|
||||
<SelectOption value={KeyCode.Alt}>Alt</SelectOption>
|
||||
</Select>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user