New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.0.3 to 0.0.4

examples/scene/assets/321103__nsstudios__blip1.wav

57

dist/aframe-gamepad-controls.js

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

/* 1 */
/***/ function(module, exports) {
/***/ function(module, exports, __webpack_require__) {

@@ -72,2 +72,4 @@ /**

var GamepadButtonEvent = __webpack_require__(2);
var MAX_DELTA = 0.2,

@@ -132,2 +134,5 @@ PI_2 = Math.PI / 2;

// Button state
this.buttons = {};
scene.addBehavior(this);

@@ -150,2 +155,3 @@

this.updateRotation();
this.updateButtonState();
},

@@ -249,2 +255,37 @@

/*******************************************************************
* Button events
*/
updateButtonState: function () {
var gamepad = this.getGamepad();
if (this.data.enabled && gamepad) {
// Fire DOM events for button state changes.
for (var i = 0; i < gamepad.buttons.length; i++) {
if (gamepad.buttons[i].pressed && !this.buttons[i]) {
this.emit(new GamepadButtonEvent('gamepadbuttondown', i, gamepad.buttons[i]));
} else if (!gamepad.buttons[i].pressed && this.buttons[i]) {
this.emit(new GamepadButtonEvent('gamepadbuttonup', i, gamepad.buttons[i]));
}
this.buttons[i] = gamepad.buttons[i].pressed;
}
} else if (Object.keys(this.buttons)) {
// Reset state if controls are disabled or controller is lost.
this.buttons = {};
}
},
emit: function (event) {
// Emit original event.
this.el.emit(event.type, event);
// Emit convenience event, identifying button index.
this.el.emit(
event.type + ':' + event.index,
new GamepadButtonEvent(event.type, event.index, event)
);
},
/*******************************************************************
* Gamepad state

@@ -314,3 +355,17 @@ */

/***/ },
/* 2 */
/***/ function(module, exports) {
function GamepadButtonEvent (type, index, details) {
this.type = type;
this.index = index;
this.pressed = details.pressed;
this.value = details.value;
}
module.exports = GamepadButtonEvent;
/***/ }
/******/ ]);

2

dist/aframe-gamepad-controls.min.js

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

!function(t){function e(a){if(i[a])return i[a].exports;var o=i[a]={exports:{},id:a,loaded:!1};return t[a].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){var i=.2,a=Math.PI/2,o=.2;t.exports={dependencies:["proxy-controls"],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=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),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()},remove:function(){},updatePosition:function(t){var e=this.data,a=e.acceleration,n=e.easing,r=this.velocity,s=window.performance.now(),d=(s-this.prevTime)/1e3,c=e.rollAxis,h=e.pitchAxis,l=this.el,u=this.getGamepad();if(this.prevTime=s,t||d>i)return r[c]=0,void(r[h]=0);r[c]-=r[c]*n*d,r[h]-=r[h]*n*d;var p=l.getComputedAttribute("position");e.enabled&&e.movementEnabled&&u&&(Math.abs(this.getJoystick(0).x)>o&&(r[h]+=this.getJoystick(0).x*a*d),Math.abs(this.getJoystick(0).y)>o&&(r[c]+=this.getJoystick(0).y*a*d));var y=this.getMovementVector(d);l.object3D.translateX(y.x),l.object3D.translateY(y.y),l.object3D.translateZ(y.z),l.setAttribute("position",{x:p.x+y.x,y:p.y+y.y,z:p.z+y.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.data.lookEnabled&&this.getGamepad()){var t=this.getJoystick(1);Math.abs(t.x)<=o&&(t.x=0),Math.abs(t.y)<=o&&(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})}},getGamepad:function(){return navigator.getGamepads()[this.data.controller]},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(){return this.getGamepad().connected},getId:function(){return this.getGamepad().id}}}]);
!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":!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=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,h=e.rollAxis,u=e.pitchAxis,c=this.el,l=this.getGamepad();if(this.prevTime=r,t||d>n)return a[h]=0,void(a[u]=0);a[h]-=a[h]*o*d,a[u]-=a[u]*o*d;var p=c.getComputedAttribute("position");e.enabled&&e.movementEnabled&&l&&(Math.abs(this.getJoystick(0).x)>s&&(a[u]+=this.getJoystick(0).x*i*d),Math.abs(this.getJoystick(0).y)>s&&(a[h]+=this.getJoystick(0).y*i*d));var f=this.getMovementVector(d);c.object3D.translateX(f.x),c.object3D.translateY(f.y),c.object3D.translateZ(f.z),c.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.data.lookEnabled&&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(){return navigator.getGamepads()[this.data.controller]},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(){return this.getGamepad().connected},getId:function(){return this.getGamepad().id}}},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}]);

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

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

