grab.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. 'use strict';
  3. require('./src/misc/grab');
  4. },{"./src/misc/grab":2}],2:[function(require,module,exports){
  5. 'use strict';
  6. /* global CANNON */
  7. /**
  8. * Based on aframe/examples/showcase/tracked-controls.
  9. *
  10. * Handles events coming from the hand-controls.
  11. * Determines if the entity is grabbed or released.
  12. * Updates its position to move along the controller.
  13. */
  14. module.exports = AFRAME.registerComponent('grab', {
  15. init: function init() {
  16. this.system = this.el.sceneEl.systems.physics;
  17. this.GRABBED_STATE = 'grabbed';
  18. this.grabbing = false;
  19. this.hitEl = /** @type {AFRAME.Element} */null;
  20. this.physics = /** @type {AFRAME.System} */this.el.sceneEl.systems.physics;
  21. this.constraint = /** @type {CANNON.Constraint} */null;
  22. // Bind event handlers
  23. this.onHit = this.onHit.bind(this);
  24. this.onGripOpen = this.onGripOpen.bind(this);
  25. this.onGripClose = this.onGripClose.bind(this);
  26. },
  27. play: function play() {
  28. var el = this.el;
  29. el.addEventListener('hit', this.onHit);
  30. el.addEventListener('gripdown', this.onGripClose);
  31. el.addEventListener('gripup', this.onGripOpen);
  32. el.addEventListener('trackpaddown', this.onGripClose);
  33. el.addEventListener('trackpadup', this.onGripOpen);
  34. el.addEventListener('triggerdown', this.onGripClose);
  35. el.addEventListener('triggerup', this.onGripOpen);
  36. },
  37. pause: function pause() {
  38. var el = this.el;
  39. el.removeEventListener('hit', this.onHit);
  40. el.removeEventListener('gripdown', this.onGripClose);
  41. el.removeEventListener('gripup', this.onGripOpen);
  42. el.removeEventListener('trackpaddown', this.onGripClose);
  43. el.removeEventListener('trackpadup', this.onGripOpen);
  44. el.removeEventListener('triggerdown', this.onGripClose);
  45. el.removeEventListener('triggerup', this.onGripOpen);
  46. },
  47. onGripClose: function onGripClose() {
  48. this.grabbing = true;
  49. },
  50. onGripOpen: function onGripOpen() {
  51. var hitEl = this.hitEl;
  52. this.grabbing = false;
  53. if (!hitEl) {
  54. return;
  55. }
  56. hitEl.removeState(this.GRABBED_STATE);
  57. this.hitEl = undefined;
  58. this.system.removeConstraint(this.constraint);
  59. this.constraint = null;
  60. },
  61. onHit: function onHit(evt) {
  62. var hitEl = evt.detail.el;
  63. // If the element is already grabbed (it could be grabbed by another controller).
  64. // If the hand is not grabbing the element does not stick.
  65. // If we're already grabbing something you can't grab again.
  66. if (!hitEl || hitEl.is(this.GRABBED_STATE) || !this.grabbing || this.hitEl) {
  67. return;
  68. }
  69. hitEl.addState(this.GRABBED_STATE);
  70. this.hitEl = hitEl;
  71. this.constraint = new CANNON.LockConstraint(this.el.body, hitEl.body);
  72. this.system.addConstraint(this.constraint);
  73. }
  74. });
  75. },{}]},{},[1]);