import { injectable } from "inversify" ; import { MeshLambertMaterial , Scene } from "three" ; import { lazyInject } from "../../../../inversify.config" ; import SceneStore from "../../../../stores/SceneStore" ; import { SoftVis3dMesh } from "../../domain/SoftVis3dMesh" ; import { SoftVis3dShape } from "../../domain/SoftVis3dShape" ; import { ObjectFactory } from "./ObjectFactory" ; class struct constructor @ injectable ( ) export class Wrangler { private objectsInView : SoftVis3dMesh [ ] = [ ] ; @ lazyInject ( "SceneStore" ) private readonly sceneStore ! : SceneStore ; public loadSoftVis3d ( scene , data ) { this . removeAllFromScene ( scene ) ; this . objectsInView = ObjectFactory . getSceneObjects ( data ) ; for ( const object of this . objectsInView ) { scene . add ( object ) ; } } public updateColorsWithUpdatedShapes ( shapes ) { const resultObjects = ObjectFactory . getSceneObjects ( shapes ) ; for ( let index = 0 ; index < resultObjects . length ; index ++ ) { this . objectsInView [ index ] . material . color = resultObjects [ index ] . material . color ; } if ( this . sceneStore . selectedTreeObjects . length > 0 ) { const formerSelectedObjectId = this . sceneStore . selectedTreeObjects [ 0 ] . object . getSoftVis3dId ( ) ; this . sceneStore . selectedTreeObjects = [ ] ; this . selectSceneTreeObject ( formerSelectedObjectId ) ; } } public selectSceneTreeObject ( objectSoftVis3dId ) { for ( const previousSelection of this . sceneStore . selectedTreeObjects ) { previousSelection . object . material . color . setHex ( previousSelection . color ) ; } this . sceneStore . selectedTreeObjects = [ ] ; if ( objectSoftVis3dId !== null ) { for ( const obj of this . objectsInView ) { if ( objectSoftVis3dId === obj . getSoftVis3dId ( ) ) { const selectedObjectMaterial = obj . material ; const selectedObjectInformation = { object : obj , color : selectedObjectMaterial . color . getHex ( ) , } ; this . sceneStore . selectedTreeObjects . push ( selectedObjectInformation ) ; selectedObjectMaterial . color . setHex ( 0xffc519 ) ; } } } } public getObjectsInView ( ) { return this . objectsInView ; } public destroy ( scene ) { this . removeAllFromScene ( scene ) ; this . objectsInView = [ ] ; this . sceneStore . selectedTreeObjects = [ ] ; } private removeAllFromScene ( scene ) { while ( this . objectsInView . length ) { scene . remove ( this . objectsInView . pop ( ) as SoftVis3dMesh ) ; } } }