File

projects/atft/src/lib/raycaster/raycaster-group.directive.ts

Description

Only components marked as atft-raycaster-group emit raycaster events. NOTE: All childs are also considered by raycaster (very usefull, for composite components).

Implements

AfterViewInit OnDestroy

Metadata

Index

Methods
Outputs

Constructor

constructor(host: AbstractObject3D<any>, raycasterService: RaycasterService)
Parameters :
Name Type Optional
host AbstractObject3D<any> No
raycasterService RaycasterService No

Outputs

click
Type : EventEmitter
mouseEnter
Type : EventEmitter
mouseExit
Type : EventEmitter

Methods

ngAfterViewInit
ngAfterViewInit()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
Private onClick
onClick(event: any)
Parameters :
Name Type Optional
event any No
Returns : void
Private onMouseEnter
onMouseEnter(event: any)
Parameters :
Name Type Optional
event any No
Returns : void
Private onMouseExit
onMouseExit()
Returns : void
Private subscribeEvents
subscribeEvents()
Returns : void
Private unSubscribeEvents
unSubscribeEvents()
Returns : void
import {AfterViewInit, Directive, EventEmitter, OnDestroy, Output} from '@angular/core';
import {AbstractObject3D} from '../object/abstract-object-3d';
import {RaycasterService} from './raycaster.service';
import {RaycasterEvent} from './raycaster-event';
import * as THREE from 'three';

export interface RaycasterEmitEvent {
  component: AbstractObject3D<any>;
  face?: THREE.Face;
}

/**
 * Only components marked as atft-raycaster-group emit raycaster events.
 * NOTE: All childs are also considered by raycaster (very usefull, for composite components).
 */
@Directive({selector: '[atft-raycaster-group]'})
export class RaycasterGroupDirective implements AfterViewInit, OnDestroy {

  @Output() mouseEnter = new EventEmitter<RaycasterEmitEvent>();
  @Output() mouseExit = new EventEmitter<RaycasterEmitEvent>();
  @Output() click = new EventEmitter<RaycasterEmitEvent>();

  constructor(
    private host: AbstractObject3D<any>,
    private raycasterService: RaycasterService
  ) {
    this.onMouseEnter = this.onMouseEnter.bind(this);
    this.onMouseExit = this.onMouseExit.bind(this);
    this.onClick = this.onClick.bind(this);
  }

  ngAfterViewInit(): void {
    this.raycasterService.addGroup(this.host);
    this.subscribeEvents();
  }

  private subscribeEvents() {
    const obj = this.host.getObject();
    obj.addEventListener(RaycasterEvent.mouseEnter, this.onMouseEnter);
    obj.addEventListener(RaycasterEvent.mouseExit, this.onMouseExit);
    obj.addEventListener(RaycasterEvent.click, this.onClick);
  }

  private unSubscribeEvents() {
    const obj = this.host.getObject();
    if (obj) {
      obj.removeEventListener(RaycasterEvent.mouseEnter, this.onMouseEnter);
      obj.removeEventListener(RaycasterEvent.mouseExit, this.onMouseExit);
      obj.removeEventListener(RaycasterEvent.click, this.onClick);
    }
  }

  private onMouseExit() {
    this.mouseExit.emit({
      component: this.host
    });
  }

  private onMouseEnter(event : any) {
    // console.log('RaycasterGroupDirective.onMouseEnter', event);
    this.mouseEnter.emit({
      component: this.host,
      face: event.face
    });
  }

  private onClick(event : any) {
    // console.log('onClick', event);
    this.click.emit({
      component: this.host,
      face: event.face
    });
  }

  ngOnDestroy(): void {
    this.unSubscribeEvents();
  }

}

results matching ""

    No results matching ""