import { lazyInject } from "../../../inversify.config" ; import { HtmlDomService } from "../../../services/HtmlDomService" ; import Scene from "../Scene" ; import Event from "./Event" ; import { EventDispatcher } from "./EventDispatcher" ; export class SceneMouseInteractions { public static create ( ) { return new SceneMouseInteractions ( ) ; } private static EVENT_MOUSE_DOWN = "mousedown" ; @ lazyInject ( "HtmlDomService" ) private readonly htmlDomService ! : HtmlDomService ; /**
     * global window object mouse down event.
     */ private _onMouseDownEvent : EventDispatcher < boolean > = new EventDispatcher < boolean > ( ) ; private _onMouseMovedEvent : EventDispatcher < void > = new EventDispatcher < void > ( ) ; private _onSelectObjectEvent : EventDispatcher < MouseEvent > = new EventDispatcher < MouseEvent > ( ) ; private _mouseMoved = false ; private constructor ( ) { window . addEventListener ( SceneMouseInteractions . EVENT_MOUSE_DOWN , ( event ) => { this . handleMouseDown ( event as MouseEvent ) ; } ) ; } public destroy ( ) { window . removeEventListener ( SceneMouseInteractions . EVENT_MOUSE_DOWN , ( event ) => { this . handleMouseDown ( event as MouseEvent ) ; } ) ; } public addMouseDownEventListener ( callback ) { this . _onMouseDownEvent . addEventListener ( callback ) ; } public addMouseMovedEventListener ( callback ) { this . _onMouseMovedEvent . addEventListener ( callback ) ; } public addSelectObjectEventEventListener ( callback ) { this . _onSelectObjectEvent . addEventListener ( callback ) ; } public handleMouseDown ( event ) { const self = document . getElementById ( Scene . SCENE_CONTAINER_ID ) ; const isWithinScene = event . target === self || this . htmlDomService . isDescendant ( self , event . target as HTMLElement ) ; this . _onMouseDownEvent . dispatchEvent ( new Event < boolean > ( isWithinScene ) ) ; } public setMouseMoved ( mouseMoved ) { this . _mouseMoved = mouseMoved ; } public onMouseUp ( event ) { this . _onMouseMovedEvent . dispatchEvent ( new Event < void > ( undefined ) ) ; if ( ! this . _mouseMoved ) { this . _onSelectObjectEvent . dispatchEvent ( new Event < MouseEvent > ( event ) ) ; } } }