PickTool.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. module INSPECTOR {
  2. export class PickTool extends AbstractTool {
  3. private _isActive : boolean = false;
  4. private _pickHandler: (evt: Event) => void;
  5. constructor(parent:HTMLElement, inspector:Inspector) {
  6. super('fa', 'fa-mouse-pointer', parent, inspector, 'Select a mesh in the scene');
  7. // Create handler
  8. this._pickHandler = this._pickMesh.bind(this);
  9. }
  10. // Action : find the corresponding tree item in the correct tab and display it
  11. public action() {
  12. if (this._isActive) {
  13. this._deactivate();
  14. } else {
  15. this.toHtml().classList.add('active');
  16. // Add event handler : pick on a mesh in the scene
  17. let canvas = <HTMLElement>this._inspector.scene.getEngine().getRenderingCanvas();
  18. canvas.addEventListener('click', this._pickHandler);
  19. this._isActive = true;
  20. }
  21. }
  22. /** Deactivate this tool */
  23. private _deactivate() {
  24. this.toHtml().classList.remove('active');
  25. // Remove event handler
  26. let canvas = <HTMLElement>this._inspector.scene.getEngine().getRenderingCanvas();
  27. canvas.removeEventListener('click', this._pickHandler);
  28. this._isActive = false;
  29. }
  30. /** Pick a mesh in the scene */
  31. private _pickMesh(evt: PointerEvent) {
  32. let pos = this._updatePointerPosition(evt);
  33. let pi = this._inspector.scene.pick(pos.x, pos.y, (mesh:BABYLON.AbstractMesh) => {return true});
  34. if (pi && pi.pickedMesh) {
  35. this._inspector.displayObjectDetails(pi.pickedMesh);
  36. }
  37. this._deactivate();
  38. }
  39. private _updatePointerPosition(evt: PointerEvent) : {x:number, y:number}{
  40. let canvasRect = <ClientRect>this._inspector.scene.getEngine().getRenderingCanvasClientRect();
  41. let pointerX = evt.clientX - canvasRect.left;
  42. let pointerY = evt.clientY - canvasRect.top;
  43. return {x:pointerX, y:pointerY};
  44. };
  45. }
  46. }