From 45dd7cc6d3e707d3263134d64a419cb7121cf9eb Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Thu, 14 Sep 2023 11:17:57 -0500 Subject: [PATCH] Cleaned up diagram connection to stop creating duplicates. --- src/diagram/diagramConnection.ts | 26 ++++++++++------------ src/diagram/diagramEventHandler.ts | 11 ++++++++- src/diagram/functions/fromDiagramEntity.ts | 2 +- src/menus/editMenu.ts | 2 +- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/diagram/diagramConnection.ts b/src/diagram/diagramConnection.ts index 4c75bf0..09a0218 100644 --- a/src/diagram/diagramConnection.ts +++ b/src/diagram/diagramConnection.ts @@ -8,17 +8,26 @@ import { TransformNode, Vector3 } from "@babylonjs/core"; +import {v4 as uuidv4} from 'uuid'; import log, {Logger} from "loglevel"; export class DiagramConnection { private logger: Logger = log.getLogger('DiagramConnection'); + private readonly id: string; - constructor(from: string, to: string, scene?: Scene, pointerInfo?: PointerInfo) { + constructor(from: string, to: string, id: string, scene?: Scene, pointerInfo?: PointerInfo) { this.logger.debug('buildConnection constructor'); + if (id) { + this.id = id; + } else { + this.id = "connection_" + uuidv4(); + } + this.scene = scene; this._to = to; this._from = from; + const fromMesh = this.scene.getMeshById(from); if (fromMesh) { this.fromAnchor = fromMesh; @@ -38,6 +47,8 @@ export class DiagramConnection { } this.toAnchor = to; + } else { + this.logger.error("no fromMesh"); } } this.buildConnection(); @@ -83,9 +94,6 @@ export class DiagramConnection { return this?.fromAnchor?.id; } - public get id(): string { - return "connection_" + this._from + "_" + this._to; - } private tick: number = 0; private recalculate() { const start = this.fromAnchor?.absolutePosition; @@ -96,19 +104,9 @@ export class DiagramConnection { this._mesh.rotation.x = Math.PI / 2; this._mesh.scaling.y = Math.abs(start.subtract(end).length()); } - - /*if (this.fromAnchor && this.toAnchor) { - this.points = [this.fromAnchor.absolutePosition, this.toAnchor.absolutePosition]; - } else { - this.points = [Vector3.Zero(), Vector3.Zero()]; - }*/ - } private setPoints() { - if (this.points.length > 1) { - //this._mesh.setPoints([GreasedLineTools.ToNumberArray(this.points)]); - } } diff --git a/src/diagram/diagramEventHandler.ts b/src/diagram/diagramEventHandler.ts index d29900c..fa7184d 100644 --- a/src/diagram/diagramEventHandler.ts +++ b/src/diagram/diagramEventHandler.ts @@ -57,7 +57,16 @@ export function diagramEventHandler(event: DiagramEvent, case DiagramEventType.REMOVE: if (mesh) { mesh?.physicsBody?.dispose(); - mesh.dispose(); + if (mesh?.metadata?.template == '#connection-template') { + if (mesh.parent) { + mesh.parent.dispose(); + } else { + mesh.dispose(); + } + } else { + mesh.dispose(); + } + sounds.exit.play(); } break; diff --git a/src/diagram/functions/fromDiagramEntity.ts b/src/diagram/functions/fromDiagramEntity.ts index 6330306..db00f3d 100644 --- a/src/diagram/functions/fromDiagramEntity.ts +++ b/src/diagram/functions/fromDiagramEntity.ts @@ -22,7 +22,7 @@ export function fromDiagramEntity(entity: DiagramEntity, scene: Scene): Abstract newMesh = oldMesh; } else { if (entity.template == "#connection-template") { - const connection: DiagramConnection = new DiagramConnection(entity.from, entity.to, scene); + const connection: DiagramConnection = new DiagramConnection(entity.from, entity.to, entity.id, scene); logger.debug(`connection.mesh = ${connection.mesh.id}`); newMesh = connection.mesh; } else { diff --git a/src/menus/editMenu.ts b/src/menus/editMenu.ts index 84bd30a..7789a08 100644 --- a/src/menus/editMenu.ts +++ b/src/menus/editMenu.ts @@ -209,7 +209,7 @@ export class EditMenu extends AbstractMenu { }, -1); this.connection = null; } else { - this.connection = new DiagramConnection(mesh.id, null, this.scene, pointerInfo); + this.connection = new DiagramConnection(mesh.id, null, null, this.scene, pointerInfo); } }