function bind(t,e){return o=Array.prototype.slice.call(arguments,2),function(){var n=o.concat(Array.prototype.slice.call(arguments,0));return t.apply(e,n)};var o}function PolyfillControls(t){var e;window.VRFrameData&&(e=new window.VRFrameData),this.update=function(){var o;vrDisplay&&polyfilledVRDisplay&&(vrDisplay.getFrameData(e),o=e.pose,null!==o.orientation&&t.quaternion.fromArray(o.orientation),null!==o.position?t.position.fromArray(o.position):t.position.set(0,0,0))}}var vrDisplay,polyfilledVRDisplay,THREE=AFRAME.THREE,GRABBING_CLASS="a-grabbing",PI_2=Math.PI/2,radToDeg=THREE.Math.radToDeg,checkHasPositionalTracking=AFRAME.utils.device.checkHasPositionalTracking,POLYFILL_VRDISPLAY_ID="Cardboard VRDisplay (webvr-polyfill)";AFRAME.registerComponent("touch-look-controls",{dependencies:["position","rotation"],schema:{enabled:{default:!0},touchEnabled:{default:!0},hmdEnabled:{default:!0},pointerLockEnabled:{default:!1},reverseMouseDrag:{default:!1}},init:function(){this.previousHMDPosition=new THREE.Vector3,this.hmdQuaternion=new THREE.Quaternion,this.hmdEuler=new THREE.Euler,this.position=new THREE.Vector3,this.savedRotation=new THREE.Vector3,this.savedPosition=new THREE.Vector3,this.polyfillObject=new THREE.Object3D,this.polyfillControls=new PolyfillControls(this.polyfillObject),this.rotation={},this.deltaRotation={},this.savedPose=null,this.pointerLocked=!1,this.setupMouseControls(),this.bindMethods(),this.el.sceneEl.is("vr-mode")&&this.onEnterVR()},update:function(t){var e=this.data;e.enabled!==t.enabled&&this.updateGrabCursor(e.enabled),!t||e.hmdEnabled||t.hmdEnabled||(this.pitchObject.rotation.set(0,0,0),this.yawObject.rotation.set(0,0,0)),t&&!e.pointerLockEnabled!==t.pointerLockEnabled&&(this.removeEventListeners(),this.addEventListeners(),this.pointerLocked&&document.exitPointerLock())},tick:function(t){var e=this.data;e.enabled&&this.updateOrientation()},play:function(){this.addEventListeners()},pause:function(){this.removeEventListeners()},remove:function(){this.removeEventListeners()},bindMethods:function(){this.onMouseDown=bind(this.onMouseDown,this),this.onMouseMove=bind(this.onMouseMove,this),this.onMouseUp=bind(this.onMouseUp,this),this.onTouchStart=bind(this.onTouchStart,this),this.onTouchMove=bind(this.onTouchMove,this),this.onTouchEnd=bind(this.onTouchEnd,this),this.onEnterVR=bind(this.onEnterVR,this),this.onExitVR=bind(this.onExitVR,this),this.onPointerLockChange=bind(this.onPointerLockChange,this),this.onPointerLockError=bind(this.onPointerLockError,this)},setupMouseControls:function(){this.mouseDown=!1,this.pitchObject=new THREE.Object3D,this.yawObject=new THREE.Object3D,this.yawObject.position.y=10,this.yawObject.add(this.pitchObject)},addEventListeners:function(){var t=this.el.sceneEl,e=t.canvas;e?(e.addEventListener("mousedown",this.onMouseDown,!1),window.addEventListener("mousemove",this.onMouseMove,!1),window.addEventListener("mouseup",this.onMouseUp,!1),e.addEventListener("touchstart",this.onTouchStart),window.addEventListener("touchmove",this.onTouchMove),window.addEventListener("touchend",this.onTouchEnd),t.addEventListener("enter-vr",this.onEnterVR),t.addEventListener("exit-vr",this.onExitVR),this.data.pointerLockEnabled&&(document.addEventListener("pointerlockchange",this.onPointerLockChange,!1),document.addEventListener("mozpointerlockchange",this.onPointerLockChange,!1),document.addEventListener("pointerlockerror",this.onPointerLockError,!1))):t.addEventListener("render-target-loaded",bind(this.addEventListeners,this))},removeEventListeners:function(){var t=this.el.sceneEl,e=t&&t.canvas;e&&(e.removeEventListener("mousedown",this.onMouseDown),window.removeEventListener("mousemove",this.onMouseMove),window.removeEventListener("mouseup",this.onMouseUp),e.removeEventListener("touchstart",this.onTouchStart),window.removeEventListener("touchmove",this.onTouchMove),window.removeEventListener("touchend",this.onTouchEnd),t.removeEventListener("enter-vr",this.onEnterVR),t.removeEventListener("exit-vr",this.onExitVR),document.removeEventListener("pointerlockchange",this.onPointerLockChange,!1),document.removeEventListener("mozpointerlockchange",this.onPointerLockChange,!1),document.removeEventListener("pointerlockerror",this.onPointerLockError,!1))},updateOrientation:function(){var t=this.hmdEuler,e=this.pitchObject,o=this.yawObject,n=this.el.sceneEl,i=this.rotation;n.is("vr-mode")&&n.checkHeadsetConnected()||(this.polyfillControls.update(),t.setFromQuaternion(this.polyfillObject.quaternion,"YXZ"),i.x=radToDeg(t.x)+radToDeg(e.rotation.x),i.y=radToDeg(t.y)+radToDeg(o.rotation.y),i.z=0,this.el.setAttribute("rotation",i))},onMouseMove:function(t){var e,o,n=this.pitchObject,i=this.yawObject,s=this.previousMouseEvent;this.data.enabled&&(this.mouseDown||this.pointerLocked)&&(e=t.movementX||t.mozMovementX,o=t.movementY||t.mozMovementY,void 0!==e&&void 0!==o||(e=t.screenX-s.screenX,o=t.screenY-s.screenY),this.previousMouseEvent=t,i.rotation.y-=.002*e,n.rotation.x-=.002*o,n.rotation.x=Math.max(-PI_2,Math.min(PI_2,n.rotation.x)))},onMouseDown:function(t){if(this.data.enabled&&0===t.button){var e=this.el.sceneEl,o=e&&e.canvas;this.mouseDown=!0,this.previousMouseEvent=t,document.body.classList.add(GRABBING_CLASS),this.data.pointerLockEnabled&&!this.pointerLocked&&(o.requestPointerLock?o.requestPointerLock():o.mozRequestPointerLock&&o.mozRequestPointerLock())}},onMouseUp:function(){this.mouseDown=!1,document.body.classList.remove(GRABBING_CLASS)},onTouchStart:function(t){1===t.touches.length&&this.data.touchEnabled&&(this.touchStart={x:t.touches[0].pageX,y:t.touches[0].pageY},this.touchStarted=!0)},onTouchMove:function(t){var e,o,n=this.el.sceneEl.canvas,i=this.pitchObject,s=this.yawObject;this.touchStarted&&this.data.touchEnabled&&(o=2*Math.PI*(t.touches[0].pageX-this.touchStart.x)/n.clientWidth,e=2*Math.PI*(t.touches[0].pageY-this.touchStart.y)/n.clientHeight,s.rotation.y-=.5*o,i.rotation.x-=.5*e,i.rotation.x=Math.max(-PI_2,Math.min(PI_2,i.rotation.x)),this.touchStart={x:t.touches[0].pageX,y:t.touches[0].pageY})},onTouchEnd:function(){this.touchStarted=!1},onEnterVR:function(){this.saveCameraPose()},onExitVR:function(){this.restoreCameraPose(),this.previousHMDPosition.set(0,0,0)},onPointerLockChange:function(){this.pointerLocked=!(!document.pointerLockElement&&!document.mozPointerLockElement)},onPointerLockError:function(){this.pointerLocked=!1},updateGrabCursor:function(t){function e(){n.canvas.classList.add("a-grab-cursor")}function o(){n.canvas.classList.remove("a-grab-cursor")}var n=this.el.sceneEl;n.canvas?t?e():o():t?n.addEventListener("render-target-loaded",e):n.addEventListener("render-target-loaded",o)},saveCameraPose:function(){var t=this.el,e=t.getAttribute("position"),o=t.getAttribute("rotation"),n=void 0!==this.hasPositionalTracking?this.hasPositionalTracking:checkHasPositionalTracking();!this.savedPose&&n&&(this.savedPose={position:this.savedPosition.copy(e),rotation:this.savedRotation.copy(o)})},restoreCameraPose:function(){var t=this.el,e=this.savedPose,o=void 0!==this.hasPositionalTracking?this.hasPositionalTracking:checkHasPositionalTracking();e&&o&&(t.setAttribute("position",e.position),t.setAttribute("rotation",e.rotation),this.savedPose=null)}});