diff --git a/src/core/display/Container.js b/src/core/display/Container.js index d037edd..039e124 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -95,6 +95,13 @@ }); /** + * Overridable method that can be used by Container subclasses whenever the children array is modified + * + * @private + */ +Container.prototype.onChildrenChange = function () {}; + +/** * Adds a child to the container. * * @param child {DisplayObject} The DisplayObject to add to the container @@ -130,6 +137,7 @@ child.parent = this; this.children.splice(index, 0, child); + this.onChildrenChange(); child.emit('added', this); @@ -164,6 +172,7 @@ this.children[index1] = child2; this.children[index2] = child; + this.onChildrenChange(); }; /** @@ -201,6 +210,7 @@ this.children.splice(currentIndex, 1); //remove from old position this.children.splice(index, 0, child); //add at new position + this.onChildrenChange(); }; /** @@ -249,6 +259,7 @@ child.parent = null; this.children.splice(index, 1); + this.onChildrenChange(); child.emit('removed', this); @@ -276,6 +287,13 @@ removed[i].parent = null; } + this.onChildrenChange(); + + for (var i = 0; i < removed.length; ++i) + { + removed[i].emit('removed', this); + } + return removed; } else if (range === 0 && this.children.length === 0) diff --git a/src/core/display/Container.js b/src/core/display/Container.js index d037edd..039e124 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -95,6 +95,13 @@ }); /** + * Overridable method that can be used by Container subclasses whenever the children array is modified + * + * @private + */ +Container.prototype.onChildrenChange = function () {}; + +/** * Adds a child to the container. * * @param child {DisplayObject} The DisplayObject to add to the container @@ -130,6 +137,7 @@ child.parent = this; this.children.splice(index, 0, child); + this.onChildrenChange(); child.emit('added', this); @@ -164,6 +172,7 @@ this.children[index1] = child2; this.children[index2] = child; + this.onChildrenChange(); }; /** @@ -201,6 +210,7 @@ this.children.splice(currentIndex, 1); //remove from old position this.children.splice(index, 0, child); //add at new position + this.onChildrenChange(); }; /** @@ -249,6 +259,7 @@ child.parent = null; this.children.splice(index, 1); + this.onChildrenChange(); child.emit('removed', this); @@ -276,6 +287,13 @@ removed[i].parent = null; } + this.onChildrenChange(); + + for (var i = 0; i < removed.length; ++i) + { + removed[i].emit('removed', this); + } + return removed; } else if (range === 0 && this.children.length === 0) diff --git a/src/core/particles/ParticleContainer.js b/src/core/particles/ParticleContainer.js index 4c23040..784bc25 100644 --- a/src/core/particles/ParticleContainer.js +++ b/src/core/particles/ParticleContainer.js @@ -161,57 +161,14 @@ }; /** - * Adds a child to this particle container at a specified index. If the index is out of bounds an error will be thrown + * Set the flag that static data should be updated to true * - * @param child {DisplayObject} The child to add - * @param index {Number} The index to place the child in - * @return {DisplayObject} The child that was added. + * @private */ -ParticleContainer.prototype.addChildAt = function (child, index) +ParticleContainer.prototype.onChildrenChange = function () { - // prevent adding self as child - if (child === this) - { - return child; - } - - if (index >= 0 && index <= this.children.length) - { - if (child.parent) - { - child.parent.removeChild(child); - } - - child.parent = this; - - this.children.splice(index, 0, child); - - this._updateStatic = true; - - return child; - } - else - { - throw new Error(child + 'addChildAt: The index '+ index +' supplied is out of bounds ' + this.children.length); - } -}; - -/** - * Removes a child from the specified index position. - * - * @param index {Number} The index to get the child from - * @return {DisplayObject} The child that was removed. - */ -ParticleContainer.prototype.removeChildAt = function (index) -{ - var child = this.getChildAt(index); - - child.parent = null; - this.children.splice(index, 1); this._updateStatic = true; - - return child; -}; +} /** * Renders the object using the Canvas renderer diff --git a/src/core/display/Container.js b/src/core/display/Container.js index d037edd..039e124 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -95,6 +95,13 @@ }); /** + * Overridable method that can be used by Container subclasses whenever the children array is modified + * + * @private + */ +Container.prototype.onChildrenChange = function () {}; + +/** * Adds a child to the container. * * @param child {DisplayObject} The DisplayObject to add to the container @@ -130,6 +137,7 @@ child.parent = this; this.children.splice(index, 0, child); + this.onChildrenChange(); child.emit('added', this); @@ -164,6 +172,7 @@ this.children[index1] = child2; this.children[index2] = child; + this.onChildrenChange(); }; /** @@ -201,6 +210,7 @@ this.children.splice(currentIndex, 1); //remove from old position this.children.splice(index, 0, child); //add at new position + this.onChildrenChange(); }; /** @@ -249,6 +259,7 @@ child.parent = null; this.children.splice(index, 1); + this.onChildrenChange(); child.emit('removed', this); @@ -276,6 +287,13 @@ removed[i].parent = null; } + this.onChildrenChange(); + + for (var i = 0; i < removed.length; ++i) + { + removed[i].emit('removed', this); + } + return removed; } else if (range === 0 && this.children.length === 0) diff --git a/src/core/particles/ParticleContainer.js b/src/core/particles/ParticleContainer.js index 4c23040..784bc25 100644 --- a/src/core/particles/ParticleContainer.js +++ b/src/core/particles/ParticleContainer.js @@ -161,57 +161,14 @@ }; /** - * Adds a child to this particle container at a specified index. If the index is out of bounds an error will be thrown + * Set the flag that static data should be updated to true * - * @param child {DisplayObject} The child to add - * @param index {Number} The index to place the child in - * @return {DisplayObject} The child that was added. + * @private */ -ParticleContainer.prototype.addChildAt = function (child, index) +ParticleContainer.prototype.onChildrenChange = function () { - // prevent adding self as child - if (child === this) - { - return child; - } - - if (index >= 0 && index <= this.children.length) - { - if (child.parent) - { - child.parent.removeChild(child); - } - - child.parent = this; - - this.children.splice(index, 0, child); - - this._updateStatic = true; - - return child; - } - else - { - throw new Error(child + 'addChildAt: The index '+ index +' supplied is out of bounds ' + this.children.length); - } -}; - -/** - * Removes a child from the specified index position. - * - * @param index {Number} The index to get the child from - * @return {DisplayObject} The child that was removed. - */ -ParticleContainer.prototype.removeChildAt = function (index) -{ - var child = this.getChildAt(index); - - child.parent = null; - this.children.splice(index, 1); this._updateStatic = true; - - return child; -}; +} /** * Renders the object using the Canvas renderer diff --git a/src/core/particles/webgl/ParticleBuffer.js b/src/core/particles/webgl/ParticleBuffer.js index 44ed52d..f319f18 100644 --- a/src/core/particles/webgl/ParticleBuffer.js +++ b/src/core/particles/webgl/ParticleBuffer.js @@ -17,7 +17,7 @@ * @memberof PIXI * @param renderer {WebGLRenderer} The renderer this sprite batch works for. */ -function ParticleBuffer(gl, properties, size) +function ParticleBuffer(gl, properties, dynamicPropertyFlags, size) { /** * the current WebGL drawing context @@ -40,7 +40,7 @@ this.vertByteSize = this.vertSize * 4; /** - * The number of images in the SpriteBatch before it flushes. + * The number of particles the buffer can hold * * @member {number} */ @@ -64,7 +64,7 @@ { var property = properties[i]; - if(property.dynamic) + if(dynamicPropertyFlags[i]) { this.dynamicProperties.push(property); } diff --git a/src/core/display/Container.js b/src/core/display/Container.js index d037edd..039e124 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -95,6 +95,13 @@ }); /** + * Overridable method that can be used by Container subclasses whenever the children array is modified + * + * @private + */ +Container.prototype.onChildrenChange = function () {}; + +/** * Adds a child to the container. * * @param child {DisplayObject} The DisplayObject to add to the container @@ -130,6 +137,7 @@ child.parent = this; this.children.splice(index, 0, child); + this.onChildrenChange(); child.emit('added', this); @@ -164,6 +172,7 @@ this.children[index1] = child2; this.children[index2] = child; + this.onChildrenChange(); }; /** @@ -201,6 +210,7 @@ this.children.splice(currentIndex, 1); //remove from old position this.children.splice(index, 0, child); //add at new position + this.onChildrenChange(); }; /** @@ -249,6 +259,7 @@ child.parent = null; this.children.splice(index, 1); + this.onChildrenChange(); child.emit('removed', this); @@ -276,6 +287,13 @@ removed[i].parent = null; } + this.onChildrenChange(); + + for (var i = 0; i < removed.length; ++i) + { + removed[i].emit('removed', this); + } + return removed; } else if (range === 0 && this.children.length === 0) diff --git a/src/core/particles/ParticleContainer.js b/src/core/particles/ParticleContainer.js index 4c23040..784bc25 100644 --- a/src/core/particles/ParticleContainer.js +++ b/src/core/particles/ParticleContainer.js @@ -161,57 +161,14 @@ }; /** - * Adds a child to this particle container at a specified index. If the index is out of bounds an error will be thrown + * Set the flag that static data should be updated to true * - * @param child {DisplayObject} The child to add - * @param index {Number} The index to place the child in - * @return {DisplayObject} The child that was added. + * @private */ -ParticleContainer.prototype.addChildAt = function (child, index) +ParticleContainer.prototype.onChildrenChange = function () { - // prevent adding self as child - if (child === this) - { - return child; - } - - if (index >= 0 && index <= this.children.length) - { - if (child.parent) - { - child.parent.removeChild(child); - } - - child.parent = this; - - this.children.splice(index, 0, child); - - this._updateStatic = true; - - return child; - } - else - { - throw new Error(child + 'addChildAt: The index '+ index +' supplied is out of bounds ' + this.children.length); - } -}; - -/** - * Removes a child from the specified index position. - * - * @param index {Number} The index to get the child from - * @return {DisplayObject} The child that was removed. - */ -ParticleContainer.prototype.removeChildAt = function (index) -{ - var child = this.getChildAt(index); - - child.parent = null; - this.children.splice(index, 1); this._updateStatic = true; - - return child; -}; +} /** * Renders the object using the Canvas renderer diff --git a/src/core/particles/webgl/ParticleBuffer.js b/src/core/particles/webgl/ParticleBuffer.js index 44ed52d..f319f18 100644 --- a/src/core/particles/webgl/ParticleBuffer.js +++ b/src/core/particles/webgl/ParticleBuffer.js @@ -17,7 +17,7 @@ * @memberof PIXI * @param renderer {WebGLRenderer} The renderer this sprite batch works for. */ -function ParticleBuffer(gl, properties, size) +function ParticleBuffer(gl, properties, dynamicPropertyFlags, size) { /** * the current WebGL drawing context @@ -40,7 +40,7 @@ this.vertByteSize = this.vertSize * 4; /** - * The number of images in the SpriteBatch before it flushes. + * The number of particles the buffer can hold * * @member {number} */ @@ -64,7 +64,7 @@ { var property = properties[i]; - if(property.dynamic) + if(dynamicPropertyFlags[i]) { this.dynamicProperties.push(property); } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 47dc445..bf40314 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -94,7 +94,6 @@ // verticesData { attribute:this.shader.attributes.aVertexPosition, - dynamic:false, size:2, uploadFunction:this.uploadVertices, offset:0 @@ -102,7 +101,6 @@ // positionData { attribute:this.shader.attributes.aPositionCoord, - dynamic:true, size:2, uploadFunction:this.uploadPosition, offset:0 @@ -110,7 +108,6 @@ // rotationData { attribute:this.shader.attributes.aRotation, - dynamic:false, size:1, uploadFunction:this.uploadRotation, offset:0 @@ -118,7 +115,6 @@ // uvsData { attribute:this.shader.attributes.aTextureCoord, - dynamic:false, size:2, uploadFunction:this.uploadUvs, offset:0 @@ -126,7 +122,6 @@ // alphaData { attribute:this.shader.attributes.aColor, - dynamic:false, size:1, uploadFunction:this.uploadAlpha, offset:0 @@ -194,7 +189,7 @@ gl.uniform1f(this.shader.uniforms.uAlpha._location, container.worldAlpha); - // if this variable is true then we will upload the static contents as well as the dynamic contens + // if this variable is true then we will upload the static contents as well as the dynamic contents var uploadStatic = container._updateStatic; // make sure the texture is bound.. @@ -208,7 +203,7 @@ return; } - if(!this.properties[0].dynamic || !this.properties[3].dynamic) + if(!container._properties[0] || !container._properties[3]) { uploadStatic = true; } @@ -237,6 +232,7 @@ if(uploadStatic) { buffer.uploadStatic(children, i, amount); + container._updateStatic = false; } // bind the buffer @@ -246,8 +242,6 @@ gl.drawElements(gl.TRIANGLES, amount * 6, gl.UNSIGNED_SHORT, 0); this.renderer.drawCount++; } - - container._updateStatic = false; }; /** @@ -261,17 +255,12 @@ buffers = [], size = container._maxSize, batchSize = container._batchSize, + dynamicPropertyFlags = container._properties, i; - // update the properties to match the state of the container.. - for (i = 0; i < container._properties.length; i++) - { - this.properties[i].dynamic = container._properties[i]; - } - for (i = 0; i < size; i += batchSize) { - buffers.push( new ParticleBuffer(gl, this.properties, batchSize, this.shader) ); + buffers.push(new ParticleBuffer(gl, this.properties, dynamicPropertyFlags, this.size)); } return buffers;