Cleaned up diagram connection to stop creating duplicates.

This commit is contained in:
Michael Mainguy 2023-09-14 11:17:57 -05:00
parent 60afe249b8
commit 45dd7cc6d3
4 changed files with 24 additions and 17 deletions

View File

@ -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)]);
}
}

View File

@ -57,7 +57,16 @@ export function diagramEventHandler(event: DiagramEvent,
case DiagramEventType.REMOVE:
if (mesh) {
mesh?.physicsBody?.dispose();
if (mesh?.metadata?.template == '#connection-template') {
if (mesh.parent) {
mesh.parent.dispose();
} else {
mesh.dispose();
}
} else {
mesh.dispose();
}
sounds.exit.play();
}
break;

View File

@ -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 {

View File

@ -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);
}
}