All files / app/codeCharta/ui/codeMap/threeViewer threeViewer.service.ts

69.23% Statements 45/65
11.11% Branches 2/18
56.25% Functions 9/16
69.84% Lines 44/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 11621x 21x 21x 21x 21x 21x     21x       100x 100x 100x 100x 100x       7x   7x 7x 7x 7x 7x 7x 7x   7x   7x 7x 7x   7x 7x                       100x 4x 4x 4x 4x 4x             100x 1x 1x       100x 1x 1x         13x 13x       8x 8x       1x                 1x                                            
import { Injectable } from "@angular/core"
import { ThreeSceneService } from "./threeSceneService"
import { ThreeCameraService } from "./threeCamera.service"
import { ThreeMapControlsService } from "./threeMapControls.service"
import { ThreeRendererService } from "./threeRenderer.service"
import { ThreeStatsService } from "./threeStats.service"
 
@Injectable({ providedIn: "root" })
export class ThreeViewerService {
    private animationFrameId: number
 
    constructor(
        private threeSceneService: ThreeSceneService,
        private threeCameraService: ThreeCameraService,
        private threeMapControlsService: ThreeMapControlsService,
        private threeRendererService: ThreeRendererService,
        private threeStatsService: ThreeStatsService
    ) {}
 
    init(target: Element) {
        this.threeCameraService.init(window.innerWidth, window.innerHeight)
 
        const camera = this.threeCameraService.camera
        const scene = this.threeSceneService.scene
        camera.lookAt(scene.position)
        scene.add(camera)
        this.threeRendererService.init(window.innerWidth, window.innerHeight, scene, camera)
        this.threeStatsService.init(target)
        this.threeMapControlsService.init(this.threeRendererService.renderer.domElement)
 
        target.append(this.threeRendererService.renderer.domElement)
 
        window.addEventListener("resize", this.onWindowResize)
        window.addEventListener("focusin", this.onFocusIn)
        window.addEventListener("focusout", this.onFocusOut)
 
        this.animate()
        this.animateStats()
    }
 
    restart(target: Element) {
        this.stopAnimate()
        this.destroy()
        this.init(target)
        this.autoFitTo()
        this.animate()
        this.animateStats()
    }
 
    onWindowResize = () => {
        this.threeSceneService.scene.updateMatrixWorld(false)
        this.threeRendererService.renderer.setSize(window.innerWidth, window.innerHeight)
        this.threeCameraService.camera.aspect = window.innerWidth / window.innerHeight
        this.threeCameraService.camera.updateProjectionMatrix()
        this.animate()
    }
 
    enableRotation(value: boolean) {
        this.threeMapControlsService.controls.enableRotate = value
    }
 
    onFocusIn = event => {
        if (event.target.nodeName === "INPUT") {
            this.threeMapControlsService.controls.stopListenToKeyEvents()
        }
    }
 
    onFocusOut = event => {
        if (event.target.nodeName === "INPUT") {
            this.threeMapControlsService.controls.listenToKeyEvents(window)
        }
    }
 
    animate() {
        this.threeMapControlsService.controls.update()
        this.threeRendererService.render()
    }
 
    animateStats() {
        this.animationFrameId = requestAnimationFrame(() => this.animateStats())
        this.threeStatsService.updateStats()
    }
 
    getRenderCanvas() {
        return this.threeRendererService.renderer.domElement
    }
 
    getRenderLoseExtention() {
        const gl = this.threeRendererService.renderer.getContext()
        return gl.getExtension("WEBGL_lose_context")
    }
 
    autoFitTo() {
        this.threeMapControlsService.autoFitTo()
    }
 
    stopAnimate() {
        cancelAnimationFrame(this.animationFrameId)
    }
 
    dispose() {
        this.threeRendererService?.composer?.dispose()
        this.threeRendererService?.renderer?.dispose()
    }
 
    destroy() {
        window.removeEventListener("resize", this.onWindowResize)
        window.removeEventListener("focusin", this.onFocusIn)
        window.removeEventListener("focusout", this.onFocusOut)
        this.dispose()
        this.threeStatsService.destroy()
        this.getRenderCanvas().remove()
        this.dispose()
    }
}