|
| 1 | +/* global AFRAME, THREE */ |
| 2 | +AFRAME.registerComponent('hand-menu-button', { |
| 3 | + schema: { |
| 4 | + src: {default: ''}, |
| 5 | + srcHover: {default: ''}, |
| 6 | + mixin: {default: ''} |
| 7 | + }, |
| 8 | + |
| 9 | + init: function () { |
| 10 | + this.onWatchButtonHovered = this.onWatchButtonHovered.bind(this); |
| 11 | + this.onAnimationComplete = this.onAnimationComplete.bind(this); |
| 12 | + this.onCollisionStarted = this.onCollisionStarted.bind(this); |
| 13 | + this.onCollisionEnded = this.onCollisionEnded.bind(this); |
| 14 | + this.onAnimationBegin = this.onAnimationBegin.bind(this); |
| 15 | + this.onPinchEnded = this.onPinchEnded.bind(this); |
| 16 | + |
| 17 | + this.el.addEventListener('obbcollisionstarted', this.onCollisionStarted); |
| 18 | + this.el.addEventListener('obbcollisionended', this.onCollisionEnded); |
| 19 | + this.el.object3D.renderOrder = 1000; |
| 20 | + |
| 21 | + this.menuEl = this.el.parentEl; |
| 22 | + this.handMenuEl = this.el.sceneEl.querySelector('[hand-menu]'); |
| 23 | + |
| 24 | + this.menuEl.addEventListener('animationbegin', this.onAnimationBegin); |
| 25 | + this.menuEl.addEventListener('animationcomplete', this.onAnimationComplete); |
| 26 | + }, |
| 27 | + |
| 28 | + onAnimationBegin: function (evt) { |
| 29 | + // To prevent menu activations while animation is running. |
| 30 | + if (evt.detail.name === 'animation__open') { this.menuOpen = false; } |
| 31 | + }, |
| 32 | + |
| 33 | + onAnimationComplete: function (evt) { |
| 34 | + if (evt.detail.name === 'animation__open') { this.menuOpen = true; } |
| 35 | + if (evt.detail.name === 'animation__close') { this.menuOpen = false; } |
| 36 | + }, |
| 37 | + |
| 38 | + onCollisionStarted: function (evt) { |
| 39 | + var withEl = evt.detail.withEl; |
| 40 | + if (this.handMenuEl === withEl || |
| 41 | + !withEl.components['hand-tracking-controls']) { return; } |
| 42 | + if (!this.menuOpen) { return; } |
| 43 | + this.handHoveringEl = withEl; |
| 44 | + this.el.emit('watchbuttonhoverstarted'); |
| 45 | + }, |
| 46 | + |
| 47 | + onCollisionEnded: function (evt) { |
| 48 | + var withEl = evt.detail.withEl; |
| 49 | + if (this.handMenuEl === withEl || |
| 50 | + !withEl.components['hand-tracking-controls']) { return; } |
| 51 | + this.disableHover(); |
| 52 | + this.handHoveringEl = undefined; |
| 53 | + this.el.emit('watchbuttonhoverended'); |
| 54 | + }, |
| 55 | + |
| 56 | + enableHover: function () { |
| 57 | + this.handHoveringEl.addEventListener('pinchended', this.onPinchEnded); |
| 58 | + this.el.setAttribute('material', 'src', this.data.srcHover); |
| 59 | + }, |
| 60 | + |
| 61 | + disableHover: function () { |
| 62 | + if (!this.handHoveringEl) { return; } |
| 63 | + this.handHoveringEl.removeEventListener('pinchended', this.onPinchEnded); |
| 64 | + this.el.setAttribute('material', 'src', this.data.src); |
| 65 | + }, |
| 66 | + |
| 67 | + onPinchEnded: (function () { |
| 68 | + var spawnPosition = new THREE.Vector3(0, 1, 0); |
| 69 | + return function () { |
| 70 | + var cubeEl; |
| 71 | + var newEntityEl; |
| 72 | + if (!this.menuOpen) { return; } |
| 73 | + this.menuOpen = false; |
| 74 | + if (!this.handHoveringEl || !this.data.mixin) { return; } |
| 75 | + // Spawn shape a little above the menu. |
| 76 | + spawnPosition.set(0, 1, 0); |
| 77 | + // Apply rotation of the menu. |
| 78 | + spawnPosition.applyQuaternion(this.el.parentEl.object3D.quaternion); |
| 79 | + // 20cm above the menu. |
| 80 | + spawnPosition.normalize().multiplyScalar(0.2); |
| 81 | + spawnPosition.add(this.el.parentEl.object3D.position); |
| 82 | + |
| 83 | + newEntityEl = document.createElement('a-entity'); |
| 84 | + newEntityEl.setAttribute('mixin', this.data.mixin); |
| 85 | + newEntityEl.setAttribute('position', spawnPosition); |
| 86 | + this.el.sceneEl.appendChild(newEntityEl); |
| 87 | + this.handHoveringEl.removeEventListener('pinchended', this.onPinchEnded); |
| 88 | + }; |
| 89 | + })(), |
| 90 | + |
| 91 | + onWatchButtonHovered: function (evt) { |
| 92 | + if (evt.target === this.el || !this.handHoveringEl) { return; } |
| 93 | + this.disableHover(); |
| 94 | + this.handHoveringEl = undefined; |
| 95 | + } |
| 96 | +}); |
| 97 | + |
| 98 | +/* |
| 99 | + User's hand can collide with multiple buttons simulatenously but only want one in a hovered state. |
| 100 | + This system keeps track of all the collided buttons, keeping just the closest to the hand in a hovered state. |
| 101 | +*/ |
| 102 | +AFRAME.registerSystem('hand-menu-button', { |
| 103 | + init: function () { |
| 104 | + this.onWatchButtonHovered = this.onWatchButtonHovered.bind(this); |
| 105 | + this.el.parentEl.addEventListener('watchbuttonhoverended', this.onWatchButtonHovered); |
| 106 | + this.el.parentEl.addEventListener('watchbuttonhoverstarted', this.onWatchButtonHovered); |
| 107 | + this.hoveredButtonEls = []; |
| 108 | + }, |
| 109 | + |
| 110 | + tick: function () { |
| 111 | + var buttonWorldPosition = new THREE.Vector3(); |
| 112 | + var thumbPosition; |
| 113 | + var smallestDistance = 1000000; |
| 114 | + var currentDistance; |
| 115 | + var closestButtonEl; |
| 116 | + if (this.hoveredButtonEls.length < 2) { return; } |
| 117 | + thumbPosition = this.hoveredButtonEls[0].components['hand-menu-button'].handHoveringEl.components['obb-collider'].trackedObject3D.position; |
| 118 | + for (var i = 0; i < this.hoveredButtonEls.length; ++i) { |
| 119 | + this.hoveredButtonEls[i].object3D.getWorldPosition(buttonWorldPosition); |
| 120 | + currentDistance = buttonWorldPosition.distanceTo(thumbPosition); |
| 121 | + if (currentDistance < smallestDistance) { |
| 122 | + closestButtonEl = this.hoveredButtonEls[i]; |
| 123 | + smallestDistance = currentDistance; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (this.hoveredButtonEl === closestButtonEl) { return; } |
| 128 | + |
| 129 | + this.hoveredButtonEl = closestButtonEl; |
| 130 | + |
| 131 | + for (i = 0; i < this.hoveredButtonEls.length; ++i) { |
| 132 | + if (!this.hoveredButtonEls[i].components['hand-menu-button'].handHoveringEl) { continue; } |
| 133 | + if (this.hoveredButtonEls[i] === closestButtonEl) { |
| 134 | + this.hoveredButtonEls[i].components['hand-menu-button'].enableHover(); |
| 135 | + continue; |
| 136 | + } |
| 137 | + this.hoveredButtonEls[i].components['hand-menu-button'].disableHover(); |
| 138 | + } |
| 139 | + }, |
| 140 | + |
| 141 | + onWatchButtonHovered: function (evt) { |
| 142 | + this.buttonEls = this.el.sceneEl.querySelectorAll('[hand-menu-button]'); |
| 143 | + this.hoveredButtonEls = []; |
| 144 | + for (var i = 0; i < this.buttonEls.length; ++i) { |
| 145 | + if (!this.buttonEls[i].components['hand-menu-button'].handHoveringEl) { continue; } |
| 146 | + this.hoveredButtonEls.push(this.buttonEls[i]); |
| 147 | + } |
| 148 | + if (this.hoveredButtonEls.length === 1) { |
| 149 | + this.hoveredButtonEl = this.hoveredButtonEls[0]; |
| 150 | + this.hoveredButtonEls[0].components['hand-menu-button'].enableHover(); |
| 151 | + } |
| 152 | + } |
| 153 | +}); |
0 commit comments