@@ -68,2 +70,5 @@ PI_2 = Math.PI / 2;

// Button state
this.buttons = {};
scene.addBehavior(this);

@@ -86,2 +91,3 @@

this.updateRotation();
this.updateButtonState();
},

@@ -185,2 +191,37 @@

/*******************************************************************
* Button events
*/
updateButtonState: function () {
var gamepad = this.getGamepad();
if (this.data.enabled && gamepad) {
// Fire DOM events for button state changes.
for (var i = 0; i < gamepad.buttons.length; i++) {
if (gamepad.buttons[i].pressed && !this.buttons[i]) {
this.emit(new GamepadButtonEvent('gamepadbuttondown', i, gamepad.buttons[i]));
} else if (!gamepad.buttons[i].pressed && this.buttons[i]) {
this.emit(new GamepadButtonEvent('gamepadbuttonup', i, gamepad.buttons[i]));
}
this.buttons[i] = gamepad.buttons[i].pressed;
}
} else if (Object.keys(this.buttons)) {
// Reset state if controls are disabled or controller is lost.
this.buttons = {};
}
},
emit: function (event) {
// Emit original event.
this.el.emit(event.type, event);
// Emit convenience event, identifying button index.
this.el.emit(
event.type + ':' + event.index,
new GamepadButtonEvent(event.type, event.index, event)
);
},
/*******************************************************************
* Gamepad state

@@ -187,0 +228,0 @@ */

{
"name": "aframe-gamepad-controls",
"version": "0.0.3",
"version": "0.0.4",
"description": "Gamepad controls for A-Frame VR.",

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

@@ -61,2 +61,37 @@ # A-Frame `gamepad-controls` Component

## Button Events
When buttons are pressed on the gamepad, a [GamepadButtonEvent](https://github.com/donmccurdy/aframe-gamepad-controls/blob/master/lib/GamepadButtonEvent.js) is emitted on the element. Components and entities may listen for these events and modify behavior as needed. Example:
```javascript
el.addEventListener('gamepadbuttondown', function (e) {
console.log('Button "%d" has been pressed.', e.index);
});
```
**GamepadButtonEvent:**
Property | Type | Description
---------|---------|--------------
type | string | Either `gamepadbuttondown` or `gamepadbuttonup`.
index | int | Index of the button affected, 0..N.
pressed | boolean | Whether
value | float | Distance the button was pressed, if applicable. `value` will be 0 or 1 in most cases, but may return a float for trigger-like buttons.
For convenience, additional events are fired including the button index, providing a way to bind events to specific buttons using only markup. For example, to play "pew-pew" sound when `Button 7` is pressed (right trigger on an Xbox controller), you might do the following:
```html
<a-entity gamepad-controls
sound="src: pew-pew.wav;
on: gamepadbuttondown:7">
</a-entity>
```
Finally, your code may call the `gamepad-controls` component directly to request the state of a button, as a [GamepadButton](https://developer.mozilla.org/en-US/docs/Web/API/GamepadButton) instance:
```javascript
el.components['gamepad-controls'].getButton(index);
// Returns a GamepadButton instance.
```
## Options

@@ -67,6 +102,6 @@

controller | 0 | Which controller (0..3) the object should be attached to.
enabled | true | Enables/disables all events on this controller.
movementEnabled | true | Enables/disables movement via the left thumbstick.
lookEnabled | true | Enables/disables view rotation via the right thumbstick.
flyEnabled | false | Enables/disables movement off the horizontal plane.
debug | false | Enables/disables debug logging to the console.
enabled | true | Enables all events on this controller.
movementEnabled | true | Enables movement via the left thumbstick.
lookEnabled | true | Enables view rotation via the right thumbstick.
flyEnabled | false | Whether or not movement is restricted to the entity’s initial plane.
debug | false | When true, shows debugging info in the console.

Sorry, the diff of this file is not supported yet

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