From 2841522f3b3f41e10963cd246a05b6248f8768e8 Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 12 Jul 2014 17:34:54 +0100 Subject: [PATCH] added composite events --- src/body/Composite.js | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/body/Composite.js b/src/body/Composite.js index 84663a3..06fc6ef 100644 --- a/src/body/Composite.js +++ b/src/body/Composite.js @@ -63,6 +63,7 @@ var Composite = {}; /** * Generic add function. Adds one or many body(s), constraint(s) or a composite(s) to the given composite. + * Triggers `beforeAdd` and `afterAdd` events on the `composite`. * @method add * @param {composite} composite * @param {} object @@ -71,6 +72,8 @@ var Composite = {}; Composite.add = function(composite, object) { var objects = [].concat(object); + Events.trigger(composite, 'beforeAdd', { object: object }); + for (var i = 0; i < objects.length; i++) { var obj = objects[i]; @@ -92,12 +95,15 @@ var Composite = {}; } } + Events.trigger(composite, 'afterAdd', { object: object }); + return composite; }; /** * Generic remove function. Removes one or many body(s), constraint(s) or a composite(s) to the given composite. * Optionally searching its children recursively. + * Triggers `beforeRemove` and `afterRemove` events on the `composite`. * @method remove * @param {composite} composite * @param {} object @@ -107,6 +113,8 @@ var Composite = {}; Composite.remove = function(composite, object, deep) { var objects = [].concat(object); + Events.trigger(composite, 'beforeRemove', { object: object }); + for (var i = 0; i < objects.length; i++) { var obj = objects[i]; @@ -128,6 +136,8 @@ var Composite = {}; } } + Events.trigger(composite, 'afterRemove', { object: object }); + return composite; }; @@ -431,6 +441,52 @@ var Composite = {}; return composite; }; + /* + * + * Events Documentation + * + */ + + /** + * Fired when a call to `Composite.add` is made, before objects have been added. + * + * @event beforeAdd + * @param {} event An event object + * @param {} event.object The object(s) to be added (may be a single body, constraint, composite or a mixed array of these) + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + + /** + * Fired when a call to `Composite.add` is made, after objects have been added. + * + * @event afterAdd + * @param {} event An event object + * @param {} event.object The object(s) that have been added (may be a single body, constraint, composite or a mixed array of these) + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + + /** + * Fired when a call to `Composite.remove` is made, before objects have been removed. + * + * @event beforeRemove + * @param {} event An event object + * @param {} event.object The object(s) to be removed (may be a single body, constraint, composite or a mixed array of these) + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + + /** + * Fired when a call to `Composite.remove` is made, after objects have been removed. + * + * @event afterRemove + * @param {} event An event object + * @param {} event.object The object(s) that have been removed (may be a single body, constraint, composite or a mixed array of these) + * @param {} event.source The source object of the event + * @param {} event.name The name of the event + */ + /* * * Properties Documentation