diff --git a/packages/display/src/Container.js b/packages/display/src/Container.js index b3c4447..f3d58ad 100644 --- a/packages/display/src/Container.js +++ b/packages/display/src/Container.js @@ -64,6 +64,24 @@ * @member {boolean} */ this.sortDirty = false; + + /** + * Fired when a DisplayObject is added to this Container. + * + * @event PIXI.Container#childAdded + * @param {PIXI.DisplayObject} child - The child added to the Container. + * @param {PIXI.Container} container - The container that added the child. + * @param {number} index - The children's index of the added child. + */ + + /** + * Fired when a DisplayObject is removed from this Container. + * + * @event PIXI.DisplayObject#removedFrom + * @param {PIXI.DisplayObject} child - The child removed from the Container. + * @param {PIXI.Container} container - The container that removed removed the child. + * @param {number} index - The former children's index of the removed child + */ } /** @@ -119,6 +137,7 @@ // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(this.children.length - 1); + this.emit('childAdded', child, this, this.children.length - 1); child.emit('added', this); } @@ -158,6 +177,7 @@ // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); child.emit('added', this); + this.emit('childAdded', child, this, index); return child; } @@ -275,6 +295,7 @@ // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); child.emit('removed', this); + this.emit('childRemoved', child, this, index); } return child; @@ -301,6 +322,7 @@ // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); child.emit('removed', this); + this.emit('childRemoved', child, this, index); return child; } @@ -339,6 +361,7 @@ for (let i = 0; i < removed.length; ++i) { removed[i].emit('removed', this); + this.emit('childRemoved', removed[i], this, i); } return removed; diff --git a/packages/display/src/Container.js b/packages/display/src/Container.js index b3c4447..f3d58ad 100644 --- a/packages/display/src/Container.js +++ b/packages/display/src/Container.js @@ -64,6 +64,24 @@ * @member {boolean} */ this.sortDirty = false; + + /** + * Fired when a DisplayObject is added to this Container. + * + * @event PIXI.Container#childAdded + * @param {PIXI.DisplayObject} child - The child added to the Container. + * @param {PIXI.Container} container - The container that added the child. + * @param {number} index - The children's index of the added child. + */ + + /** + * Fired when a DisplayObject is removed from this Container. + * + * @event PIXI.DisplayObject#removedFrom + * @param {PIXI.DisplayObject} child - The child removed from the Container. + * @param {PIXI.Container} container - The container that removed removed the child. + * @param {number} index - The former children's index of the removed child + */ } /** @@ -119,6 +137,7 @@ // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(this.children.length - 1); + this.emit('childAdded', child, this, this.children.length - 1); child.emit('added', this); } @@ -158,6 +177,7 @@ // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); child.emit('added', this); + this.emit('childAdded', child, this, index); return child; } @@ -275,6 +295,7 @@ // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); child.emit('removed', this); + this.emit('childRemoved', child, this, index); } return child; @@ -301,6 +322,7 @@ // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); child.emit('removed', this); + this.emit('childRemoved', child, this, index); return child; } @@ -339,6 +361,7 @@ for (let i = 0; i < removed.length; ++i) { removed[i].emit('removed', this); + this.emit('childRemoved', removed[i], this, i); } return removed; diff --git a/packages/display/test/Container.js b/packages/display/test/Container.js index 43baea4..11fb7cc 100644 --- a/packages/display/test/Container.js +++ b/packages/display/test/Container.js @@ -52,12 +52,14 @@ describe('events', function () { - it('should trigger "added" and "removed" events on its children', function () + it('should trigger "added", "removed", "childAdded", and "childRemoved" events on itself and children', function () { const container = new Container(); const child = new DisplayObject(); let triggeredAdded = false; let triggeredRemoved = false; + let triggeredChildAdded = false; + let triggeredChildRemoved = false; child.on('added', (to) => { @@ -73,12 +75,30 @@ expect(container).to.be.equals(from); }); + container.on('childAdded', (childAdded, containerFrom, index) => + { + triggeredChildAdded = true; + expect(child).to.be.equals(childAdded); + expect(container).to.be.equals(containerFrom); + expect(index).to.be.equals(0); + }); + container.on('childRemoved', (childRemoved, containerFrom, index) => + { + triggeredChildRemoved = true; + expect(child).to.be.equals(childRemoved); + expect(container).to.be.equals(containerFrom); + expect(index).to.be.equals(0); + }); + container.addChild(child); expect(triggeredAdded).to.be.true; expect(triggeredRemoved).to.be.false; + expect(triggeredChildAdded).to.be.true; + expect(triggeredChildRemoved).to.be.false; container.removeChild(child); expect(triggeredRemoved).to.be.true; + expect(triggeredChildRemoved).to.be.true; }); });