-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrab.js
More file actions
89 lines (75 loc) · 3.26 KB
/
grab.js
File metadata and controls
89 lines (75 loc) · 3.26 KB
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
(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){
'use strict';
require('./src/misc/grab');
},{"./src/misc/grab":2}],2:[function(require,module,exports){
'use strict';
/* global CANNON */
/**
* Based on aframe/examples/showcase/tracked-controls.
*
* Handles events coming from the hand-controls.
* Determines if the entity is grabbed or released.
* Updates its position to move along the controller.
*/
module.exports = AFRAME.registerComponent('grab', {
init: function init() {
this.system = this.el.sceneEl.systems.physics;
this.GRABBED_STATE = 'grabbed';
this.grabbing = false;
this.hitEl = /** @type {AFRAME.Element} */null;
this.physics = /** @type {AFRAME.System} */this.el.sceneEl.systems.physics;
this.constraint = /** @type {CANNON.Constraint} */null;
// Bind event handlers
this.onHit = this.onHit.bind(this);
this.onGripOpen = this.onGripOpen.bind(this);
this.onGripClose = this.onGripClose.bind(this);
},
play: function play() {
var el = this.el;
el.addEventListener('hit', this.onHit);
el.addEventListener('gripdown', this.onGripClose);
el.addEventListener('gripup', this.onGripOpen);
el.addEventListener('trackpaddown', this.onGripClose);
el.addEventListener('trackpadup', this.onGripOpen);
el.addEventListener('triggerdown', this.onGripClose);
el.addEventListener('triggerup', this.onGripOpen);
},
pause: function pause() {
var el = this.el;
el.removeEventListener('hit', this.onHit);
el.removeEventListener('gripdown', this.onGripClose);
el.removeEventListener('gripup', this.onGripOpen);
el.removeEventListener('trackpaddown', this.onGripClose);
el.removeEventListener('trackpadup', this.onGripOpen);
el.removeEventListener('triggerdown', this.onGripClose);
el.removeEventListener('triggerup', this.onGripOpen);
},
onGripClose: function onGripClose() {
this.grabbing = true;
},
onGripOpen: function onGripOpen() {
var hitEl = this.hitEl;
this.grabbing = false;
if (!hitEl) {
return;
}
hitEl.removeState(this.GRABBED_STATE);
this.hitEl = undefined;
this.system.removeConstraint(this.constraint);
this.constraint = null;
},
onHit: function onHit(evt) {
var hitEl = evt.detail.el;
// If the element is already grabbed (it could be grabbed by another controller).
// If the hand is not grabbing the element does not stick.
// If we're already grabbing something you can't grab again.
if (!hitEl || hitEl.is(this.GRABBED_STATE) || !this.grabbing || this.hitEl) {
return;
}
hitEl.addState(this.GRABBED_STATE);
this.hitEl = hitEl;
this.constraint = new CANNON.LockConstraint(this.el.body, hitEl.body);
this.system.addConstraint(this.constraint);
}
});
},{}]},{},[1]);