diff --git a/src/pixi/display/DisplayObjectContainer.js b/src/pixi/display/DisplayObjectContainer.js index ce58214..7ad1ad5 100644 --- a/src/pixi/display/DisplayObjectContainer.js +++ b/src/pixi/display/DisplayObjectContainer.js @@ -144,8 +144,8 @@ return; } - var index1 = this.children.indexOf(child); - var index2 = this.children.indexOf(child2); + var index1 = this.getChildIndex(child); + var index2 = this.getChildIndex(child2); if(index1 < 0 || index2 < 0) { throw new Error('swapChildren: Both the supplied DisplayObjects must be a child of the caller.'); @@ -154,6 +154,42 @@ this.children[index1] = child2; this.children[index2] = child; + +}; + +/** + * Returns the index position of a child DisplayObject instance + * + * @method getChildIndex + * @param child {DisplayObject} The DisplayObject instance to identify + * @return {Number} The index position of the child display object to identify + */ +PIXI.DisplayObjectContainer.prototype.getChildIndex = function(child) +{ + var index = this.children.indexOf(child); + if (index === -1) + { + throw new Error('The supplied DisplayObject must be a child of the caller'); + } + return index; +}; + +/** + * Changes the position of an existing child in the display object container + * + * @method setChildIndex + * @param child {DisplayObject} The child DisplayObject instance for which you want to change the index number + * @param index {Number} The resulting index number for the child display object + */ +PIXI.DisplayObjectContainer.prototype.setChildIndex = function(child, index) +{ + if (index < 0 || index >= this.children.length) + { + throw new Error('The supplied index is out of bounds'); + } + var currentIndex = this.getChildIndex(child); + this.children.splice(currentIndex, 1); //remove from old position + this.children.splice(index, 0, child); //add at new position }; /** @@ -164,14 +200,12 @@ */ PIXI.DisplayObjectContainer.prototype.getChildAt = function(index) { - if(index >= 0 && index < this.children.length) - { - return this.children[index]; - } - else + if (index < 0 || index >= this.children.length) { throw new Error('getChildAt: Supplied index '+ index +' does not exist in the child list, or the supplied DisplayObject must be a child of the caller'); } + return this.children[index]; + }; /** diff --git a/src/pixi/display/DisplayObjectContainer.js b/src/pixi/display/DisplayObjectContainer.js index ce58214..7ad1ad5 100644 --- a/src/pixi/display/DisplayObjectContainer.js +++ b/src/pixi/display/DisplayObjectContainer.js @@ -144,8 +144,8 @@ return; } - var index1 = this.children.indexOf(child); - var index2 = this.children.indexOf(child2); + var index1 = this.getChildIndex(child); + var index2 = this.getChildIndex(child2); if(index1 < 0 || index2 < 0) { throw new Error('swapChildren: Both the supplied DisplayObjects must be a child of the caller.'); @@ -154,6 +154,42 @@ this.children[index1] = child2; this.children[index2] = child; + +}; + +/** + * Returns the index position of a child DisplayObject instance + * + * @method getChildIndex + * @param child {DisplayObject} The DisplayObject instance to identify + * @return {Number} The index position of the child display object to identify + */ +PIXI.DisplayObjectContainer.prototype.getChildIndex = function(child) +{ + var index = this.children.indexOf(child); + if (index === -1) + { + throw new Error('The supplied DisplayObject must be a child of the caller'); + } + return index; +}; + +/** + * Changes the position of an existing child in the display object container + * + * @method setChildIndex + * @param child {DisplayObject} The child DisplayObject instance for which you want to change the index number + * @param index {Number} The resulting index number for the child display object + */ +PIXI.DisplayObjectContainer.prototype.setChildIndex = function(child, index) +{ + if (index < 0 || index >= this.children.length) + { + throw new Error('The supplied index is out of bounds'); + } + var currentIndex = this.getChildIndex(child); + this.children.splice(currentIndex, 1); //remove from old position + this.children.splice(index, 0, child); //add at new position }; /** @@ -164,14 +200,12 @@ */ PIXI.DisplayObjectContainer.prototype.getChildAt = function(index) { - if(index >= 0 && index < this.children.length) - { - return this.children[index]; - } - else + if (index < 0 || index >= this.children.length) { throw new Error('getChildAt: Supplied index '+ index +' does not exist in the child list, or the supplied DisplayObject must be a child of the caller'); } + return this.children[index]; + }; /** diff --git a/test/lib/pixi/display/DisplayObjectContainer.js b/test/lib/pixi/display/DisplayObjectContainer.js index 085c59f..58160a9 100644 --- a/test/lib/pixi/display/DisplayObjectContainer.js +++ b/test/lib/pixi/display/DisplayObjectContainer.js @@ -9,6 +9,8 @@ expect(obj).to.respondTo('addChildAt'); expect(obj).to.respondTo('swapChildren'); expect(obj).to.respondTo('getChildAt'); + expect(obj).to.respondTo('getChildIndex'); + expect(obj).to.respondTo('setChildIndex'); expect(obj).to.respondTo('removeChild'); expect(obj).to.respondTo('updateTransform'); diff --git a/src/pixi/display/DisplayObjectContainer.js b/src/pixi/display/DisplayObjectContainer.js index ce58214..7ad1ad5 100644 --- a/src/pixi/display/DisplayObjectContainer.js +++ b/src/pixi/display/DisplayObjectContainer.js @@ -144,8 +144,8 @@ return; } - var index1 = this.children.indexOf(child); - var index2 = this.children.indexOf(child2); + var index1 = this.getChildIndex(child); + var index2 = this.getChildIndex(child2); if(index1 < 0 || index2 < 0) { throw new Error('swapChildren: Both the supplied DisplayObjects must be a child of the caller.'); @@ -154,6 +154,42 @@ this.children[index1] = child2; this.children[index2] = child; + +}; + +/** + * Returns the index position of a child DisplayObject instance + * + * @method getChildIndex + * @param child {DisplayObject} The DisplayObject instance to identify + * @return {Number} The index position of the child display object to identify + */ +PIXI.DisplayObjectContainer.prototype.getChildIndex = function(child) +{ + var index = this.children.indexOf(child); + if (index === -1) + { + throw new Error('The supplied DisplayObject must be a child of the caller'); + } + return index; +}; + +/** + * Changes the position of an existing child in the display object container + * + * @method setChildIndex + * @param child {DisplayObject} The child DisplayObject instance for which you want to change the index number + * @param index {Number} The resulting index number for the child display object + */ +PIXI.DisplayObjectContainer.prototype.setChildIndex = function(child, index) +{ + if (index < 0 || index >= this.children.length) + { + throw new Error('The supplied index is out of bounds'); + } + var currentIndex = this.getChildIndex(child); + this.children.splice(currentIndex, 1); //remove from old position + this.children.splice(index, 0, child); //add at new position }; /** @@ -164,14 +200,12 @@ */ PIXI.DisplayObjectContainer.prototype.getChildAt = function(index) { - if(index >= 0 && index < this.children.length) - { - return this.children[index]; - } - else + if (index < 0 || index >= this.children.length) { throw new Error('getChildAt: Supplied index '+ index +' does not exist in the child list, or the supplied DisplayObject must be a child of the caller'); } + return this.children[index]; + }; /** diff --git a/test/lib/pixi/display/DisplayObjectContainer.js b/test/lib/pixi/display/DisplayObjectContainer.js index 085c59f..58160a9 100644 --- a/test/lib/pixi/display/DisplayObjectContainer.js +++ b/test/lib/pixi/display/DisplayObjectContainer.js @@ -9,6 +9,8 @@ expect(obj).to.respondTo('addChildAt'); expect(obj).to.respondTo('swapChildren'); expect(obj).to.respondTo('getChildAt'); + expect(obj).to.respondTo('getChildIndex'); + expect(obj).to.respondTo('setChildIndex'); expect(obj).to.respondTo('removeChild'); expect(obj).to.respondTo('updateTransform'); diff --git a/test/unit/pixi/display/DisplayObjectContainer.js b/test/unit/pixi/display/DisplayObjectContainer.js index ce7f02f..15a3376 100644 --- a/test/unit/pixi/display/DisplayObjectContainer.js +++ b/test/unit/pixi/display/DisplayObjectContainer.js @@ -17,4 +17,51 @@ expect(obj).to.have.property('renderable', false); expect(obj).to.have.property('stage', null); }); + + it('Gets child index', function() { + var container = new PIXI.DisplayObjectContainer(); + var children = []; + for (var i = 0; i < 10; i++) { + var child = new PIXI.DisplayObject(); + children.push(child); + container.addChild(child); + } + + for (i = 0; i < children.length; i++) { + expect(i).to.eql(container.getChildIndex(children[i])); + } + }); + + it('throws error when trying to get index of not a child', function() { + var container = new PIXI.DisplayObjectContainer(); + var child = new PIXI.DisplayObject(); + + expect(function() { container.getChildIndex(child); }).to.throw(); + }); + + it('Sets child index', function() { + var container = new PIXI.DisplayObjectContainer(); + var children = []; + + for (var i = 0; i < 10; i++) { + var child = new PIXI.DisplayObject(); + children.push(child); + container.addChild(child); + } + children.reverse(); + + for (i = 0; i < children.length; i++) { + container.setChildIndex(children[i], i); + expect(i).to.eql(container.getChildIndex(children[i])); + } + }); + + it('throws error when trying to set incorect index', function() { + var container = new PIXI.DisplayObjectContainer(); + var child = new PIXI.DisplayObject(); + container.addChild(child); + + expect(function() { container.setChildIndex(child, -1); }).to.throw(); + expect(function() { container.setChildIndex(child, 1); }).to.throw(); + }); });