优化节点初始化逻辑

This commit is contained in:
2026-04-16 15:52:36 +08:00
parent 28cd9c131b
commit 4330be8d0e
2 changed files with 32 additions and 42 deletions

View File

@@ -104,7 +104,6 @@ export default defineComponent({
const isDraggingOver = ref(false);
const currentScenarioEditing = ref<boolean>(false);
const currentScenario = ref<Scenario | null>(null);
const currentGraph = ref<GraphContainer | null>(null);
const selectedModelNode = ref<Node<NodeProperties> | null>(null);
const selectedNodeTaskElement = ref<GraphTaskElement | null>(null);
const changed = ref<boolean>(false);
@@ -403,10 +402,6 @@ export default defineComponent({
(graph.value as any).currentScenario = null;
}
currentGraph.value = {
edges: [],
nodes: [],
};
selectedModelNode.value = null;
selectedNodeTaskElement.value = null;
@@ -743,7 +738,6 @@ export default defineComponent({
handleCreate,
currentScenarioEditing,
currentScenario,
currentGraph,
selectedNodeTaskElement,
selectedModelNode,
graph,

View File

@@ -115,7 +115,6 @@ export default defineComponent({
const isDraggingOver = ref(false);
const currentTreeEditing = ref<boolean>(false);
const currentBehaviorTree = ref<BehaviorTree | null>(null);
const currentGraph = ref<GraphContainer | null>(null);
const selectedModelNode = ref<Node<NodeProperties> | null>(null);
const selectedNodeTaskElement = ref<GraphTaskElement | null>(null);
const changed = ref<boolean>(false);
@@ -309,30 +308,25 @@ export default defineComponent({
const createElements = () => {
clearGraph();
nextTick(() => {
setTimeout(() => {
if (currentBehaviorTree.value?.graph && graph.value) {
if (currentBehaviorTree.value?.graph.nodes) {
currentBehaviorTree.value?.graph.nodes.forEach(ele => {
const node = createGraphTaskElement(ele as GraphTaskElement);
console.info('create node: ', ele);
// 将节点添加到画布
graph.value?.addNode(node as Node);
});
fitToScreen();
}
if (currentBehaviorTree.value?.graph.edges) {
// 然后添加所有边,确保包含桩点信息
setTimeout(() => {
currentBehaviorTree.value?.graph.edges.forEach(edgeData => {
graph.value?.addEdge({
...edgeData,
...createLineOptions(),
});
});
}, 100); // 延迟一会儿,免得连线错位
}
if (currentBehaviorTree.value?.graph && graph.value) {
if (currentBehaviorTree.value?.graph.nodes) {
currentBehaviorTree.value?.graph.nodes.forEach(ele => {
const node = createGraphTaskElement(ele as GraphTaskElement);
console.info('create node: ', ele);
// 将节点添加到画布
graph.value?.addNode(node as Node);
});
fitToScreen();
}
}, 100);
// 然后添加所有边,确保包含桩点信息
currentBehaviorTree.value?.graph.edges.forEach(edgeData => {
graph.value?.addEdge({
...edgeData,
...createLineOptions(),
});
});
}
});
};
@@ -354,7 +348,6 @@ export default defineComponent({
currentScenarioId.value = storedId;
fromScenarioPage.value = true;
} else {
currentScenarioId.value = undefined;
fromScenarioPage.value = false;
}
}
@@ -371,8 +364,11 @@ export default defineComponent({
currentScenarioId.value = scenario.id;
};
const handleCreateTree = () => {
destroyGraph();
const initGraphConfig = (_graph?: GraphContainer) => {
const graph: GraphContainer = _graph ? _graph : {
nodes: [],
edges: [],
};
currentBehaviorTree.value = {
id: 0,
@@ -383,16 +379,15 @@ export default defineComponent({
createdAt: null,
platformId: currentPlatformId.value,
scenarioId: currentScenarioId.value,
graph: {
edges: [],
nodes: [],
},
graph: { ...graph },
updatedAt: null,
};
currentGraph.value = {
edges: [],
nodes: [],
};
};
const handleCreateTree = () => {
destroyGraph();
initGraphConfig();
selectedModelNode.value = null;
selectedNodeTaskElement.value = null;
subPlatforms.value = []; // 重置下属平台
@@ -412,6 +407,7 @@ export default defineComponent({
try {
graph.value = createCanvas(canvas.value);
console.log('画布初始化成功');
initGraphConfig();
createElements();
// 监听缩放变化
@@ -503,6 +499,7 @@ export default defineComponent({
message.error('当前决策树不存在');
return;
}
// return;
const newTree: BehaviorTree = {
...currentBehaviorTree.value,
graph: graphData,
@@ -553,7 +550,6 @@ export default defineComponent({
handleSelectScenario,
currentTreeEditing,
currentBehaviorTree,
currentGraph,
selectedNodeTaskElement,
selectedModelNode,
graph,