diff --git a/cocos2d/CCScheduler.js b/cocos2d/CCScheduler.js index 442db0169c..5fd64c219e 100644 --- a/cocos2d/CCScheduler.js +++ b/cocos2d/CCScheduler.js @@ -170,8 +170,8 @@ cc.ArrayContainsObject = function (arr, findObj) { /** * find object from array by target * @param {Array} arr source array - * @param {cc.ListEntry|cc.HashUpdateEntry|cc.HashSelectorEntry} findInt find target - * @return {cc.ListEntry|cc.HashUpdateEntry|cc.HashSelectorEntry} + * @param {cc.ListEntry|cc.HashUpdateEntry} findInt find target + * @return {cc.ListEntry|cc.HashUpdateEntry} */ cc.HASH_FIND_INT = function (arr, findInt) { if (arr == null) { @@ -331,12 +331,13 @@ cc.Timer = cc.Class.extend(/** @lends cc.Timer# */{ this._elapsed = 0; this._timesExecuted = 0; } else { + var locTarget = this._target, locSelector = this._selector; if (this._runForever && !this._useDelay) { //standard timer usage this._elapsed += dt; if (this._elapsed >= this._interval) { - if (this._target && this._selector) + if (locTarget && locSelector) this._callSelector(); this._elapsed = 0; } @@ -345,7 +346,7 @@ cc.Timer = cc.Class.extend(/** @lends cc.Timer# */{ this._elapsed += dt; if (this._useDelay) { if (this._elapsed >= this._delay) { - if (this._target && this._selector) + if (locTarget && locSelector) this._callSelector(); this._elapsed = this._elapsed - this._delay; @@ -354,7 +355,7 @@ cc.Timer = cc.Class.extend(/** @lends cc.Timer# */{ } } else { if (this._elapsed >= this._interval) { - if (this._target && this._selector) + if (locTarget && locSelector) this._callSelector(); this._elapsed = 0; @@ -363,7 +364,7 @@ cc.Timer = cc.Class.extend(/** @lends cc.Timer# */{ } if (this._timesExecuted > this._repeat) - cc.Director.getInstance().getScheduler().unscheduleCallbackForTarget(this._target, this._selector); + cc.Director.getInstance().getScheduler().unscheduleCallbackForTarget(locTarget, locSelector); } } } @@ -999,4 +1000,3 @@ cc.Scheduler = cc.Class.extend(/** @lends cc.Scheduler# */{ } }); - diff --git a/cocos2d/base_nodes/CCNode.js b/cocos2d/base_nodes/CCNode.js index 876efdc95c..f620db75ac 100644 --- a/cocos2d/base_nodes/CCNode.js +++ b/cocos2d/base_nodes/CCNode.js @@ -48,9 +48,6 @@ cc.NODE_ON_EXIT = null; cc.s_globalOrderOfArrival = 1; - -//cc.NodeBase = cc.Class.extend(/** @lends cc.Node# */{ - /**

cc.Node is the main element. Anything thats gets drawn or contains things that get drawn is a cc.Node.
The most popular cc.Nodes are: cc.Scene, cc.Layer, cc.Sprite, cc.Menu. (WebGL implement)

@@ -160,6 +157,9 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ _componentContainer:null, _isTransitionFinished:false, + _rotationRadiansX: 0, + _rotationRadiansY: 0, + _initNode:function () { this._anchorPoint = cc.p(0, 0); this._anchorPointInPoints = cc.p(0, 0); @@ -252,6 +252,14 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ } }, + /** + * set the dirty node + */ + setNodeDirty:function () { + if(this._transformDirty === false) + this._transformDirty = this._inverseDirty = true; + }, + /** *

get the skew degrees in X
* The X skew angle of the node in degrees.
@@ -383,8 +391,6 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ return this._rotationX; }, - _rotationRadiansX: 0, - _rotationRadiansY: 0, /** *

* Sets the rotation (angle) of the node in degrees.
@@ -526,27 +532,20 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ * node.setPosition( cc.p(size.width/2, size.height/2) ) */ setPosition:function (newPosOrxValue, yValue) { + var locPosition = this._position; if (arguments.length == 2) { - this._position = new cc.Point(newPosOrxValue, yValue); + locPosition.x = newPosOrxValue; + locPosition.y = yValue; } else if (arguments.length == 1) { - this._position = new cc.Point(newPosOrxValue.x, newPosOrxValue.y); - } - this.setNodeDirty(); - }, - - _setPositionByValue:function (newPosOrxValue, yValue) { - if (arguments.length == 2) { - this._position.x = newPosOrxValue; - this._position.y = yValue; - } else if (arguments.length == 1) { - this._position.x = newPosOrxValue.x; - this._position.y = newPosOrxValue.y; + locPosition.x = newPosOrxValue.x; + locPosition.y = newPosOrxValue.y; } this.setNodeDirty(); }, /** *

Position (x,y) of the node in OpenGL coordinates. (0,0) is the left-bottom corner.

+ * @const * @return {cc.Point} */ getPosition:function () { @@ -631,6 +630,7 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ * The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
* But you can use values higher than (1,1) and lower than (0,0) too.
* The default anchorPoint is (0.5,0.5), so it starts in the center of the node.

+ * @const * @return {cc.Point} The anchor point of node. */ getAnchorPoint:function () { @@ -650,9 +650,13 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ * @param {cc.Point} point The anchor point of node. */ setAnchorPoint:function (point) { - if (!cc.pointEqualToPoint(point, this._anchorPoint)) { - this._anchorPoint = new cc.Point(point.x, point.y); - this._anchorPointInPoints = new cc.Point(this._contentSize.width * point.x, this._contentSize.height * point.y); + var locAnchorPoint = this._anchorPoint; + if (!cc.pointEqualToPoint(point, locAnchorPoint)) { + locAnchorPoint.x = point.x; + locAnchorPoint.y = point.y; + var locAPP = this._anchorPointInPoints, locSize = this._contentSize; + locAPP.x = locSize.width * point.x; + locAPP.y = locSize.height * point.y; this.setNodeDirty(); } }, @@ -661,6 +665,7 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ * The anchorPoint in absolute pixels.
* you can only read it. If you wish to modify it, use anchorPoint instead * @see getAnchorPoint() + * @const * @return {cc.Point} The anchor point in absolute pixels. */ getAnchorPointInPoints:function () { @@ -671,6 +676,7 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ *

The untransformed size of the node.
* The contentSize remains the same no matter the node is scaled or rotated.
* All nodes has a size. Layer and Scene has the same size of the screen.

+ * @const * @return {cc.Size} The untransformed size of the node. */ getContentSize:function () { @@ -687,9 +693,13 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ * @param {cc.Size} size The untransformed size of the node. */ setContentSize:function (size) { - if (!cc.sizeEqualToSize(size, this._contentSize)) { - this._contentSize = new cc.Size(size.width, size.height); - this._anchorPointInPoints = new cc.Point(this._contentSize.width * this._anchorPoint.x, this._contentSize.height * this._anchorPoint.y); + var locContentSize = this._contentSize; + if (!cc.sizeEqualToSize(size, locContentSize)) { + locContentSize.width = size.width; + locContentSize.height = size.height; + var locAPP = this._anchorPointInPoints, locAnchorPoint = this._anchorPoint; + locAPP.x = locContentSize.width * locAnchorPoint.x; + locAPP.y = locContentSize.height * locAnchorPoint.y; this.setNodeDirty(); } }, @@ -912,6 +922,7 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ * Returns a "local" axis aligned bounding box of the node.
* The returned box is relative only to its parent. * @note This method returns a temporaty variable, so it can't returns const CCRect& + * @const * @return {cc.Rect} */ getBoundingBox:function () { @@ -1679,7 +1690,7 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ if (!this._visible) return; var context = cc.renderContext, i, currentStack = cc.current_stack; - this._stackMatrix = this._stackMatrix || new cc.kmMat4(); + //cc.kmGLPushMatrixWitMat4(this._stackMatrix); //optimize performance for javascript currentStack.stack.push(currentStack.top); @@ -1728,12 +1739,6 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ transform:function () { //optimize performance for javascript var t4x4 = this._transform4x4, topMat4 = cc.current_stack.top; - if(!t4x4){ - t4x4 = new cc.kmMat4(); - t4x4.mat[2] = t4x4.mat[3] = t4x4.mat[6] = t4x4.mat[7] = t4x4.mat[8] = t4x4.mat[9] = t4x4.mat[11] = t4x4.mat[14] = 0.0; - t4x4.mat[10] = t4x4.mat[15] = 1.0; - this._transform4x4 = t4x4; - } // Convert 3x3 into 4x4 matrix //cc.CGAffineToGL(this.nodeToParentTransform(), this._transform4x4.mat); @@ -1830,13 +1835,6 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ return this._transform; }, - /** - * set the dirty node - */ - setNodeDirty:function () { - this._transformDirty = this._inverseDirty = true; - }, - /** * Returns a camera object that lets you move the node using a gluLookAt * @return {cc.Camera} A CCCamera object that lets you move the node using a gluLookAt @@ -1908,25 +1906,6 @@ cc.NodeWebGL = cc.Class.extend(/** @lends cc.NodeWebGL# */{ } }); -/** - * cc.Node's state callback type - * @constant - * @type Number - */ -cc.NodeWebGL.StateCallbackType = {onEnter:1, onExit:2, cleanup:3, onEnterTransitionDidFinish:4, updateTransform:5, onExitTransitionDidStart:6, sortAllChildren:7}; - -/** - * allocates and initializes a node. - * @constructs - * @return {cc.NodeWebGL} - * @example - * // example - * var node = cc.NodeWebGL.create(); - */ -cc.NodeWebGL.create = function () { - return new cc.NodeWebGL(); -}; - /**

cc.Node is the main element. Anything thats gets drawn or contains things that get drawn is a cc.Node.
The most popular cc.Nodes are: cc.Scene, cc.Layer, cc.Sprite, cc.Menu. (Canvas implement)

@@ -2030,6 +2009,9 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ _componentContainer:null, _isTransitionFinished:false, + _rotationRadiansX:0, + _rotationRadiansY:0, + _initNode:function () { this._anchorPoint = cc.p(0, 0); this._anchorPointInPoints = cc.p(0, 0); @@ -2253,8 +2235,6 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ return this._rotationX; }, - _rotationRadiansX:0, - _rotationRadiansY:0, /** *

* Sets the rotation (angle) of the node in degrees.
@@ -2395,27 +2375,20 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ * node.setPosition( cc.p(size.width/2, size.height/2) ) */ setPosition:function (newPosOrxValue, yValue) { - if (arguments.length === 2) { - this._position = new cc.Point(newPosOrxValue, yValue); - } else if (arguments.length === 1) { - this._position = new cc.Point(newPosOrxValue.x, newPosOrxValue.y); - } - this.setNodeDirty(); - }, - - _setPositionByValue:function (newPosOrxValue, yValue) { - if (arguments.length === 2) { - this._position.x = newPosOrxValue; - this._position.y = yValue; - } else if (arguments.length === 1) { - this._position.x = newPosOrxValue.x; - this._position.y = newPosOrxValue.y; + var locPosition = this._position; + if (arguments.length == 2) { + locPosition.x = newPosOrxValue; + locPosition.y = yValue; + } else if (arguments.length == 1) { + locPosition.x = newPosOrxValue.x; + locPosition.y = newPosOrxValue.y; } this.setNodeDirty(); }, /** *

Position (x,y) of the node in OpenGL coordinates. (0,0) is the left-bottom corner.

+ * @const * @return {cc.Point} The position (x,y) of the node in OpenGL coordinates */ getPosition:function () { @@ -2500,6 +2473,7 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ * The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
* But you can use values higher than (1,1) and lower than (0,0) too.
* The default anchorPoint is (0.5,0.5), so it starts in the center of the node.

+ * @const * @return {cc.Point} The anchor point of node. */ getAnchorPoint:function () { @@ -2519,9 +2493,13 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ * @param {cc.Point} point The anchor point of node. */ setAnchorPoint:function (point) { - if (!cc.pointEqualToPoint(point, this._anchorPoint)) { - this._anchorPoint = new cc.Point(point.x, point.y); - this._anchorPointInPoints = new cc.Point(this._contentSize.width * point.x, this._contentSize.height * point.y); + var locAnchorPoint = this._anchorPoint; + if (!cc.pointEqualToPoint(point, locAnchorPoint)) { + locAnchorPoint.x = point.x; + locAnchorPoint.y = point.y; + var locAPP = this._anchorPointInPoints, locSize = this._contentSize; + locAPP.x = locSize.width * point.x; + locAPP.y = locSize.height * point.y; this.setNodeDirty(); } }, @@ -2530,6 +2508,7 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ * The anchorPoint in absolute pixels.
* you can only read it. If you wish to modify it, use anchorPoint instead * @see getAnchorPoint() + * @const * @return {cc.Point} The anchor point in absolute pixels. */ getAnchorPointInPoints:function () { @@ -2540,6 +2519,7 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ *

The untransformed size of the node.
* The contentSize remains the same no matter the node is scaled or rotated.
* All nodes has a size. Layer and Scene has the same size of the screen.

+ * @const * @return {cc.Size} The untransformed size of the node. */ getContentSize:function () { @@ -2556,9 +2536,13 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ * @param {cc.Size} size The untransformed size of the node. */ setContentSize:function (size) { - if (!cc.sizeEqualToSize(size, this._contentSize)) { - this._contentSize = new cc.Size(size.width, size.height); - this._anchorPointInPoints = new cc.Point(this._contentSize.width * this._anchorPoint.x, this._contentSize.height * this._anchorPoint.y); + var locContentSize = this._contentSize; + if (!cc.sizeEqualToSize(size, locContentSize)) { + locContentSize.width = size.width; + locContentSize.height = size.height; + var locAPP = this._anchorPointInPoints, locAnchorPoint = this._anchorPoint; + locAPP.x = locContentSize.width * locAnchorPoint.x; + locAPP.y = locContentSize.height * locAnchorPoint.y; this.setNodeDirty(); } }, @@ -3666,7 +3650,8 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ */ setNodeDirty:function () { this._setNodeDirtyForCache(); - this._transformDirty = this._inverseDirty = true; + if(this._transformDirty === false) + this._transformDirty = this._inverseDirty = true; }, _setNodeDirtyForCache:function () { @@ -3700,26 +3685,26 @@ cc.NodeCanvas = cc.Class.extend(/** @lends cc.NodeCanvas# */{ } }); -/** - * cc.Node's state callback type - * @constant - * @type Number - */ -cc.NodeCanvas.StateCallbackType = {onEnter:1, onExit:2, cleanup:3, onEnterTransitionDidFinish:4, updateTransform:5, onExitTransitionDidStart:6, sortAllChildren:7}; +cc.Node = cc.Browser.supportWebGL ? cc.NodeWebGL : cc.NodeCanvas; /** * allocates and initializes a node. * @constructs - * @return {cc.NodeCanvas} + * @return {cc.Node} * @example * // example - * var node = cc.NodeWebGL.create(); + * var node = cc.Node.create(); */ -cc.NodeCanvas.create = function () { - return new cc.NodeCanvas(); +cc.Node.create = function () { + return new cc.Node(); }; -cc.Node = cc.Browser.supportWebGL ? cc.NodeWebGL : cc.NodeCanvas; +/** + * cc.Node's state callback type + * @constant + * @type Number + */ +cc.Node.StateCallbackType = {onEnter:1, onExit:2, cleanup:3, onEnterTransitionDidFinish:4, updateTransform:5, onExitTransitionDidStart:6, sortAllChildren:7}; /** *

@@ -3739,11 +3724,21 @@ cc.NodeRGBA = cc.Node.extend(/** @lends cc.NodeRGBA# */{ RGBAProtocol:true, _displayedOpacity:255, _realOpacity:255, - _displayedColor:cc.WHITE, - _realColor:cc.WHITE, + _displayedColor:null, + _realColor:null, _cascadeColorEnabled:false, _cascadeOpacityEnabled:false, + ctor:function(){ + cc.Node.prototype.ctor.call(this); + this._displayedOpacity = 255; + this._realOpacity = 255; + this._displayedColor = cc.WHITE; + this._realColor = cc.WHITE; + this._cascadeColorEnabled = false; + this._cascadeOpacityEnabled = false; + }, + init:function(){ if(cc.Node.prototype.init.call(this)){ return true; @@ -3799,8 +3794,13 @@ cc.NodeRGBA = cc.Node.extend(/** @lends cc.NodeRGBA# */{ }, setColor:function(color){ - this._displayedColor = cc.c3b(color.r, color.g, color.b); - this._realColor = cc.c3b(color.r, color.g, color.b); + var locDisplayedColor = this._displayedColor, locRealColor = this._realColor; + locDisplayedColor.r = color.r; + locDisplayedColor.g = color.g; + locDisplayedColor.b = color.b; + locRealColor.r = color.r; + locRealColor.g = color.g; + locRealColor.b = color.b; if (this._cascadeColorEnabled) { var parentColor = cc.white(); diff --git a/cocos2d/draw_nodes/CCDrawNode.js b/cocos2d/draw_nodes/CCDrawNode.js index 9085aed136..866379fa9e 100644 --- a/cocos2d/draw_nodes/CCDrawNode.js +++ b/cocos2d/draw_nodes/CCDrawNode.js @@ -115,19 +115,19 @@ cc.DrawNodeCanvas = cc.Node.extend(/** @lends cc.DrawNodeCanvas# */{ for (var i = 0; i < this._buffer.length; i++) { var element = this._buffer[i]; - if (element.type === cc.DRAWNODE_TYPE_DOT) { + if (element.type === cc.DrawNode.TYPE_DOT) { context.fillStyle = "rgba(" + (0 | (element.color.r * 255)) + "," + (0 | (element.color.g * 255)) + "," + (0 | (element.color.b * 255)) + "," + element.color.a + ")"; cc.drawingUtil.drawPoint(element.position, element.radius); } - if (element.type === cc.DRAWNODE_TYPE_SEGMENT) { + if (element.type === cc.DrawNode.TYPE_SEGMENT) { context.strokeStyle = "rgba(" + (0 | (element.color.r * 255)) + "," + (0 | (element.color.g * 255)) + "," + (0 | (element.color.b * 255)) + "," + element.color.a + ")"; context.lineWidth = element.radius * 2; context.lineCap = "round"; cc.drawingUtil.drawLine(element.from, element.to); } - if (element.type === cc.DRAWNODE_TYPE_POLY) { + if (element.type === cc.DrawNode.TYPE_POLY ) { context.fillStyle = "rgba(" + (0 | (element.fillColor.r * 255)) + "," + (0 | (element.fillColor.g * 255)) + "," + (0 | (element.fillColor.b * 255)) + "," + element.fillColor.a + ")"; cc.drawingUtil.drawPoly(element.verts, element.count, false, true); @@ -147,7 +147,7 @@ cc.DrawNodeCanvas = cc.Node.extend(/** @lends cc.DrawNodeCanvas# */{ * @param {cc.Color4F} color */ drawDot:function (pos, radius, color) { - var element = new cc._DrawNodeElement(cc.DRAWNODE_TYPE_DOT); + var element = new cc._DrawNodeElement(cc.DrawNode.TYPE_DOT); element.position = pos; element.radius = radius; element.color = color; @@ -162,7 +162,7 @@ cc.DrawNodeCanvas = cc.Node.extend(/** @lends cc.DrawNodeCanvas# */{ * @param {cc.Color4F} color */ drawSegment:function (from, to, radius, color) { - var element = new cc._DrawNodeElement(cc.DRAWNODE_TYPE_SEGMENT); + var element = new cc._DrawNodeElement(cc.DrawNode.TYPE_SEGMENT); element.from = from; element.to = to; element.radius = radius; @@ -178,7 +178,7 @@ cc.DrawNodeCanvas = cc.Node.extend(/** @lends cc.DrawNodeCanvas# */{ * @param {cc.Color4F} borderColor */ drawPoly:function (verts, fillColor, borderWidth, borderColor) { - var element = new cc._DrawNodeElement(cc.DRAWNODE_TYPE_POLY); + var element = new cc._DrawNodeElement(cc.DrawNode.TYPE_POLY ); element.verts = verts; element.count = verts.length; element.fillColor = fillColor; @@ -195,17 +195,6 @@ cc.DrawNodeCanvas = cc.Node.extend(/** @lends cc.DrawNodeCanvas# */{ } }); -/** - * Creates a DrawNodeCanvas - * @return {cc.DrawNodeCanvas} - */ -cc.DrawNodeCanvas.create = function () { - var ret = new cc.DrawNodeCanvas(); - if (ret && ret.init()) - return ret; - return null; -}; - /** *

CCDrawNode for WebGL
* Node that draws dots, segments and polygons.
@@ -276,20 +265,21 @@ cc.DrawNodeWebGL = cc.Node.extend(/** @lends cc.DrawNodeWebGL# */{ _ensureCapacity:function(count){ if(this._buffer.length + count > this._bufferCapacity){ + var TriangleLength = cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT; this._bufferCapacity += Math.max(this._bufferCapacity, count); //re alloc if((this._buffer == null) || (this._buffer.length === 0)){ //init this._buffer = []; - this._trianglesArrayBuffer = new ArrayBuffer(cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT * this._bufferCapacity); + this._trianglesArrayBuffer = new ArrayBuffer(TriangleLength * this._bufferCapacity); this._trianglesReader = new Uint8Array(this._trianglesArrayBuffer); } else { var newTriangles = []; - var newArrayBuffer = new ArrayBuffer(cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT * this._bufferCapacity); + var newArrayBuffer = new ArrayBuffer(TriangleLength * this._bufferCapacity); for(var i = 0; i < this._buffer.length;i++){ newTriangles[i] = new cc.V2F_C4B_T2F_Triangle(this._buffer[i].a,this._buffer[i].b,this._buffer[i].c, - newArrayBuffer,i * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT); + newArrayBuffer,i * TriangleLength); } this._trianglesReader = new Uint8Array(newArrayBuffer); this._buffer = newTriangles; @@ -351,29 +341,30 @@ cc.DrawNodeWebGL = cc.Node.extend(/** @lends cc.DrawNodeWebGL# */{ var v6 = cc.v2fsub(a, cc.v2fsub(nw, tw)); var v7 = cc.v2fadd(a, cc.v2fadd(nw, tw)); + var TriangleLength = cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT, triangleBuffer = this._trianglesArrayBuffer; this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: v0, colors: c4bColor, texCoords: cc.__t(cc.v2fneg(cc.v2fadd(n, t)))}, {vertices: v1, colors: c4bColor, texCoords: cc.__t(cc.v2fsub(n, t))}, {vertices: v2, colors: c4bColor, texCoords: cc.__t(cc.v2fneg(n))}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); + triangleBuffer, this._buffer.length * TriangleLength)); this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: v3, colors: c4bColor, texCoords: cc.__t(n)}, {vertices: v1, colors: c4bColor, texCoords: cc.__t(cc.v2fsub(n, t))}, {vertices: v2, colors: c4bColor, texCoords: cc.__t(cc.v2fneg(n))}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); + triangleBuffer, this._buffer.length * TriangleLength)); this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: v3, colors: c4bColor, texCoords: cc.__t(n)}, {vertices: v4, colors: c4bColor, texCoords: cc.__t(cc.v2fneg(n))}, {vertices: v2, colors: c4bColor, texCoords: cc.__t(cc.v2fneg(n))}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); + triangleBuffer, this._buffer.length * TriangleLength)); this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: v3, colors: c4bColor, texCoords: cc.__t(n)}, {vertices: v4, colors: c4bColor, texCoords: cc.__t(cc.v2fneg(n))}, {vertices: v5, colors: c4bColor, texCoords: cc.__t(n)}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); + triangleBuffer, this._buffer.length * TriangleLength)); this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: v6, colors: c4bColor, texCoords: cc.__t(cc.v2fsub(t, n))}, {vertices: v4, colors: c4bColor, texCoords: cc.__t(cc.v2fneg(n))}, {vertices: v5, colors: c4bColor, texCoords: cc.__t(n)}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); + triangleBuffer, this._buffer.length * TriangleLength)); this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: v6, colors: c4bColor, texCoords: cc.__t(cc.v2fsub(t, n))}, {vertices: v7, colors: c4bColor, texCoords: cc.__t(cc.v2fadd(n, t))}, {vertices: v5, colors: c4bColor, texCoords: cc.__t(n)}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); + triangleBuffer, this._buffer.length * TriangleLength)); this._dirty = true; }, @@ -405,14 +396,16 @@ cc.DrawNodeWebGL = cc.Node.extend(/** @lends cc.DrawNodeWebGL# */{ var vertexCount = 3 * triangleCount; this._ensureCapacity(vertexCount); + var triangleBytesLen = cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT, trianglesBuffer = this._trianglesArrayBuffer; + var locBuffer = this._buffer; var inset = (outline == false ? 0.5 : 0.0); for (i = 0; i < count - 2; i++) { v0 = cc.v2fsub(cc.__v2f(verts[0]), cc.v2fmult(extrude[0].offset, inset)); v1 = cc.v2fsub(cc.__v2f(verts[i + 1]), cc.v2fmult(extrude[i + 1].offset, inset)); v2 = cc.v2fsub(cc.__v2f(verts[i + 2]), cc.v2fmult(extrude[i + 2].offset, inset)); - this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: v0, colors: c4bFillColor, texCoords: cc.__t(cc.v2fzero())}, + locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: v0, colors: c4bFillColor, texCoords: cc.__t(cc.v2fzero())}, {vertices: v1, colors: c4bFillColor, texCoords: cc.__t(cc.v2fzero())}, {vertices: v2, colors: c4bFillColor, texCoords: cc.__t(cc.v2fzero())}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); + trianglesBuffer, locBuffer.length * triangleBytesLen)); } for (i = 0; i < count; i++) { @@ -429,19 +422,19 @@ cc.DrawNodeWebGL = cc.Node.extend(/** @lends cc.DrawNodeWebGL# */{ var outer1 = outline ? cc.v2fadd(v1, cc.v2fmult(offset1, borderWidth)) : cc.v2fadd(v1, cc.v2fmult(offset1, 0.5)); if (outline) { - this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bBorderColor, texCoords: cc.__t(cc.v2fneg(n0))}, + locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bBorderColor, texCoords: cc.__t(cc.v2fneg(n0))}, {vertices: inner1, colors: c4bBorderColor, texCoords: cc.__t(cc.v2fneg(n0))}, {vertices: outer1, colors: c4bBorderColor, texCoords: cc.__t(n0)}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); - this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bBorderColor, texCoords: cc.__t(cc.v2fneg(n0))}, + trianglesBuffer, locBuffer.length * triangleBytesLen)); + locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bBorderColor, texCoords: cc.__t(cc.v2fneg(n0))}, {vertices: outer0, colors: c4bBorderColor, texCoords: cc.__t(n0)}, {vertices: outer1, colors: c4bBorderColor, texCoords: cc.__t(n0)}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); + trianglesBuffer, locBuffer.length * triangleBytesLen)); } else { - this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bFillColor, texCoords: cc.__t(cc.v2fzero())}, + locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bFillColor, texCoords: cc.__t(cc.v2fzero())}, {vertices: inner1, colors: c4bFillColor, texCoords: cc.__t(cc.v2fzero())}, {vertices: outer1, colors: c4bFillColor, texCoords: cc.__t(n0)}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); - this._buffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bFillColor, texCoords: cc.__t(cc.v2fzero())}, + trianglesBuffer, locBuffer.length * triangleBytesLen)); + locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bFillColor, texCoords: cc.__t(cc.v2fzero())}, {vertices: outer0, colors: c4bFillColor, texCoords: cc.__t(n0)}, {vertices: outer1, colors: c4bFillColor, texCoords: cc.__t(n0)}, - this._trianglesArrayBuffer, this._buffer.length * cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT)); + trianglesBuffer, locBuffer.length * triangleBytesLen)); } } extrude = null; @@ -457,23 +450,23 @@ cc.DrawNodeWebGL = cc.Node.extend(/** @lends cc.DrawNodeWebGL# */{ } }); +cc.DrawNode = cc.Browser.supportWebGL ? cc.DrawNodeWebGL : cc.DrawNodeCanvas; + /** - * Creates a DrawNodeCanvas - * @return {cc.DrawNodeWebGL} + * Creates a DrawNode + * @return {cc.DrawNode} */ -cc.DrawNodeWebGL.create = function () { - var ret = new cc.DrawNodeWebGL(); +cc.DrawNode.create = function () { + var ret = new cc.DrawNode(); if (ret && ret.init()) return ret; return null; }; -cc.DrawNode = cc.Browser.supportWebGL ? cc.DrawNodeWebGL : cc.DrawNodeCanvas; - cc._DrawNodeElement = function (type) { this.type = type; }; -cc.DRAWNODE_TYPE_DOT = 0; -cc.DRAWNODE_TYPE_SEGMENT = 1; -cc.DRAWNODE_TYPE_POLY = 2; +cc.DrawNode.TYPE_DOT = 0; +cc.DrawNode.TYPE_SEGMENT = 1; +cc.DrawNode.TYPE_POLY = 2; diff --git a/cocos2d/kazmath/mat4.js b/cocos2d/kazmath/mat4.js index eed25b2efe..74e88b88e9 100644 --- a/cocos2d/kazmath/mat4.js +++ b/cocos2d/kazmath/mat4.js @@ -27,8 +27,9 @@ /* A 4x4 matrix + mat = | 0 4 8 12 | - mat = | 1 5 9 13 | + | 1 5 9 13 | | 2 6 10 14 | | 3 7 11 15 | */ diff --git a/cocos2d/label_nodes/CCLabelAtlas.js b/cocos2d/label_nodes/CCLabelAtlas.js index aa417c5e58..f04bbbfe8b 100644 --- a/cocos2d/label_nodes/CCLabelAtlas.js +++ b/cocos2d/label_nodes/CCLabelAtlas.js @@ -184,27 +184,6 @@ cc.LabelAtlasCanvas = cc.AtlasNode.extend(/** @lends cc.LabelAtlasCanvas# */{ } }); -/** - * It accepts two groups of parameters: - * a) string, fntFile - * b) label, textureFilename, width, height, startChar - * @return {cc.LabelAtlas|Null} returns the LabelAtlas object on success - * @example - * //Example - * //creates the cc.LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas - * var myLabel = cc.LabelAtlas.create('Text to display', 'CharMapfile.png', 12, 20, ' ') - * - * //creates the cc.LabelAtlas with a string, a fnt file - * var myLabel = cc.LabelAtlas.create('Text to display', 'CharMapFile.plist‘); - */ -cc.LabelAtlasCanvas.create = function (strText, charMapFile, itemWidth, itemHeight, startCharMap) { - var ret = new cc.LabelAtlasCanvas(); - if (ret && cc.LabelAtlasCanvas.prototype.initWithString.apply(ret,arguments)) { - return ret; - } - return null; -}; - /** * using image file to print text label on the screen, might be a bit slower than cc.Label, similar to cc.LabelBMFont (WebGL version) * @class @@ -390,6 +369,8 @@ cc.LabelAtlasWebGL = cc.AtlasNode.extend(/** @lends cc.LabelAtlasWebGL# */{ } }); +cc.LabelAtlas = cc.Browser.supportWebGL ? cc.LabelAtlasWebGL : cc.LabelAtlasCanvas; + /** * It accepts two groups of parameters: * a) string, fntFile @@ -408,13 +389,11 @@ cc.LabelAtlasWebGL = cc.AtlasNode.extend(/** @lends cc.LabelAtlasWebGL# */{ * //creates the cc.LabelAtlas with a string, a fnt file * var myLabel = cc.LabelAtlas.create('Text to display', 'CharMapFile.plist‘); */ -cc.LabelAtlasWebGL.create = function (strText, charMapFile, itemWidth, itemHeight, startCharMap) { - var ret = new cc.LabelAtlasWebGL(); - if (ret && cc.LabelAtlasWebGL.prototype.initWithString.apply(ret,arguments)) { +cc.LabelAtlas.create = function (strText, charMapFile, itemWidth, itemHeight, startCharMap) { + var ret = new cc.LabelAtlas(); + if (ret && cc.LabelAtlas.prototype.initWithString.apply(ret,arguments)) { return ret; } return null; }; -cc.LabelAtlas = cc.Browser.supportWebGL ? cc.LabelAtlasWebGL : cc.LabelAtlasCanvas; - diff --git a/cocos2d/label_nodes/CCLabelTTF.js b/cocos2d/label_nodes/CCLabelTTF.js index d6ec0fd7ba..9317c9b566 100644 --- a/cocos2d/label_nodes/CCLabelTTF.js +++ b/cocos2d/label_nodes/CCLabelTTF.js @@ -111,7 +111,8 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{ }, _setColorStyleStr:function () { - this._colorStyleStr = "rgba(" + this._textFillColor.r + "," + this._textFillColor.g + "," + this._textFillColor.b + ", " + this._realOpacity / 255 + ")"; + var locFillColor = this._textFillColor; + this._colorStyleStr = "rgba(" + locFillColor.r + "," + locFillColor.g + "," + locFillColor.b + ", " + this._realOpacity / 255 + ")"; }, /** @@ -200,9 +201,9 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{ this._fontName = fontName; this._hAlignment = hAlignment; this._vAlignment = vAlignment; - this._fontSize = fontSize; - this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'"; - this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize); + this._fontSize = fontSize * cc.CONTENT_SCALE_FACTOR(); + this._fontStyleStr = this._fontSize + "px '" + fontName + "'"; + this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName,this._fontSize); this.setString(strInfo); return true; } @@ -258,9 +259,10 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{ if (false === this._shadowEnabled) this._shadowEnabled = true; - if ((this._shadowOffset.width != shadowOffset.width) || (this._shadowOffset.height != shadowOffset.height)) { - this._shadowOffset.width = shadowOffset.width; - this._shadowOffset.height = shadowOffset.height; + var locShadowOffset = this._shadowOffset; + if ((locShadowOffset.width != shadowOffset.width) || (locShadowOffset.height != shadowOffset.height)) { + locShadowOffset.width = shadowOffset.width; + locShadowOffset.height = shadowOffset.height; } if (this._shadowOpacity != shadowOpacity ) @@ -333,7 +335,6 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{ this._fontSize = textDefinition.fontSize; this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'"; - // shadow if ( textDefinition.shadowEnabled) this.enableShadow(textDefinition.shadowOffset, textDefinition.shadowOpacity, textDefinition.shadowBlur, false); @@ -567,9 +568,8 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{ } var locVAlignment = this._vAlignment, locHAlignment = this._hAlignment, - locContentSizeWidth = this._contentSize.width* cc.CONTENT_SCALE_FACTOR(), - locContentSizeHeight = this._contentSize.height* cc.CONTENT_SCALE_FACTOR(); - var locFontHeight = this._fontClientHeight* cc.CONTENT_SCALE_FACTOR(); + locContentSizeWidth = this._contentSize.width, locContentSizeHeight = this._contentSize.height; + var locFontHeight = this._fontClientHeight; context.textBaseline = cc.LabelTTF._textBaseline[locVAlignment]; context.textAlign = cc.LabelTTF._textAlign[locHAlignment]; @@ -618,42 +618,6 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{ } }); -cc.LabelTTFCanvas._textAlign = ["left", "center", "right"]; - -cc.LabelTTFCanvas._textBaseline = ["top", "middle", "bottom"]; - -/** - * creates a cc.LabelTTF from a fontname, alignment, dimension and font size - * @param {String} label - * @param {String} fontName - * @param {Number} fontSize - * @param {cc.Size} dimensions - * @param {cc.TEXT_ALIGNMENT_LEFT|cc.TEXT_ALIGNMENT_CENTER|cc.TEXT_ALIGNMENT_RIGHT} alignment - * @return {cc.LabelTTF|Null} - * @example - * // Example - * var myLabel = cc.LabelTTF.create('label text', 'Times New Roman', 32, cc.size(32,16), cc.TEXT_ALIGNMENT_LEFT); - */ -cc.LabelTTFCanvas.create = function (/* Multi arguments */) { - var ret = new cc.LabelTTFCanvas(); - if (ret.initWithString(arguments)) - return ret; - return null; -}; - -/** - * Create a label with string and a font definition - * @param {String} text - * @param {cc.FontDefinition} textDefinition - * @return {cc.LabelTTF|Null} - */ -cc.LabelTTFCanvas.createWithFontDefinition = function(text, textDefinition){ - var ret = new cc.LabelTTF(); - if(ret && ret.initWithStringAndTextDefinition(text, textDefinition)) - return ret; - return null; -}; - /** * cc.LabelTTF is a subclass of cc.TextureNode that knows how to render text labels (WebGL implement)
* All features from cc.TextureNode are valid in cc.LabelTTF
@@ -672,7 +636,6 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ _string:"", _isMultiLine:false, _fontStyleStr:null, - _scaledFontStyleStr:null, _colorStyleStr:null, // font shadow @@ -705,7 +668,6 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ this._vAlignment = cc.VERTICAL_TEXT_ALIGNMENT_TOP; this._opacityModifyRGB = false; this._fontStyleStr = ""; - this._scaledFontStyleStr = ""; this._colorStyleStr = ""; this._fontName = "Arial"; this._opacity = 255; @@ -840,9 +802,8 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ this._fontName = fontName; this._hAlignment = hAlignment; this._vAlignment = vAlignment; - this._fontSize = fontSize; + this._fontSize = fontSize * cc.CONTENT_SCALE_FACTOR(); this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'"; - this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'"; this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize); this.setString(strInfo); this._updateTexture(); @@ -991,7 +952,6 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ this._fontName = textDefinition.fontName; this._fontSize = textDefinition.fontSize || 12; this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'"; - this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'"; this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize); // shadow @@ -1012,7 +972,6 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ _prepareTextDefinition:function(adjustForResolution){ var texDef = new cc.FontDefinition(); - //Do these reference to CONTENT_SCALE_FACTOR need to be removed ? if (adjustForResolution){ texDef.fontSize = this._fontSize * cc.CONTENT_SCALE_FACTOR(); texDef.fontDimensions = cc.SIZE_POINTS_TO_PIXELS(this._dimensions); @@ -1114,7 +1073,6 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ if (this._fontSize != fontSize) { this._fontSize = fontSize; this._fontStyleStr = fontSize + "px '" + this._fontName + "'"; - this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'"; this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,fontSize); // Force update this._needUpdateTexture = true; @@ -1129,7 +1087,6 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ if (this._fontName && this._fontName != fontName) { this._fontName = fontName; this._fontStyleStr = this._fontSize + "px '" + fontName + "'"; - this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'"; this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName,this._fontSize); // Force update this._needUpdateTexture = true; @@ -1145,8 +1102,8 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ context.setTransform(1, 0, 0, 1, 0, locContentSizeHeight); //this is fillText for canvas - if (context.font != this._scaledFontStyleStr) - context.font = this._scaledFontStyleStr; + if (context.font != this._fontStyleStr) + context.font = this._fontStyleStr; context.fillStyle = this._fillColorStr; //stroke style setup @@ -1344,8 +1301,8 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ this._labelContext.font = this._fontStyleStr; this._updateTTF(); var width = this._contentSize.width, height = this._contentSize.height; - this._labelCanvas.width = width * cc.CONTENT_SCALE_FACTOR(); - this._labelCanvas.height = height * cc.CONTENT_SCALE_FACTOR();; + this._labelCanvas.width = width; + this._labelCanvas.height = height; //draw text to labelCanvas this._drawTTFInCanvasForWebGL(this._labelContext); @@ -1422,9 +1379,11 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{ } }); -cc.LabelTTFWebGL._textAlign = ["left", "center", "right"]; +cc.LabelTTF = (cc.Browser.supportWebGL) ? cc.LabelTTFWebGL : cc.LabelTTFCanvas; + +cc.LabelTTF._textAlign = ["left", "center", "right"]; -cc.LabelTTFWebGL._textBaseline = ["top", "middle", "bottom"]; +cc.LabelTTF._textBaseline = ["top", "middle", "bottom"]; /** * creates a cc.LabelTTF from a fontname, alignment, dimension and font size @@ -1438,8 +1397,8 @@ cc.LabelTTFWebGL._textBaseline = ["top", "middle", "bottom"]; * // Example * var myLabel = cc.LabelTTF.create('label text', 'Times New Roman', 32, cc.size(32,16), cc.TEXT_ALIGNMENT_LEFT); */ -cc.LabelTTFWebGL.create = function (/* Multi arguments */) { - var ret = new cc.LabelTTFWebGL(); +cc.LabelTTF.create = function (/* Multi arguments */) { + var ret = new cc.LabelTTF(); if (ret.initWithString(arguments)) return ret; return null; @@ -1451,15 +1410,13 @@ cc.LabelTTFWebGL.create = function (/* Multi arguments */) { * @param {cc.FontDefinition} textDefinition * @return {cc.LabelTTF|Null} */ -cc.LabelTTFWebGL.createWithFontDefinition = function(text, textDefinition){ +cc.LabelTTF.createWithFontDefinition = function(text, textDefinition){ var ret = new cc.LabelTTF(); if(ret && ret.initWithStringAndTextDefinition(text, textDefinition)) return ret; return null; }; -cc.LabelTTF = (cc.Browser.supportWebGL) ? cc.LabelTTFWebGL : cc.LabelTTFCanvas; - if(cc.USE_LA88_LABELS) cc.LabelTTF._SHADER_PROGRAM = cc.SHADER_POSITION_TEXTURECOLOR; else diff --git a/cocos2d/layers_scenes_transitions_nodes/CCLayer.js b/cocos2d/layers_scenes_transitions_nodes/CCLayer.js index 42e3f5401e..d8f7737eca 100644 --- a/cocos2d/layers_scenes_transitions_nodes/CCLayer.js +++ b/cocos2d/layers_scenes_transitions_nodes/CCLayer.js @@ -839,7 +839,7 @@ cc.LayerColorCanvas = cc.LayerRGBA.extend(/** @lends cc.LayerColorCanvas# */{ /** * renders the layer - * @param {CanvasContext|Null} ctx + * @param {CanvasRenderingContext2D|Null} ctx */ draw:function (ctx) { var context = ctx || cc.renderContext; @@ -855,41 +855,6 @@ cc.LayerColorCanvas = cc.LayerRGBA.extend(/** @lends cc.LayerColorCanvas# */{ } }); -/** - * creates a cc.LayerColorCanvas with color, width and height in Points - * @param {cc.Color4B} color - * @param {Number|Null} width - * @param {Number|Null} height - * @return {cc.LayerColor} - * @example - * // Example - * //Create a yellow color layer as background - * var yellowBackground = cc.LayerColor.create(cc.c4b(255,255,0,255)); - * //If you didnt pass in width and height, it defaults to the same size as the canvas - * - * //create a yellow box, 200 by 200 in size - * var yellowBox = cc.LayerColorCanvas.create(cc.c3b(255,255,0,255), 200, 200); - */ -cc.LayerColorCanvas.create = function (color, width, height) { - var ret = new cc.LayerColorCanvas(); - switch (arguments.length) { - case 0: - ret.init(); - break; - case 1: - ret.init(color); - break; - case 3: - ret.init(color, width, height); - break; - default : - ret.init(); - break; - } - return ret; -}; - - /** * CCLayerColor is a subclass of CCLayer that implements the CCRGBAProtocol protocol. (WebGL implement)
* All features from CCLayer are valid, plus the following new features:
@@ -1097,11 +1062,13 @@ cc.LayerColorWebGL = cc.LayerRGBA.extend(/** @lends cc.LayerColorCanvas# */{ } }); +cc.LayerColor = cc.Browser.supportWebGL ? cc.LayerColorWebGL : cc.LayerColorCanvas; + /** * creates a cc.Layer with color, width and height in Points * @param {cc.Color4B} color - * @param {Number|Null} width - * @param {Number|Null} height + * @param {Number|Null} [width=] + * @param {Number|Null} [height=] * @return {cc.LayerColor} * @example * // Example @@ -1112,8 +1079,8 @@ cc.LayerColorWebGL = cc.LayerRGBA.extend(/** @lends cc.LayerColorCanvas# */{ * //create a yellow box, 200 by 200 in size * var yellowBox = cc.LayerColor.create(cc.c4b(255,255,0,255), 200, 200); */ -cc.LayerColorWebGL.create = function (color, width, height) { - var ret = new cc.LayerColorWebGL(); +cc.LayerColor.create = function (color, width, height) { + var ret = new cc.LayerColor(); switch (arguments.length) { case 0: ret.init(); @@ -1131,8 +1098,6 @@ cc.LayerColorWebGL.create = function (color, width, height) { return ret; }; -cc.LayerColor = cc.Browser.supportWebGL ? cc.LayerColorWebGL : cc.LayerColorCanvas; - /** *

* CCLayerGradient is a subclass of cc.LayerColor that draws gradients across the background.
diff --git a/cocos2d/platform/CCApplication.js b/cocos2d/platform/CCApplication.js index 66968acaf1..3c7099284a 100644 --- a/cocos2d/platform/CCApplication.js +++ b/cocos2d/platform/CCApplication.js @@ -94,8 +94,8 @@ cc.WEBGL = 1; cc.drawingUtil = null; /** - * main Canvas 2D Context of game engine - * @type CanvasContext + * main Canvas 2D/3D Context of game engine + * @type CanvasRenderingContext2D|WebGLRenderingContext */ cc.renderContext = null; diff --git a/cocos2d/sprite_nodes/CCAnimation.js b/cocos2d/sprite_nodes/CCAnimation.js index 1e2b817d96..e1cc01cdcb 100644 --- a/cocos2d/sprite_nodes/CCAnimation.js +++ b/cocos2d/sprite_nodes/CCAnimation.js @@ -67,9 +67,9 @@ cc.AnimationFrame = cc.Class.extend(/** @lends cc.AnimationFrame# */{ * @param {object} userInfo */ initWithSpriteFrame:function (spriteFrame, delayUnits, userInfo) { - this.setSpriteFrame(spriteFrame); - this.setDelayUnits(delayUnits); - this.setUserInfo(userInfo); + this._spriteFrame = spriteFrame; + this._delayPerUnit = delayUnits; + this._userInfo = userInfo; return true; }, diff --git a/cocos2d/sprite_nodes/CCAnimationCache.js b/cocos2d/sprite_nodes/CCAnimationCache.js index 5bae6e3379..f6bcf49d30 100644 --- a/cocos2d/sprite_nodes/CCAnimationCache.js +++ b/cocos2d/sprite_nodes/CCAnimationCache.js @@ -36,7 +36,6 @@ * cc.AnimationCache.getInstance().addAnimation(animation,"animation1"); */ cc.AnimationCache = cc.Class.extend(/** @lends cc.AnimationCache# */{ - /** * Adds a cc.Animation with a name. * @param {cc.Animation} animation diff --git a/cocos2d/sprite_nodes/CCSprite.js b/cocos2d/sprite_nodes/CCSprite.js index a514f31fe3..460545df8b 100644 --- a/cocos2d/sprite_nodes/CCSprite.js +++ b/cocos2d/sprite_nodes/CCSprite.js @@ -657,6 +657,7 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{ *

The scale factor of the node. 1.0 is the default scale factor.
* It modifies the X and Y scale at the same time. (override cc.Node )

* @param {Number} scale + * @param {Number} [scaleY=] * @override */ setScale:function (scale, scaleY) { @@ -1052,22 +1053,23 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{ //cc.Assert(this._batchNode, "updateTransform is only valid when cc.Sprite is being rendered using an cc.SpriteBatchNode"); // recaculate matrix only if it is dirty - if (this.isDirty()) { + if (this._dirty) { // If it is not visible, or one of its ancestors is not visible, then do nothing: - if (!this._visible || ( this._parent && this._parent != this._batchNode && this._parent._shouldBeHidden)) { + var locParent = this._parent; + if (!this._visible || ( locParent && locParent != this._batchNode && locParent._shouldBeHidden)) { this._shouldBeHidden = true; } else { this._shouldBeHidden = false; - if (!this._parent || this._parent == this._batchNode) { + if (!locParent || locParent == this._batchNode) { this._transformToBatch = this.nodeToParentTransform(); } else { //cc.Assert(this._parent instanceof cc.Sprite, "Logic error in CCSprite. Parent must be a CCSprite"); - this._transformToBatch = cc.AffineTransformConcat(this.nodeToParentTransform(), this._parent._transformToBatch); + this._transformToBatch = cc.AffineTransformConcat(this.nodeToParentTransform(), locParent._transformToBatch); } } this._recursiveDirty = false; - this.setDirty(false); + this._dirty = false; } // recursively iterate over children @@ -1130,7 +1132,11 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{ */ setDisplayFrame:function (newFrame) { this.setNodeDirty(); - this._unflippedOffsetPositionFromCenter = newFrame.getOffset(); + + var frameOffset = newFrame.getOffset(); + this._unflippedOffsetPositionFromCenter.x = frameOffset.x; + this._unflippedOffsetPositionFromCenter.y = frameOffset.y; + var pNewTexture = newFrame.getTexture(); // update texture before updating texture rect if (pNewTexture != this._texture) @@ -1292,140 +1298,6 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{ } }); -/** - *

- * Creates a sprite with an exsiting texture contained
- * After creation, the rect will be the size of the texture, and the offset will be (0,0). - *

- * @constructs - * @param {HTMLImageElement|HTMLCanvasElement|cc.Texture2D} texture A pointer to an existing CCTexture2D object. You can use a CCTexture2D object for many sprites. - * @param {cc.Rect} rect Only the contents inside the rect of this texture will be applied for this sprite. - * @param {cc.Point} offset offset of the texture - * @return {cc.Sprite} A valid sprite object - * @example - * //get an image - * var img = cc.TextureCache.getInstance().addImage("HelloHTML5World.png"); - * - * //create a sprite with texture - * var sprite1 = cc.Sprite.createWithTexture(img); - * - * //create a sprite with texture and rect - * var sprite2 = cc.Sprite.createWithTexture(img, cc.rect(0,0,480,320)); - * - * //create a sprite with texture and rect and offset - * var sprite3 = cc.Sprite.createWithTexture(img, cc.rect(0,0,480,320),cc.p(0,0)); - */ -cc.SpriteCanvas.createWithTexture = function (texture, rect, offset) { - var argnum = arguments.length; - var sprite = new cc.SpriteCanvas(); - switch (argnum) { - case 1: - /** Creates an sprite with a texture. - The rect used will be the size of the texture. - The offset will be (0,0). - */ - if (sprite && sprite.initWithTexture(texture)) { - return sprite; - } - return null; - break; - - case 2: - /** Creates an sprite with a texture and a rect. - The offset will be (0,0). - */ - if (sprite && sprite.initWithTexture(texture, rect)) { - return sprite; - } - return null; - break; - - case 3: - /** Creates an sprite with a texture, a rect and offset. */ - // not implement - cc.Assert(0, ""); - return null; - break; - - default: - throw "Sprite.createWithTexture(): Argument must be non-nil "; - break; - } -}; - -/** - * Create a sprite with filename and rect - * @constructs - * @param {String} fileName The string which indicates a path to image file, e.g., "scene1/monster.png". - * @param {cc.Rect} rect Only the contents inside rect of pszFileName's texture will be applied for this sprite. - * @return {cc.Sprite} A valid sprite object - * @example - * //create a sprite with filename - * var sprite1 = cc.Sprite.create("HelloHTML5World.png"); - * - * //create a sprite with filename and rect - * var sprite2 = cc.Sprite.create("HelloHTML5World.png",cc.rect(0,0,480,320)); - */ -cc.SpriteCanvas.create = function (fileName, rect) { - var argnum = arguments.length; - var sprite = new cc.SpriteCanvas(); - if (argnum === 0) { - if (sprite.init()) - return sprite; - } else { - if (sprite && sprite.init(fileName, rect)) - return sprite; - } - return null; -}; - -/** - * Creates a sprite with a sprite frame name - * @param {String} spriteFrameName name - * @return {cc.Sprite} A valid sprite object - * @example - * - * //create a sprite with a sprite frame - * var sprite = cc.Sprite.createWithSpriteFrameName('grossini_dance_01.png'); - */ -cc.SpriteCanvas.createWithSpriteFrameName = function (spriteFrameName) { - var spriteFrame = null; - if (typeof(spriteFrameName) == 'string') { - spriteFrame = cc.SpriteFrameCache.getInstance().getSpriteFrame(spriteFrameName); - if (!spriteFrame) { - cc.log("Invalid spriteFrameName: " + spriteFrameName); - return null; - } - } else { - cc.log("Invalid argument. Expecting string."); - return null; - } - var sprite = new cc.SpriteCanvas(); - if (sprite && sprite.initWithSpriteFrame(spriteFrame)) { - return sprite; - } - return null; -}; - -/** - * Creates a sprite with a sprite frame. - * @param {cc.SpriteFrame} spriteFrame A sprite frame which involves a texture and a rect - * @return {cc.Sprite} A valid sprite object - * @example - * //get a sprite frame - * var spriteFrame = cc.SpriteFrameCache.getInstance().getSpriteFrame("grossini_dance_01.png"); - * - * //create a sprite with a sprite frame - * var sprite = cc.Sprite.createWithSpriteFrame(spriteFrame); - */ -cc.SpriteCanvas.createWithSpriteFrame = function (spriteFrame) { - var sprite = new cc.SpriteCanvas(); - if (sprite && sprite.initWithSpriteFrame(spriteFrame)) { - return sprite; - } - return null; -}; - /** *

cc.Sprite is a 2d image ( http://en.wikipedia.org/wiki/Sprite_(computer_graphics) ) (WebGL implement)
* @@ -2691,6 +2563,8 @@ cc.SpriteWebGL = cc.NodeRGBA.extend(/** @lends cc.SpriteWebGL# */{ } }); +cc.Sprite = cc.Browser.supportWebGL ? cc.SpriteWebGL : cc.SpriteCanvas; + /** *

* Creates a sprite with an exsiting texture contained in a CCTexture2D object
@@ -2714,9 +2588,9 @@ cc.SpriteWebGL = cc.NodeRGBA.extend(/** @lends cc.SpriteWebGL# */{ * //create a sprite with texture and rect and offset * var sprite3 = cc.Sprite.createWithTexture(img, cc.rect(0,0,480,320),cc.p(0,0)); */ -cc.SpriteWebGL.createWithTexture = function (texture, rect, offset) { +cc.Sprite.createWithTexture = function (texture, rect, offset) { var argnum = arguments.length; - var sprite = new cc.SpriteWebGL(); + var sprite = new cc.Sprite(); switch (argnum) { case 1: /** Creates an sprite with a texture. @@ -2765,9 +2639,9 @@ cc.SpriteWebGL.createWithTexture = function (texture, rect, offset) { * //create a sprite with filename and rect * var sprite2 = cc.Sprite.create("HelloHTML5World.png",cc.rect(0,0,480,320)); */ -cc.SpriteWebGL.create = function (fileName, rect) { +cc.Sprite.create = function (fileName, rect) { var argnum = arguments.length; - var sprite = new cc.SpriteWebGL(); + var sprite = new cc.Sprite(); if (argnum === 0) { if (sprite.init()) return sprite; @@ -2796,7 +2670,7 @@ cc.SpriteWebGL.create = function (fileName, rect) { * //create a sprite with a sprite frame * var sprite = cc.Sprite.createWithSpriteFrameName('grossini_dance_01.png'); */ -cc.SpriteWebGL.createWithSpriteFrameName = function (spriteFrameName) { +cc.Sprite.createWithSpriteFrameName = function (spriteFrameName) { var spriteFrame = null; if (typeof(spriteFrameName) == 'string') { spriteFrame = cc.SpriteFrameCache.getInstance().getSpriteFrame(spriteFrameName); @@ -2808,7 +2682,7 @@ cc.SpriteWebGL.createWithSpriteFrameName = function (spriteFrameName) { cc.log("Invalid argument. Expecting string."); return null; } - var sprite = new cc.SpriteWebGL(); + var sprite = new cc.Sprite(); if (sprite && sprite.initWithSpriteFrame(spriteFrame)) { return sprite; } @@ -2831,12 +2705,10 @@ cc.SpriteWebGL.createWithSpriteFrameName = function (spriteFrameName) { * //create a sprite with a sprite frame * var sprite = cc.Sprite.createWithSpriteFrame(spriteFrame); */ -cc.SpriteWebGL.createWithSpriteFrame = function (spriteFrame) { - var sprite = new cc.SpriteWebGL(); +cc.Sprite.createWithSpriteFrame = function (spriteFrame) { + var sprite = new cc.Sprite(); if (sprite && sprite.initWithSpriteFrame(spriteFrame)) { return sprite; } return null; }; - -cc.Sprite = cc.Browser.supportWebGL ? cc.SpriteWebGL : cc.SpriteCanvas; diff --git a/cocos2d/sprite_nodes/CCSpriteBatchNode.js b/cocos2d/sprite_nodes/CCSpriteBatchNode.js index 04e42a2131..d037c01f08 100644 --- a/cocos2d/sprite_nodes/CCSpriteBatchNode.js +++ b/cocos2d/sprite_nodes/CCSpriteBatchNode.js @@ -78,15 +78,15 @@ cc.SpriteBatchNodeCanvas = cc.Node.extend(/** @lends cc.SpriteBatchNodeCanvas# * child.setAtlasIndex(z); // XXX: optimize with a binary search - var i = 0; - if (this._descendants && this._descendants.length > 0) { - for (var index = 0; index < this._descendants.length; index++) { - var obj = this._descendants[index]; + var i = 0, locDescendants = this._descendants; + if (locDescendants && locDescendants.length > 0) { + for (var index = 0; index < locDescendants.length; index++) { + var obj = locDescendants[index]; if (obj && (obj.getAtlasIndex() >= z)) ++i; } } - this._descendants = cc.ArrayAppendObjectToIndex(this._descendants, child, i); + this._descendants = cc.ArrayAppendObjectToIndex(locDescendants, child, i); // IMPORTANT: Call super, and not self. Avoid adding it to the texture atlas array cc.Node.prototype.addChild.call(this, child, z, aTag); @@ -372,8 +372,9 @@ cc.SpriteBatchNodeCanvas = cc.Node.extend(/** @lends cc.SpriteBatchNodeCanvas# * if (fileImage) this.init(fileImage, cc.DEFAULT_SPRITE_BATCH_CAPACITY); - this._renderTexture = cc.RenderTexture.create(cc.canvas.width, cc.canvas.height); - this.setContentSize(cc.size(cc.canvas.width, cc.canvas.height)); + var locCanvas = cc.canvas; + this._renderTexture = cc.RenderTexture.create(locCanvas.width, locCanvas.height); + this.setContentSize(cc.size(locCanvas.width, locCanvas.height)); }, /** @@ -462,7 +463,7 @@ cc.SpriteBatchNodeCanvas = cc.Node.extend(/** @lends cc.SpriteBatchNodeCanvas# * sprite.setBatchNode(this); sprite.setDirty(true); - cc.ArrayAppendObject(this._descendants, sprite); + this._descendants.push(sprite); var index = this._descendants.length - 1; sprite.setAtlasIndex(index); @@ -479,15 +480,15 @@ cc.SpriteBatchNodeCanvas = cc.Node.extend(/** @lends cc.SpriteBatchNodeCanvas# * removeSpriteFromAtlas:function (sprite) { // Cleanup sprite. It might be reused (issue #569) sprite.setBatchNode(null); - - var index = cc.ArrayGetIndexOfObject(this._descendants, sprite); + var locDescendants = this._descendants; + var index = cc.ArrayGetIndexOfObject(locDescendants, sprite); if (index != -1) { - cc.ArrayRemoveObjectAtIndex(this._descendants, index); + cc.ArrayRemoveObjectAtIndex(locDescendants, index); // update all sprites beyond this one - var len = this._descendants.length; + var len = locDescendants.length; for (; index < len; ++index) { - var s = this._descendants[index]; + var s = locDescendants[index]; s.setAtlasIndex(s.getAtlasIndex() - 1); } } @@ -517,14 +518,15 @@ cc.SpriteBatchNodeCanvas = cc.Node.extend(/** @lends cc.SpriteBatchNodeCanvas# * */ setTexture:function (texture) { this._textureForCanvas = texture; - for (var i = 0; i < this._children.length; i++) - this._children[i].setTexture(texture); + var locChildren = this._children; + for (var i = 0; i < locChildren.length; i++) + locChildren[i].setTexture(texture); }, /** * don't call visit on it's children ( override visit of cc.Node ) * @override - * @param {CanvasContext} ctx + * @param {CanvasRenderingContext2D} ctx */ visit:function (ctx) { var context = ctx || cc.renderContext; @@ -534,31 +536,32 @@ cc.SpriteBatchNodeCanvas = cc.Node.extend(/** @lends cc.SpriteBatchNodeCanvas# * context.save(); this.transform(ctx); - var i; + var i, locChildren = this._children; if (this._useCache) { if (this._cacheDirty) { //add dirty region - this._renderTexture.clear(); - this._renderTexture.context.save(); - this._renderTexture.context.translate(this._anchorPointInPoints.x, -(this._anchorPointInPoints.y )); - if (this._children) { + var locRenderTexture = this._renderTexture; + locRenderTexture.clear(); + locRenderTexture.context.save(); + locRenderTexture.context.translate(this._anchorPointInPoints.x, -(this._anchorPointInPoints.y )); + if (locChildren) { this.sortAllChildren(); - for (i = 0; i < this._children.length; i++) { - if (this._children[i]) - this._children[i].visit(this._renderTexture.context); + for (i = 0; i < locChildren.length; i++) { + if (locChildren[i]) + locChildren[i].visit(locRenderTexture.context); } } - this._renderTexture.context.restore(); + locRenderTexture.context.restore(); this._cacheDirty = false; } // draw RenderTexture this.draw(ctx); } else { - if (this._children) { + if (locChildren) { this.sortAllChildren(); - for (i = 0; i < this._children.length; i++) { - if (this._children[i]) - this._children[i].visit(context); + for (i = 0; i < locChildren.length; i++) { + if (locChildren[i]) + locChildren[i].visit(context); } } } @@ -602,10 +605,11 @@ cc.SpriteBatchNodeCanvas = cc.Node.extend(/** @lends cc.SpriteBatchNodeCanvas# * removeAllChildren:function (cleanup) { // Invalidate atlas index. issue #569 // useSelfRender should be performed on all descendants. issue #1216 - if (this._descendants && this._descendants.length > 0) { - for (var i = 0; i < this._descendants.length; i++) { - if (this._descendants[i]) - this._descendants[i].setBatchNode(null); + var locDescendants = this._descendants; + if (locDescendants && locDescendants.length > 0) { + for (var i = 0; i < locDescendants.length; i++) { + if (locDescendants[i]) + locDescendants[i].setBatchNode(null); } } @@ -615,25 +619,26 @@ cc.SpriteBatchNodeCanvas = cc.Node.extend(/** @lends cc.SpriteBatchNodeCanvas# * sortAllChildren:function () { if (this._reorderChildDirty) { - var i = 0, j = 0, length = this._children.length; + var i, j = 0, locChildren = this._children; + var length = locChildren.length; //insertion sort for (i = 1; i < length; i++) { - var tempItem = this._children[i]; + var tempItem = locChildren[i]; j = i - 1; //continue moving element downwards while zOrder is smaller or when zOrder is the same but orderOfArrival is smaller - while (j >= 0 && (tempItem.getZOrder() < this._children[j].getZOrder() || - (tempItem.getZOrder() == this._children[j].getZOrder() && tempItem.getOrderOfArrival() < this._children[j].getOrderOfArrival()))) { - this._children[j + 1] = this._children[j]; + while (j >= 0 && (tempItem.getZOrder() < locChildren[j].getZOrder() || + (tempItem.getZOrder() == locChildren[j].getZOrder() && tempItem.getOrderOfArrival() < locChildren[j].getOrderOfArrival()))) { + locChildren[j + 1] = locChildren[j]; j--; } - this._children[j + 1] = tempItem; + locChildren[j + 1] = tempItem; } //sorted now check all children - if (this._children.length > 0) { + if (locChildren.length > 0) { //first sort all children recursively based on zOrder - this._arrayMakeObjectsPerformSelector(this._children, cc.Node.StateCallbackType.sortAllChildren); + this._arrayMakeObjectsPerformSelector(locChildren, cc.Node.StateCallbackType.sortAllChildren); } this._reorderChildDirty = false; } @@ -641,54 +646,19 @@ cc.SpriteBatchNodeCanvas = cc.Node.extend(/** @lends cc.SpriteBatchNodeCanvas# * /** * draw cc.SpriteBatchNode (override draw of cc.Node) - * @param {CanvasContext} ctx + * @param {CanvasRenderingContext2D} ctx */ draw:function (ctx) { var context = ctx || cc.renderContext; //context.globalAlpha = this._opacity / 255; - var pos = cc.p(0 | ( -this._anchorPointInPoints.x), 0 | ( -this._anchorPointInPoints.y)); + var posX = 0 | ( -this._anchorPointInPoints.x), posY = 0 | ( -this._anchorPointInPoints.y); + var locRenderTexture = this._renderTexture; //direct draw image by canvas drawImage - if (this._renderTexture) - context.drawImage(this._renderTexture.getCanvas(), pos.x, -(pos.y + this._renderTexture.getCanvas().height)); + if (locRenderTexture) + context.drawImage(locRenderTexture.getCanvas(), posX, -(posY + locRenderTexture.getCanvas().height)); } }); -/** - *

- * creates a cc.SpriteBatchNodeCanvas with a file image (.png, .jpg etc) with a default capacity of 29 children.
- * The capacity will be increased in 33% in runtime if it run out of space.
- * The file will be loaded using the TextureMgr.
- *

- * @param {String} fileImage - * @param {Number} capacity - * @return {cc.SpriteBatchNodeCanvas} - * @example - * //create a SpriteBatchNode - * var parent2 = cc.SpriteBatchNode.create("res/animations/grossini.png", 50); - */ -cc.SpriteBatchNodeCanvas.create = function (fileImage, capacity) { - capacity = capacity || cc.DEFAULT_SPRITE_BATCH_CAPACITY; - var batchNode = new cc.SpriteBatchNodeCanvas(); - batchNode.init(fileImage, capacity); - return batchNode; -}; - -/** - *

- * creates a cc.SpriteBatchNodeCanvas with a texture2d and a default capacity of 29 children.
- * The capacity will be increased in 33% in runtime if it run out of space.
- *

- * @param {cc.Texture2D} texture - * @param {Number} capacity - * @return {cc.SpriteBatchNodeCanvas} - */ -cc.SpriteBatchNodeCanvas.createWithTexture = function (texture, capacity) { - capacity = capacity || cc.DEFAULT_SPRITE_BATCH_CAPACITY; - var batchNode = new cc.SpriteBatchNodeCanvas(); - batchNode.initWithTexture(texture, capacity); - return batchNode; -}; - /** *

* In WebGL render mode ,cc.SpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call
@@ -1426,6 +1396,8 @@ cc.SpriteBatchNodeWebGL = cc.Node.extend(/** @lends cc.SpriteBatchNodeWebGL# */{ } }); +cc.SpriteBatchNode = (cc.Browser.supportWebGL)?cc.SpriteBatchNodeWebGL:cc.SpriteBatchNodeCanvas; + /** *

* creates a cc.SpriteBatchNodeCanvas with a file image (.png, .jpg etc) with a default capacity of 29 children.
@@ -1434,14 +1406,14 @@ cc.SpriteBatchNodeWebGL = cc.Node.extend(/** @lends cc.SpriteBatchNodeWebGL# */{ *

* @param {String} fileImage * @param {Number} capacity - * @return {cc.SpriteBatchNodeWebGL} + * @return {cc.SpriteBatchNode} * @example * //create a SpriteBatchNode * var parent2 = cc.SpriteBatchNode.create("res/animations/grossini.png", 50); */ -cc.SpriteBatchNodeWebGL.create = function (fileImage, capacity) { +cc.SpriteBatchNode.create = function (fileImage, capacity) { capacity = capacity || cc.DEFAULT_SPRITE_BATCH_CAPACITY; - var batchNode = new cc.SpriteBatchNodeWebGL(); + var batchNode = new cc.SpriteBatchNode(); batchNode.init(fileImage, capacity); return batchNode; }; @@ -1453,15 +1425,13 @@ cc.SpriteBatchNodeWebGL.create = function (fileImage, capacity) { *

* @param {cc.Texture2D} texture * @param {Number} capacity - * @return {cc.SpriteBatchNodeWebGL} + * @return {cc.SpriteBatchNode} */ -cc.SpriteBatchNodeWebGL.createWithTexture = function (texture, capacity) { +cc.SpriteBatchNode.createWithTexture = function (texture, capacity) { capacity = capacity || cc.DEFAULT_SPRITE_BATCH_CAPACITY; - var batchNode = new cc.SpriteBatchNodeWebGL(); + var batchNode = new cc.SpriteBatchNode(); batchNode.initWithTexture(texture, capacity); return batchNode; }; -cc.SpriteBatchNode = (cc.Browser.supportWebGL)?cc.SpriteBatchNodeWebGL:cc.SpriteBatchNodeCanvas; - diff --git a/cocos2d/sprite_nodes/CCSpriteFrame.js b/cocos2d/sprite_nodes/CCSpriteFrame.js index 9df2204ccf..09aa85ae7f 100644 --- a/cocos2d/sprite_nodes/CCSpriteFrame.js +++ b/cocos2d/sprite_nodes/CCSpriteFrame.js @@ -132,6 +132,7 @@ cc.SpriteFrame = cc.Class.extend(/** @lends cc.SpriteFrame# */{ /** * get original size of the trimmed image + * @const * @return {cc.Size} */ getOriginalSizeInPixels:function () { @@ -148,6 +149,7 @@ cc.SpriteFrame = cc.Class.extend(/** @lends cc.SpriteFrame# */{ /** * get original size of the trimmed image + * @const * @return {cc.Size} */ getOriginalSize:function () { @@ -186,6 +188,7 @@ cc.SpriteFrame = cc.Class.extend(/** @lends cc.SpriteFrame# */{ /** * Offset getter + * @const * @return {cc.Point} */ getOffset:function () { @@ -197,8 +200,8 @@ cc.SpriteFrame = cc.Class.extend(/** @lends cc.SpriteFrame# */{ * @param {cc.Point} offsets */ setOffset:function (offsets) { - this._offset = offsets; - this._offsetInPixels = cc.POINT_POINTS_TO_PIXELS(offsetInPixels); + this._offset.x = offsets.x; + this._offset.y = offsets.y; }, clone: function(){ diff --git a/cocos2d/textures/CCTextureCache.js b/cocos2d/textures/CCTextureCache.js index 8b3363e41b..dd7a4b2cde 100644 --- a/cocos2d/textures/CCTextureCache.js +++ b/cocos2d/textures/CCTextureCache.js @@ -406,23 +406,11 @@ cc.TextureCacheCanvas = cc.Class.extend(/** @lends cc.TextureCacheCanvas# */{ }); /** - * Return ths shared instance of the cache - * @return {cc.TextureCache} - */ -cc.TextureCacheCanvas.getInstance = function () { - if (!cc.g_sharedTextureCache) - cc.g_sharedTextureCache = new cc.TextureCacheCanvas(); - return cc.g_sharedTextureCache; -}; - -/** - * Purges the cache. It releases the retained instance. + * Implementation TextureCache (WebGL implement) + * @class + * @extends cc.Class */ -cc.TextureCacheCanvas.purgeSharedTextureCache = function () { - cc.g_sharedTextureCache = null; -}; - -cc.TextureCacheWebGL = cc.Class.extend({ +cc.TextureCacheWebGL = cc.Class.extend(/** @lends cc.TextureCacheWebGL# */{ /// ---- common properties start ---- _textures:null, _textureColorsCache:null, @@ -821,23 +809,21 @@ cc.TextureCacheWebGL = cc.Class.extend({ } }); +cc.TextureCache = cc.Browser.supportWebGL ? cc.TextureCacheWebGL : cc.TextureCacheCanvas; + /** * Return ths shared instance of the cache * @return {cc.TextureCache} */ -cc.TextureCacheWebGL.getInstance = function () { +cc.TextureCache.getInstance = function () { if (!cc.g_sharedTextureCache) - cc.g_sharedTextureCache = new cc.TextureCacheWebGL(); + cc.g_sharedTextureCache = new cc.TextureCache(); return cc.g_sharedTextureCache; }; /** * Purges the cache. It releases the retained instance. */ -cc.TextureCacheWebGL.purgeSharedTextureCache = function () { +cc.TextureCache.purgeSharedTextureCache = function () { cc.g_sharedTextureCache = null; }; - -cc.TextureCache = cc.Browser.supportWebGL ? cc.TextureCacheWebGL : cc.TextureCacheCanvas; - - diff --git a/extensions/GUI/CCControlExtension/CCScale9Sprite.js b/extensions/GUI/CCControlExtension/CCScale9Sprite.js index fa02e3655f..1a88e7a41b 100644 --- a/extensions/GUI/CCControlExtension/CCScale9Sprite.js +++ b/extensions/GUI/CCControlExtension/CCScale9Sprite.js @@ -82,16 +82,18 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{ _spriteFrameRotated: false, _updateCapInset: function () { - var insets; - if (this._insetLeft == 0 && this._insetTop == 0 && this._insetRight == 0 && this._insetBottom == 0) { + var insets, locInsetLeft = this._insetLeft, locInsetTop = this._insetTop, locInsetRight = this._insetRight; + var locSpriteRect = this._spriteRect, locInsetBottom = this._insetBottom; + if (locInsetLeft === 0 && locInsetTop === 0 && locInsetRight === 0 && locInsetBottom === 0) { insets = cc.RectZero(); } else { - insets = this._spriteFrameRotated ? cc.RectMake(this._insetBottom, this._insetLeft, - this._spriteRect.width - this._insetRight - this._insetLeft, - this._spriteRect.height - this._insetTop - this._insetBottom) : - cc.RectMake(this._insetLeft, this._insetTop, - this._spriteRect.width - this._insetLeft - this._insetRight, - this._spriteRect.height - this._insetTop - this._insetBottom); + + insets = this._spriteFrameRotated ? cc.RectMake(locInsetBottom, locInsetLeft, + locSpriteRect.width - locInsetRight - locInsetLeft, + locSpriteRect.height - locInsetTop - locInsetBottom) : + cc.RectMake(locInsetLeft, locInsetTop, + locSpriteRect.width - locInsetLeft - locInsetRight, + locSpriteRect.height - locInsetTop - locInsetBottom); } this.setCapInsets(insets); }, @@ -105,64 +107,70 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{ } var size = this._contentSize; - var sizableWidth = size.width - this._topLeft.getContentSize().width - this._topRight.getContentSize().width; - var sizableHeight = size.height - this._topLeft.getContentSize().height - this._bottomRight.getContentSize().height; - var horizontalScale = sizableWidth / this._centre.getContentSize().width; - var verticalScale = sizableHeight / this._centre.getContentSize().height; - var rescaledWidth = this._centre.getContentSize().width * horizontalScale; - var rescaledHeight = this._centre.getContentSize().height * verticalScale; + var locTopLeft = this._topLeft, locTopRight = this._topRight, locBottomRight = this._bottomRight; + var locCenter = this._centre, locCenterContentSize = this._centre.getContentSize(); + + var sizableWidth = size.width - locTopLeft.getContentSize().width - locTopRight.getContentSize().width; + var sizableHeight = size.height - locTopLeft.getContentSize().height - locBottomRight.getContentSize().height; + var horizontalScale = sizableWidth / locCenterContentSize.width; + var verticalScale = sizableHeight / locCenterContentSize.height; + var rescaledWidth = locCenterContentSize.width * horizontalScale; + var rescaledHeight = locCenterContentSize.height * verticalScale; - var leftWidth = this._bottomLeft.getContentSize().width; - var bottomHeight = this._bottomLeft.getContentSize().height; + var locBottomLeft = this._bottomLeft; + var leftWidth = locBottomLeft.getContentSize().width; + var bottomHeight = locBottomLeft.getContentSize().height; if(!cc.Browser.supportWebGL) { //browser is in canvas mode, need to manually control rounding to prevent overlapping pixels var roundedRescaledWidth = Math.round(rescaledWidth); if(rescaledWidth != roundedRescaledWidth) { rescaledWidth = roundedRescaledWidth; - horizontalScale = rescaledWidth/this._centre.getContentSize().width; + horizontalScale = rescaledWidth/locCenterContentSize.width; } var roundedRescaledHeight = Math.round(rescaledHeight); if(rescaledHeight != roundedRescaledHeight) { rescaledHeight = roundedRescaledHeight; - verticalScale = rescaledHeight/this._centre.getContentSize().height; + verticalScale = rescaledHeight/locCenterContentSize.height; } } - this._centre.setScaleX(horizontalScale); - this._centre.setScaleY(verticalScale); - - this._bottomLeft.setAnchorPoint(cc.p(0, 0)); - this._bottomRight.setAnchorPoint(cc.p(0, 0)); - this._topLeft.setAnchorPoint(cc.p(0, 0)); - this._topRight.setAnchorPoint(cc.p(0, 0)); - this._left.setAnchorPoint(cc.p(0, 0)); - this._right.setAnchorPoint(cc.p(0, 0)); - this._top.setAnchorPoint(cc.p(0, 0)); - this._bottom.setAnchorPoint(cc.p(0, 0)); - this._centre.setAnchorPoint(cc.p(0, 0)); + locCenter.setScaleX(horizontalScale); + locCenter.setScaleY(verticalScale); + + var locLeft = this._left, locRight = this._right, locTop = this._top, locBottom = this._bottom; + var tempAP = cc.p(0, 0); + locBottomLeft.setAnchorPoint(tempAP); + locBottomRight.setAnchorPoint(tempAP); + locTopLeft.setAnchorPoint(tempAP); + locTopRight.setAnchorPoint(tempAP); + locLeft.setAnchorPoint(tempAP); + locRight.setAnchorPoint(tempAP); + locTop.setAnchorPoint(tempAP); + locBottom.setAnchorPoint(tempAP); + locCenter.setAnchorPoint(tempAP); // Position corners - this._bottomLeft.setPosition(cc.p(0, 0)); - this._bottomRight.setPosition(cc.p(leftWidth + rescaledWidth, 0)); - this._topLeft.setPosition(cc.p(0, bottomHeight + rescaledHeight)); - this._topRight.setPosition(cc.p(leftWidth + rescaledWidth, bottomHeight + rescaledHeight)); + locBottomLeft.setPosition(0, 0); + locBottomRight.setPosition(leftWidth + rescaledWidth, 0); + locTopLeft.setPosition(0, bottomHeight + rescaledHeight); + locTopRight.setPosition(leftWidth + rescaledWidth, bottomHeight + rescaledHeight); // Scale and position borders - this._left.setPosition(cc.p(0, bottomHeight)); - this._left.setScaleY(verticalScale); - this._right.setPosition(cc.p(leftWidth + rescaledWidth, bottomHeight)); - this._right.setScaleY(verticalScale); - this._bottom.setPosition(cc.p(leftWidth, 0)); - this._bottom.setScaleX(horizontalScale); - this._top.setPosition(cc.p(leftWidth, bottomHeight + rescaledHeight)); - this._top.setScaleX(horizontalScale); + locLeft.setPosition(0, bottomHeight); + locLeft.setScaleY(verticalScale); + locRight.setPosition(leftWidth + rescaledWidth, bottomHeight); + locRight.setScaleY(verticalScale); + locBottom.setPosition(leftWidth, 0); + locBottom.setScaleX(horizontalScale); + locTop.setPosition(leftWidth, bottomHeight + rescaledHeight); + locTop.setScaleX(horizontalScale); // Position centre - this._centre.setPosition(cc.p(leftWidth, bottomHeight)); + locCenter.setPosition(leftWidth, bottomHeight); }, ctor: function () { - this._super(); + cc.Node.prototype.ctor.call(this); this._spriteRect = cc.RectZero(); this._capInsetsInternal = cc.RectZero(); @@ -196,9 +204,9 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{ this._opacity = opacity; var scaleChildren = this._scale9Image.getChildren(); for (var i = 0; i < scaleChildren.length; i++) { - if (scaleChildren[i] && scaleChildren[i].RGBAProtocol) { - scaleChildren[i].setOpacity(this._opacity); - } + var selChild = scaleChildren[i]; + if (selChild && selChild.RGBAProtocol) + selChild.setOpacity(opacity); } }, @@ -210,9 +218,9 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{ this._color = color; var scaleChildren = this._scale9Image.getChildren(); for (var i = 0; i < scaleChildren.length; i++) { - if (scaleChildren[i] && scaleChildren[i].RGBAProtocol) { - scaleChildren[i].setColor(this._color); - } + var selChild = scaleChildren[i]; + if (selChild && selChild.RGBAProtocol) + selChild.setColor(color); } }, @@ -443,10 +451,11 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{ if (this._scale9Image != batchNode) this._scale9Image = batchNode; - this._scale9Image.removeAllChildren(true); + var locScale9Image = this._scale9Image; + locScale9Image.removeAllChildren(true); this._capInsets = capInsets; - var selTexture = this._scale9Image.getTexture(); + var selTexture = locScale9Image.getTexture(); var rectZero = cc.RectZero(); // If there is no given rect @@ -464,24 +473,36 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{ // Set the given rect's size as original size this._spriteRect = rect; var rectSize = rect.size; - this._originalSize = new cc.Size(rectSize.width, rectSize.height); - this._preferredSize = new cc.Size(rectSize.width, rectSize.height); - this._capInsetsInternal = capInsets || cc.RectZero(); + this._originalSize.width = rectSize.width; + this._originalSize.height = rectSize.height; + this._preferredSize.width = rectSize.width; + this._preferredSize.height = rectSize.height; + + var locCapInsetsInternal = this._capInsetsInternal; + if(!capInsets){ + locCapInsetsInternal.x = capInsets.x; + locCapInsetsInternal.y = capInsets.y; + locCapInsetsInternal.width = capInsets.width; + locCapInsetsInternal.height = capInsets.height; + } var w = rectSize.width; var h = rectSize.height; // If there is no specified center region - if (cc.rectEqualToRect(this._capInsetsInternal, rectZero)) { + if (cc.rectEqualToRect(locCapInsetsInternal, rectZero)) { // CCLog("... cap insets not specified : using default cap insets ..."); - this._capInsetsInternal = cc.rect(w / 3, h / 3, w / 3, h / 3); + locCapInsetsInternal.x = w / 3; + locCapInsetsInternal.y = h / 3; + locCapInsetsInternal.width = w / 3; + locCapInsetsInternal.height = h / 3; } - var left_w = this._capInsetsInternal.x; - var center_w = this._capInsetsInternal.width; + var left_w = locCapInsetsInternal.x; + var center_w = locCapInsetsInternal.width; var right_w = w - (left_w + center_w); - var top_h = this._capInsetsInternal.y; - var center_h = this._capInsetsInternal.height; + var top_h = locCapInsetsInternal.y; + var center_h = locCapInsetsInternal.height; var bottom_h = h - (top_h + center_h); // calculate rects @@ -533,9 +554,9 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{ x += center_w; var rightbottombounds = cc.RectMake(x, y, right_w, bottom_h); + var t = cc.AffineTransformMakeIdentity(); if (!rotated) { // CCLog("!rotated"); - var t = cc.AffineTransformMakeIdentity(); t = cc.AffineTransformTranslate(t, rect.x, rect.y); centerbounds = cc.RectApplyAffineTransform(centerbounds, t); @@ -551,55 +572,52 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{ // Centre this._centre = new cc.Sprite(); this._centre.initWithTexture(selTexture, centerbounds); - this._scale9Image.addChild(this._centre, 0, cc.POSITIONS_CENTRE); + locScale9Image.addChild(this._centre, 0, cc.POSITIONS_CENTRE); // Top this._top = new cc.Sprite(); this._top.initWithTexture(selTexture, centertopbounds); - this._scale9Image.addChild(this._top, 1, cc.POSITIONS_TOP); + locScale9Image.addChild(this._top, 1, cc.POSITIONS_TOP); // Bottom this._bottom = new cc.Sprite(); this._bottom.initWithTexture(selTexture, centerbottombounds); - this._scale9Image.addChild(this._bottom, 1, cc.POSITIONS_BOTTOM); + locScale9Image.addChild(this._bottom, 1, cc.POSITIONS_BOTTOM); // Left this._left = new cc.Sprite(); this._left.initWithTexture(selTexture, leftcenterbounds); - this._scale9Image.addChild(this._left, 1, cc.POSITIONS_LEFT); + locScale9Image.addChild(this._left, 1, cc.POSITIONS_LEFT); // Right this._right = new cc.Sprite(); this._right.initWithTexture(selTexture, rightcenterbounds); - this._scale9Image.addChild(this._right, 1, cc.POSITIONS_RIGHT); + locScale9Image.addChild(this._right, 1, cc.POSITIONS_RIGHT); // Top left this._topLeft = new cc.Sprite(); this._topLeft.initWithTexture(selTexture, lefttopbounds); - this._scale9Image.addChild(this._topLeft, 2, cc.POSITIONS_TOPLEFT); + locScale9Image.addChild(this._topLeft, 2, cc.POSITIONS_TOPLEFT); // Top right this._topRight = new cc.Sprite(); this._topRight.initWithTexture(selTexture, righttopbounds); - this._scale9Image.addChild(this._topRight, 2, cc.POSITIONS_TOPRIGHT); + locScale9Image.addChild(this._topRight, 2, cc.POSITIONS_TOPRIGHT); // Bottom left this._bottomLeft = new cc.Sprite(); this._bottomLeft.initWithTexture(selTexture, leftbottombounds); - this._scale9Image.addChild(this._bottomLeft, 2, cc.POSITIONS_BOTTOMLEFT); + locScale9Image.addChild(this._bottomLeft, 2, cc.POSITIONS_BOTTOMLEFT); // Bottom right this._bottomRight = new cc.Sprite(); this._bottomRight.initWithTexture(selTexture, rightbottombounds); - this._scale9Image.addChild(this._bottomRight, 2, cc.POSITIONS_BOTTOMRIGHT); + locScale9Image.addChild(this._bottomRight, 2, cc.POSITIONS_BOTTOMRIGHT); } else { // set up transformation of coordinates // to handle the case where the sprite is stored rotated // in the spritesheet // CCLog("rotated"); - - var t = cc.AffineTransformMakeIdentity(); - var rotatedcenterbounds = centerbounds; var rotatedrightbottombounds = rightbottombounds; var rotatedleftbottombounds = leftbottombounds; @@ -623,64 +641,81 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{ centerbottombounds = cc.RectApplyAffineTransform(centerbottombounds, t); centertopbounds = cc.RectApplyAffineTransform(centertopbounds, t); - rotatedcenterbounds.origin = {x: centerbounds.x, y: centerbounds.y}; - rotatedrightbottombounds.origin = {x: rightbottombounds.x, y: rightbottombounds.y}; - rotatedleftbottombounds.origin = {x: leftbottombounds.x, y: leftbottombounds.y}; - rotatedrighttopbounds.origin = {x: righttopbounds.x, y: righttopbounds.y}; - rotatedlefttopbounds.origin = {x: lefttopbounds.x, y: lefttopbounds.y}; - rotatedrightcenterbounds.origin = {x: rightcenterbounds.x, y: rightcenterbounds.y}; - rotatedleftcenterbounds.origin = {x: leftcenterbounds.x, y: leftcenterbounds.y}; - rotatedcenterbottombounds.origin = {x: centerbottombounds.x, y: centerbottombounds.y}; - rotatedcentertopbounds.origin = {x: centertopbounds.x, y: centertopbounds.y}; + rotatedcenterbounds.x = centerbounds.x; + rotatedcenterbounds.y = centerbounds.y; + + rotatedrightbottombounds.x = rightbottombounds.x; + rotatedrightbottombounds.y = rightbottombounds.y; + + rotatedleftbottombounds.x = leftbottombounds.x; + rotatedleftbottombounds.y = leftbottombounds.y; + + rotatedrighttopbounds.x = righttopbounds.x; + rotatedrighttopbounds.y = righttopbounds.y; + + rotatedlefttopbounds.x = lefttopbounds.x; + rotatedlefttopbounds.y = lefttopbounds.y; + + rotatedrightcenterbounds.x = rightcenterbounds.x; + rotatedrightcenterbounds.y = rightcenterbounds.y; + + rotatedleftcenterbounds.x = leftcenterbounds.x; + rotatedleftcenterbounds.y = leftcenterbounds.y; + + rotatedcenterbottombounds.x = centerbottombounds.x; + rotatedcenterbottombounds.y = centerbottombounds.y; + + rotatedcentertopbounds.x = centertopbounds.x; + rotatedcentertopbounds.y = centertopbounds.y; // Centre this._centre = new cc.Sprite(); this._centre.initWithTexture(selTexture, rotatedcenterbounds, true); - this._scale9Image.addChild(this._centre, 0, cc.POSITIONS_CENTRE); + locScale9Image.addChild(this._centre, 0, cc.POSITIONS_CENTRE); // Top this._top = new cc.Sprite(); this._top.initWithTexture(selTexture, rotatedcentertopbounds, true); - this._scale9Image.addChild(this._top, 1, cc.POSITIONS_TOP); + locScale9Image.addChild(this._top, 1, cc.POSITIONS_TOP); // Bottom this._bottom = new cc.Sprite(); this._bottom.initWithTexture(selTexture, rotatedcenterbottombounds, true); - this._scale9Image.addChild(this._bottom, 1, cc.POSITIONS_BOTTOM); + locScale9Image.addChild(this._bottom, 1, cc.POSITIONS_BOTTOM); // Left this._left = new cc.Sprite(); this._left.initWithTexture(selTexture, rotatedleftcenterbounds, true); - this._scale9Image.addChild(this._left, 1, cc.POSITIONS_LEFT); + locScale9Image.addChild(this._left, 1, cc.POSITIONS_LEFT); // Right this._right = new cc.Sprite(); this._right.initWithTexture(selTexture, rotatedrightcenterbounds, true); - this._scale9Image.addChild(this._right, 1, cc.POSITIONS_RIGHT); + locScale9Image.addChild(this._right, 1, cc.POSITIONS_RIGHT); // Top left this._topLeft = new cc.Sprite(); this._topLeft.initWithTexture(selTexture, rotatedlefttopbounds, true); - this._scale9Image.addChild(this._topLeft, 2, cc.POSITIONS_TOPLEFT); + locScale9Image.addChild(this._topLeft, 2, cc.POSITIONS_TOPLEFT); // Top right this._topRight = new cc.Sprite(); this._topRight.initWithTexture(selTexture, rotatedrighttopbounds, true); - this._scale9Image.addChild(this._topRight, 2, cc.POSITIONS_TOPRIGHT); + locScale9Image.addChild(this._topRight, 2, cc.POSITIONS_TOPRIGHT); // Bottom left this._bottomLeft = new cc.Sprite(); this._bottomLeft.initWithTexture(selTexture, rotatedleftbottombounds, true); - this._scale9Image.addChild(this._bottomLeft, 2, cc.POSITIONS_BOTTOMLEFT); + locScale9Image.addChild(this._bottomLeft, 2, cc.POSITIONS_BOTTOMLEFT); // Bottom right this._bottomRight = new cc.Sprite(); this._bottomRight.initWithTexture(selTexture, rotatedrightbottombounds, true); - this._scale9Image.addChild(this._bottomRight, 2, cc.POSITIONS_BOTTOMRIGHT); + locScale9Image.addChild(this._bottomRight, 2, cc.POSITIONS_BOTTOMRIGHT); } this.setContentSize(rect.size); - this.addChild(this._scale9Image); + this.addChild(locScale9Image); if (this._spritesGenerated) { // Restore color and opacity diff --git a/extensions/GUI/CCScrollView/CCScrollView.js b/extensions/GUI/CCScrollView/CCScrollView.js index a384204931..670cffbadd 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.js +++ b/extensions/GUI/CCScrollView/CCScrollView.js @@ -79,14 +79,20 @@ cc.ScrollView = cc.Layer.extend({ _parentScissorRect:null, _scissorRestored:false, + // cache object + _tmpViewRect:null, + ctor:function () { - this._super(); + cc.Layer.prototype.ctor.call(this); + this._contentOffset = new cc.Point(0,0); this._maxInset = new cc.Point(0, 0); this._minInset = new cc.Point(0, 0); this._scrollDistance = new cc.Point(0, 0); this._touchPoint = new cc.Point(0, 0); this._touches = []; this._viewSize = new cc.Size(0, 0); + this._parentScissorRect = new cc.Rect(0,0,0,0); + this._tmpViewRect = new cc.Rect(0,0,0,0); }, init:function () { @@ -104,26 +110,27 @@ cc.ScrollView = cc.Layer.extend({ * @return {Boolean} */ initWithViewSize:function (size, container) { + var pZero = cc.p(0,0); if (cc.Layer.prototype.init.call(this)) { this._container = container; if (!this._container) { this._container = cc.Layer.create(); this._container.ignoreAnchorPointForPosition(false); - this._container.setAnchorPoint(cc.p(0.0, 0.0)); + this._container.setAnchorPoint(pZero); } this.setViewSize(size); this.setTouchEnabled(true); - this._touches = []; + this._touches.length = 0; this._delegate = null; this._bounceable = true; this._clippingToBounds = true; //this._container.setContentSize(CCSizeZero); this._direction = cc.SCROLLVIEW_DIRECTION_BOTH; - this._container.setPosition(cc.p(0.0, 0.0)); + this._container.setPosition(pZero); this._touchLength = 0.0; this.addChild(this._container); @@ -137,7 +144,7 @@ cc.ScrollView = cc.Layer.extend({ * Sets a new content offset. It ignores max/min offset. It just sets what's given. (just like UIKit's UIScrollView) * * @param {cc.Point} offset new offset - * @param {Number} animated If YES, the view scrolls to the new offset + * @param {Number} [animated=] If YES, the view scrolls to the new offset */ setContentOffset:function (offset, animated) { if (animated) { //animate scrolling @@ -152,8 +159,9 @@ cc.ScrollView = cc.Layer.extend({ } this._container.setPosition(offset); - if (this._delegate != null && this._delegate.scrollViewDidScroll) { - this._delegate.scrollViewDidScroll(this); + var locDelegate = this._delegate; + if (locDelegate != null && locDelegate.scrollViewDidScroll) { + locDelegate.scrollViewDidScroll(this); } } }, @@ -184,7 +192,8 @@ cc.ScrollView = cc.Layer.extend({ */ setZoomScale:function (scale, animated) { if (arguments.length === 1) { - if (this._container.getScale() != scale) { + var locContainer = this._container; + if (locContainer.getScale() != scale) { var oldCenter, newCenter; var center; @@ -194,15 +203,15 @@ cc.ScrollView = cc.Layer.extend({ } else center = this._touchPoint; - oldCenter = this._container.convertToNodeSpace(center); - this._container.setScale(Math.max(this._minScale, Math.min(this._maxScale, scale))); - newCenter = this._container.convertToWorldSpace(oldCenter); + oldCenter = locContainer.convertToNodeSpace(center); + locContainer.setScale(Math.max(this._minScale, Math.min(this._maxScale, scale))); + newCenter = locContainer.convertToWorldSpace(oldCenter); var offset = cc.pSub(center, newCenter); if (this._delegate != null) { this._delegate.scrollViewDidZoom(this); } - this.setContentOffset(cc.pAdd(this._container.getPosition(), offset)); + this.setContentOffset(cc.pAdd(locContainer.getPosition(), offset)); } } else if (arguments.length === 2) { if (animated) @@ -237,8 +246,9 @@ cc.ScrollView = cc.Layer.extend({ * Returns the current container's minimum offset. You may want this while you animate scrolling by yourself */ minContainerOffset:function () { - return cc.p(this._viewSize.width - this._container.getContentSize().width * this._container.getScaleX(), - this._viewSize.height - this._container.getContentSize().height * this._container.getScaleY()); + var locContentSize = this._container.getContentSize(); + return cc.p(this._viewSize.width - locContentSize.width * this._container.getScaleX(), + this._viewSize.height - locContentSize.height * this._container.getScaleY()); }, /** @@ -260,7 +270,7 @@ cc.ScrollView = cc.Layer.extend({ var viewRect = cc.RectMake(-offset.x / scale, -offset.y / scale, size.width / scale, size.height / scale); - return cc.CCRectIntersectsRect(viewRect, node.getBoundingBox()); + return cc.rectIntersectsRect(viewRect, node.getBoundingBox()); }, /** @@ -362,25 +372,28 @@ cc.ScrollView = cc.Layer.extend({ var frame = this._getViewRect(); //dispatcher does not know about clipping. reject touches outside visible bounds. - var locPoint = this._container.convertToWorldSpace(this._container.convertTouchToNodeSpace(touch)); - if (this._touches.length > 2 || this._touchMoved || !cc.rectContainsPoint(frame, locPoint)) + var locContainer = this._container; + var locPoint = locContainer.convertToWorldSpace(locContainer.convertTouchToNodeSpace(touch)); + var locTouches = this._touches; + if (locTouches.length > 2 || this._touchMoved || !cc.rectContainsPoint(frame, locPoint)) return false; //if (!cc.ArrayContainsObject(this._touches, touch)) { - this._touches.push(touch); + locTouches.push(touch); //} - if (this._touches.length == 1) { // scrolling + if (locTouches.length == 1) { // scrolling this._touchPoint = this.convertTouchToNodeSpace(touch); this._touchMoved = false; this._dragging = true; //dragging started - this._scrollDistance = cc.p(0.0, 0.0); + this._scrollDistance.x = 0; + this._scrollDistance.y = 0; this._touchLength = 0.0; - } else if (this._touches.length == 2) { - this._touchPoint = cc.pMidpoint(this.convertTouchToNodeSpace(this._touches[0]), - this.convertTouchToNodeSpace(this._touches[1])); - this._touchLength = cc.pDistance(this._container.convertTouchToNodeSpace(this._touches[0]), - this._container.convertTouchToNodeSpace(this._touches[1])); + } else if (locTouches.length == 2) { + this._touchPoint = cc.pMidpoint(this.convertTouchToNodeSpace(locTouches[0]), + this.convertTouchToNodeSpace(locTouches[1])); + this._touchLength = cc.pDistance(locContainer.convertTouchToNodeSpace(locTouches[0]), + locContainer.convertTouchToNodeSpace(locTouches[1])); this._dragging = false; } return true; @@ -407,7 +420,7 @@ cc.ScrollView = cc.Layer.extend({ else if (this._direction === cc.SCROLLVIEW_DIRECTION_HORIZONTAL) dis = moveDistance.x; else - dis = Math.sqrt(moveDistance.x * moveDistance.x + moveDistance.y*moveDistance.y); + dis = Math.sqrt(moveDistance.x * moveDistance.x + moveDistance.y * moveDistance.y); if (!this._touchMoved && Math.abs(cc.convertDistanceFromPointToInch(dis)) < MOVE_INCH ){ //CCLOG("Invalid movement, distance = [%f, %f], disInch = %f", moveDistance.x, moveDistance.y); @@ -425,22 +438,23 @@ cc.ScrollView = cc.Layer.extend({ if (cc.rectContainsPoint(frame, this.convertToWorldSpace(newPoint))) { switch (this._direction) { case cc.SCROLLVIEW_DIRECTION_VERTICAL: - moveDistance = cc.p(0.0, moveDistance.y); + moveDistance.x = 0.0; break; case cc.SCROLLVIEW_DIRECTION_HORIZONTAL: - moveDistance = cc.p(moveDistance.x, 0.0); + moveDistance.y = 0.0; break; default: break; } - var newX = this._container.getPosition().x + moveDistance.x; - var newY = this._container.getPosition().y + moveDistance.y; + var locPosition = this._container.getPosition(); + var newX = locPosition.x + moveDistance.x; + var newY = locPosition.y + moveDistance.y; this._scrollDistance = moveDistance; this.setContentOffset(cc.p(newX, newY)); } - } else if (this._touches.length == 2 && !this._dragging) { + } else if (this._touches.length === 2 && !this._dragging) { var len = cc.pDistance(this._container.convertTouchToNodeSpace(this._touches[0]), this._container.convertTouchToNodeSpace(this._touches[1])); this.setZoomScale(this.getZoomScale() * len / this._touchLength); @@ -481,12 +495,13 @@ cc.ScrollView = cc.Layer.extend({ updateInset:function () { if (this.getContainer() != null) { - this._maxInset = this.maxContainerOffset(); - this._maxInset = cc.p(this._maxInset.x + this._viewSize.width * INSET_RATIO, - this._maxInset.y + this._viewSize.height * INSET_RATIO); - this._minInset = this.minContainerOffset(); - this._minInset = cc.p(this._minInset.x - this._viewSize.width * INSET_RATIO, - this._minInset.y - this._viewSize.height * INSET_RATIO); + var locViewSize = this._viewSize; + var tempOffset = this.maxContainerOffset(); + this._maxInset.x = tempOffset.x + locViewSize.width * INSET_RATIO; + this._maxInset.y = tempOffset.y + locViewSize.height * INSET_RATIO; + tempOffset = this.minContainerOffset(); + this._minInset.x = tempOffset.x - locViewSize.width * INSET_RATIO; + this._minInset.y = tempOffset.y - locViewSize.height * INSET_RATIO; } }, @@ -507,18 +522,20 @@ cc.ScrollView = cc.Layer.extend({ return; var context = ctx || cc.renderContext; - var i; + var i, locChildren = this._children, selChild, childrenLen; if (cc.renderContextType === cc.CANVAS) { context.save(); this.transform(context); this._beforeDraw(context); - if (this._children && this._children.length > 0) { + if (locChildren && locChildren.length > 0) { + childrenLen = locChildren.length; this.sortAllChildren(); // draw children zOrder < 0 - for (i = 0; i < this._children.length; i++) { - if (this._children[i] && this._children[i]._zOrder < 0) - this._children[i].visit(context); + for (i = 0; i < childrenLen; i++) { + selChild = locChildren[i]; + if (selChild && selChild._zOrder < 0) + selChild.visit(context); else break; } @@ -526,12 +543,8 @@ cc.ScrollView = cc.Layer.extend({ this.draw(context); // self draw // draw children zOrder >= 0 - if (this._children) { - for (; i < this._children.length; i++) { - if (this._children[i] && this._children[i]._zOrder >= 0) - this._children[i].visit(context); - } - } + for (; i < childrenLen; i++) + locChildren[i].visit(context); } else this.draw(context); // self draw @@ -540,19 +553,21 @@ cc.ScrollView = cc.Layer.extend({ context.restore(); } else { cc.kmGLPushMatrix(); - if (this._grid && this._grid.isActive()) { - this._grid.beforeDraw(); + var locGrid = this._grid; + if (locGrid && locGrid.isActive()) { + locGrid.beforeDraw(); this.transformAncestors(); } this.transform(context); this._beforeDraw(context); - if (this._children) { - i = 0; + if (locChildren && locChildren.length > 0) { + childrenLen = locChildren.length; // draw children zOrder < 0 - for (; i < this._children.length; i++) { - if (this._children[i].getZOrder() < 0) - this._children[i].visit(); + for (i = 0; i < childrenLen; i++) { + selChild = locChildren[i]; + if (selChild && selChild._zOrder < 0) + selChild.visit(); else break; } @@ -561,14 +576,14 @@ cc.ScrollView = cc.Layer.extend({ this.draw(context); // draw children zOrder >= 0 - for (; i < this._children.length; i++) - this._children[i].visit(); + for (; i < childrenLen; i++) + locChildren[i].visit(); } else this.draw(context); this._afterDraw(context); - if (this._grid && this._grid.isActive()) - this._grid.afterDraw(this); + if (locGrid && locGrid.isActive()) + locGrid.afterDraw(this); cc.kmGLPopMatrix(); } @@ -617,16 +632,17 @@ cc.ScrollView = cc.Layer.extend({ _relocateContainer:function (animated) { var min = this.minContainerOffset(); var max = this.maxContainerOffset(); + var locDirection = this._direction; var oldPoint = this._container.getPosition(); var newX = oldPoint.x; var newY = oldPoint.y; - if (this._direction == cc.SCROLLVIEW_DIRECTION_BOTH || this._direction == cc.SCROLLVIEW_DIRECTION_HORIZONTAL) { + if (locDirection === cc.SCROLLVIEW_DIRECTION_BOTH || locDirection === cc.SCROLLVIEW_DIRECTION_HORIZONTAL) { newX = Math.max(newX, min.x); newX = Math.min(newX, max.x); } - if (this._direction == cc.SCROLLVIEW_DIRECTION_BOTH || this._direction == cc.SCROLLVIEW_DIRECTION_VERTICAL) { + if (locDirection == cc.SCROLLVIEW_DIRECTION_BOTH || locDirection == cc.SCROLLVIEW_DIRECTION_VERTICAL) { newY = Math.min(newY, max.y); newY = Math.max(newY, min.y); } @@ -648,7 +664,9 @@ cc.ScrollView = cc.Layer.extend({ } var maxInset, minInset; - this._container.setPosition(cc.pAdd(this._container.getPosition(), this._scrollDistance)); + var oldPosition = this._container.getPosition(); + var locScrollDistance = this._scrollDistance; + this._container.setPosition(oldPosition.x + locScrollDistance.x , oldPosition.y + locScrollDistance.y); if (this._bounceable) { maxInset = this._maxInset; minInset = this._minInset; @@ -662,15 +680,21 @@ cc.ScrollView = cc.Layer.extend({ newX = Math.max(newX, minInset.x); var newY = Math.min(this._container.getPosition().y, maxInset.y); newY = Math.max(newY, minInset.y);*/ - var newX = this._container.getPosition().x; - var newY = this._container.getPosition().y; + oldPosition = this._container.getPosition(); + var newX = oldPosition.x; + var newY = oldPosition.y; + + //this._scrollDistance = cc.pSub(this._scrollDistance, cc.p(newX - this._container.getPosition().x, newY - this._container.getPosition().y)); + //= this._scrollDistance = cc.pSub(this._scrollDistance, cc.p(0, 0)); = do nothing + + //this._scrollDistance = cc.pMult(this._scrollDistance, SCROLL_DEACCEL_RATE); + locScrollDistance.x = locScrollDistance.x * SCROLL_DEACCEL_RATE; + locScrollDistance.y = locScrollDistance.y * SCROLL_DEACCEL_RATE; - this._scrollDistance = cc.pSub(this._scrollDistance, cc.p(newX - this._container.getPosition().x, newY - this._container.getPosition().y)); - this._scrollDistance = cc.pMult(this._scrollDistance, SCROLL_DEACCEL_RATE); this.setContentOffset(cc.p(newX, newY)); - if ((Math.abs(this._scrollDistance.x) <= SCROLL_DEACCEL_DIST && - Math.abs(this._scrollDistance.y) <= SCROLL_DEACCEL_DIST) || + if ((Math.abs(locScrollDistance.x) <= SCROLL_DEACCEL_DIST && + Math.abs(locScrollDistance.y) <= SCROLL_DEACCEL_DIST) || newY > maxInset.y || newY < minInset.y || newX > maxInset.x || newX < minInset.x || newX == maxInset.x || newX == minInset.x || @@ -726,9 +750,10 @@ cc.ScrollView = cc.Layer.extend({ ctx.clip(); ctx.closePath(); } else { - if(cc.EGLView.getInstance().isScissorEnabled()){ + var EGLViewer = cc.EGLView.getInstance(); + if(EGLViewer.isScissorEnabled()){ this._scissorRestored = true; - this._parentScissorRect = cc.EGLView.getInstance().getScissorRect(); + this._parentScissorRect = EGLViewer.getScissorRect(); //set the intersection of m_tParentScissorRect and frame as the new scissor rect if (cc.rectIntersection(frame, this._parentScissorRect)) { var locPSRect = this._parentScissorRect; @@ -736,12 +761,12 @@ cc.ScrollView = cc.Layer.extend({ var y = Math.max(frame.y, locPSRect.y); var xx = Math.min(frame.x + frame.width, locPSRect.x + locPSRect.width); var yy = Math.min(frame.y + frame.height, locPSRect.y + locPSRect.height); - cc.EGLView.getInstance().setScissorInPoints(x, y, xx - x, yy - y); + EGLViewer.setScissorInPoints(x, y, xx - x, yy - y); } }else{ ctx.enable(ctx.SCISSOR_TEST); //clip - cc.EGLView.getInstance().setScissorInPoints(frame.x, frame.y, frame.width, frame.height); + EGLViewer.setScissorInPoints(frame.x, frame.y, frame.width, frame.height); } } @@ -770,6 +795,7 @@ cc.ScrollView = cc.Layer.extend({ _getViewRect:function(){ var screenPos = this.convertToWorldSpace(cc.PointZero()); + var locViewSize = this._viewSize; var scaleX = this.getScaleX(); var scaleY = this.getScaleY(); @@ -782,15 +808,21 @@ cc.ScrollView = cc.Layer.extend({ // Support negative scaling. Not doing so causes intersectsRect calls // (eg: to check if the touch was within the bounds) to return false. // Note, CCNode::getScale will assert if X and Y scales are different. - if(scaleX<0) { - screenPos.x += this._viewSize.width*scaleX; + if (scaleX < 0) { + screenPos.x += locViewSize.width * scaleX; scaleX = -scaleX; } - if(scaleY<0) { - screenPos.y += this._viewSize.height*scaleY; + if (scaleY < 0) { + screenPos.y += locViewSize.height * scaleY; scaleY = -scaleY; } - return cc.RectMake(screenPos.x, screenPos.y, this._viewSize.width * scaleX, this._viewSize.height * scaleY); + + var locViewRect = this._tmpViewRect; + locViewRect.x = screenPos.x; + locViewRect.y = screenPos.y; + locViewRect.width = locViewSize.width * scaleX; + locViewRect.height = locViewSize.height * scaleY; + return locViewRect; } }); diff --git a/extensions/GUI/CCScrollView/CCTableView.js b/extensions/GUI/CCScrollView/CCTableView.js index a015bc0ee1..2948ee6e89 100644 --- a/extensions/GUI/CCScrollView/CCTableView.js +++ b/extensions/GUI/CCScrollView/CCTableView.js @@ -175,9 +175,9 @@ cc.TableView = cc.ScrollView.extend({ var offset = this.__offsetFromIndex(index); var cellSize = this._dataSource.cellSizeForTable(this); - if (this._vOrdering == cc.TABLEVIEW_FILL_TOPDOWN) { + if (this._vOrdering === cc.TABLEVIEW_FILL_TOPDOWN) offset.y = this.getContainer().getContentSize().height - offset.y - cellSize.height; - } + return offset; }, @@ -262,11 +262,7 @@ cc.TableView = cc.ScrollView.extend({ return this._tableViewDelegate; }, - setDelegate:function (delegate, isDirectCall) { - if (isDirectCall != null && isDirectCall == true) { - this._super(delegate); - return; - } + setDelegate:function (delegate) { this._tableViewDelegate = delegate; }, @@ -286,7 +282,7 @@ cc.TableView = cc.ScrollView.extend({ }, initWithViewSize:function (size, container) { - if (this._super(size, container)) { + if (cc.ScrollView.prototype.initWithViewSize.call(this, size, container)) { this._cellsUsed = new cc.ArrayForObjectSorting(); this._cellsFreed = new cc.ArrayForObjectSorting(); this._indices = new cc.Set(); @@ -294,7 +290,7 @@ cc.TableView = cc.ScrollView.extend({ this._vOrdering = cc.TABLEVIEW_FILL_BOTTOMUP; this.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL); - this.setDelegate(this, true); + cc.ScrollView.prototype.setDelegate.call(this, this); return true; } return false; @@ -311,9 +307,8 @@ cc.TableView = cc.ScrollView.extend({ } var cell = this._cellWithIndex(idx); - if (cell) { + if (cell) this._moveCellOutOfSight(cell); - } cell = this._dataSource.tableCellAtIndex(this, idx); this._setIndexForCell(idx, cell); @@ -330,13 +325,12 @@ cc.TableView = cc.ScrollView.extend({ return; } - var newIdx; - - var cell = this._cellsUsed.objectWithObjectID(idx); + var newIdx, locCellsUsed = this._cellsUsed; + var cell = locCellsUsed.objectWithObjectID(idx); if (cell) { - newIdx = this._cellsUsed.indexOfSortedObject(cell); - for (var i = newIdx; i < this._cellsUsed.count(); i++) { - cell = this._cellsUsed.objectAtIndex(i); + newIdx = locCellsUsed.indexOfSortedObject(cell); + for (var i = newIdx; i < locCellsUsed.count(); i++) { + cell = locCellsUsed.objectAtIndex(i); this._setIndexForCell(cell.getIdx() + 1, cell); } } @@ -360,11 +354,11 @@ cc.TableView = cc.ScrollView.extend({ } var cell = this._cellWithIndex(idx); - if (!cell) { + if (!cell) return; - } - var newIdx = this._cellsUsed.indexOfSortedObject(cell); + var locCellsUsed = this._cellsUsed; + var newIdx = locCellsUsed.indexOfSortedObject(cell); //remove first this._moveCellOutOfSight(cell); @@ -372,8 +366,8 @@ cc.TableView = cc.ScrollView.extend({ this._indices.removeObject(idx); //cc.ArrayRemoveObjectAtIndex(this._indices,idx); - for (var i = this._cellsUsed.count() - 1; i > newIdx; i--) { - cell = this._cellsUsed.objectAtIndex(i); + for (var i = locCellsUsed.count() - 1; i > newIdx; i--) { + cell = locCellsUsed.objectAtIndex(i); this._setIndexForCell(cell.getIdx() - 1, cell); } }, @@ -382,8 +376,9 @@ cc.TableView = cc.ScrollView.extend({ * reloads data from data source. the view will be refreshed. */ reloadData:function () { - for (var i = 0; i < this._cellsUsed.count(); i++) { - var cell = this._cellsUsed.objectAtIndex(i); + var locCellsUsed = this._cellsUsed; + for (var i = 0; i < locCellsUsed.count(); i++) { + var cell = locCellsUsed.objectAtIndex(i); this._cellsFreed.addObject(cell); cell.reset(); if (cell.getParent() == this.getContainer()) { @@ -426,7 +421,8 @@ cc.TableView = cc.ScrollView.extend({ }, scrollViewDidScroll:function (view) { - var countOfItems = this._dataSource.numberOfCellsInTableView(this); + var locDataSource = this._dataSource; + var countOfItems = locDataSource.numberOfCellsInTableView(this); if (0 === countOfItems) return; @@ -435,32 +431,32 @@ cc.TableView = cc.ScrollView.extend({ var idx = 0; var offset = cc.pMult(this.getContentOffset(), -1); - var maxIdx = Math.max(this._dataSource.numberOfCellsInTableView(this) - 1, 0); - - var cellSize = this._dataSource.cellSizeForTable(this); + var maxIdx = Math.max(locDataSource.numberOfCellsInTableView(this) - 1, 0); - if (this._vOrdering == cc.TABLEVIEW_FILL_TOPDOWN) { - offset.y = offset.y + this._viewSize.height / this.getContainer().getScaleY() - cellSize.height; + var cellSize = locDataSource.cellSizeForTable(this); + var locViewSize = this._viewSize, locContainer = this.getContainer(); + if (this._vOrdering === cc.TABLEVIEW_FILL_TOPDOWN) { + offset.y = offset.y + locViewSize.height / locContainer.getScaleY() - cellSize.height; } var startIdx = 0 | this._indexFromOffset(offset); - if (this._vOrdering == cc.TABLEVIEW_FILL_TOPDOWN) { - offset.y -= this._viewSize.height / this.getContainer().getScaleY(); + if (this._vOrdering === cc.TABLEVIEW_FILL_TOPDOWN) { + offset.y -= locViewSize.height / locContainer.getScaleY(); } else { - offset.y += this._viewSize.height / this.getContainer().getScaleY(); + offset.y += locViewSize.height / locContainer.getScaleY(); } - offset.x += this._viewSize.width / this.getContainer().getScaleX(); + offset.x += locViewSize.width / locContainer.getScaleX(); var endIdx = 0 | this._indexFromOffset(offset); - var cell; - if (this._cellsUsed.count() > 0) { - cell = this._cellsUsed.objectAtIndex(0); + var cell, locCellsUsed = this._cellsUsed; + if (locCellsUsed.count() > 0) { + cell = locCellsUsed.objectAtIndex(0); idx = cell.getIdx(); while (idx < startIdx) { this._moveCellOutOfSight(cell); - if (this._cellsUsed.count() > 0) { - cell = this._cellsUsed.objectAtIndex(0); + if (locCellsUsed.count() > 0) { + cell = locCellsUsed.objectAtIndex(0); idx = cell.getIdx(); } else { break; @@ -468,13 +464,13 @@ cc.TableView = cc.ScrollView.extend({ } } - if (this._cellsUsed.count() > 0) { - cell = this._cellsUsed.lastObject(); + if (locCellsUsed.count() > 0) { + cell = locCellsUsed.lastObject(); idx = cell.getIdx(); while (idx <= maxIdx && idx > endIdx) { this._moveCellOutOfSight(cell); - if (this._cellsUsed.count() > 0) { - cell = this._cellsUsed.lastObject(); + if (locCellsUsed.count() > 0) { + cell = locCellsUsed.lastObject(); idx = cell.getIdx(); } else { break; @@ -483,9 +479,8 @@ cc.TableView = cc.ScrollView.extend({ } for (var i = startIdx; i <= endIdx; i++) { - if (this._indices.containsObject(i)) { + if (this._indices.containsObject(i)) continue; - } this.updateCellAtIndex(i); } }, @@ -494,12 +489,12 @@ cc.TableView = cc.ScrollView.extend({ }, onTouchEnded:function (touch, event) { - if (!this.isVisible()) { + if (!this.isVisible()) return; - } + if (this._touches.length == 1 && !this.isTouchMoved()) { var point = this.getContainer().convertTouchToNodeSpace(touch); - if (this._vOrdering == cc.TABLEVIEW_FILL_TOPDOWN) { + if (this._vOrdering === cc.TABLEVIEW_FILL_TOPDOWN) { var cellSize = this._dataSource.cellSizeForTable(this); point.y -= cellSize.height; } @@ -510,17 +505,17 @@ cc.TableView = cc.ScrollView.extend({ this._tableViewDelegate.tableCellTouched(this, cell); } } - this._super(touch, event); + cc.ScrollView.prototype.onTouchEnded.call(this, touch, event); } }); /** * An initialized table view object * - * @param dataSource data source; - * @param size view size - * @param container parent object for cells - * @return table view + * @param {cc.TableViewDataSource} dataSource data source; + * @param {cc.Size} size view size + * @param {cc.Node} container parent object for cells + * @return {cc.TableView} table view */ cc.TableView.create = function (dataSource, size, container) { var table = new cc.TableView();