UPDATE: VERSION-20260315

This commit is contained in:
libertyspy
2026-03-15 19:32:20 +08:00
parent 83a38c6db8
commit f2e81c6e5c
16 changed files with 197 additions and 178 deletions

View File

@@ -6,7 +6,7 @@
<div class="ks-model-builder-body">
<div class="ks-model-builder-left">
<PlatformCard
ref="treesCardRef"
ref="scenariosCardRef"
@create="handleCreate"
@select="handleSelect"
/>
@@ -52,21 +52,19 @@ import { Wrapper } from '@/components/wrapper';
import { safePreventDefault, safeStopPropagation } from '@/utils/event';
import Header from '../header.vue';
import type { Scenario } from './types';
import { createLineOptions } from '../builder/line';
import type { GraphTaskElement, NodeGraph } from '../builder/element';
import { useGraphCanvas } from '../builder/hooks';
import { registerNodeElement } from '../builder/register';
import { createTree, findOneTreeById, updateTree } from '../designer/api';
import { createGraphTaskElement, hasElements, hasRootElementNode, resolveNodeGraph } from '../builder/utils';
import { createGraphTaskElementFromTemplate } from '../utils/node';
import type { PlatformWithComponents, Scenario } from './types';
import { createGraphTaskElement, createLineOptions, type GraphContainer, type GraphTaskElement, hasElements, hasRootElementNode, resolveGraph, useGraphCanvas } from '../graph';
import { registerScenarioElement } from './register';
import { createGraphTaskElementFromScenario } from './utils';
import PlatformCard from './platform-card.vue';
import NodesCard from './nodes-card.vue';
import { saveScenario } from '@/views/decision/communication/api.ts';
const TeleportContainer = defineComponent(getTeleport());
registerNodeElement();
registerScenarioElement();
export default defineComponent({
components: {
@@ -84,15 +82,15 @@ export default defineComponent({
const canvas = ref<HTMLDivElement | null>(null);
const graph = ref<Graph | null>(null);
const currentZoom = ref<number>(1);
const draggedNodeData = ref<Scenario | null>(null);
const draggedNodeData = ref<PlatformWithComponents | null>(null);
const isDraggingOver = ref(false);
const currentTreeEditing = ref<boolean>(false);
const currentScenarioEditing = ref<boolean>(false);
const currentScenario = ref<Scenario | null>(null);
const currentNodeGraph = ref<NodeGraph | 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);
const treesCardRef = ref<InstanceType<typeof PlatformCard> | null>(null);
const scenariosCardRef = ref<InstanceType<typeof PlatformCard> | null>(null);
const {
handleGraphEvent,
@@ -105,7 +103,7 @@ export default defineComponent({
} = useGraphCanvas();
// 处理拖动开始
const handleDragStart = (nm: Scenario) => {
const handleDragStart = (nm: PlatformWithComponents) => {
draggedNodeData.value = nm;
};
@@ -142,10 +140,10 @@ export default defineComponent({
safePreventDefault(e);
safeStopPropagation(e);
isDraggingOver.value = false;
currentTreeEditing.value = false;
currentScenarioEditing.value = false;
if (!currentScenario.value) {
message.error('请先选择或者创建行为树.');
message.error('请先选择或者创建场景.');
return;
}
@@ -156,17 +154,17 @@ export default defineComponent({
try {
// 获取拖动的数据
const template = draggedNodeData.value as Scenario;
const pwc = draggedNodeData.value as PlatformWithComponents;
if (!hasElements(graph.value as Graph)) {
message.error('请先添加根节点.');
return;
}
// if (!hasElements(graph.value as Graph)) {
// message.error('请先添加根节点.');
// return;
// }
if (hasRootElementNode(graph.value as Graph)) {
message.error('根节点已经存在.');
return;
}
// if (hasRootElementNode(graph.value as Graph)) {
// message.error('根节点已经存在.');
// return;
// }
// 计算相对于画布的位置(考虑缩放)
const rect = canvas.value.getBoundingClientRect();
@@ -174,12 +172,12 @@ export default defineComponent({
const x = (e.clientX - rect.left) / scale;
const y = (e.clientY - rect.top) / scale;
console.log('放置节点:', { ...template, x, y });
console.log('放置节点:', { ...pwc, x, y });
// 创建节点数据
const settingTaskElement: GraphTaskElement = createGraphTaskElementFromTemplate(template, { x, y });
const settingTaskElement: GraphTaskElement = createGraphTaskElementFromScenario(pwc, { x, y });
// 创建节点
const settingTaskNode = createGraphTaskElement(settingTaskElement);
const settingTaskNode = createGraphTaskElement(settingTaskElement, 250, 120, 'scenario');
console.info('create settingTaskNode: ', settingTaskElement, settingTaskNode);
// 将节点添加到画布
@@ -193,32 +191,27 @@ export default defineComponent({
}
};
const handleSelect = (tree: Scenario) => {
console.info('handleSelect', tree);
findOneTreeById(tree.id).then(r => {
if (r.data) {
let nodeGraph: NodeGraph | null = null;
try {
nodeGraph = JSON.parse(r.data?.xmlContent as unknown as string) as unknown as NodeGraph;
} catch (e: any) {
console.error('parse error,cause:', e);
}
if (!nodeGraph) {
nodeGraph = {
nodes: [],
edges: [],
};
}
currentScenario.value = {
...r.data,
graph: nodeGraph,
};
currentTreeEditing.value = true;
createElements();
} else {
message.error(r.msg ?? '行为树不存在.');
}
});
const handleSelect = (scenario: Scenario) => {
console.info('handleSelect', scenario);
let nodeGraph: GraphContainer | null = null;
try {
nodeGraph = JSON.parse(scenario.communicationGraph as unknown as string) as unknown as GraphContainer;
} catch (e: any) {
console.error('parse error,cause:', e);
}
if (!nodeGraph) {
nodeGraph = {
nodes: [],
edges: [],
};
}
currentScenario.value = {
...scenario,
graph: nodeGraph,
};
currentScenarioEditing.value = true;
createElements();
};
const createElements = () => {
@@ -257,18 +250,15 @@ export default defineComponent({
const handleCreate = () => {
currentScenario.value = {
id: 0,
name: '行为树',
name: null,
description: null,
englishName: null,
xmlContent: null,
createdAt: null,
communicationGraph: null,
graph: {
edges: [],
nodes: [],
},
updatedAt: null,
}
};
currentNodeGraph.value = {
currentGraph.value = {
edges: [],
nodes: [],
};
@@ -298,7 +288,7 @@ export default defineComponent({
handleGraphEvent('blank:click', () => {
selectedModelNode.value = null;
selectedNodeTaskElement.value = null;
currentTreeEditing.value = null !== currentScenario.value;
currentScenarioEditing.value = null !== currentScenario.value;
});
handleGraphEvent('node:click', (args: any) => {
@@ -348,34 +338,30 @@ export default defineComponent({
};
const handleSave = () => {
const graphData: NodeGraph = resolveNodeGraph(graph.value as Graph);
const graphData: GraphContainer = resolveGraph(graph.value as Graph);
console.info('handleSave', graphData);
if (!currentScenario.value) {
message.error('当前决策树不存在');
return;
}
const newTree: currentScenario = {
const newScenario: Scenario = {
...currentScenario.value,
graph: graphData,
xmlContent: JSON.stringify(graphData),
communicationGraph: JSON.stringify(graphData),
};
if (!newTree.name) {
message.error('行为树名称不能为空.');
return;
}
if (!newTree.englishName) {
message.error('行为树英文名称不能为空.');
if (!newScenario.name) {
message.error('场景名称不能为空.');
return;
}
let res = null;
if (currentScenario.value.id > 0) {
res = createTree(newTree);
res = saveScenario(newScenario);
} else {
res = updateTree(newTree);
res = saveScenario(newScenario);
}
res.then(r => {
if (r.code === 200) {
treesCardRef.value?.refresh();
scenariosCardRef.value?.refresh();
message.success(r.msg ?? '操作成功.');
} else {
message.error(r.msg ?? '操作失败.');
@@ -403,11 +389,11 @@ export default defineComponent({
});
return {
treesCardRef,
scenariosCardRef,
handleCreate,
currentTreeEditing,
currentScenarioEditing,
currentScenario,
currentNodeGraph,
currentGraph,
selectedNodeTaskElement,
selectedModelNode,
graph,