422 lines
13 KiB
Vue
Raw Normal View History

<template>
<div class="left">
2024-12-28 14:25:23 +08:00
<CCDock name="Hierarchy">
2025-01-23 12:27:27 +08:00
<template v-slot:tab-name-before> </template>
2025-01-12 17:57:02 +08:00
<template v-slot:title>
<Refresh @click="onClickRefresh" :type="rotateType"></Refresh>
2025-01-12 17:57:02 +08:00
<div class="engine-version" v-if="engineVersion">Cocos Creator V{{ engineVersion }}</div>
</template>
2024-12-28 14:25:23 +08:00
<CCInput style="flex: none" placeholder="enter keywords to filter" :data="filterText" v-if="false">
<slot>
<i class="matchCase iconfont icon_font_size" @click.stop="onChangeCase" title="match case" :style="{ color: matchCase ? 'red' : '' }"></i>
</slot>
</CCInput>
2025-02-01 22:22:39 +08:00
<CCTree @do-search="doSearch" :search="true" @node-menu="onMenu" @contextmenu.prevent.stop="onMenu" style="flex: 1" ref="elTree" :expand-keys="expandedKeys" :default-expand-all="false" :value="treeData" @node-expand="onNodeExpand" @node-collapse="onNodeCollapse" @node-click="handleNodeClick" @node-unclick="handleNodeUnclick" @node-enter="handleNodeEnter" @node-leave="handleNodeLeave"></CCTree>
2024-12-28 14:25:23 +08:00
</CCDock>
</div>
</template>
<script lang="ts">
import ccui from "@xuyanfeng/cc-ui";
2024-12-28 16:24:16 +08:00
import { IUiMenuItem } from "@xuyanfeng/cc-ui/types/cc-menu/const";
import { HandExpandOptions } from "@xuyanfeng/cc-ui/types/cc-tree/const";
2025-01-06 19:46:38 +08:00
import Mousetrap, { MousetrapInstance } from "mousetrap";
import { storeToRefs } from "pinia";
2024-12-28 14:25:23 +08:00
import { defineComponent, nextTick, onMounted, onUnmounted, ref, toRaw, watch } from "vue";
2025-01-12 17:57:02 +08:00
import { Msg, PluginEvent, RequestTreeInfoData, RequestUseFrameData, ResponseSetPropertyData, ResponseSupportData } from "../../core/types";
2025-01-10 15:22:32 +08:00
import { ga } from "../../ga";
import { GA_EventName } from "../../ga/type";
import { bridge } from "./bridge";
import { Bus, BusMsg } from "./bus";
import { RotateType } from "./const";
import { EngineData, TreeData } from "./data";
2025-01-14 17:44:26 +08:00
import GameInfo from "./game-info.vue";
import Refresh from "./refresh.vue";
2024-12-28 14:25:23 +08:00
import { appStore } from "./store";
import { Timer } from "./timer";
const { CCTree, CCFootBar, CCDock, CCDialog, CCInput, CCButton, CCInputNumber, CCSelect, CCButtonGroup, CCCheckBox, CCColor, CCDivider } = ccui.components;
export default defineComponent({
name: "hierarchy",
components: { Refresh, CCButtonGroup, CCInput, CCTree, CCDock },
setup() {
2024-12-28 15:05:05 +08:00
const funcShowPlace = (data: EngineData) => {
console.log(data);
_expand(data.engineNode);
2024-12-28 15:05:05 +08:00
};
const funcEnableSchedule = (b: boolean) => {
2024-12-28 14:25:23 +08:00
if (b) {
timer.create();
2024-12-28 14:25:23 +08:00
} else {
timer.clean();
2024-12-28 14:25:23 +08:00
}
2024-12-28 15:05:05 +08:00
};
const timer: Timer = new Timer();
timer.onWork = () => {
rotateType.value = RotateType.Loop;
2025-01-17 10:18:26 +08:00
config.value.refreshHirarchy = true;
appStore().save();
2024-12-28 14:25:23 +08:00
updateTree();
};
timer.onClean = () => {
rotateType.value = RotateType.None;
2025-01-17 10:18:26 +08:00
config.value.refreshHirarchy = false;
appStore().save();
};
timer.name = "hierarchy";
2025-01-06 19:46:38 +08:00
let ins: MousetrapInstance | null = null;
function onQuickVisible() {
2025-01-10 15:22:32 +08:00
ga.fireEvent(GA_EventName.SpaceVisible);
2025-01-06 19:46:38 +08:00
console.log("onQuickVisible");
if (selectedUUID) {
bridge.send(Msg.RequestVisible, selectedUUID);
}
}
function changeContent(data: RequestUseFrameData) {
treeData.value = [];
selectedUUID = null;
}
function onInspectNode(data: PluginEvent) {
const uuid = data.data as string;
if (!uuid) {
return;
}
updateSelect(uuid);
nextTick(() => {
if (elTree.value) {
elTree.value.handExpand(uuid, { highlight: true, select: true, scroll: true } as HandExpandOptions);
}
});
}
2024-12-28 14:25:23 +08:00
onMounted(() => {
2025-01-06 19:46:38 +08:00
if (elTree.value) {
const el = toRaw(elTree.value);
ins = new Mousetrap(el.treeElement);
ins.bind(["space"], onQuickVisible, "keydown");
}
Bus.on(BusMsg.ChangeContent, changeContent);
2024-12-28 15:05:05 +08:00
Bus.on(BusMsg.ShowPlace, funcShowPlace);
Bus.on(BusMsg.EnableSchedule, funcEnableSchedule);
bridge.on(Msg.InspectNode, onInspectNode);
2025-01-17 10:18:26 +08:00
if (config.value.refreshHirarchy) {
timer.create(true);
} else {
updateTree();
}
2024-12-28 14:25:23 +08:00
});
onUnmounted(() => {
2025-01-06 19:46:38 +08:00
if (ins) {
ins.unbind(["space"], "keydown");
}
Bus.off(BusMsg.ChangeContent, changeContent);
2024-12-28 15:05:05 +08:00
Bus.off(BusMsg.ShowPlace, funcShowPlace);
Bus.off(BusMsg.EnableSchedule, funcEnableSchedule);
bridge.off(Msg.InspectNode, onInspectNode);
timer.clean();
});
function _expand(uuid: string) {
if (elTree.value) {
elTree.value.handExpand(uuid, { highlight: true });
}
}
2024-12-28 14:25:23 +08:00
function updateTree() {
console.log("update tree info");
const id = toRaw(frameID.value);
bridge.send(Msg.RequstTreeInfo, { frameID: id } as RequestTreeInfoData);
}
2024-12-28 14:25:23 +08:00
function updateFilterText(val: any) {
(elTree.value as any)?.filter(val);
}
const filterText = ref<string>("");
watch(filterText, (val) => {
// TODO: 过滤树
updateFilterText(val);
});
const { config, frameID } = storeToRefs(appStore());
const matchCase = ref<boolean>(false);
2025-01-06 19:46:38 +08:00
const elTree = ref<typeof CCTree>(null);
const treeData = ref<TreeData[]>([]);
let selectedUUID: string | null = null;
2025-01-12 17:57:02 +08:00
const engineVersion = ref("");
bridge.on(Msg.ResponseSupport, (event: PluginEvent) => {
let data: ResponseSupportData = event.data;
const isCocosGame: boolean = data.support;
if (isCocosGame) {
engineVersion.value = data.version;
} else {
engineVersion.value = "";
}
});
2024-12-28 14:25:23 +08:00
bridge.on(Msg.ResponseTreeInfo, (event: PluginEvent) => {
let data: Array<TreeData> = event.data;
if (!Array.isArray(data)) {
data = [data];
}
treeData.value = data;
2024-12-28 14:25:23 +08:00
nextTick(() => {
if (elTree.value) {
elTree.value.handChoose(selectedUUID);
}
});
});
2024-12-28 14:25:23 +08:00
bridge.on(Msg.ResponseSetProperty, (event: PluginEvent) => {
let data: ResponseSetPropertyData = event.data;
const uuid = data.path[0];
const key = data.path[1];
const value = data.data;
let treeArray: Array<TreeData> = [];
function circle(array: Array<TreeData>) {
array.forEach((item) => {
treeArray.push(item);
circle(item.children);
});
}
// 更新指定uuid节点的tree的name
circle(treeData.value);
let ret = treeArray.find((el) => el.id === uuid);
if (ret) {
if (key === "name") {
ret.text = value;
}
if (key === "active") {
ret.active = !!value;
}
}
});
const expandedKeys = ref<Array<string>>([]);
2024-12-28 15:05:05 +08:00
function updateSelect(uuid: string | null) {
selectedUUID = uuid;
Bus.emit(BusMsg.SelectNode, uuid);
if (config.value.clickInspect) {
bridge.send(Msg.SelectNode, uuid);
}
2024-12-28 15:05:05 +08:00
}
const rotateType = ref<RotateType>(RotateType.None);
if (config.value.refreshHirarchy) {
rotateType.value = RotateType.Loop;
}
2025-02-01 22:22:39 +08:00
let preSearch = "";
return {
2025-02-01 22:22:39 +08:00
doSearch(v: string) {
if (v && preSearch !== v) {
ga.fireEventWithParam(GA_EventName.TreeSearch, v);
preSearch = v;
}
},
onClickRefresh() {
updateTree();
},
rotateType,
2025-01-12 17:57:02 +08:00
engineVersion,
expandedKeys,
elTree,
filterText,
treeData,
matchCase,
frameID,
2024-12-28 14:25:23 +08:00
handleNodeUnclick() {
2024-12-28 15:05:05 +08:00
updateSelect(null);
2024-12-28 14:25:23 +08:00
},
handleNodeEnter(data: TreeData | null) {
if (!config.value.hoverInspect) {
return;
}
if (data) {
bridge.send(Msg.HoverNode, data.id);
}
},
handleNodeLeave(data: TreeData | null) {
if (data) {
bridge.send(Msg.HoverNode, null);
}
},
handleNodeClick(data: TreeData | null) {
if (data) {
2024-12-28 15:05:05 +08:00
updateSelect(data.id);
} else {
2024-12-28 15:05:05 +08:00
updateSelect(null);
}
},
2025-02-01 22:22:39 +08:00
onNodeExpand(data: TreeData) {
if (data.id) {
expandedKeys.value.push(data.id);
2025-01-10 15:22:32 +08:00
ga.fireEventWithParam(GA_EventName.Hierarchy, "node expand");
}
},
onNodeCollapse(data: TreeData) {
if (data.id) {
2025-01-10 15:22:32 +08:00
ga.fireEventWithParam(GA_EventName.Hierarchy, "node collapse");
const keys = toRaw(expandedKeys.value);
const index = keys.findIndex((el) => el === data.id);
if (index !== -1) {
keys.splice(index, 1);
}
expandedKeys.value = keys;
}
},
// TODO: 暂时这个版本先不实现
filterNode(value: any, data: any) {
if (!value) {
return true;
} else {
if (matchCase) {
// 严格匹配大写
return data?.name?.indexOf(value) !== -1;
} else {
return data?.name?.toLowerCase().indexOf(value.toLowerCase()) !== -1;
}
}
},
2025-01-25 19:36:11 +08:00
onMenu(event: MouseEvent, data: TreeData) {
2024-12-28 16:24:16 +08:00
const menus: IUiMenuItem[] = [];
menus.push({
name: "update hierarchy",
enabled: true,
callback: (item) => {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
2024-12-28 16:24:16 +08:00
updateTree();
},
});
menus.push({ type: ccui.menu.MenuType.Separator });
2025-01-14 18:33:28 +08:00
menus.push({
name: "fresh auto",
callback: (item) => {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
2025-01-24 17:42:26 +08:00
timer.create(true);
2025-01-14 18:33:28 +08:00
},
});
menus.push({
name: "fresh manual",
callback: (item) => {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
timer.clean();
2025-01-14 18:33:28 +08:00
},
});
menus.push({ type: ccui.menu.MenuType.Separator });
2025-01-14 18:33:28 +08:00
menus.push({
name: "fps show",
callback: (item) => {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
2025-01-14 18:33:28 +08:00
bridge.send(Msg.VisibleFPS, true);
},
});
menus.push({
name: "fps hide",
callback: (item) => {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
2025-01-14 18:33:28 +08:00
bridge.send(Msg.VisibleFPS, false);
},
});
menus.push({ type: ccui.menu.MenuType.Separator });
2025-01-14 17:44:26 +08:00
menus.push({
name: "game info",
callback(item) {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
2025-01-14 17:44:26 +08:00
ccui.dialog.showDialog({
comp: GameInfo,
title: "Game Info",
});
},
});
menus.push({ type: ccui.menu.MenuType.Separator });
menus.push({
name: "hover inspect",
selected: config.value.hoverInspect,
callback: (item) => {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
config.value.hoverInspect = !config.value.hoverInspect;
appStore().save();
},
});
menus.push({
name: "click inspect",
selected: config.value.clickInspect,
callback: (item) => {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
config.value.clickInspect = !config.value.clickInspect;
appStore().save();
},
});
2025-01-25 19:36:11 +08:00
if (data) {
menus.push({ type: ccui.menu.MenuType.Separator });
2025-01-25 19:36:11 +08:00
menus.push({
name: "copy name",
enabled: true,
callback(item) {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
2025-01-25 19:36:11 +08:00
console.log(data.text);
if (!data.text) {
return;
}
navigator.clipboard
.writeText(data.text)
.then(() => {
ccui.footbar.showTips("copy success");
})
.catch((e) => {
console.log(e);
bridge.send(Msg.RequestLogCustom, data.text);
// bridge.send(Msg.ReqWriteClipboard, data.text);
});
},
});
2024-12-28 16:24:16 +08:00
menus.push({
2025-01-18 21:57:51 +08:00
name: "visible",
shortKey: "space",
2024-12-28 16:24:16 +08:00
enabled: true,
callback: (item) => {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
2025-01-06 19:46:38 +08:00
onQuickVisible();
2024-12-28 16:24:16 +08:00
},
});
menus.push({
name: "destroy",
2025-01-06 19:46:38 +08:00
enabled: true,
callback: (item) => {
ga.fireEventWithParam(GA_EventName.MouseMenu, item.name);
2025-01-25 19:36:11 +08:00
bridge.send(Msg.RequestDestroy, data.id);
2024-12-28 16:24:16 +08:00
},
});
}
ccui.menu.showMenuByMouseEvent(event, menus);
},
onChangeCase() {
matchCase.value = !matchCase.value;
updateFilterText(filterText);
},
};
},
});
</script>
<style lang="less" scoped>
.left {
display: flex;
flex-direction: column;
min-width: 200px;
width: 300px;
2025-01-12 17:57:02 +08:00
.engine-version {
flex: 1;
text-align: right;
padding-right: 5px;
font-size: 10px;
user-select: none;
2025-01-23 12:27:27 +08:00
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
2025-01-12 17:57:02 +08:00
}
.matchCase {
width: 30px;
height: 26px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
}
</style>