Connection saving/loading.

This commit is contained in:
Michael Mainguy 2023-08-05 06:52:30 -05:00
parent 4b93e3856e
commit 781e6fbd3e
2 changed files with 42 additions and 32 deletions

View File

@ -1,4 +1,5 @@
import { import {
AbstractMesh,
CreateGreasedLine, CreateGreasedLine,
GreasedLineMesh, GreasedLineMesh,
GreasedLineTools, GreasedLineTools,
@ -37,6 +38,7 @@ export class DiagramConnection {
} }
this.buildConnection(); this.buildConnection();
} }
private readonly scene: Scene; private readonly scene: Scene;
private toAnchor: TransformNode; private toAnchor: TransformNode;
private fromAnchor: TransformNode; private fromAnchor: TransformNode;
@ -85,7 +87,6 @@ export class DiagramConnection {
} else { } else {
this.points = [Vector3.Zero(), Vector3.Zero()]; this.points = [Vector3.Zero(), Vector3.Zero()];
} }
} }
private setPoints() { private setPoints() {
@ -96,9 +97,7 @@ export class DiagramConnection {
} }
private buildConnection() { private buildConnection() {
this.logger.debug('buildConnection'); this.logger.debug(`buildConnection from ${this._from} to ${this._to}`);
this.logger.debug(this._to);
this.logger.debug(this._from);
this.recalculate(); this.recalculate();
this._mesh = CreateGreasedLine(this.id, this._mesh = CreateGreasedLine(this.id,
@ -115,25 +114,33 @@ export class DiagramConnection {
this.recalculate(); this.recalculate();
this.setPoints(); this.setPoints();
}); });
this.scene.onNewMeshAddedObservable.add((mesh) => { this.scene.onNewMeshAddedObservable.add(this.onMeshAdded, -1, true, this);
if (mesh && mesh.id) { return;
if (!this.toAnchor || !this.fromAnchor) { }
this.logger.debug('render');
if (mesh?.id == this?._to) { private onMeshAdded = (mesh: AbstractMesh) => {
this.logger.debug("Found to anchor"); if (mesh && mesh.id) {
this.toAnchor = mesh; if (!this.toAnchor || !this.fromAnchor) {
this._mesh.metadata.to = this.to; if (mesh?.id == this?._to) {
} this.logger.debug("Found to anchor");
if (mesh?.id == this?._from) { this.toAnchor = mesh;
this.logger.debug("Found from anchor"); this._mesh.metadata.to = this.to;
this.fromAnchor = mesh; }
this._mesh.metadata.from = this.from; if (mesh?.id == this?._from) {
} this.logger.debug("Found from anchor");
this.fromAnchor = mesh;
this._mesh.metadata.from = this.from;
}
if (this.toAnchor && this.fromAnchor) {
this.logger.debug(`connection built from ${this._from} to ${this._to}`);
this.removeObserver();
} }
} }
}
}
}, -1, true, this); private removeObserver() {
return; this.logger.debug("removing observer");
this.scene.onNewMeshAddedObservable.removeCallback(this.onMeshAdded);
} }
} }

View File

@ -44,22 +44,25 @@ export class MeshConverter {
} }
let mesh: AbstractMesh = scene.getMeshById(entity.id); let mesh: AbstractMesh = scene.getMeshById(entity.id);
if (mesh) { if (mesh) {
log.debug('mesh already exists'); this.logger.debug(`mesh ${mesh.id} already exists`);
} else { } else {
if (entity.template == "#connection-template") { if (entity.template == "#connection-template") {
const connection: DiagramConnection = new DiagramConnection(entity.from, entity.to, scene); const connection: DiagramConnection = new DiagramConnection(entity.from, entity.to, scene);
this.logger.debug(`connection.mesh = ${connection.mesh.id}`);
} mesh = connection.mesh;
mesh = scene.getMeshById("tool-" + entity.template + "-" + entity.color);
if (mesh) {
if (mesh.isAnInstance) {
log.debug('error: mesh is an instance');
} else {
mesh = new InstancedMesh(entity.id, (mesh as Mesh));
}
} else { } else {
log.debug('no mesh found for ' + entity.template + "-" + entity.color); mesh = scene.getMeshById("tool-" + entity.template + "-" + entity.color);
if (mesh) {
if (mesh.isAnInstance) {
this.logger.error(`mesh ${mesh.id} is an instance`);
} else {
mesh = new InstancedMesh(entity.id, (mesh as Mesh));
}
} else {
this.logger.warn('no mesh found for ' + entity.template + "-" + entity.color);
}
} }
} }
if (mesh) { if (mesh) {
mesh.metadata = {template: entity.template}; mesh.metadata = {template: entity.template};