Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

aframe-gamepad-controls

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aframe-gamepad-controls - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

lib/GamepadButton.js

73

dist/aframe-gamepad-controls.js

@@ -71,3 +71,4 @@ /******/ (function(modules) { // webpackBootstrap

var GamepadButtonEvent = __webpack_require__(2);
var GamepadButton = __webpack_require__(2),
GamepadButtonEvent = __webpack_require__(3);

@@ -81,4 +82,8 @@ var MAX_DELTA = 0.2,

dependencies: ['proxy-controls'],
/*******************************************************************
* Statics
*/
GamepadButton: GamepadButton,
/*******************************************************************

@@ -152,4 +157,4 @@ * Schema

update: function (previousData) {
this.updateRotation();
this.updatePosition(!!previousData);
this.updateRotation();
this.updateButtonState();

@@ -195,7 +200,10 @@ },

if (data.enabled && data.movementEnabled && gamepad) {
if (Math.abs(this.getJoystick(0).x) > JOYSTICK_EPS) {
velocity[pitchAxis] += this.getJoystick(0).x * acceleration * delta;
var dpad = this.getDpad(),
inputX = dpad.x || this.getJoystick(0).x,
inputY = dpad.y || this.getJoystick(0).y;
if (Math.abs(inputX) > JOYSTICK_EPS) {
velocity[pitchAxis] += inputX * acceleration * delta;
}
if (Math.abs(this.getJoystick(0).y) > JOYSTICK_EPS) {
velocity[rollAxis] += this.getJoystick(0).y * acceleration * delta;
if (Math.abs(inputY) > JOYSTICK_EPS) {
velocity[rollAxis] += inputY * acceleration * delta;
}

@@ -342,2 +350,19 @@ }

/**
* Returns the state of the dpad as a THREE.Vector2.
* @return {THREE.Vector2}
*/
getDpad: function () {
var gamepad = this.getGamepad();
if (!gamepad.buttons[GamepadButton.DPAD_RIGHT]) {
return new THREE.Vector2();
}
return new THREE.Vector2(
(gamepad.buttons[GamepadButton.DPAD_RIGHT].pressed ? 1 : 0)
+ (gamepad.buttons[GamepadButton.DPAD_LEFT].pressed ? -1 : 0),
(gamepad.buttons[GamepadButton.DPAD_UP].pressed ? -1 : 0)
+ (gamepad.buttons[GamepadButton.DPAD_DOWN].pressed ? 1 : 0)
);
},
/**
* Returns true if the gamepad is currently connected to the system.

@@ -361,10 +386,9 @@ * @return {boolean}

isLookEnabled: function () {
if (this.data.lookEnabled === 'true') return true;
if (this.data.lookEnabled !== 'auto') return this.data.lookEnabled === 'true';
// For 'auto' setting, look-controls component takes priority in VR mode.
// TODO: This isn't a reliable way to detect VR mode.
var isVRMode = document.fullscreen || document.mozFullScreen || document.webkitIsFullScreen,
hasLookControls = !!this.el.components['look-controls'];
// For 'auto', look-controls component takes priority in VR mode.
return !(this.data.lookEnabled === 'auto' && isVRMode && hasLookControls);
return !(isVRMode && hasLookControls);
}

@@ -379,2 +403,29 @@

module.exports = Object.assign(function GamepadButton () {}, {
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,
});
/***/ },
/* 3 */
/***/ function(module, exports) {
function GamepadButtonEvent (type, index, details) {

@@ -381,0 +432,0 @@ this.type = type;

2

dist/aframe-gamepad-controls.min.js

@@ -1,1 +0,1 @@

!function(t){function e(o){if(i[o])return i[o].exports;var n=i[o]={exports:{},id:o,loaded:!1};return t[o].call(n.exports,n,n.exports,e),n.loaded=!0,n.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 o=i(2),n=.2,a=Math.PI/2,s=.2;t.exports={dependencies:["proxy-controls"],schema:{controller:{"default":0,oneOf:[0,1,2,3]},enabled:{"default":!0},movementEnabled:{"default":!0},lookEnabled:{"default":"auto",oneOf:["auto","true","false"]},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=Date.now(),this.velocity=new THREE.Vector3(0,0,0),this.direction=new THREE.Vector3(0,0,0),this.rotation=new THREE.Euler(0,0,0,"YXZ"),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.updatePosition(!!t),this.updateRotation(),this.updateButtonState()},remove:function(){},updatePosition:function(t){var e=this.data,i=e.acceleration,o=e.easing,a=this.velocity,r=window.performance.now(),d=(r-this.prevTime)/1e3,u=e.rollAxis,c=e.pitchAxis,h=this.el,l=this.getGamepad();if(this.prevTime=r,t||d>n)return a[u]=0,void(a[c]=0);a[u]-=a[u]*o*d,a[c]-=a[c]*o*d;var p=h.getComputedAttribute("position");e.enabled&&e.movementEnabled&&l&&(Math.abs(this.getJoystick(0).x)>s&&(a[c]+=this.getJoystick(0).x*i*d),Math.abs(this.getJoystick(0).y)>s&&(a[u]+=this.getJoystick(0).y*i*d));var f=this.getMovementVector(d);h.object3D.translateX(f.x),h.object3D.translateY(f.y),h.object3D.translateZ(f.z),h.setAttribute("position",{x:p.x+f.x,y:p.y+f.y,z:p.z+f.z})},getMovementVector:function(t){var e=this.el.getAttribute("rotation");return this.direction.copy(this.velocity),this.direction.multiplyScalar(t),e?(this.data.flyEnabled||(e.x=0),this.rotation.set(THREE.Math.degToRad(e.x),THREE.Math.degToRad(e.y),0),this.direction.applyEuler(this.rotation),this.direction):this.direction},updateRotation:function(){if(this.isLookEnabled()&&this.getGamepad()){var t=this.getJoystick(1);Math.abs(t.x)<=s&&(t.x=0),Math.abs(t.y)<=s&&(t.y=0),t.multiplyScalar(this.data.sensitivity),this.yaw.rotation.y-=t.x,this.pitch.rotation.x-=t.y,this.pitch.rotation.x=Math.max(-a,Math.min(a,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})}},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()[this.data.controller],e=this.el.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)}},isConnected:function(){var t=this.getGamepad();return!(!t||!t.connected)},getID:function(){return this.getGamepad().id},isLookEnabled:function(){if("true"===this.data.lookEnabled)return!0;var t=document.fullscreen||document.mozFullScreen||document.webkitIsFullScreen,e=!!this.el.components["look-controls"];return!("auto"===this.data.lookEnabled&&t&&e)}}},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(i){if(o[i])return o[i].exports;var n=o[i]={exports:{},id:i,loaded:!1};return t[i].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var o={};return e.m=t,e.c=o,e.p="",e(0)}([function(t,e,o){!function(t){return t?void(t.aframeCore||t).registerComponent("gamepad-controls",o(1)):void console.error("Component attempted to register before AFRAME was available.")}(window.AFRAME)},function(t,e,o){var i=o(2),n=o(3),a=.2,s=Math.PI/2,r=.2;t.exports={GamepadButton:i,schema:{controller:{"default":0,oneOf:[0,1,2,3]},enabled:{"default":!0},movementEnabled:{"default":!0},lookEnabled:{"default":"auto",oneOf:["auto","true","false"]},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=Date.now(),this.velocity=new THREE.Vector3(0,0,0),this.direction=new THREE.Vector3(0,0,0),this.rotation=new THREE.Euler(0,0,0,"YXZ"),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()},remove:function(){},updatePosition:function(t){var e=this.data,o=e.acceleration,i=e.easing,n=this.velocity,s=window.performance.now(),d=(s-this.prevTime)/1e3,u=e.rollAxis,c=e.pitchAxis,h=this.el,l=this.getGamepad();if(this.prevTime=s,t||d>a)return n[u]=0,void(n[c]=0);n[u]-=n[u]*i*d,n[c]-=n[c]*i*d;var p=h.getComputedAttribute("position");if(e.enabled&&e.movementEnabled&&l){var f=this.getDpad(),E=f.x||this.getJoystick(0).x,b=f.y||this.getJoystick(0).y;Math.abs(E)>r&&(n[c]+=E*o*d),Math.abs(b)>r&&(n[u]+=b*o*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:p.x+m.x,y:p.y+m.y,z:p.z+m.z})},getMovementVector:function(t){var e=this.el.getAttribute("rotation");return this.direction.copy(this.velocity),this.direction.multiplyScalar(t),e?(this.data.flyEnabled||(e.x=0),this.rotation.set(THREE.Math.degToRad(e.x),THREE.Math.degToRad(e.y),0),this.direction.applyEuler(this.rotation),this.direction):this.direction},updateRotation:function(){if(this.isLookEnabled()&&this.getGamepad()){var t=this.getJoystick(1);Math.abs(t.x)<=r&&(t.x=0),Math.abs(t.y)<=r&&(t.y=0),t.multiplyScalar(this.data.sensitivity),this.yaw.rotation.y-=t.x,this.pitch.rotation.x-=t.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})}},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 n("gamepadbuttondown",e,t.buttons[e])):!t.buttons[e].pressed&&this.buttons[e]&&this.emit(new n("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 n(t.type,t.index,t))},getGamepad:function(){var t=navigator.getGamepads()[this.data.controller],e=this.el.components["proxy-controls"],o=e&&e.isConnected()&&e.getGamepad(this.data.controller);return o||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[i.DPAD_RIGHT]?new THREE.Vector2((t.buttons[i.DPAD_RIGHT].pressed?1:0)+(t.buttons[i.DPAD_LEFT].pressed?-1:0),(t.buttons[i.DPAD_UP].pressed?-1:0)+(t.buttons[i.DPAD_DOWN].pressed?1:0)):new THREE.Vector2},isConnected:function(){var t=this.getGamepad();return!(!t||!t.connected)},getID:function(){return this.getGamepad().id},isLookEnabled:function(){if("auto"!==this.data.lookEnabled)return"true"===this.data.lookEnabled;var t=document.fullscreen||document.mozFullScreen||document.webkitIsFullScreen,e=!!this.el.components["look-controls"];return!(t&&e)}}},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 o(t,e,o){this.type=t,this.index=e,this.pressed=o.pressed,this.value=o.value}t.exports=o}]);

@@ -8,3 +8,4 @@ /**

var GamepadButtonEvent = require('./lib/GamepadButtonEvent');
var GamepadButton = require('./lib/GamepadButton'),
GamepadButtonEvent = require('./lib/GamepadButtonEvent');

@@ -18,4 +19,8 @@ var MAX_DELTA = 0.2,

dependencies: ['proxy-controls'],
/*******************************************************************
* Statics
*/
GamepadButton: GamepadButton,
/*******************************************************************

@@ -89,4 +94,4 @@ * Schema

update: function (previousData) {
this.updateRotation();
this.updatePosition(!!previousData);
this.updateRotation();
this.updateButtonState();

@@ -132,7 +137,10 @@ },

if (data.enabled && data.movementEnabled && gamepad) {
if (Math.abs(this.getJoystick(0).x) > JOYSTICK_EPS) {
velocity[pitchAxis] += this.getJoystick(0).x * acceleration * delta;
var dpad = this.getDpad(),
inputX = dpad.x || this.getJoystick(0).x,
inputY = dpad.y || this.getJoystick(0).y;
if (Math.abs(inputX) > JOYSTICK_EPS) {
velocity[pitchAxis] += inputX * acceleration * delta;
}
if (Math.abs(this.getJoystick(0).y) > JOYSTICK_EPS) {
velocity[rollAxis] += this.getJoystick(0).y * acceleration * delta;
if (Math.abs(inputY) > JOYSTICK_EPS) {
velocity[rollAxis] += inputY * acceleration * delta;
}

@@ -279,2 +287,19 @@ }

/**
* Returns the state of the dpad as a THREE.Vector2.
* @return {THREE.Vector2}
*/
getDpad: function () {
var gamepad = this.getGamepad();
if (!gamepad.buttons[GamepadButton.DPAD_RIGHT]) {
return new THREE.Vector2();
}
return new THREE.Vector2(
(gamepad.buttons[GamepadButton.DPAD_RIGHT].pressed ? 1 : 0)
+ (gamepad.buttons[GamepadButton.DPAD_LEFT].pressed ? -1 : 0),
(gamepad.buttons[GamepadButton.DPAD_UP].pressed ? -1 : 0)
+ (gamepad.buttons[GamepadButton.DPAD_DOWN].pressed ? 1 : 0)
);
},
/**
* Returns true if the gamepad is currently connected to the system.

@@ -298,12 +323,11 @@ * @return {boolean}

isLookEnabled: function () {
if (this.data.lookEnabled === 'true') return true;
if (this.data.lookEnabled !== 'auto') return this.data.lookEnabled === 'true';
// For 'auto' setting, look-controls component takes priority in VR mode.
// TODO: This isn't a reliable way to detect VR mode.
var isVRMode = document.fullscreen || document.mozFullScreen || document.webkitIsFullScreen,
hasLookControls = !!this.el.components['look-controls'];
// For 'auto', look-controls component takes priority in VR mode.
return !(this.data.lookEnabled === 'auto' && isVRMode && hasLookControls);
return !(isVRMode && hasLookControls);
}
};
{
"name": "aframe-gamepad-controls",
"version": "0.2.0",
"version": "0.3.0",
"description": "Gamepad controls for A-Frame.",

@@ -5,0 +5,0 @@ "main": "gamepad-controls.js",

@@ -16,3 +16,5 @@ var Aframe = require('aframe-core');

axes: [0, 0, 0, 0],
buttons: [],
buttons: '.................'.split('.').map(function () {
return { pressed: 0, value: 0 };
}),
connected: true,

@@ -19,0 +21,0 @@ id: 'test-gamepad'

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc