diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ed8cd5f..17fde8d 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -73,11 +73,12 @@ } child.parent = this; + // ensure child transform will be recalculated + child.transform._parentID = -1; this.children.push(child); - // ensure a transform will be recalculated.. - this.transform._parentID = -1; + // ensure bounds will be recalculated this._boundsID++; // TODO - lets either do all callbacks or all events.. not both! @@ -108,11 +109,12 @@ } child.parent = this; + // ensure child transform will be recalculated + child.transform._parentID = -1; this.children.splice(index, 0, child); - // ensure a transform will be recalculated.. - this.transform._parentID = -1; + // ensure bounds will be recalculated this._boundsID++; // TODO - lets either do all callbacks or all events.. not both! @@ -225,10 +227,11 @@ if (index === -1) return null; child.parent = null; + // ensure child transform will be recalculated + child.transform._parentID = -1; removeItems(this.children, index, 1); - // ensure a transform will be recalculated.. - this.transform._parentID = -1; + // ensure bounds will be recalculated this._boundsID++; // TODO - lets either do all callbacks or all events.. not both! @@ -249,11 +252,12 @@ { const child = this.getChildAt(index); + // ensure child transform will be recalculated.. child.parent = null; + child.transform._parentID = -1; removeItems(this.children, index, 1); - // ensure a transform will be recalculated.. - this.transform._parentID = -1; + // ensure bounds will be recalculated this._boundsID++; // TODO - lets either do all callbacks or all events.. not both! @@ -284,13 +288,12 @@ for (let i = 0; i < removed.length; ++i) { removed[i].parent = null; + if (removed[i].transform) + { + removed[i].transform._parentID = -1; + } } - // ensure a transform will be recalculated.. - if (this.transform) - { - this.transform._parentID = -1; - } this._boundsID++; this.onChildrenChange(beginIndex); diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ed8cd5f..17fde8d 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -73,11 +73,12 @@ } child.parent = this; + // ensure child transform will be recalculated + child.transform._parentID = -1; this.children.push(child); - // ensure a transform will be recalculated.. - this.transform._parentID = -1; + // ensure bounds will be recalculated this._boundsID++; // TODO - lets either do all callbacks or all events.. not both! @@ -108,11 +109,12 @@ } child.parent = this; + // ensure child transform will be recalculated + child.transform._parentID = -1; this.children.splice(index, 0, child); - // ensure a transform will be recalculated.. - this.transform._parentID = -1; + // ensure bounds will be recalculated this._boundsID++; // TODO - lets either do all callbacks or all events.. not both! @@ -225,10 +227,11 @@ if (index === -1) return null; child.parent = null; + // ensure child transform will be recalculated + child.transform._parentID = -1; removeItems(this.children, index, 1); - // ensure a transform will be recalculated.. - this.transform._parentID = -1; + // ensure bounds will be recalculated this._boundsID++; // TODO - lets either do all callbacks or all events.. not both! @@ -249,11 +252,12 @@ { const child = this.getChildAt(index); + // ensure child transform will be recalculated.. child.parent = null; + child.transform._parentID = -1; removeItems(this.children, index, 1); - // ensure a transform will be recalculated.. - this.transform._parentID = -1; + // ensure bounds will be recalculated this._boundsID++; // TODO - lets either do all callbacks or all events.. not both! @@ -284,13 +288,12 @@ for (let i = 0; i < removed.length; ++i) { removed[i].parent = null; + if (removed[i].transform) + { + removed[i].transform._parentID = -1; + } } - // ensure a transform will be recalculated.. - if (this.transform) - { - this.transform._parentID = -1; - } this._boundsID++; this.onChildrenChange(beginIndex); diff --git a/test/core/Container.js b/test/core/Container.js index c8fe618..d11be33 100644 --- a/test/core/Container.js +++ b/test/core/Container.js @@ -1,5 +1,33 @@ 'use strict'; +function testAddChild(fn) +{ + fn(function (container, obj) + { + container.addChild(obj); + }); + fn(function (container, obj) + { + container.addChildAt(obj); + }); +} + +function testRemoveChild(fn) +{ + fn(function (container, obj) + { + container.removeChild(obj); + }); + fn(function (container, obj) + { + container.removeChildAt(container.children.indexOf(obj)); + }); + fn(function (container, obj) + { + container.removeChildren(container.children.indexOf(obj), container.children.indexOf(obj) + 1); + }); +} + describe('PIXI.Container', function () { describe('parent', function () @@ -71,20 +99,48 @@ expect(spy).to.have.been.calledWith(0); }); - it('should flag transform for recalculation', function () + it('should flag child transform and container bounds for recalculation', testAddChild(function (mockAddChild) { const container = new PIXI.Container(); + const child = new PIXI.Container(); container.getBounds(); + child.getBounds(); - const parentID = container.transform._parentID; const boundsID = container._boundsID; + const childParentID = child.transform._parentID; - container.addChild(new PIXI.Container()); + mockAddChild(container, child); - expect(parentID).to.not.be.equals(container.transform._parentID); expect(boundsID).to.not.be.equals(container._boundsID); - }); + expect(childParentID).to.not.be.equals(child.transform._parentID); + })); + + it('should recalculate added child correctly', testAddChild(function (mockAddChild) + { + const parent = new PIXI.Container(); + const container = new PIXI.Container(); + const graphics = new PIXI.Graphics(); + + parent.addChild(container); + + graphics.drawRect(0, 0, 10, 10); + container.position.set(100, 200); + container.updateTransform(); + + graphics.getBounds(); + // Oops, that can happen sometimes! + graphics.transform._parentID = container.transform._worldID + 1; + + mockAddChild(container, graphics); + + const bounds = graphics.getBounds(); + + expect(bounds.x).to.be.equal(100); + expect(bounds.y).to.be.equal(200); + expect(bounds.width).to.be.equal(10); + expect(bounds.height).to.be.equal(10); + })); }); describe('removeChildAt', function () @@ -110,22 +166,6 @@ expect(spy).to.have.been.called; expect(spy).to.have.been.calledWith(0); }); - - it('should flag transform for recalculation', function () - { - const container = new PIXI.Container(); - - container.addChild(new PIXI.Container()); - container.getBounds(); - - const parentID = container.transform._parentID; - const boundsID = container._boundsID; - - container.removeChildAt(0); - - expect(parentID).to.not.be.equals(container.transform._parentID); - expect(boundsID).to.not.be.equals(container._boundsID); - }); }); describe('addChildAt', function () @@ -188,18 +228,6 @@ expect(spy).to.have.been.called; expect(spy).to.have.been.calledWith(0); }); - - it('should flag transform for recalculation', function () - { - const container = new PIXI.Container(); - const parentID = container.transform._parentID; - const boundsID = container._boundsID; - - container.addChildAt(new PIXI.Container(), 0); - - expect(parentID).to.not.be.equals(container.transform._parentID); - expect(boundsID).to.not.be.equals(container._boundsID); - }); }); describe('removeChild', function () @@ -246,7 +274,7 @@ expect(spy).to.have.been.calledWith(0); }); - it('should flag transform for recalculation', function () + it('should flag transform for recalculation', testRemoveChild(function (mockRemoveChild) { const container = new PIXI.Container(); const child = new PIXI.Container(); @@ -254,14 +282,35 @@ container.addChild(child); container.getBounds(); - const parentID = container.transform._parentID; + const childParentID = child.transform._parentID; const boundsID = container._boundsID; - container.removeChild(child); + mockRemoveChild(container, child); - expect(parentID).to.not.be.equals(container.transform._parentID); + expect(childParentID).to.not.be.equals(child.transform._parentID); expect(boundsID).to.not.be.equals(container._boundsID); - }); + })); + + it('should recalculate removed child correctly', testRemoveChild(function (mockRemoveChild) + { + const parent = new PIXI.Container(); + const container = new PIXI.Container(); + const graphics = new PIXI.Graphics(); + + parent.addChild(container); + + graphics.drawRect(0, 0, 10, 10); + container.position.set(100, 200); + container.addChild(graphics); + graphics.getBounds(); + + mockRemoveChild(container, graphics); + + const bounds = graphics.getBounds(); + + expect(bounds.x).to.be.equal(0); + expect(bounds.y).to.be.equal(0); + })); }); describe('getChildIndex', function () @@ -535,22 +584,6 @@ expect(() => container.removeChildren(-1, 1)) .to.throw('removeChildren: numeric values are outside the acceptable range.'); }); - - it('should flag transform for recalculation', function () - { - const container = new PIXI.Container(); - - container.addChild(new PIXI.Container()); - container.getBounds(); - - const parentID = container.transform._parentID; - const boundsID = container._boundsID; - - container.removeChildren(); - - expect(parentID).to.not.be.equals(container.transform._parentID); - expect(boundsID).to.not.be.equals(container._boundsID); - }); }); describe('destroy', function ()