Skip to content

Commit

Permalink
Merge branch 'node-tests' into browserify
Browse files Browse the repository at this point in the history
Conflicts:
	.jshintrc
  • Loading branch information
liabru committed Aug 17, 2015
2 parents 99c3bb3 + b0f72ab commit 6e0e7e4
Show file tree
Hide file tree
Showing 72 changed files with 1,422,145 additions and 78 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ matter-doc-theme
build/matter-dev.js
build/matter-dev.min.js
demo/js/lib/matter-dev.js
test/browser/diffs
test/browser/diffs
test/node/diffs
6 changes: 4 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
// variables
"undef": true,
"-W079": true, // Silence redefinition errors (they are false positives).
"-W020": true, // Silence readonly error (needed to simplify support for node).
"predef": [
"Matter", "window", "document", "Element", "MatterTools", "PIXI", "phantom", "module",
"$", "Image", "navigator", "setTimeout", "decomp", "HTMLElement", "require",
"Matter", "window", "document", "Element", "MatterTools",
"phantom", "process", "HTMLElement", "require", "PIXI",
"$", "Image", "navigator", "setTimeout", "decomp", "module",
"Body", "Composite", "World", "Contact", "Detector", "Grid",
"Pairs", "Pair", "Resolver", "SAT", "Constraint", "MouseConstraint",
"Common", "Engine", "Mouse", "Sleeping", "Bodies", "Composites",
Expand Down
39 changes: 33 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module.exports = function(grunt) {
browserify: {
options: {
banner: '/**\n* <%= buildName %>.js <%= buildVersion %> <%= grunt.template.today("yyyy-mm-dd") %>\n* <%= pkg.homepage %>\n* License: <%= pkg.license %>\n*/\n\n',
browserifyOptions: {
standalone: '<%= pkg.name %>'
}
},
'build/<%= buildName %>.js': ['src/module/main.js']
},
Expand Down Expand Up @@ -45,7 +48,7 @@ module.exports = function(grunt) {
options: {
jshintrc: '.jshintrc'
},
all: ['src/**/*.js', 'demo/js/*.js', 'test/browser/TestDemo.js', '!src/module/*']
all: ['src/**/*.js', 'demo/js/*.js', 'test/browser/TestDemo.js', 'test/node/TestDemo.js', '!src/module/*']
},
connect: {
watch: {
Expand Down Expand Up @@ -102,7 +105,7 @@ module.exports = function(grunt) {
}
},
shell: {
testDemo: {
testDemoBrowser: {
command: function(arg) {
arg = arg ? ' --' + arg : '';
return 'phantomjs test/browser/TestDemo.js' + arg;
Expand All @@ -112,6 +115,17 @@ module.exports = function(grunt) {
timeout: 1000 * 60
}
}
},
testDemoNode: {
command: function(arg) {
arg = arg ? ' --' + arg : '';
return 'node test/node/TestDemo.js' + arg;
},
options: {
execOptions: {
timeout: 1000 * 60
}
}
}
}
});
Expand All @@ -127,19 +141,32 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-shell');

grunt.registerTask('default', ['test', 'build']);
grunt.registerTask('test', ['build:dev', 'connect:serve', 'jshint', 'test:demo']);
grunt.registerTask('test', ['build:dev', 'connect:serve', 'jshint', 'test:demo', 'test:demoNode']);
grunt.registerTask('dev', ['build:dev', 'connect:watch', 'watch']);

grunt.registerTask('test:demo', function() {
var updateAll = grunt.option('updateAll'),
diff = grunt.option('diff');

if (updateAll) {
grunt.task.run('shell:testDemo:updateAll');
grunt.task.run('shell:testDemoBrowser:updateAll');
} else if (diff) {
grunt.task.run('shell:testDemoBrowser:diff');
} else {
grunt.task.run('shell:testDemoBrowser');
}
});

grunt.registerTask('test:demoNode', function() {
var updateAll = grunt.option('updateAll'),
diff = grunt.option('diff');

if (updateAll) {
grunt.task.run('shell:testDemoNode:updateAll');
} else if (diff) {
grunt.task.run('shell:testDemo:diff');
grunt.task.run('shell:testDemoNode:diff');
} else {
grunt.task.run('shell:testDemo');
grunt.task.run('shell:testDemoNode');
}
});

Expand Down
120 changes: 71 additions & 49 deletions demo/js/Demo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
(function() {

var _isBrowser = typeof window !== 'undefined',
Matter = _isBrowser ? window.Matter : require('../../build/matter-dev.js');

var Demo = {};
Matter.Demo = Demo;

if (!_isBrowser) {
module.exports = Demo;
window = {};
}

// Matter aliases
var Engine = Matter.Engine,
World = Matter.World,
Expand All @@ -24,25 +35,20 @@
Inspector = MatterTools.Inspector;
}

var Demo = {};
Matter.Demo = Demo;

var _engine,
_runner,
_gui,
_inspector,
_sceneName,
_mouseConstraint,
_sceneEvents = [],
_useInspector = window.location.hash.indexOf('-inspect') !== -1,
_isMobile = /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent),
_isAutomatedTest = window._phantom ? true : false;
_useInspector = _isBrowser && window.location.hash.indexOf('-inspect') !== -1,
_isMobile = _isBrowser && /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent),
_isAutomatedTest = !_isBrowser || window._phantom;

// initialise the demo

Demo.init = function() {
var container = document.getElementById('canvas-container');

// some example engine options
var options = {
positionIterations: 6,
Expand All @@ -53,11 +59,18 @@

// create a Matter engine
// NOTE: this is actually Matter.Engine.create(), see the aliases at top of this file
_engine = Engine.create(container, options);

// add a mouse controlled constraint
_mouseConstraint = MouseConstraint.create(_engine);
World.add(_engine.world, _mouseConstraint);
if (_isBrowser) {
var container = document.getElementById('canvas-container');
_engine = Engine.create(container, options);

// add a mouse controlled constraint
_mouseConstraint = MouseConstraint.create(_engine);
World.add(_engine.world, _mouseConstraint);
} else {
_engine = Engine.create(options);
_engine.render = {};
_engine.render.options = {};
}

// engine reference for external use
Matter.Demo._engine = _engine;
Expand All @@ -83,7 +96,7 @@
};

// call init when the page has loaded fully

if (window.addEventListener) {
window.addEventListener('load', Demo.init);
} else if (window.attachEvent) {
Expand Down Expand Up @@ -676,9 +689,6 @@
var renderOptions = _engine.render.options;
renderOptions.wireframes = false;
renderOptions.showAngleIndicator = false;

if (window.chrome)
renderOptions.showShadows = true;
};

Demo.chains = function() {
Expand Down Expand Up @@ -1714,21 +1724,26 @@
};

Demo.reset = function() {
var _world = _engine.world;
var _world = _engine.world,
i;

World.clear(_world);
Engine.clear(_engine);

// clear scene graph (if defined in controller)
var renderController = _engine.render.controller;
if (renderController.clear)
renderController.clear(_engine.render);
if (_engine.render) {
var renderController = _engine.render.controller;
if (renderController && renderController.clear)
renderController.clear(_engine.render);
}

// clear all scene events
for (var i = 0; i < _sceneEvents.length; i++)
Events.off(_engine, _sceneEvents[i]);
if (_engine.events) {
for (i = 0; i < _sceneEvents.length; i++)
Events.off(_engine, _sceneEvents[i]);
}

if (_mouseConstraint.events) {
if (_mouseConstraint && _mouseConstraint.events) {
for (i = 0; i < _sceneEvents.length; i++)
Events.off(_mouseConstraint, _sceneEvents[i]);
}
Expand All @@ -1743,7 +1758,7 @@
Events.off(_runner, _sceneEvents[i]);
}

if (_engine.render.events) {
if (_engine.render && _engine.render.events) {
for (i = 0; i < _sceneEvents.length; i++)
Events.off(_engine.render, _sceneEvents[i]);
}
Expand All @@ -1757,8 +1772,10 @@
Common._seed = 0;

// reset mouse offset and scale (only required for Demo.views)
Mouse.setScale(_mouseConstraint.mouse, { x: 1, y: 1 });
Mouse.setOffset(_mouseConstraint.mouse, { x: 0, y: 0 });
if (_mouseConstraint) {
Mouse.setScale(_mouseConstraint.mouse, { x: 1, y: 1 });
Mouse.setOffset(_mouseConstraint.mouse, { x: 0, y: 0 });
}

_engine.enableSleeping = false;
_engine.world.gravity.y = 1;
Expand All @@ -1773,29 +1790,34 @@
Bodies.rectangle(-offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true })
]);

World.add(_world, _mouseConstraint);
if (_mouseConstraint) {
World.add(_world, _mouseConstraint);
}

var renderOptions = _engine.render.options;
renderOptions.wireframes = true;
renderOptions.hasBounds = false;
renderOptions.showDebug = false;
renderOptions.showBroadphase = false;
renderOptions.showBounds = false;
renderOptions.showVelocity = false;
renderOptions.showCollisions = false;
renderOptions.showAxes = false;
renderOptions.showPositions = false;
renderOptions.showAngleIndicator = true;
renderOptions.showIds = false;
renderOptions.showShadows = false;
renderOptions.showVertexNumbers = false;
renderOptions.showConvexHulls = false;
renderOptions.showInternalEdges = false;
renderOptions.showSeparations = false;
renderOptions.background = '#fff';

if (_isMobile)
renderOptions.showDebug = true;
if (_engine.render) {
var renderOptions = _engine.render.options;
renderOptions.wireframes = true;
renderOptions.hasBounds = false;
renderOptions.showDebug = false;
renderOptions.showBroadphase = false;
renderOptions.showBounds = false;
renderOptions.showVelocity = false;
renderOptions.showCollisions = false;
renderOptions.showAxes = false;
renderOptions.showPositions = false;
renderOptions.showAngleIndicator = true;
renderOptions.showIds = false;
renderOptions.showShadows = false;
renderOptions.showVertexNumbers = false;
renderOptions.showConvexHulls = false;
renderOptions.showInternalEdges = false;
renderOptions.showSeparations = false;
renderOptions.background = '#fff';

if (_isMobile) {
renderOptions.showDebug = true;
}
}
};

})();
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"homepage": "http://brm.io/matter-js/",
"author": "Liam Brummitt <liam@brm.io> (http://brm.io/)",
"description": "a 2D rigid body physics engine for the web",
"main": "build/matter-0.8.0.min.js",
"main": "build/matter-dev.js",
"repository": {
"type": "git",
"url": "https://github.com/liabru/matter-js.git"
Expand All @@ -20,6 +20,7 @@
"rigid body physics"
],
"devDependencies": {
"cheerio": "^0.19.0",
"fast-json-patch": "^0.5.4",
"grunt": "~0.4.2",
"grunt-browserify": "~3.7.0",
Expand All @@ -30,7 +31,9 @@
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-yuidoc": "~0.5.1",
"grunt-preprocess": "^4.1.0",
"grunt-shell": "^1.1.2"
"grunt-shell": "^1.1.2",
"mkdirp": "^0.5.1",
"rimraf": "^2.4.2"
},
"scripts": {
"dev": "npm install && grunt dev",
Expand Down
2 changes: 1 addition & 1 deletion src/core/Events.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var Common = require('./Common');
var callbacks = object.events[names[i]],
newCallbacks = [];

if (callback) {
if (callback && callbacks) {
for (var j = 0; j < callbacks.length; j++) {
if (callbacks[j] !== callback)
newCallbacks.push(callbacks[j]);
Expand Down
14 changes: 7 additions & 7 deletions src/core/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ var Common = require('./Common');

(function() {

if (typeof window === 'undefined') {
// TODO: support Runner on non-browser environments.
return;
}
var _requestAnimationFrame,
_cancelAnimationFrame;

var _requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame
if (typeof window !== 'undefined') {
_requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame || window.msRequestAnimationFrame
|| function(callback){ window.setTimeout(function() { callback(Common.now()); }, 1000 / 60); };

var _cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame
_cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame
|| window.webkitCancelAnimationFrame || window.msCancelAnimationFrame;
}

/**
* Creates a new Runner. The options parameter is an object that specifies any properties you wish to override the defaults.
Expand Down Expand Up @@ -169,7 +169,7 @@ var Common = require('./Common');
Events.trigger(runner, 'afterUpdate', event);

// render
if (engine.render) {
if (engine.render && engine.render.controller) {
Events.trigger(runner, 'beforeRender', event);
Events.trigger(engine, 'beforeRender', event); // @deprecated

Expand Down
Loading

0 comments on commit 6e0e7e4

Please sign in to comment.