diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index a9ba5f7..b4b02a0 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index a9ba5f7..b4b02a0 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index a9ba5f7..b4b02a0 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index 75ed244..afbb360 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -422,9 +422,10 @@ * * @param position {PIXI.Point} The world origin to calculate from * @param [from] {PIXI.DisplayObject} The DisplayObject to calculate the global position from + * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new Point) * @return {PIXI.Point} A point object representing the position of this object */ -DisplayObject.prototype.toLocal = function (position, from) +DisplayObject.prototype.toLocal = function (position, from, point) { if (from) { @@ -446,7 +447,7 @@ } // simply apply the matrix.. - return this.worldTransform.applyInverse(position); + return this.worldTransform.applyInverse(position, point); }; /** @@ -530,7 +531,7 @@ this.position.y = y || 0; this.scale.x = !scaleX ? 1 : scaleX; this.scale.y = !scaleY ? 1 : scaleY; - this.rotation = rotation || 0; + this.rotation = rotation || 0; this.skew.x = skewX || 0; this.skew.y = skewY || 0; this.pivot.x = pivotX || 0; @@ -549,7 +550,7 @@ this.scale = null; this.pivot = null; this.skew = null; - + this.parent = null; this._bounds = null; diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index a9ba5f7..b4b02a0 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index 75ed244..afbb360 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -422,9 +422,10 @@ * * @param position {PIXI.Point} The world origin to calculate from * @param [from] {PIXI.DisplayObject} The DisplayObject to calculate the global position from + * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new Point) * @return {PIXI.Point} A point object representing the position of this object */ -DisplayObject.prototype.toLocal = function (position, from) +DisplayObject.prototype.toLocal = function (position, from, point) { if (from) { @@ -446,7 +447,7 @@ } // simply apply the matrix.. - return this.worldTransform.applyInverse(position); + return this.worldTransform.applyInverse(position, point); }; /** @@ -530,7 +531,7 @@ this.position.y = y || 0; this.scale.x = !scaleX ? 1 : scaleX; this.scale.y = !scaleY ? 1 : scaleY; - this.rotation = rotation || 0; + this.rotation = rotation || 0; this.skew.x = skewX || 0; this.skew.y = skewY || 0; this.pivot.x = pivotX || 0; @@ -549,7 +550,7 @@ this.scale = null; this.pivot = null; this.skew = null; - + this.parent = null; this._bounds = null; diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index a9ba5f7..b4b02a0 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index 75ed244..afbb360 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -422,9 +422,10 @@ * * @param position {PIXI.Point} The world origin to calculate from * @param [from] {PIXI.DisplayObject} The DisplayObject to calculate the global position from + * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new Point) * @return {PIXI.Point} A point object representing the position of this object */ -DisplayObject.prototype.toLocal = function (position, from) +DisplayObject.prototype.toLocal = function (position, from, point) { if (from) { @@ -446,7 +447,7 @@ } // simply apply the matrix.. - return this.worldTransform.applyInverse(position); + return this.worldTransform.applyInverse(position, point); }; /** @@ -530,7 +531,7 @@ this.position.y = y || 0; this.scale.x = !scaleX ? 1 : scaleX; this.scale.y = !scaleY ? 1 : scaleY; - this.rotation = rotation || 0; + this.rotation = rotation || 0; this.skew.x = skewX || 0; this.skew.y = skewY || 0; this.pivot.x = pivotX || 0; @@ -549,7 +550,7 @@ this.scale = null; this.pivot = null; this.skew = null; - + this.parent = null; this._bounds = null; diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 49ecd64..29cd7fc 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -231,11 +231,37 @@ * @param n {number} * @returns {number} 0 if n is 0, -1 if n is negative, 1 if n i positive */ - sign: function (n) { + sign: function (n) + { return n ? (n < 0 ? -1 : 1) : 0; }, /** + * removeItems + * + * @param {array} arr The target array + * @param {number} startIdx The index to begin removing from (inclusive) + * @param {number} removeCount How many items to remove + */ + removeItems: function (arr, startIdx, removeCount) + { + var length = arr.length; + + if (startIdx >= length || removeCount === 0) + { + return; + } + + removeCount = (startIdx+removeCount > length ? length-startIdx : removeCount); + for (var i = startIdx, len = length-removeCount; i < len; ++i) + { + arr[i] = arr[i + removeCount]; + } + + arr.length = len; + }, + + /** * @todo Describe property usage * @private */ diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index a9ba5f7..b4b02a0 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index 75ed244..afbb360 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -422,9 +422,10 @@ * * @param position {PIXI.Point} The world origin to calculate from * @param [from] {PIXI.DisplayObject} The DisplayObject to calculate the global position from + * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new Point) * @return {PIXI.Point} A point object representing the position of this object */ -DisplayObject.prototype.toLocal = function (position, from) +DisplayObject.prototype.toLocal = function (position, from, point) { if (from) { @@ -446,7 +447,7 @@ } // simply apply the matrix.. - return this.worldTransform.applyInverse(position); + return this.worldTransform.applyInverse(position, point); }; /** @@ -530,7 +531,7 @@ this.position.y = y || 0; this.scale.x = !scaleX ? 1 : scaleX; this.scale.y = !scaleY ? 1 : scaleY; - this.rotation = rotation || 0; + this.rotation = rotation || 0; this.skew.x = skewX || 0; this.skew.y = skewY || 0; this.pivot.x = pivotX || 0; @@ -549,7 +550,7 @@ this.scale = null; this.pivot = null; this.skew = null; - + this.parent = null; this._bounds = null; diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 49ecd64..29cd7fc 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -231,11 +231,37 @@ * @param n {number} * @returns {number} 0 if n is 0, -1 if n is negative, 1 if n i positive */ - sign: function (n) { + sign: function (n) + { return n ? (n < 0 ? -1 : 1) : 0; }, /** + * removeItems + * + * @param {array} arr The target array + * @param {number} startIdx The index to begin removing from (inclusive) + * @param {number} removeCount How many items to remove + */ + removeItems: function (arr, startIdx, removeCount) + { + var length = arr.length; + + if (startIdx >= length || removeCount === 0) + { + return; + } + + removeCount = (startIdx+removeCount > length ? length-startIdx : removeCount); + for (var i = startIdx, len = length-removeCount; i < len; ++i) + { + arr[i] = arr[i + removeCount]; + } + + arr.length = len; + }, + + /** * @todo Describe property usage * @private */ diff --git a/src/extras/BitmapText.js b/src/extras/BitmapText.js index 5298760..42ae05c 100644 --- a/src/extras/BitmapText.js +++ b/src/extras/BitmapText.js @@ -253,7 +253,7 @@ if (lastSpace !== -1 && this.maxWidth > 0 && pos.x * scale > this.maxWidth) { - chars.splice(lastSpace, i - lastSpace); + core.utils.removeItems(chars, lastSpace, i - lastSpace); i = lastSpace; lastSpace = -1; diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index a9ba5f7..b4b02a0 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index 75ed244..afbb360 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -422,9 +422,10 @@ * * @param position {PIXI.Point} The world origin to calculate from * @param [from] {PIXI.DisplayObject} The DisplayObject to calculate the global position from + * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new Point) * @return {PIXI.Point} A point object representing the position of this object */ -DisplayObject.prototype.toLocal = function (position, from) +DisplayObject.prototype.toLocal = function (position, from, point) { if (from) { @@ -446,7 +447,7 @@ } // simply apply the matrix.. - return this.worldTransform.applyInverse(position); + return this.worldTransform.applyInverse(position, point); }; /** @@ -530,7 +531,7 @@ this.position.y = y || 0; this.scale.x = !scaleX ? 1 : scaleX; this.scale.y = !scaleY ? 1 : scaleY; - this.rotation = rotation || 0; + this.rotation = rotation || 0; this.skew.x = skewX || 0; this.skew.y = skewY || 0; this.pivot.x = pivotX || 0; @@ -549,7 +550,7 @@ this.scale = null; this.pivot = null; this.skew = null; - + this.parent = null; this._bounds = null; diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 49ecd64..29cd7fc 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -231,11 +231,37 @@ * @param n {number} * @returns {number} 0 if n is 0, -1 if n is negative, 1 if n i positive */ - sign: function (n) { + sign: function (n) + { return n ? (n < 0 ? -1 : 1) : 0; }, /** + * removeItems + * + * @param {array} arr The target array + * @param {number} startIdx The index to begin removing from (inclusive) + * @param {number} removeCount How many items to remove + */ + removeItems: function (arr, startIdx, removeCount) + { + var length = arr.length; + + if (startIdx >= length || removeCount === 0) + { + return; + } + + removeCount = (startIdx+removeCount > length ? length-startIdx : removeCount); + for (var i = startIdx, len = length-removeCount; i < len; ++i) + { + arr[i] = arr[i + removeCount]; + } + + arr.length = len; + }, + + /** * @todo Describe property usage * @private */ diff --git a/src/extras/BitmapText.js b/src/extras/BitmapText.js index 5298760..42ae05c 100644 --- a/src/extras/BitmapText.js +++ b/src/extras/BitmapText.js @@ -253,7 +253,7 @@ if (lastSpace !== -1 && this.maxWidth > 0 && pos.x * scale > this.maxWidth) { - chars.splice(lastSpace, i - lastSpace); + core.utils.removeItems(chars, lastSpace, i - lastSpace); i = lastSpace; lastSpace = -1; diff --git a/src/interaction/InteractionData.js b/src/interaction/InteractionData.js index d0131ff..b29d188 100644 --- a/src/interaction/InteractionData.js +++ b/src/interaction/InteractionData.js @@ -38,10 +38,10 @@ * * @param displayObject {PIXI.DisplayObject} The DisplayObject that you would like the local coords off * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new point) - * param [globalPos] {PIXI.Point} A Point object containing your custom global coords, optional (otherwise will use the current global coords) + * @param [globalPos] {PIXI.Point} A Point object containing your custom global coords, optional (otherwise will use the current global coords) * @return {PIXI.Point} A point containing the coordinates of the InteractionData position relative to the DisplayObject */ InteractionData.prototype.getLocalPosition = function (displayObject, point, globalPos) { - return displayObject.toLocal(globalPos ? globalPos : this.global, point); + return displayObject.toLocal(globalPos || this.global, null, point); }; diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index a9ba5f7..b4b02a0 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index 75ed244..afbb360 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -422,9 +422,10 @@ * * @param position {PIXI.Point} The world origin to calculate from * @param [from] {PIXI.DisplayObject} The DisplayObject to calculate the global position from + * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new Point) * @return {PIXI.Point} A point object representing the position of this object */ -DisplayObject.prototype.toLocal = function (position, from) +DisplayObject.prototype.toLocal = function (position, from, point) { if (from) { @@ -446,7 +447,7 @@ } // simply apply the matrix.. - return this.worldTransform.applyInverse(position); + return this.worldTransform.applyInverse(position, point); }; /** @@ -530,7 +531,7 @@ this.position.y = y || 0; this.scale.x = !scaleX ? 1 : scaleX; this.scale.y = !scaleY ? 1 : scaleY; - this.rotation = rotation || 0; + this.rotation = rotation || 0; this.skew.x = skewX || 0; this.skew.y = skewY || 0; this.pivot.x = pivotX || 0; @@ -549,7 +550,7 @@ this.scale = null; this.pivot = null; this.skew = null; - + this.parent = null; this._bounds = null; diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 49ecd64..29cd7fc 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -231,11 +231,37 @@ * @param n {number} * @returns {number} 0 if n is 0, -1 if n is negative, 1 if n i positive */ - sign: function (n) { + sign: function (n) + { return n ? (n < 0 ? -1 : 1) : 0; }, /** + * removeItems + * + * @param {array} arr The target array + * @param {number} startIdx The index to begin removing from (inclusive) + * @param {number} removeCount How many items to remove + */ + removeItems: function (arr, startIdx, removeCount) + { + var length = arr.length; + + if (startIdx >= length || removeCount === 0) + { + return; + } + + removeCount = (startIdx+removeCount > length ? length-startIdx : removeCount); + for (var i = startIdx, len = length-removeCount; i < len; ++i) + { + arr[i] = arr[i + removeCount]; + } + + arr.length = len; + }, + + /** * @todo Describe property usage * @private */ diff --git a/src/extras/BitmapText.js b/src/extras/BitmapText.js index 5298760..42ae05c 100644 --- a/src/extras/BitmapText.js +++ b/src/extras/BitmapText.js @@ -253,7 +253,7 @@ if (lastSpace !== -1 && this.maxWidth > 0 && pos.x * scale > this.maxWidth) { - chars.splice(lastSpace, i - lastSpace); + core.utils.removeItems(chars, lastSpace, i - lastSpace); i = lastSpace; lastSpace = -1; diff --git a/src/interaction/InteractionData.js b/src/interaction/InteractionData.js index d0131ff..b29d188 100644 --- a/src/interaction/InteractionData.js +++ b/src/interaction/InteractionData.js @@ -38,10 +38,10 @@ * * @param displayObject {PIXI.DisplayObject} The DisplayObject that you would like the local coords off * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new point) - * param [globalPos] {PIXI.Point} A Point object containing your custom global coords, optional (otherwise will use the current global coords) + * @param [globalPos] {PIXI.Point} A Point object containing your custom global coords, optional (otherwise will use the current global coords) * @return {PIXI.Point} A point containing the coordinates of the InteractionData position relative to the DisplayObject */ InteractionData.prototype.getLocalPosition = function (displayObject, point, globalPos) { - return displayObject.toLocal(globalPos ? globalPos : this.global, point); + return displayObject.toLocal(globalPos || this.global, null, point); }; diff --git a/test/unit/core/utils/util.test.js b/test/unit/core/utils/util.test.js index ee9e2cb..199a1a4 100644 --- a/test/unit/core/utils/util.test.js +++ b/test/unit/core/utils/util.test.js @@ -107,4 +107,34 @@ } }); }); + + describe('.removeItems', function () { + var arr; + + beforeEach(function () { + arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + }); + + it('should return if the start index is greater than or equal to the length of the array', function () { + PIXI.utils.removeItems(arr, arr.length+1, 5); + expect(arr.length).to.be.equal(10); + }); + + it('should return if the remove count is 0', function () { + PIXI.utils.removeItems(arr, 2, 0); + expect(arr.length).to.be.equal(10); + }); + + it('should remove the number of elements specified from the array, starting from the start index', function () { + var res = [ 1, 2, 3, 8, 9, 10 ]; + PIXI.utils.removeItems(arr, 3, 4); + expect(arr).to.be.deep.equal(res); + }); + + it('should remove rest of elements if the delete count is > than the number of elements after start index', function () { + var res = [ 1, 2, 3, 4, 5, 6, 7 ]; + PIXI.utils.removeItems(arr, 7, 10); + expect(arr).to.be.deep.equal(res); + }); + }); }); diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index a9ba5f7..b4b02a0 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index 75ed244..afbb360 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -422,9 +422,10 @@ * * @param position {PIXI.Point} The world origin to calculate from * @param [from] {PIXI.DisplayObject} The DisplayObject to calculate the global position from + * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new Point) * @return {PIXI.Point} A point object representing the position of this object */ -DisplayObject.prototype.toLocal = function (position, from) +DisplayObject.prototype.toLocal = function (position, from, point) { if (from) { @@ -446,7 +447,7 @@ } // simply apply the matrix.. - return this.worldTransform.applyInverse(position); + return this.worldTransform.applyInverse(position, point); }; /** @@ -530,7 +531,7 @@ this.position.y = y || 0; this.scale.x = !scaleX ? 1 : scaleX; this.scale.y = !scaleY ? 1 : scaleY; - this.rotation = rotation || 0; + this.rotation = rotation || 0; this.skew.x = skewX || 0; this.skew.y = skewY || 0; this.pivot.x = pivotX || 0; @@ -549,7 +550,7 @@ this.scale = null; this.pivot = null; this.skew = null; - + this.parent = null; this._bounds = null; diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 49ecd64..29cd7fc 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -231,11 +231,37 @@ * @param n {number} * @returns {number} 0 if n is 0, -1 if n is negative, 1 if n i positive */ - sign: function (n) { + sign: function (n) + { return n ? (n < 0 ? -1 : 1) : 0; }, /** + * removeItems + * + * @param {array} arr The target array + * @param {number} startIdx The index to begin removing from (inclusive) + * @param {number} removeCount How many items to remove + */ + removeItems: function (arr, startIdx, removeCount) + { + var length = arr.length; + + if (startIdx >= length || removeCount === 0) + { + return; + } + + removeCount = (startIdx+removeCount > length ? length-startIdx : removeCount); + for (var i = startIdx, len = length-removeCount; i < len; ++i) + { + arr[i] = arr[i + removeCount]; + } + + arr.length = len; + }, + + /** * @todo Describe property usage * @private */ diff --git a/src/extras/BitmapText.js b/src/extras/BitmapText.js index 5298760..42ae05c 100644 --- a/src/extras/BitmapText.js +++ b/src/extras/BitmapText.js @@ -253,7 +253,7 @@ if (lastSpace !== -1 && this.maxWidth > 0 && pos.x * scale > this.maxWidth) { - chars.splice(lastSpace, i - lastSpace); + core.utils.removeItems(chars, lastSpace, i - lastSpace); i = lastSpace; lastSpace = -1; diff --git a/src/interaction/InteractionData.js b/src/interaction/InteractionData.js index d0131ff..b29d188 100644 --- a/src/interaction/InteractionData.js +++ b/src/interaction/InteractionData.js @@ -38,10 +38,10 @@ * * @param displayObject {PIXI.DisplayObject} The DisplayObject that you would like the local coords off * @param [point] {PIXI.Point} A Point object in which to store the value, optional (otherwise will create a new point) - * param [globalPos] {PIXI.Point} A Point object containing your custom global coords, optional (otherwise will use the current global coords) + * @param [globalPos] {PIXI.Point} A Point object containing your custom global coords, optional (otherwise will use the current global coords) * @return {PIXI.Point} A point containing the coordinates of the InteractionData position relative to the DisplayObject */ InteractionData.prototype.getLocalPosition = function (displayObject, point, globalPos) { - return displayObject.toLocal(globalPos ? globalPos : this.global, point); + return displayObject.toLocal(globalPos || this.global, null, point); }; diff --git a/test/unit/core/utils/util.test.js b/test/unit/core/utils/util.test.js index ee9e2cb..199a1a4 100644 --- a/test/unit/core/utils/util.test.js +++ b/test/unit/core/utils/util.test.js @@ -107,4 +107,34 @@ } }); }); + + describe('.removeItems', function () { + var arr; + + beforeEach(function () { + arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + }); + + it('should return if the start index is greater than or equal to the length of the array', function () { + PIXI.utils.removeItems(arr, arr.length+1, 5); + expect(arr.length).to.be.equal(10); + }); + + it('should return if the remove count is 0', function () { + PIXI.utils.removeItems(arr, 2, 0); + expect(arr.length).to.be.equal(10); + }); + + it('should remove the number of elements specified from the array, starting from the start index', function () { + var res = [ 1, 2, 3, 8, 9, 10 ]; + PIXI.utils.removeItems(arr, 3, 4); + expect(arr).to.be.deep.equal(res); + }); + + it('should remove rest of elements if the delete count is > than the number of elements after start index', function () { + var res = [ 1, 2, 3, 4, 5, 6, 7 ]; + PIXI.utils.removeItems(arr, 7, 10); + expect(arr).to.be.deep.equal(res); + }); + }); }); diff --git a/test/unit/interaction/InteractionData.test.js b/test/unit/interaction/InteractionData.test.js new file mode 100644 index 0000000..70660fc --- /dev/null +++ b/test/unit/interaction/InteractionData.test.js @@ -0,0 +1,17 @@ +describe('PIXI.interaction.InteractionData', function() { + describe('getLocalPosition', function() { + it('should populate second parameter with result', function() { + var data = new PIXI.interaction.InteractionData(), + displayObject = new PIXI.DisplayObject(), + point = new PIXI.Point(); + + data.global.set(10, 10); + displayObject.position.set(5, 3); + + data.getLocalPosition(displayObject, point); + + expect(point.x).to.equal(5); + expect(point.y).to.equal(7); + }); + }); +});