268 lines
8.2 KiB
Vue
Raw Normal View History

<template>
<div class="left">
2024-12-28 14:25:23 +08:00
<CCDock name="Hierarchy">
2025-01-12 17:57:02 +08:00
<template v-slot:title>
<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>
2024-12-28 16:24:16 +08:00
<CCTree @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"></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";
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 { EngineData, TreeData } from "./data";
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",
2024-12-28 14:25:23 +08:00
components: { 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();
} else {
timer.clean();
}
2024-12-28 15:05:05 +08:00
};
2024-12-28 14:25:23 +08:00
const timer: Timer = new Timer(() => {
updateTree();
});
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;
}
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);
2024-12-28 14:25:23 +08:00
timer.create();
});
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);
2024-12-28 14:25:23 +08:00
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);
}
return {
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
},
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);
}
},
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;
}
}
},
2024-12-28 16:24:16 +08:00
onMenu(event: MouseEvent) {
const menus: IUiMenuItem[] = [];
menus.push({
name: "update hierarchy",
enabled: true,
callback: () => {
2025-01-10 15:22:32 +08:00
ga.fireEventWithParam(GA_EventName.MouseMenu, "update hierarchy");
2024-12-28 16:24:16 +08:00
updateTree();
},
});
if (selectedUUID) {
menus.push({
2025-01-06 19:46:38 +08:00
name: "visible (sapce)",
2024-12-28 16:24:16 +08:00
enabled: true,
callback: () => {
2025-01-10 15:22:32 +08:00
ga.fireEventWithParam(GA_EventName.MouseMenu, "visible");
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,
2024-12-28 16:24:16 +08:00
callback: () => {
2025-01-10 15:22:32 +08:00
ga.fireEventWithParam(GA_EventName.MouseMenu, "destroy");
2025-01-06 19:46:38 +08:00
bridge.send(Msg.RequestDestroy, selectedUUID);
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;
}
.matchCase {
width: 30px;
height: 26px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
}
</style>