All files / app/codeCharta/ui/viewCube viewCube.mouseEvents.service.ts

85.22% Statements 75/88
78.94% Branches 15/19
60% Functions 15/25
91.35% Lines 74/81

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 16320x 20x 20x 20x 20x 20x 20x 20x                   20x 49x         49x   49x   49x     1x 1x 1x 1x 1x               1x 1x 1x 1x 1x       6x 6x 6x 6x 6x 6x       1x 1x       1x 1x         1x               2x 1x         1x 1x 1x 1x 1x 1x 1x     1x 1x       1x   1x 1x 1x 1x 1x       5x       5x       5x     5x 5x 2x 1x 1x 1x     3x 1x   3x         3x 3x 3x 1x   2x         1x 1x 1x       1x 1x 1x                  
import { hierarchy } from "d3-hierarchy"
import { CodeMapMouseEventService, CursorType } from "../codeMap/codeMap.mouseEvent.service"
import { Group, Mesh, PerspectiveCamera, Raycaster, Vector2, WebGLRenderer } from "three"
import { isLeaf } from "../../util/codeMapHelper"
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"
import { ThreeMapControlsService } from "../codeMap/threeViewer/threeMapControls.service"
import { Injectable } from "@angular/core"
import { EventEmitter } from "../../util/EventEmitter"
 
type ViewCubeEvents = {
    viewCubeEventPropagation: (data: { event: MouseEvent; type: string }) => void
    viewCubeHoveredEvent: (data: { cube: Mesh }) => void
    viewCubeUnHoveredEvent: (data: undefined) => void
    viewCubeClicked: (data: { cube: Mesh }) => void
}
 
@Injectable({ providedIn: "root" })
export class ViewCubeMouseEventsService {
    private eventEmitter = new EventEmitter<ViewCubeEvents>()
 
    private cubeGroup: Group
    private camera: PerspectiveCamera
    private renderer: WebGLRenderer
    private currentlyHovered: Mesh | null = null
    private controls: OrbitControls
    private isDragging = false
 
    constructor(private threeMapControlsService: ThreeMapControlsService) {}
 
    init(cubeGroup: Group, camera: PerspectiveCamera, renderer: WebGLRenderer) {
        this.cubeGroup = cubeGroup
        this.camera = camera
        this.renderer = renderer
        this.initOrbitalControl(camera, renderer)
        this.initRendererEventListeners(renderer)
    }
 
    resetIsDragging() {
        this.isDragging = false
    }
 
    private initOrbitalControl(camera: PerspectiveCamera, renderer: WebGLRenderer) {
        this.controls = new OrbitControls(camera, renderer.domElement)
        this.controls.maxPolarAngle = Math.PI / 2
        this.controls.enableZoom = false
        this.controls.enablePan = false
        this.controls.rotateSpeed = 1
    }
 
    private initRendererEventListeners(renderer: WebGLRenderer) {
        renderer.domElement.addEventListener("mousemove", (event: MouseEvent) => this.onDocumentMouseMove(event))
        renderer.domElement.addEventListener("mouseup", (event: MouseEvent) => this.onDocumentMouseUp(event))
        renderer.domElement.addEventListener("mousedown", (event: MouseEvent) => this.onDocumentMouseClick(event, "mousedown"))
        renderer.domElement.addEventListener("dblclick", (event: MouseEvent) => this.onDocumentMouseClick(event, "dblclick"))
        renderer.domElement.addEventListener("mouseleave", (event: MouseEvent) => this.onWindowMouseLeave(event))
        renderer.domElement.addEventListener("mouseenter", () => this.onDocumentMouseEnter())
    }
 
    private onDocumentMouseClick(event: MouseEvent, mouseAction: string) {
        this.isDragging = true
        this.checkMouseIntersection(event, mouseAction)
    }
 
    private onWindowMouseLeave(event: MouseEvent) {
        if (event.relatedTarget == null || !(event.relatedTarget instanceof HTMLCanvasElement)) {
            this.enableRotation(false)
        }
    }
 
    private onDocumentMouseEnter() {
        this.enableRotation(true)
    }
 
    enableRotation(value: boolean) {
        this.controls.enableRotate = value
    }
 
    private checkMouseIntersection(event: MouseEvent, type: string) {
        if (!this.getCubeIntersectedByMouse(event)) {
            this.eventEmitter.emit("viewCubeEventPropagation", { type, event })
        }
    }
 
    private getCubeIntersectedByMouse(event: MouseEvent): Mesh | null {
        const vector = this.transformIntoCanvasVector(event)
        const ray = new Raycaster()
        ray.setFromCamera(vector, this.camera)
        const nodes: Group[] = []
        for (const node of hierarchy(this.cubeGroup)) {
            if (isLeaf(node)) {
                nodes.push(node.data)
            }
        }
        const [intersection] = ray.intersectObjects(nodes)
        return intersection ? (intersection.object as Mesh) : null
    }
 
    private transformIntoCanvasVector(event: MouseEvent) {
        const { domElement } = this.renderer
 
        const pixelRatio = this.renderer.getPixelRatio()
        const rect = domElement.getBoundingClientRect()
        const x = ((event.clientX - rect.left) / domElement.width) * pixelRatio * 2 - 1
        const y = -(((event.clientY - rect.top) / domElement.height) * pixelRatio) * 2 + 1
        return new Vector2(x, y)
    }
 
    propagateMovement() {
        Iif (this.isDragging) {
            const vec3 = this.camera.position
            this.threeMapControlsService.rotateCameraInVectorDirection(-vec3.x, -vec3.y, -vec3.z)
        }
        return this.isDragging
    }
 
    private onDocumentMouseMove(event: MouseEvent) {
        Iif (this.propagateMovement()) {
            return
        }
        const cube = this.getCubeIntersectedByMouse(event)
        if (cube) {
            if (this.currentlyHovered && cube.uuid !== this.currentlyHovered.uuid) {
                this.triggerViewCubeUnhoverEvent()
            } else if (!this.currentlyHovered) {
                this.triggerViewCubeHoverEvent(cube)
            }
        } else {
            if (this.currentlyHovered) {
                this.triggerViewCubeUnhoverEvent()
            }
            this.eventEmitter.emit("viewCubeEventPropagation", { type: "mousemove", event })
        }
    }
 
    private onDocumentMouseUp(event: MouseEvent) {
        this.isDragging = false
        const cube = this.getCubeIntersectedByMouse(event)
        if (cube) {
            this.eventEmitter.emit("viewCubeClicked", { cube })
        } else {
            this.eventEmitter.emit("viewCubeEventPropagation", { type: "mouseup", event })
        }
    }
 
    private triggerViewCubeHoverEvent(cube: Mesh) {
        this.currentlyHovered = cube
        CodeMapMouseEventService.changeCursorIndicator(CursorType.Pointer)
        this.eventEmitter.emit("viewCubeHoveredEvent", { cube })
    }
 
    private triggerViewCubeUnhoverEvent() {
        this.currentlyHovered = null
        CodeMapMouseEventService.changeCursorIndicator(CursorType.Default)
        this.eventEmitter.emit("viewCubeUnHoveredEvent")
    }
 
    subscribe<Key extends keyof ViewCubeEvents>(key: Key, callback: ViewCubeEvents[Key]) {
        this.eventEmitter.on(key, data => {
            callback(data)
        })
    }
}