Skip to content

Commit

Permalink
Autoregister all components. Fixes c-frame#67.
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Nov 23, 2017
1 parent 42a31be commit 41a3dfa
Show file tree
Hide file tree
Showing 38 changed files with 106 additions and 281 deletions.
16 changes: 3 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,15 @@ npm install --save aframe-extras
```

```javascript
// custom-extras.js

var extras = require('aframe-extras');

// Register a single component.
AFRAME.registerComponent('checkpoint', extras.misc.checkpoint);

// Register a particular package, and its dependencies.
extras.controls.registerAll();

// Register everything.
extras.registerAll();
// index.js
require('aframe-extras');
```

Once installed, you'll need to compile your JavaScript using something like [Browserify](http://browserify.org/) or [Webpack](http://webpack.github.io/). Example:

```bash
npm install -g browserify
browserify custom-extras.js -o bundle.js
browserify index.js -o bundle.js
```

`bundle.js` may then be included in your page. See [here](http://browserify.org/#middle-section) for a better introduction to Browserify.
2 changes: 1 addition & 1 deletion browser.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
window.AFRAME = require('aframe');
require('./').registerAll();
require('./');
20 changes: 5 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
module.exports = {
controls: require('./src/controls'),
loaders: require('./src/loaders'),
misc: require('./src/misc'),
pathfinding: require('./src/pathfinding'),
primitives: require('./src/primitives'),

registerAll: function () {
this.controls.registerAll();
this.loaders.registerAll();
this.misc.registerAll();
this.pathfinding.registerAll();
this.primitives.registerAll();
}
};
require('./src/controls');
require('./src/loaders');
require('./src/misc');
require('./src/pathfinding');
require('./src/primitives');
2 changes: 1 addition & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const writeStream = fs.createWriteStream(path.join(BUILD_DIR, fileName));
const readStream = new Readable();
readStream.push([
'window.AFRAME = require("aframe");',
'window.AFRAME.extras = require("./").registerAll();'
'require("./");'
].join('\n'));
readStream.push(null);
browserify()
Expand Down
6 changes: 3 additions & 3 deletions scripts/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const streams = {};

// Full build.
const stream = new Readable();
stream.push(`require('./').registerAll();`);
stream.push(`require('./');`);
stream.push(null);
streams['aframe-extras.js'] = stream;

// Individual packages.
PACKAGES.forEach((name) => {
const stream = new Readable();
stream.push(`require('./src/${name}').registerAll();`);
stream.push(`require('./src/${name}');`);
stream.push(null);
streams[`aframe-extras.${name}.js`] = stream;
});
Expand All @@ -32,7 +32,7 @@ PACKAGES.forEach((name) => {
COMPONENTS.forEach((name => {
const stream = new Readable(),
basename = path.basename(name);
stream.push(`AFRAME.registerComponent('${basename}', require('./src/${name}'));`);
stream.push(`require('./src/${name}');`);
stream.push(null);
stream._isComponent = true;
streams[`${basename}.js`] = stream;
Expand Down
4 changes: 2 additions & 2 deletions src/controls/checkpoint-controls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var EPS = 0.1;

module.exports = {
module.exports = AFRAME.registerComponent('checkpoint-controls', {
schema: {
enabled: {default: true},
mode: {default: 'teleport', oneOf: ['teleport', 'animate']},
Expand Down Expand Up @@ -80,4 +80,4 @@ module.exports = {
targetPosition.add(this.checkpoint.components.checkpoint.getOffset());
offset.copy(targetPosition).sub(position);
}
};
});
4 changes: 2 additions & 2 deletions src/controls/gamepad-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var GamepadButton = require('../../lib/GamepadButton'),

var JOYSTICK_EPS = 0.2;

module.exports = {
module.exports = AFRAME.registerComponent('gamepad-controls', {

/*******************************************************************
* Statics
Expand Down Expand Up @@ -244,4 +244,4 @@ module.exports = {
getID: function () {
return this.getGamepad().id;
}
};
});
4 changes: 2 additions & 2 deletions src/controls/hmd-controls.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var radToDeg = THREE.Math.radToDeg,
isMobile = AFRAME.utils.device.isMobile();

module.exports = {
module.exports = AFRAME.registerComponent('hmd-controls', {
schema: {
enabled: {default: true},
standing: {default: true}
Expand Down Expand Up @@ -74,7 +74,7 @@ module.exports = {
position.setFromMatrixPosition(dolly.matrix);
return position;
}
};
});

function isNullVector (vector) {
return vector.x === 0 && vector.y === 0 && vector.z === 0;
Expand Down
32 changes: 7 additions & 25 deletions src/controls/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
module.exports = {
'checkpoint-controls': require('./checkpoint-controls'),
'gamepad-controls': require('./gamepad-controls'),
'hmd-controls': require('./hmd-controls'),
'keyboard-controls': require('./keyboard-controls'),
'mouse-controls': require('./mouse-controls'),
'touch-controls': require('./touch-controls'),
'universal-controls': require('./universal-controls'),

registerAll: function (AFRAME) {
if (this._registered) return;

AFRAME = AFRAME || window.AFRAME;

if (!AFRAME.components['checkpoint-controls']) AFRAME.registerComponent('checkpoint-controls', this['checkpoint-controls']);
if (!AFRAME.components['gamepad-controls']) AFRAME.registerComponent('gamepad-controls', this['gamepad-controls']);
if (!AFRAME.components['hmd-controls']) AFRAME.registerComponent('hmd-controls', this['hmd-controls']);
if (!AFRAME.components['keyboard-controls']) AFRAME.registerComponent('keyboard-controls', this['keyboard-controls']);
if (!AFRAME.components['mouse-controls']) AFRAME.registerComponent('mouse-controls', this['mouse-controls']);
if (!AFRAME.components['touch-controls']) AFRAME.registerComponent('touch-controls', this['touch-controls']);
if (!AFRAME.components['universal-controls']) AFRAME.registerComponent('universal-controls', this['universal-controls']);

this._registered = true;
}
};
require('./checkpoint-controls');
require('./gamepad-controls');
require('./hmd-controls');
require('./keyboard-controls');
require('./mouse-controls');
require('./touch-controls');
require('./universal-controls');
4 changes: 2 additions & 2 deletions src/controls/keyboard-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var KeyboardEvent = window.KeyboardEvent;
* to the entity when pressing the keys.
* @param {bool} [enabled=true] - To completely enable or disable the controls
*/
module.exports = {
module.exports = AFRAME.registerComponent('keyboard-controls', {
schema: {
enabled: { default: true },
debug: { default: false }
Expand Down Expand Up @@ -150,4 +150,4 @@ module.exports = {
return proxyControls && proxyControls.isConnected();
}

};
});
4 changes: 2 additions & 2 deletions src/controls/mouse-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLo
*
* Based on: https://github.com/aframevr/aframe/pull/1056
*/
module.exports = {
module.exports = AFRAME.registerComponent('mouse-controls', {
schema: {
enabled: { default: true },
pointerlockEnabled: { default: true },
Expand Down Expand Up @@ -145,4 +145,4 @@ module.exports = {
onPointerLockError: function () {
this.pointerLocked = false;
}
};
});
7 changes: 5 additions & 2 deletions src/controls/touch-controls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
/**
* Touch-to-move-forward controls for mobile.
*/
module.exports = AFRAME.registerComponent('touch-controls', {
schema: {
enabled: { default: true }
},
Expand Down Expand Up @@ -65,4 +68,4 @@ module.exports = {
this.isMoving = false;
e.preventDefault();
}
};
});
4 changes: 2 additions & 2 deletions src/controls/universal-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var COMPONENT_SUFFIX = '-controls',
MAX_DELTA = 0.2, // ms
PI_2 = Math.PI / 2;

module.exports = {
module.exports = AFRAME.registerComponent('universal-controls', {

/*******************************************************************
* Schema
Expand Down Expand Up @@ -215,4 +215,4 @@ module.exports = {

if (AFRAME.components.velocity) this.el.setAttribute('velocity', velocity);
}
};
});
4 changes: 2 additions & 2 deletions src/loaders/animation-mixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var LoopMode = {
* skeletal or morph animations through THREE.AnimationMixer.
* See: https://threejs.org/docs/?q=animation#Reference/Animation/AnimationMixer
*/
module.exports = {
module.exports = AFRAME.registerComponent('animation-mixer', {
schema: {
clip: {default: '*'},
duration: {default: 0},
Expand Down Expand Up @@ -104,7 +104,7 @@ module.exports = {
tick: function (t, dt) {
if (this.mixer && !isNaN(dt)) this.mixer.update(dt / 1000);
}
};
});

/**
* Creates a RegExp from the given string, converting asterisks to .* expressions,
Expand Down
4 changes: 2 additions & 2 deletions src/loaders/fbx-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ THREE.FBXLoader = require('../../lib/FBXLoader');
*
* Loader for FBX format. Supports ASCII, but *not* binary, models.
*/
module.exports = {
module.exports = AFRAME.registerComponent('fbx-model', {
schema: {
src: { type: 'asset' },
crossorigin: { default: '' }
Expand Down Expand Up @@ -35,4 +35,4 @@ module.exports = {
remove: function () {
if (this.model) this.el.removeObject3D('mesh');
}
};
});
4 changes: 2 additions & 2 deletions src/loaders/gltf-model-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var LOADER_SRC = 'https://rawgit.com/mrdoob/three.js/r86/examples/js/loaders/GLT
* Legacy loader for glTF 1.0 models.
* Asynchronously loads THREE.GLTFLoader from rawgit.
*/
module.exports = {
module.exports = AFRAME.registerComponent('gltf-model-legacy', {
schema: {type: 'model'},

init: function () {
Expand Down Expand Up @@ -41,7 +41,7 @@ module.exports = {
if (!this.model) { return; }
this.el.removeObject3D('mesh');
}
};
});

var loadLoader = (function () {
var promise;
Expand Down
55 changes: 6 additions & 49 deletions src/loaders/index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,6 @@
module.exports = {
'animation-mixer': require('./animation-mixer'),
'fbx-model': require('./fbx-model'),
'gltf-model-legacy': require('./gltf-model-legacy'),
'json-model': require('./json-model'),
'object-model': require('./object-model'),
'ply-model': require('./ply-model'),

registerAll: function (AFRAME) {
if (this._registered) return;

AFRAME = AFRAME || window.AFRAME;

// THREE.AnimationMixer
if (!AFRAME.components['animation-mixer']) {
AFRAME.registerComponent('animation-mixer', this['animation-mixer']);
}

// THREE.PlyLoader
if (!AFRAME.systems['ply-model']) {
AFRAME.registerSystem('ply-model', this['ply-model'].System);
}
if (!AFRAME.components['ply-model']) {
AFRAME.registerComponent('ply-model', this['ply-model'].Component);
}

// THREE.FBXLoader
if (!AFRAME.components['fbx-model']) {
AFRAME.registerComponent('fbx-model', this['fbx-model']);
}

// THREE.GLTFLoader
if (!AFRAME.components['gltf-model-legacy']) {
AFRAME.registerComponent('gltf-model-legacy', this['gltf-model-legacy']);
}

// THREE.JsonLoader
if (!AFRAME.components['json-model']) {
AFRAME.registerComponent('json-model', this['json-model']);
}

// THREE.ObjectLoader
if (!AFRAME.components['object-model']) {
AFRAME.registerComponent('object-model', this['object-model']);
}

this._registered = true;
}
};
require('./animation-mixer');
require('./fbx-model');
require('./gltf-model-legacy');
require('./json-model');
require('./object-model');
require('./ply-model');
4 changes: 2 additions & 2 deletions src/loaders/json-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* See: https://clara.io/learn/user-guide/data_exchange/threejs_export
*/
module.exports = {
module.exports = AFRAME.registerComponent('json-model', {
schema: {
src: { type: 'asset' },
crossorigin: { default: '' }
Expand Down Expand Up @@ -55,4 +55,4 @@ module.exports = {
remove: function () {
if (this.model) this.el.removeObject3D('mesh');
}
};
});
4 changes: 2 additions & 2 deletions src/loaders/object-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* See: https://clara.io/learn/user-guide/data_exchange/threejs_export
*/
module.exports = {
module.exports = AFRAME.registerComponent('object-model', {
schema: {
src: { type: 'asset' },
crossorigin: { default: '' }
Expand Down Expand Up @@ -50,4 +50,4 @@ module.exports = {
remove: function () {
if (this.model) this.el.removeObject3D('mesh');
}
};
});
8 changes: 4 additions & 4 deletions src/loaders/ply-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ THREE.PLYLoader = require('../../lib/PLYLoader');
*
* @member cache - Promises that resolve geometries keyed by `src`.
*/
module.exports.System = {
module.exports.System = AFRAME.registerSystem('ply-model', {
init: function () {
this.cache = {};
},
Expand All @@ -34,9 +34,9 @@ module.exports.System = {
});
return cache[src];
},
};
});

module.exports.Component = {
module.exports.Component = AFRAME.registerComponent('ply-model', {
schema: {
skipCache: {type: 'boolean', default: false},
src: {type: 'asset'}
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports.Component = {
remove: function () {
if (this.model) { this.el.removeObject3D('mesh'); }
}
};
});

function createModel (geometry) {
return new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({
Expand Down
Loading

0 comments on commit 41a3dfa

Please sign in to comment.