aframe-gamepad-controls
Advanced tools
Comparing version 0.3.5 to 0.4.0
@@ -82,4 +82,4 @@ /******/ (function(modules) { // webpackBootstrap | ||
/******************************************************************* | ||
* Statics | ||
*/ | ||
* Statics | ||
*/ | ||
@@ -89,4 +89,4 @@ GamepadButton: GamepadButton, | ||
/******************************************************************* | ||
* Schema | ||
*/ | ||
* Schema | ||
*/ | ||
@@ -102,2 +102,3 @@ schema: { | ||
flyEnabled: { default: false }, | ||
invertAxisY: { default: false }, | ||
@@ -119,4 +120,4 @@ // Constants | ||
/******************************************************************* | ||
* Core | ||
*/ | ||
* Core | ||
*/ | ||
@@ -179,4 +180,4 @@ /** | ||
/******************************************************************* | ||
* Movement | ||
*/ | ||
* Movement | ||
*/ | ||
@@ -234,6 +235,10 @@ updatePosition: function (reset) { | ||
getMovementVector: (function () { | ||
getMovementVector: function (delta) { | ||
if (this._getMovementVector) { | ||
return this._getMovementVector(delta); | ||
} | ||
var rotation = new THREE.Euler(0, 0, 0, 'YXZ'); | ||
return function (delta) { | ||
this._getMovementVector = function (delta) { | ||
var elRotation = this.el.getAttribute('rotation'); | ||
@@ -252,9 +257,15 @@ this.direction.copy(this.velocity); | ||
}; | ||
}()), | ||
return this._getMovementVector(delta); | ||
}, | ||
/******************************************************************* | ||
* Rotation | ||
*/ | ||
updateRotation: (function () { | ||
* Rotation | ||
*/ | ||
updateRotation: function () { | ||
if (this._updateRotation) { | ||
return this._updateRotation(); | ||
} | ||
var initialRotation = new THREE.Vector3(), | ||
@@ -271,3 +282,3 @@ prevInitialRotation = new THREE.Vector3(), | ||
return function () { | ||
this._updateRotation = function () { | ||
if (!this.data.lookEnabled || !this.getGamepad()) { | ||
@@ -300,2 +311,3 @@ return; | ||
if (Math.abs(lookVector.y) <= JOYSTICK_EPS) lookVector.y = 0; | ||
if (this.data.invertAxisY) lookVector.y = -lookVector.y; | ||
@@ -321,7 +333,9 @@ // If external controls have been active more recently than gamepad, | ||
}; | ||
}()), | ||
return this._updateRotation(); | ||
}, | ||
/******************************************************************* | ||
* Button events | ||
*/ | ||
* Button events | ||
*/ | ||
@@ -360,4 +374,4 @@ updateButtonState: function () { | ||
/******************************************************************* | ||
* Gamepad state | ||
*/ | ||
* Gamepad state | ||
*/ | ||
@@ -364,0 +378,0 @@ /** |
@@ -1,1 +0,1 @@ | ||
!function(t){function e(n){if(i[n])return i[n].exports;var o=i[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e,i){!function(t){return t?void(t.aframeCore||t).registerComponent("gamepad-controls",i(1)):void console.error("Component attempted to register before AFRAME was available.")}(window.AFRAME)},function(t,e,i){var n=i(2),o=i(3),a=.2,s=Math.PI/2,r=.2;t.exports={GamepadButton:n,schema:{controller:{"default":0,oneOf:[0,1,2,3]},enabled:{"default":!0},movementEnabled:{"default":!0},lookEnabled:{"default":!0},flyEnabled:{"default":!1},easing:{"default":20},acceleration:{"default":65},sensitivity:{"default":.04},pitchAxis:{"default":"x",oneOf:["x","y","z"]},yawAxis:{"default":"y",oneOf:["x","y","z"]},rollAxis:{"default":"z",oneOf:["x","y","z"]},debug:{"default":!1}},init:function(){var t=this.el.sceneEl;this.prevTime=window.performance.now(),this.velocity=new THREE.Vector3(0,0,0),this.direction=new THREE.Vector3(0,0,0),this.pitch=new THREE.Object3D,this.yaw=new THREE.Object3D,this.yaw.position.y=10,this.yaw.add(this.pitch),this.buttons={},t.addBehavior(this),this.getGamepad()||console.warn("Gamepad #%d not found. Connect controller and press any button to continue.",this.data.controller)},update:function(t){this.updateRotation(),this.updatePosition(!!t),this.updateButtonState()},tick:function(){this.updateRotation(),this.updatePosition(),this.updateButtonState()},remove:function(){},updatePosition:function(t){var e=this.data,i=e.acceleration,n=e.easing,o=this.velocity,s=window.performance.now(),d=(s-this.prevTime)/1e3,u=e.rollAxis,c=e.pitchAxis,h=this.el,p=this.getGamepad();if(this.prevTime=s,t||d>a)return o[u]=0,void(o[c]=0);o[u]-=o[u]*n*d,o[c]-=o[c]*n*d;var l=h.getComputedAttribute("position");if(e.enabled&&e.movementEnabled&&p){var f=this.getDpad(),E=f.x||this.getJoystick(0).x,y=f.y||this.getJoystick(0).y;Math.abs(E)>r&&(o[c]+=E*i*d),Math.abs(y)>r&&(o[u]+=y*i*d)}var b=this.getMovementVector(d);h.object3D.translateX(b.x),h.object3D.translateY(b.y),h.object3D.translateZ(b.z),h.setAttribute("position",{x:l.x+b.x,y:l.y+b.y,z:l.z+b.z})},getMovementVector:function(){var t=new THREE.Euler(0,0,0,"YXZ");return function(e){var i=this.el.getAttribute("rotation");return this.direction.copy(this.velocity),this.direction.multiplyScalar(e),i?(this.data.flyEnabled||(i.x=0),t.set(THREE.Math.degToRad(i.x),THREE.Math.degToRad(i.y),0),this.direction.applyEuler(t),this.direction):this.direction}}(),updateRotation:function(){var t,e=new THREE.Vector3,i=new THREE.Vector3,n=new THREE.Vector3,o=0,a=0,d=1e-4,u=500;return function(){if(this.data.lookEnabled&&this.getGamepad()){if(t=Date.now(),e.copy(this.el.getAttribute("rotation")||e),e.distanceToSquared(i)>d&&e.distanceToSquared(n)>d)return i.copy(e),void(a=t);if(i.copy(e),!(u>t-a)){var c=this.getJoystick(1);Math.abs(c.x)<=r&&(c.x=0),Math.abs(c.y)<=r&&(c.y=0),a>o&&!c.lengthSq()||(c.multiplyScalar(this.data.sensitivity),this.yaw.rotation.y-=c.x,this.pitch.rotation.x-=c.y,this.pitch.rotation.x=Math.max(-s,Math.min(s,this.pitch.rotation.x)),this.el.setAttribute("rotation",{x:THREE.Math.radToDeg(this.pitch.rotation.x),y:THREE.Math.radToDeg(this.yaw.rotation.y),z:0}),n.copy(this.el.getAttribute("rotation")),o=t)}}}}(),updateButtonState:function(){var t=this.getGamepad();if(this.data.enabled&&t)for(var e=0;e<t.buttons.length;e++)t.buttons[e].pressed&&!this.buttons[e]?this.emit(new o("gamepadbuttondown",e,t.buttons[e])):!t.buttons[e].pressed&&this.buttons[e]&&this.emit(new o("gamepadbuttonup",e,t.buttons[e])),this.buttons[e]=t.buttons[e].pressed;else Object.keys(this.buttons)&&(this.buttons={})},emit:function(t){this.el.emit(t.type,t),this.el.emit(t.type+":"+t.index,new o(t.type,t.index,t))},getGamepad:function(){var t=navigator.getGamepads&&navigator.getGamepads()[this.data.controller],e=this.el.sceneEl.components["proxy-controls"],i=e&&e.isConnected()&&e.getGamepad(this.data.controller);return i||t},getButton:function(t){return this.getGamepad().buttons[t]},getAxis:function(t){return this.getGamepad().axes[t]},getJoystick:function(t){var e=this.getGamepad();switch(t){case 0:return new THREE.Vector2(e.axes[0],e.axes[1]);case 1:return new THREE.Vector2(e.axes[2],e.axes[3]);default:throw new Error('Unexpected joystick index "%d".',t)}},getDpad:function(){var t=this.getGamepad();return t.buttons[n.DPAD_RIGHT]?new THREE.Vector2((t.buttons[n.DPAD_RIGHT].pressed?1:0)+(t.buttons[n.DPAD_LEFT].pressed?-1:0),(t.buttons[n.DPAD_UP].pressed?-1:0)+(t.buttons[n.DPAD_DOWN].pressed?1:0)):new THREE.Vector2},isConnected:function(){var t=this.getGamepad();return!(!t||!t.connected)},getID:function(){return this.getGamepad().id}}},function(t,e){t.exports=Object.assign(function(){},{FACE_1:0,FACE_2:1,FACE_3:2,FACE_4:3,L_SHOULDER_1:4,R_SHOULDER_1:5,L_SHOULDER_2:6,R_SHOULDER_2:7,SELECT:8,START:9,DPAD_UP:12,DPAD_DOWN:13,DPAD_LEFT:14,DPAD_RIGHT:15,VENDOR:16})},function(t,e){function i(t,e,i){this.type=t,this.index=e,this.pressed=i.pressed,this.value=i.value}t.exports=i}]); | ||
!function(t){function e(n){if(i[n])return i[n].exports;var o=i[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e,i){!function(t){return t?void(t.aframeCore||t).registerComponent("gamepad-controls",i(1)):void console.error("Component attempted to register before AFRAME was available.")}(window.AFRAME)},function(t,e,i){var n=i(2),o=i(3),a=.2,s=Math.PI/2,r=.2;t.exports={GamepadButton:n,schema:{controller:{"default":0,oneOf:[0,1,2,3]},enabled:{"default":!0},movementEnabled:{"default":!0},lookEnabled:{"default":!0},flyEnabled:{"default":!1},invertAxisY:{"default":!1},easing:{"default":20},acceleration:{"default":65},sensitivity:{"default":.04},pitchAxis:{"default":"x",oneOf:["x","y","z"]},yawAxis:{"default":"y",oneOf:["x","y","z"]},rollAxis:{"default":"z",oneOf:["x","y","z"]},debug:{"default":!1}},init:function(){var t=this.el.sceneEl;this.prevTime=window.performance.now(),this.velocity=new THREE.Vector3(0,0,0),this.direction=new THREE.Vector3(0,0,0),this.pitch=new THREE.Object3D,this.yaw=new THREE.Object3D,this.yaw.position.y=10,this.yaw.add(this.pitch),this.buttons={},t.addBehavior(this),this.getGamepad()||console.warn("Gamepad #%d not found. Connect controller and press any button to continue.",this.data.controller)},update:function(t){this.updateRotation(),this.updatePosition(!!t),this.updateButtonState()},tick:function(){this.updateRotation(),this.updatePosition(),this.updateButtonState()},remove:function(){},updatePosition:function(t){var e=this.data,i=e.acceleration,n=e.easing,o=this.velocity,s=window.performance.now(),d=(s-this.prevTime)/1e3,u=e.rollAxis,c=e.pitchAxis,h=this.el,p=this.getGamepad();if(this.prevTime=s,t||d>a)return o[u]=0,void(o[c]=0);o[u]-=o[u]*n*d,o[c]-=o[c]*n*d;var l=h.getComputedAttribute("position");if(e.enabled&&e.movementEnabled&&p){var f=this.getDpad(),E=f.x||this.getJoystick(0).x,y=f.y||this.getJoystick(0).y;Math.abs(E)>r&&(o[c]+=E*i*d),Math.abs(y)>r&&(o[u]+=y*i*d)}var m=this.getMovementVector(d);h.object3D.translateX(m.x),h.object3D.translateY(m.y),h.object3D.translateZ(m.z),h.setAttribute("position",{x:l.x+m.x,y:l.y+m.y,z:l.z+m.z})},getMovementVector:function(t){if(this._getMovementVector)return this._getMovementVector(t);var e=new THREE.Euler(0,0,0,"YXZ");return this._getMovementVector=function(t){var i=this.el.getAttribute("rotation");return this.direction.copy(this.velocity),this.direction.multiplyScalar(t),i?(this.data.flyEnabled||(i.x=0),e.set(THREE.Math.degToRad(i.x),THREE.Math.degToRad(i.y),0),this.direction.applyEuler(e),this.direction):this.direction},this._getMovementVector(t)},updateRotation:function(){if(this._updateRotation)return this._updateRotation();var t,e=new THREE.Vector3,i=new THREE.Vector3,n=new THREE.Vector3,o=0,a=0,d=1e-4,u=500;return this._updateRotation=function(){if(this.data.lookEnabled&&this.getGamepad()){if(t=Date.now(),e.copy(this.el.getAttribute("rotation")||e),e.distanceToSquared(i)>d&&e.distanceToSquared(n)>d)return i.copy(e),void(a=t);if(i.copy(e),!(u>t-a)){var c=this.getJoystick(1);Math.abs(c.x)<=r&&(c.x=0),Math.abs(c.y)<=r&&(c.y=0),this.data.invertAxisY&&(c.y=-c.y),a>o&&!c.lengthSq()||(c.multiplyScalar(this.data.sensitivity),this.yaw.rotation.y-=c.x,this.pitch.rotation.x-=c.y,this.pitch.rotation.x=Math.max(-s,Math.min(s,this.pitch.rotation.x)),this.el.setAttribute("rotation",{x:THREE.Math.radToDeg(this.pitch.rotation.x),y:THREE.Math.radToDeg(this.yaw.rotation.y),z:0}),n.copy(this.el.getAttribute("rotation")),o=t)}}},this._updateRotation()},updateButtonState:function(){var t=this.getGamepad();if(this.data.enabled&&t)for(var e=0;e<t.buttons.length;e++)t.buttons[e].pressed&&!this.buttons[e]?this.emit(new o("gamepadbuttondown",e,t.buttons[e])):!t.buttons[e].pressed&&this.buttons[e]&&this.emit(new o("gamepadbuttonup",e,t.buttons[e])),this.buttons[e]=t.buttons[e].pressed;else Object.keys(this.buttons)&&(this.buttons={})},emit:function(t){this.el.emit(t.type,t),this.el.emit(t.type+":"+t.index,new o(t.type,t.index,t))},getGamepad:function(){var t=navigator.getGamepads&&navigator.getGamepads()[this.data.controller],e=this.el.sceneEl.components["proxy-controls"],i=e&&e.isConnected()&&e.getGamepad(this.data.controller);return i||t},getButton:function(t){return this.getGamepad().buttons[t]},getAxis:function(t){return this.getGamepad().axes[t]},getJoystick:function(t){var e=this.getGamepad();switch(t){case 0:return new THREE.Vector2(e.axes[0],e.axes[1]);case 1:return new THREE.Vector2(e.axes[2],e.axes[3]);default:throw new Error('Unexpected joystick index "%d".',t)}},getDpad:function(){var t=this.getGamepad();return t.buttons[n.DPAD_RIGHT]?new THREE.Vector2((t.buttons[n.DPAD_RIGHT].pressed?1:0)+(t.buttons[n.DPAD_LEFT].pressed?-1:0),(t.buttons[n.DPAD_UP].pressed?-1:0)+(t.buttons[n.DPAD_DOWN].pressed?1:0)):new THREE.Vector2},isConnected:function(){var t=this.getGamepad();return!(!t||!t.connected)},getID:function(){return this.getGamepad().id}}},function(t,e){t.exports=Object.assign(function(){},{FACE_1:0,FACE_2:1,FACE_3:2,FACE_4:3,L_SHOULDER_1:4,R_SHOULDER_1:5,L_SHOULDER_2:6,R_SHOULDER_2:7,SELECT:8,START:9,DPAD_UP:12,DPAD_DOWN:13,DPAD_LEFT:14,DPAD_RIGHT:15,VENDOR:16})},function(t,e){function i(t,e,i){this.type=t,this.index=e,this.pressed=i.pressed,this.value=i.value}t.exports=i}]); |
@@ -19,4 +19,4 @@ /** | ||
/******************************************************************* | ||
* Statics | ||
*/ | ||
* Statics | ||
*/ | ||
@@ -26,4 +26,4 @@ GamepadButton: GamepadButton, | ||
/******************************************************************* | ||
* Schema | ||
*/ | ||
* Schema | ||
*/ | ||
@@ -39,2 +39,3 @@ schema: { | ||
flyEnabled: { default: false }, | ||
invertAxisY: { default: false }, | ||
@@ -56,4 +57,4 @@ // Constants | ||
/******************************************************************* | ||
* Core | ||
*/ | ||
* Core | ||
*/ | ||
@@ -116,4 +117,4 @@ /** | ||
/******************************************************************* | ||
* Movement | ||
*/ | ||
* Movement | ||
*/ | ||
@@ -171,6 +172,10 @@ updatePosition: function (reset) { | ||
getMovementVector: (function () { | ||
getMovementVector: function (delta) { | ||
if (this._getMovementVector) { | ||
return this._getMovementVector(delta); | ||
} | ||
var rotation = new THREE.Euler(0, 0, 0, 'YXZ'); | ||
return function (delta) { | ||
this._getMovementVector = function (delta) { | ||
var elRotation = this.el.getAttribute('rotation'); | ||
@@ -189,9 +194,15 @@ this.direction.copy(this.velocity); | ||
}; | ||
}()), | ||
return this._getMovementVector(delta); | ||
}, | ||
/******************************************************************* | ||
* Rotation | ||
*/ | ||
updateRotation: (function () { | ||
* Rotation | ||
*/ | ||
updateRotation: function () { | ||
if (this._updateRotation) { | ||
return this._updateRotation(); | ||
} | ||
var initialRotation = new THREE.Vector3(), | ||
@@ -208,3 +219,3 @@ prevInitialRotation = new THREE.Vector3(), | ||
return function () { | ||
this._updateRotation = function () { | ||
if (!this.data.lookEnabled || !this.getGamepad()) { | ||
@@ -237,2 +248,3 @@ return; | ||
if (Math.abs(lookVector.y) <= JOYSTICK_EPS) lookVector.y = 0; | ||
if (this.data.invertAxisY) lookVector.y = -lookVector.y; | ||
@@ -258,7 +270,9 @@ // If external controls have been active more recently than gamepad, | ||
}; | ||
}()), | ||
return this._updateRotation(); | ||
}, | ||
/******************************************************************* | ||
* Button events | ||
*/ | ||
* Button events | ||
*/ | ||
@@ -297,4 +311,4 @@ updateButtonState: function () { | ||
/******************************************************************* | ||
* Gamepad state | ||
*/ | ||
* Gamepad state | ||
*/ | ||
@@ -301,0 +315,0 @@ /** |
{ | ||
"name": "aframe-gamepad-controls", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"description": "Gamepad controls for A-Frame.", | ||
@@ -5,0 +5,0 @@ "main": "gamepad-controls.js", |
@@ -1,2 +0,2 @@ | ||
# A-Frame `gamepad-controls` Component | ||
# A-Frame `gamepad-controls` | ||
@@ -136,2 +136,3 @@ Gamepad controls for A-Frame. | ||
flyEnabled | false | Whether or not movement is restricted to the entity’s initial plane. | ||
invertAxisY | false | Invert Y axis of view rotation thumbstick. | ||
debug | false | When true, shows debugging info in the console. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
194767
972
138