From 88668c3d64f3adcbebc75efe6383327a0de41c17 Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Wed, 23 Aug 2023 08:04:49 -0500 Subject: [PATCH] Fixed code inspections. --- src/controllers/base.ts | 11 +- src/diagram/diagramEventHandler.ts | 4 +- src/diagram/diagramManager.ts | 7 +- src/diagram/functions/fromDiagramEntity.ts | 77 ++++++++++++++ src/diagram/functions/toDiagramEntity.ts | 33 ++++++ src/diagram/meshConverter.ts | 105 ------------------- src/integration/drawioManager.ts | 2 +- src/integration/indexdbPersistenceManager.ts | 10 +- src/integration/newRelic/newRelicData.ts | 4 +- src/menus/editMenu.ts | 14 +-- src/toolbox/toolbox.ts | 15 ++- src/tutorial/introduction.ts | 2 +- src/util/appConfig.ts | 8 +- src/util/customPhysics.ts | 2 +- 14 files changed, 148 insertions(+), 146 deletions(-) delete mode 100644 src/diagram/meshConverter.ts diff --git a/src/controllers/base.ts b/src/controllers/base.ts index 0caa808..447767d 100644 --- a/src/controllers/base.ts +++ b/src/controllers/base.ts @@ -9,11 +9,11 @@ import { WebXRDefaultExperience, WebXRInputSource } from "@babylonjs/core"; -import {MeshConverter} from "../diagram/meshConverter"; import {DiagramManager} from "../diagram/diagramManager"; import {DiagramEvent, DiagramEventType} from "../diagram/diagramEntity"; import log from "loglevel"; import {Controllers} from "./controllers"; +import {toDiagramEntity} from "../diagram/functions/toDiagramEntity"; export class Base { static stickVector = Vector3.Zero(); @@ -73,7 +73,10 @@ export class Base { if (event.type == 'pulse') { this.logger.debug(event); if (event.gripId == this?.controller?.grip?.id) { - this.controller?.motionController?.pulse(.25, 30); + this.controller?.motionController?.pulse(.25, 30) + .then(() => { + this.logger.debug("pulse done"); + }); } } }); @@ -152,7 +155,7 @@ export class Base { this.previousParentId = null; const event: DiagramEvent = { type: DiagramEventType.ADD, - entity: MeshConverter.toDiagramEntity(newMesh) + entity: toDiagramEntity(newMesh) } this.diagramManager.onDiagramEventObservable.notifyObservers(event); @@ -215,7 +218,7 @@ export class Base { if (mesh?.metadata?.template.indexOf('#') == -1) { return; } - const entity = MeshConverter.toDiagramEntity(mesh); + const entity = toDiagramEntity(mesh); const event: DiagramEvent = { type: DiagramEventType.DROP, entity: entity diff --git a/src/diagram/diagramEventHandler.ts b/src/diagram/diagramEventHandler.ts index b58fb5a..5802279 100644 --- a/src/diagram/diagramEventHandler.ts +++ b/src/diagram/diagramEventHandler.ts @@ -1,12 +1,12 @@ import {DiagramEvent, DiagramEventType} from "./diagramEntity"; import log from "loglevel"; -import {MeshConverter} from "./meshConverter"; import {applyPhysics} from "./functions/diagramShapePhysics"; import {ActionManager, Color3, PhysicsMotionType, Scene} from "@babylonjs/core"; import {TextLabel} from "./textLabel"; import {Toolbox} from "../toolbox/toolbox"; import {DiaSounds} from "../util/diaSounds"; import {IPersistenceManager} from "../integration/iPersistenceManager"; +import {fromDiagramEntity} from "./functions/fromDiagramEntity"; export function diagramEventHandler(event: DiagramEvent, @@ -27,7 +27,7 @@ export function diagramEventHandler(event: DiagramEvent, log.debug('no mesh found for ' + event.entity.template + "-" + event.entity.color, 'adding it'); toolbox.updateToolbox(event.entity.color); } - mesh = MeshConverter.fromDiagramEntity(event.entity, scene); + mesh = fromDiagramEntity(event.entity, scene); if (mesh) { mesh.actionManager = actionManager; if (physicsEnabled) { diff --git a/src/diagram/diagramManager.ts b/src/diagram/diagramManager.ts index bc59455..2ddd5e2 100644 --- a/src/diagram/diagramManager.ts +++ b/src/diagram/diagramManager.ts @@ -1,7 +1,6 @@ import {AbstractMesh, Color3, InstancedMesh, Mesh, Observable, PhysicsMotionType, Scene} from "@babylonjs/core"; import {DiagramEntity, DiagramEvent, DiagramEventType} from "./diagramEntity"; import {IPersistenceManager} from "../integration/iPersistenceManager"; -import {MeshConverter} from "./meshConverter"; import log from "loglevel"; import {Controllers} from "../controllers/controllers"; import {DiaSounds} from "../util/diaSounds"; @@ -13,6 +12,8 @@ import {diagramEventHandler} from "./diagramEventHandler"; import {deepCopy} from "../util/deepCopy"; import {applyPhysics} from "./functions/diagramShapePhysics"; import {applyScaling} from "./functions/applyScaling"; +import {toDiagramEntity} from "./functions/toDiagramEntity"; +import {fromDiagramEntity} from "./functions/fromDiagramEntity"; export class DiagramManager { @@ -52,7 +53,7 @@ export class DiagramManager { this.logger.debug("removing connection", m.id); this.onDiagramEventObservable.notifyObservers({ type: DiagramEventType.REMOVE, - entity: MeshConverter.toDiagramEntity(m) + entity: toDiagramEntity(m) }); } }); @@ -100,7 +101,7 @@ export class DiagramManager { log.debug('no mesh found for ' + event.template + "-" + event.color, 'adding it'); this.toolbox.updateToolbox(event.color); } - const mesh = MeshConverter.fromDiagramEntity(event, this.scene); + const mesh = fromDiagramEntity(event, this.scene); mesh.actionManager = this.diagramEntityActionManager.manager; if (event.parent) { mesh.parent = this.scene.getMeshById(event.parent); diff --git a/src/diagram/functions/fromDiagramEntity.ts b/src/diagram/functions/fromDiagramEntity.ts index e69de29..09cbf28 100644 --- a/src/diagram/functions/fromDiagramEntity.ts +++ b/src/diagram/functions/fromDiagramEntity.ts @@ -0,0 +1,77 @@ +import {DiagramEntity} from "../diagramEntity"; +import {AbstractMesh, Color3, InstancedMesh, Mesh, Quaternion, Scene, StandardMaterial} from "@babylonjs/core"; +import {DiagramConnection} from "../diagramConnection"; +import {TextLabel} from "../textLabel"; +import log from "loglevel"; +import {v4 as uuidv4} from 'uuid'; + +const logger = log.getLogger('fromDiagramEntity'); + +export function fromDiagramEntity(entity: DiagramEntity, scene: Scene): AbstractMesh { + if (!entity) { + logger.error("fromDiagramEntity: entity is null"); + return null; + } + if (!entity.id) { + entity.id = "id" + uuidv4(); + } + let mesh: AbstractMesh = scene.getMeshById(entity.id); + if (mesh) { + logger.debug(`mesh ${mesh.id} already exists`); + } else { + if (entity.template == "#connection-template") { + const connection: DiagramConnection = new DiagramConnection(entity.from, entity.to, scene); + logger.debug(`connection.mesh = ${connection.mesh.id}`); + mesh = connection.mesh; + } else { + mesh = scene.getMeshById("tool-" + entity.template + "-" + entity.color); + if (mesh) { + if (mesh.isAnInstance) { + logger.error(`mesh ${mesh.id} is an instance`); + } else { + mesh = new InstancedMesh(entity.id, (mesh as Mesh)); + } + } else { + logger.warn('no mesh found for ' + entity.template + "-" + entity.color); + } + } + + } + if (mesh) { + mesh.metadata = {template: entity.template}; + if (entity.position) { + mesh.position = entity.position; + } + if (entity.rotation) { + if (mesh.rotationQuaternion) { + mesh.rotationQuaternion = Quaternion.FromEulerAngles(entity.rotation.x, entity.rotation.y, entity.rotation.z); + } else { + mesh.rotation = entity.rotation; + } + } + if (entity.parent) { + mesh.parent = scene.getNodeById(entity.parent); + } + if (entity.scale) { + mesh.scaling = entity.scale; + } + if (!mesh.material) { + const material = new StandardMaterial("material-" + entity.id, scene); + material.diffuseColor = Color3.FromHexString(entity.color); + mesh.material = material; + } + if (entity.text) { + mesh.metadata.text = entity.text; + TextLabel.updateTextNode(mesh, entity.text); + } + if (entity.from) { + mesh.metadata.from = entity.from; + } + if (entity.to) { + mesh.metadata.to = entity.to; + } + } else { + logger.error("fromDiagramEntity: mesh is null after it should have been created"); + } + return mesh; +} \ No newline at end of file diff --git a/src/diagram/functions/toDiagramEntity.ts b/src/diagram/functions/toDiagramEntity.ts index e69de29..db00541 100644 --- a/src/diagram/functions/toDiagramEntity.ts +++ b/src/diagram/functions/toDiagramEntity.ts @@ -0,0 +1,33 @@ +import {AbstractMesh} from "@babylonjs/core"; +import {DiagramEntity} from "../diagramEntity"; +import log from "loglevel"; +import {v4 as uuidv4} from 'uuid'; + +const logger = log.getLogger('toDiagramEntity'); + +export function toDiagramEntity(mesh: AbstractMesh): DiagramEntity { + + if (!mesh) { + logger.error("toDiagramEntity: mesh is null"); + return null; + } + const entity = {}; + if ("new" == mesh?.id) { + mesh.id = "id" + uuidv4(); + } + entity.id = mesh.id; + entity.position = mesh.position; + entity.rotation = mesh.rotation; + entity.last_seen = new Date(); + entity.template = mesh?.metadata?.template; + entity.text = mesh?.metadata?.text; + entity.from = mesh?.metadata?.from; + entity.to = mesh?.metadata?.to; + entity.scale = mesh.scaling; + if (mesh.material) { + entity.color = (mesh.material as any).diffuseColor.toHexString(); + } else { + logger.error("toDiagramEntity: mesh.material is null"); + } + return entity; +} \ No newline at end of file diff --git a/src/diagram/meshConverter.ts b/src/diagram/meshConverter.ts deleted file mode 100644 index bf71cfa..0000000 --- a/src/diagram/meshConverter.ts +++ /dev/null @@ -1,105 +0,0 @@ -import {DiagramEntity} from "./diagramEntity"; -import {AbstractMesh, Color3, InstancedMesh, Mesh, Quaternion, Scene, StandardMaterial} from "@babylonjs/core"; -import {v4 as uuidv4} from 'uuid'; -import log from "loglevel"; -import {TextLabel} from "./textLabel"; -import {DiagramConnection} from "./diagramConnection"; - -export class MeshConverter { - private static logger = log.getLogger('MeshConverter'); - - public static toDiagramEntity(mesh: AbstractMesh): DiagramEntity { - if (!mesh) { - this.logger.error("toDiagramEntity: mesh is null"); - return null; - } - const entity = {}; - if ("new" == mesh?.id) { - mesh.id = "id" + uuidv4(); - } - entity.id = mesh.id; - entity.position = mesh.position; - - entity.rotation = mesh.rotation; - entity.last_seen = new Date(); - entity.template = mesh?.metadata?.template; - entity.text = mesh?.metadata?.text; - entity.from = mesh?.metadata?.from; - entity.to = mesh?.metadata?.to; - entity.scale = mesh.scaling; - if (mesh.material) { - entity.color = (mesh.material as any).diffuseColor.toHexString(); - } else { - this.logger.error("toDiagramEntity: mesh.material is null"); - } - return entity; - } - public static fromDiagramEntity(entity: DiagramEntity, scene: Scene): AbstractMesh { - if (!entity) { - this.logger.error("fromDiagramEntity: entity is null"); - return null; - } - if (!entity.id) { - entity.id = "id" + uuidv4(); - } - let mesh: AbstractMesh = scene.getMeshById(entity.id); - if (mesh) { - this.logger.debug(`mesh ${mesh.id} already exists`); - } else { - if (entity.template == "#connection-template") { - const connection: DiagramConnection = new DiagramConnection(entity.from, entity.to, scene); - this.logger.debug(`connection.mesh = ${connection.mesh.id}`); - mesh = connection.mesh; - } else { - 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) { - mesh.metadata = {template: entity.template}; - if (entity.position) { - mesh.position = entity.position; - } - if (entity.rotation) { - if (mesh.rotationQuaternion) { - mesh.rotationQuaternion = Quaternion.FromEulerAngles(entity.rotation.x, entity.rotation.y, entity.rotation.z); - } else { - mesh.rotation = entity.rotation; - } - } - if (entity.parent) { - mesh.parent = scene.getNodeById(entity.parent); - } - if (entity.scale) { - mesh.scaling = entity.scale; - } - if (!mesh.material) { - const material = new StandardMaterial("material-" + entity.id, scene); - material.diffuseColor = Color3.FromHexString(entity.color); - mesh.material = material; - } - if (entity.text) { - mesh.metadata.text = entity.text; - TextLabel.updateTextNode(mesh, entity.text); - } - if (entity.from) { - mesh.metadata.from = entity.from; - } - if (entity.to) { - mesh.metadata.to = entity.to; - } - } else { - this.logger.error("fromDiagramEntity: mesh is null after it should have been created"); - } - return mesh; - } -} \ No newline at end of file diff --git a/src/integration/drawioManager.ts b/src/integration/drawioManager.ts index e21596c..d1e6bf5 100644 --- a/src/integration/drawioManager.ts +++ b/src/integration/drawioManager.ts @@ -186,7 +186,7 @@ export class DrawioManager { } else { const geo = cell.querySelector('[id="' + id + '"] > mxGeometry'); - let geometry = null; + let geometry; if (geo) { geometry = { x: Number.parseFloat(geo.getAttribute('x')), diff --git a/src/integration/indexdbPersistenceManager.ts b/src/integration/indexdbPersistenceManager.ts index 47949c5..ad6e5ad 100644 --- a/src/integration/indexdbPersistenceManager.ts +++ b/src/integration/indexdbPersistenceManager.ts @@ -2,9 +2,10 @@ import {DiagramListing, DiagramListingEvent, DiagramListingEventType, IPersisten import {AbstractMesh, Observable, Vector3} from "@babylonjs/core"; import {DiagramEntity} from "../diagram/diagramEntity"; import Dexie from "dexie"; -import {MeshConverter} from "../diagram/meshConverter"; + import log from "loglevel"; import {AppConfigType} from "../util/appConfigType"; +import {toDiagramEntity} from "../diagram/functions/toDiagramEntity"; export class IndexdbPersistenceManager implements IPersistenceManager { private readonly logger = log.getLogger('IndexdbPersistenceManager'); @@ -33,7 +34,7 @@ export class IndexdbPersistenceManager implements IPersistenceManager { this.logger.error("Adding null mesh, early return"); return; } - const entity = MeshConverter.toDiagramEntity(mesh); + const entity = toDiagramEntity(mesh); entity.position = this.vectoxys(mesh.position); entity.rotation = this.vectoxys(mesh.rotation); entity.scale = this.vectoxys(mesh.scaling); @@ -97,8 +98,7 @@ export class IndexdbPersistenceManager implements IPersistenceManager { public async getConfig(): Promise { const configs = await this.db['config'].toArray(); - const config = configs[0]; - return config; + return configs[0]; } public async initialize() { @@ -151,7 +151,7 @@ export class IndexdbPersistenceManager implements IPersistenceManager { this.logger.error("Modifying null mesh, early return"); return; } - const entity = MeshConverter.toDiagramEntity(mesh); + const entity = toDiagramEntity(mesh); if (!entity) { this.logger.error("Modifying null mesh, early return"); return; diff --git a/src/integration/newRelic/newRelicData.ts b/src/integration/newRelic/newRelicData.ts index cbc8b75..3f0d4d7 100644 --- a/src/integration/newRelic/newRelicData.ts +++ b/src/integration/newRelic/newRelicData.ts @@ -14,7 +14,7 @@ export class NewRelicData { private key: string; private account: string; private data: any[]; - private scene: Scene; + private readonly scene: Scene; private persistenceManager: IPersistenceManager; private policyLabels: AbstractMesh[] = []; @@ -101,7 +101,7 @@ export class NewRelicData { const policies: Map = new Map(); this.data.forEach((item) => { const policy = item.policyName ? item.policyName : "No Policy"; - let x = 0; + let x; let y: number = 0; if (policies.has(policy)) { const value = policies.get(policy); diff --git a/src/menus/editMenu.ts b/src/menus/editMenu.ts index c796885..f7a2c7d 100644 --- a/src/menus/editMenu.ts +++ b/src/menus/editMenu.ts @@ -15,7 +15,6 @@ import {Button3D, GUI3DManager, PlanePanel, TextBlock} from "@babylonjs/gui"; import {DiagramManager} from "../diagram/diagramManager"; import {EditMenuState} from "./editMenuState"; import {DiagramEvent, DiagramEventType} from "../diagram/diagramEntity"; -import {MeshConverter} from "../diagram/meshConverter"; import log from "loglevel"; import {InputTextView} from "../information/inputTextView"; import {DiaSounds} from "../util/diaSounds"; @@ -23,6 +22,7 @@ import {CameraHelper} from "../util/cameraHelper"; import {TextLabel} from "../diagram/textLabel"; import {DiagramConnection} from "../diagram/diagramConnection"; import {GLTF2Export} from "@babylonjs/serializers"; +import {toDiagramEntity} from "../diagram/functions/toDiagramEntity"; export class EditMenu { private state: EditMenuState = EditMenuState.NONE; @@ -118,7 +118,7 @@ export class EditMenu { } this.diagramManager.onDiagramEventObservable.notifyObservers({ type: DiagramEventType.MODIFY, - entity: MeshConverter.toDiagramEntity(mesh), + entity: toDiagramEntity(mesh), }); } @@ -185,7 +185,7 @@ export class EditMenu { mesh.dispose(); this.diagramManager.onDiagramEventObservable.notifyObservers({ type: DiagramEventType.MODIFY, - entity: MeshConverter.toDiagramEntity(newMesh) + entity: toDiagramEntity(newMesh) }); } else { @@ -199,7 +199,7 @@ export class EditMenu { this.connection.to = mesh.id; this.diagramManager.onDiagramEventObservable.notifyObservers({ type: DiagramEventType.ADD, - entity: MeshConverter.toDiagramEntity(this.connection.mesh) + entity: toDiagramEntity(this.connection.mesh) }); this.connection = null; } else { @@ -212,7 +212,7 @@ export class EditMenu { const event: DiagramEvent = { type: DiagramEventType.REMOVE, entity: - MeshConverter.toDiagramEntity(mesh) + toDiagramEntity(mesh) } this.diagramManager.onDiagramEventObservable.notifyObservers(event); } @@ -226,8 +226,8 @@ export class EditMenu { this.gizmoManager.attachToMesh(mesh); this.gizmoManager.gizmos.boundingBoxGizmo.onScaleBoxDragObservable.add(() => { this.diagramManager.onDiagramEventObservable.notifyObservers({ - type: DiagramEventType.MODIFY, - entity: MeshConverter.toDiagramEntity(mesh), + type: DiagramEventType.MODIFY, + entity: toDiagramEntity(mesh), } ) this.logger.debug(mesh.scaling); diff --git a/src/toolbox/toolbox.ts b/src/toolbox/toolbox.ts index 6abf402..f90f342 100644 --- a/src/toolbox/toolbox.ts +++ b/src/toolbox/toolbox.ts @@ -32,7 +32,7 @@ export class Toolbox { private readonly gridsize = 5; private readonly addPanel: StackPanel3D; private readonly controllers: Controllers; - private xObserver; + private readonly xObserver; public readonly colorChangeObservable: Observable<{ oldColor: string, newColor: string }> = new Observable<{ oldColor: string; newColor: string }>() @@ -103,28 +103,27 @@ export class Toolbox { switch (type) { case ToolType.BOX: return MeshBuilder.CreateBox(toolname, {width: 1, height: 1, depth: 1}, this.scene); - break; + case ToolType.SPHERE: return MeshBuilder.CreateSphere(toolname, {diameter: 1}, this.scene); - break; + case ToolType.CYLINDER: return MeshBuilder.CreateCylinder(toolname, {height: 1, diameter: 1}, this.scene); - break; + case ToolType.CONE: return MeshBuilder.CreateCylinder(toolname, { diameterTop: 0, height: 1, diameterBottom: 1 }, this.scene); - break; + case ToolType.PLANE: return MeshBuilder.CreatePlane(toolname, {width: 1, height: 1}, this.scene); - break; + case ToolType.OBJECT: return null; - break; + } - return null; } private toolId(tool: ToolType, color: Color3) { diff --git a/src/tutorial/introduction.ts b/src/tutorial/introduction.ts index d11788c..d07d8ff 100644 --- a/src/tutorial/introduction.ts +++ b/src/tutorial/introduction.ts @@ -18,7 +18,7 @@ import Hls from "hls.js"; export class Introduction { - private scene: Scene; + private readonly scene: Scene; private manager: GUI3DManager; private physicsHelper: PhysicsHelper; private current: AbstractMesh[] = []; diff --git a/src/util/appConfig.ts b/src/util/appConfig.ts index 807ebbe..f1ce18d 100644 --- a/src/util/appConfig.ts +++ b/src/util/appConfig.ts @@ -4,11 +4,6 @@ import round from "round"; import {IPersistenceManager} from "../integration/iPersistenceManager"; import {AppConfigType} from "./appConfigType"; -export type SnapValue = { - value: number, - label: string -} - export class AppConfig { private readonly logger = log.getLogger('AppConfig'); public readonly onConfigChangedObservable = new Observable(); @@ -24,7 +19,7 @@ export class AppConfig { if (!this._currentConfig) { this.persistenceManager.getConfig().then((config) => { if (!config) { - const newconfig = { + this._currentConfig = { id: 1, gridSnap: .1, rotateSnap: 45, @@ -35,7 +30,6 @@ export class AppConfig { physicsEnabled: false, demoCompleted: false, } - this._currentConfig = newconfig; this.save(); } else { this._currentConfig = config; diff --git a/src/util/customPhysics.ts b/src/util/customPhysics.ts index db9ea42..d26876e 100644 --- a/src/util/customPhysics.ts +++ b/src/util/customPhysics.ts @@ -3,7 +3,7 @@ import HavokPhysics from "@babylonjs/havok"; import {AppConfig} from "./appConfig"; export class CustomPhysics { - private scene: Scene; + private readonly scene: Scene; private config: AppConfig; constructor(scene: Scene, config: AppConfig) {