From e74bc01add93b679e76c01b05b1f4f97f7feb18d Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 14 Mar 2016 17:52:26 +0800 Subject: [PATCH 01/20] refactoring label, fix blink issue make it works remove unused code --- .../base-nodes/CCSGNodeCanvasRenderCmd.js | 19 ++-- cocos2d/core/label/CCSGLabel.js | 12 +-- .../core/label/CCSGLabelCanvasRenderCmd.js | 93 ++++++++++++------- cocos2d/core/label/CCSGLabelWebGLRenderCmd.js | 2 - 4 files changed, 76 insertions(+), 50 deletions(-) diff --git a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js index b0fc26c2b8..eb3a6de44d 100644 --- a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js +++ b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js @@ -36,15 +36,16 @@ cc.CustomRenderCmd = function (target, func) { }; _ccsg.Node._dirtyFlags = { - transformDirty: 1 << 0, - visibleDirty: 1 << 1, - colorDirty: 1 << 2, - opacityDirty: 1 << 3, - cacheDirty: 1 << 4, - orderDirty: 1 << 5, - textDirty: 1 << 6, - gradientDirty:1 << 7, - COUNT: 8 + transformDirty: 1 << 0, + visibleDirty: 1 << 1, + colorDirty: 1 << 2, + opacityDirty: 1 << 3, + cacheDirty: 1 << 4, + orderDirty: 1 << 5, + textDirty: 1 << 6, + gradientDirty: 1 << 7, + labelDirty: 1 << 8, + COUNT: 9 }; cc.js.get(_ccsg.Node._dirtyFlags, 'all', function () { var count = _ccsg.Node._dirtyFlags.COUNT; diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index 98966095a4..24d8871711 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -115,7 +115,6 @@ _ccsg.Label = _ccsg.Node.extend({ _blendFunc: null, _isUseSystemFont: true, - _labelSkinDirty: true, _labelType: 0, //0 is ttf, 1 is bmfont. _fontHandle: "", //a ttf font name or a bmfont file path. _lineSpacing: 0, @@ -436,16 +435,16 @@ _ccsg.Label = _ccsg.Node.extend({ _notifyLabelSkinDirty: function() { if (CC_EDITOR) { this._updateLabel(); - this._labelSkinDirty = false; } else { - this._labelSkinDirty = true; + this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.labelDirty); } }, _createRenderCmd: function() { - if (cc._renderType === cc.game.RENDER_TYPE_WEBGL) + if (cc._renderType === cc.game.RENDER_TYPE_WEBGL) { return new _ccsg.Label.WebGLRenderCmd(this); - else + } else { return new _ccsg.Label.CanvasRenderCmd(this); + } }, getContentSize: function() { @@ -916,9 +915,6 @@ cc.BMFontHelper = { this._computeHorizontalKerningForText(this._string); updateFinished = this._alignText(); } - if (updateFinished) { - this._labelSkinDirty = false; - } }, diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index ddffe659d1..b6d617b5c9 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -31,6 +31,62 @@ var proto = _ccsg.Label.TTFLabelBaker.prototype = Object.create(Object.prototype); + + proto.updateStatus = function () { + var flags = _ccsg.Node._dirtyFlags, locFlag = this._dirtyFlag; + var colorDirty = locFlag & flags.colorDirty, + opacityDirty = locFlag & flags.opacityDirty; + + if (colorDirty) + this._updateDisplayColor(); + if (opacityDirty) + this._updateDisplayOpacity(); + + if(colorDirty || opacityDirty){ + this._updateColor(); + }else if(locFlag & flags.labelDirty) { + this._rebuildLabelSkin(); + } + + if (this._dirtyFlag & flags.transformDirty){ + this.transform(this.getParentRenderCmd(), true); + this._dirtyFlag = this._dirtyFlag & _ccsg.Node._dirtyFlags.transformDirty ^ this._dirtyFlag; + } + }; + + proto._syncStatus = function (parentCmd) { + var flags = _ccsg.Node._dirtyFlags, locFlag = this._dirtyFlag; + var parentNode = parentCmd ? parentCmd._node : null; + + if(parentNode && parentNode._cascadeColorEnabled && (parentCmd._dirtyFlag & flags.colorDirty)) + locFlag |= flags.colorDirty; + + if(parentNode && parentNode._cascadeOpacityEnabled && (parentCmd._dirtyFlag & flags.opacityDirty)) + locFlag |= flags.opacityDirty; + + if(parentCmd && (parentCmd._dirtyFlag & flags.transformDirty)) + locFlag |= flags.transformDirty; + + var colorDirty = locFlag & flags.colorDirty, + opacityDirty = locFlag & flags.opacityDirty; + + this._dirtyFlag = locFlag; + + if (colorDirty) + this._syncDisplayColor(); + if (opacityDirty) + this._syncDisplayOpacity(); + + if(colorDirty || opacityDirty){ + this._updateColor(); + } else if(locFlag & flags.labelDirty) { + this._rebuildLabelSkin(); + } + + if (locFlag & flags.transformDirty) //update the transform + this.transform(parentCmd); + }; + proto._getLineHeight = function () { var nodeSpacingY = this._node.getLineHeight(); if (nodeSpacingY === 0) { @@ -162,26 +218,11 @@ return wrappedWords; }; - proto._updateDisplayOpacity = function (parentOpacity) { - _ccsg.Node.RenderCmd.prototype._updateDisplayOpacity.call(this, parentOpacity); - var node = this._node; - if (node._labelType === _ccsg.Label.Type.TTF || node._labelType === _ccsg.Label.Type.SystemFont) { - //specify opacity to quad - var color = cc.color(255, 255, 255, this._displayedOpacity); - var quad = this._quad; - quad._bl.colors = color; - quad._br.colors = color; - quad._tl.colors = color; - quad._tr.colors = color; - this._quadDirty = true; - } - }; - proto._updateDisplayColor = function (parentColor) { _ccsg.Node.RenderCmd.prototype._updateDisplayColor.call(this, parentColor); var node = this._node; if (node._labelType === _ccsg.Label.Type.TTF || node._labelType === _ccsg.Label.Type.SystemFont) { - node._labelSkinDirty = true; + this._rebuildLabelSkin(); } }; @@ -189,7 +230,7 @@ _ccsg.Node.RenderCmd.prototype._syncDisplayColor.call(this, parentColor); var node = this._node; if (node._labelType === _ccsg.Label.Type.TTF || node._labelType === _ccsg.Label.Type.SystemFont) { - node._labelSkinDirty = true; + this._rebuildLabelSkin(); } }; @@ -397,18 +438,11 @@ }; proto._rebuildLabelSkin = function () { + this._dirtyFlag = this._dirtyFlag & _ccsg.Node._dirtyFlags.labelDirty ^ this._dirtyFlag; var node = this._node; - if (node._labelSkinDirty) { - if (node._labelType === _ccsg.Label.Type.TTF || - node._labelType === _ccsg.Label.Type.SystemFont) { - this._bakeLabel(); - this._prepareQuad(); - } - else { - node._updateContent(); - } - this._node._labelSkinDirty = false; - } + this._realRenderingSize = _ccsg.Node.prototype.getContentSize.call(node); + + node._updateLabel(); }; })(); @@ -435,10 +469,7 @@ proto.constructor = _ccsg.Label.CanvasRenderCmd; proto.rendering = function (ctx, scaleX, scaleY) { - this._rebuildLabelSkin(); - var node = this._node; - this._realRenderingSize = _ccsg.Node.prototype.getContentSize.call(node); if (node._labelType === _ccsg.Label.Type.TTF || node._labelType === _ccsg.Label.Type.SystemFont) { diff --git a/cocos2d/core/label/CCSGLabelWebGLRenderCmd.js b/cocos2d/core/label/CCSGLabelWebGLRenderCmd.js index 55ddb784b7..1541f8f9ce 100644 --- a/cocos2d/core/label/CCSGLabelWebGLRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelWebGLRenderCmd.js @@ -59,9 +59,7 @@ proto.rendering = function (ctx) { var node = this._node; - this._rebuildLabelSkin(); - this._realRenderingSize = _ccsg.Node.prototype.getContentSize.call(node); if(node._labelType === _ccsg.Label.Type.TTF || node._labelType === _ccsg.Label.Type.SystemFont){ From f700ddf45127db78525266f7df763083864941a2 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 15 Mar 2016 21:46:55 +0800 Subject: [PATCH 02/20] Avoid JSB issue with new memory model (double remove listener) --- cocos2d/core/CCNode.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index daf642ddbe..18b1de4e4c 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1045,7 +1045,7 @@ var Node = cc.Class({ }, _checkTouchListeners: function () { - if (this._bubblingListeners && this._touchListener) { + if (!(this._objFlags & Destroying) && this._bubblingListeners && this._touchListener) { for (var i = 0; i < _touchEvents.length; ++i) { if (this._bubblingListeners.has(_touchEvents[i])) { return; @@ -1057,7 +1057,7 @@ var Node = cc.Class({ } }, _checkMouseListeners: function () { - if (this._bubblingListeners && this._mouseListener) { + if (!(this._objFlags & Destroying) && this._bubblingListeners && this._mouseListener) { for (var i = 0; i < _mouseEvents.length; ++i) { if (this._bubblingListeners.has(_mouseEvents[i])) { return; From 811aa0d1aa6488b9d680432a3448396cfdc4778b Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 16 Mar 2016 13:04:45 +0800 Subject: [PATCH 03/20] Fix multiply schedule failure issue in Component --- cocos2d/core/components/CCComponent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index 02dd1b56fd..f0524786d0 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -701,7 +701,7 @@ var Component = cc.Class({ repeat = isNaN(repeat) ? cc.macro.REPEAT_FOREVER : repeat; delay = delay || 0; - cc.director.getScheduler().schedule(callback, this, interval, repeat, delay, !this.enabledInHierarchy, this.__instanceId); + cc.director.getScheduler().scheduleCallbackForTarget(this, callback, interval, repeat, delay, !this.enabledInHierarchy); }, /** From bc9271da1e88a6048c08066d850a0fd1306f6001 Mon Sep 17 00:00:00 2001 From: Nan Wang Date: Wed, 16 Mar 2016 13:48:45 +0800 Subject: [PATCH 04/20] update engineVersion --- CCBoot.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/CCBoot.js b/CCBoot.js index 530df1574c..e09b238171 100644 --- a/CCBoot.js +++ b/CCBoot.js @@ -58,11 +58,7 @@ _p = null; * @name cc.ENGINE_VERSION */ var engineVersion; -if (CC_EDITOR) { - engineVersion = 'Cocos Creator v' + Editor.remote.versions.CocosCreator; -} else { - engineVersion = 'Cocos Creator'; -} +engineVersion = '0.8.0-beta.7'; window['CocosEngine'] = cc.ENGINE_VERSION = engineVersion; /** From c016ae2aec51035966c836851d82504c10f0f842 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 16 Mar 2016 14:33:40 +0800 Subject: [PATCH 05/20] fix bmfont getContentSize cause chilrenOrderDrity --- cocos2d/core/label/CCSGLabel.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index 24d8871711..a417992ea1 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -449,19 +449,19 @@ _ccsg.Label = _ccsg.Node.extend({ getContentSize: function() { if (!CC_EDITOR && !cc.sizeEqualToSize(this._contentSize, this._renderCmd._realRenderingSize)) { - this._updateLabel(); + this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.labelDirty); } return _ccsg.Node.prototype.getContentSize.call(this); }, _getWidth: function () { if (!CC_EDITOR && !cc.sizeEqualToSize(this._contentSize, this._renderCmd._realRenderingSize)) { - this._updateLabel(); + this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.labelDirty); } return _ccsg.Node.prototype._getWidth.call(this); }, _getHeight: function () { if (!CC_EDITOR && !cc.sizeEqualToSize(this._contentSize, this._renderCmd._realRenderingSize)) { - this._updateLabel(); + this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.labelDirty); } return _ccsg.Node.prototype._getHeight.call(this); }, From 9cd4dd5abafd98e4958d14bc44756ca36769a9f4 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 16 Mar 2016 14:44:29 +0800 Subject: [PATCH 06/20] reuse textDirty dirty flag --- cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js | 3 +-- cocos2d/core/label/CCSGLabel.js | 8 ++++---- cocos2d/core/label/CCSGLabelCanvasRenderCmd.js | 6 +++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js index 1d6e22e114..b7facce48e 100644 --- a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js +++ b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js @@ -44,8 +44,7 @@ _ccsg.Node._dirtyFlags = { orderDirty: 1 << 5, textDirty: 1 << 6, gradientDirty: 1 << 7, - labelDirty: 1 << 8, - COUNT: 9 + COUNT: 8 }; cc.js.get(_ccsg.Node._dirtyFlags, 'all', function () { var count = _ccsg.Node._dirtyFlags.COUNT; diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index a417992ea1..b5cfcf8d60 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -436,7 +436,7 @@ _ccsg.Label = _ccsg.Node.extend({ if (CC_EDITOR) { this._updateLabel(); } else { - this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.labelDirty); + this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.textDirty); } }, _createRenderCmd: function() { @@ -449,19 +449,19 @@ _ccsg.Label = _ccsg.Node.extend({ getContentSize: function() { if (!CC_EDITOR && !cc.sizeEqualToSize(this._contentSize, this._renderCmd._realRenderingSize)) { - this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.labelDirty); + this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.textDirty); } return _ccsg.Node.prototype.getContentSize.call(this); }, _getWidth: function () { if (!CC_EDITOR && !cc.sizeEqualToSize(this._contentSize, this._renderCmd._realRenderingSize)) { - this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.labelDirty); + this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.textDirty); } return _ccsg.Node.prototype._getWidth.call(this); }, _getHeight: function () { if (!CC_EDITOR && !cc.sizeEqualToSize(this._contentSize, this._renderCmd._realRenderingSize)) { - this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.labelDirty); + this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.textDirty); } return _ccsg.Node.prototype._getHeight.call(this); }, diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index b6d617b5c9..ff2364862e 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -44,7 +44,7 @@ if(colorDirty || opacityDirty){ this._updateColor(); - }else if(locFlag & flags.labelDirty) { + }else if(locFlag & flags.textDirty) { this._rebuildLabelSkin(); } @@ -79,7 +79,7 @@ if(colorDirty || opacityDirty){ this._updateColor(); - } else if(locFlag & flags.labelDirty) { + } else if(locFlag & flags.textDirty) { this._rebuildLabelSkin(); } @@ -438,7 +438,7 @@ }; proto._rebuildLabelSkin = function () { - this._dirtyFlag = this._dirtyFlag & _ccsg.Node._dirtyFlags.labelDirty ^ this._dirtyFlag; + this._dirtyFlag = this._dirtyFlag & _ccsg.Node._dirtyFlags.textDirty ^ this._dirtyFlag; var node = this._node; this._realRenderingSize = _ccsg.Node.prototype.getContentSize.call(node); From 34b62651d19d809e83224baa9c18ad59b1f5fb22 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 16 Mar 2016 15:36:04 +0800 Subject: [PATCH 07/20] refactoring label --- cocos2d/core/label/CCSGLabel.js | 4 +- .../core/label/CCSGLabelCanvasRenderCmd.js | 80 +++++++++++-------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index b5cfcf8d60..0367f9df57 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -790,10 +790,10 @@ cc.BMFontHelper = { var contentSize = cc.size(this._labelWidth, this._labelHeight); if (this._labelWidth <= 0) { - contentSize.width = longestLine; + contentSize.width = parseFloat(longestLine.toFixed(2)); } if (this._labelHeight <= 0) { - contentSize.height = this._textDesiredHeight; + contentSize.height = parseFloat(this._textDesiredHeight.toFixed(2)); } _ccsg.Node.prototype.setContentSize.call(this, contentSize); diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index ff2364862e..44ccd44a3f 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -222,7 +222,8 @@ _ccsg.Node.RenderCmd.prototype._updateDisplayColor.call(this, parentColor); var node = this._node; if (node._labelType === _ccsg.Label.Type.TTF || node._labelType === _ccsg.Label.Type.SystemFont) { - this._rebuildLabelSkin(); + this._updateTexture(); + this._prepareQuad(); } }; @@ -230,11 +231,12 @@ _ccsg.Node.RenderCmd.prototype._syncDisplayColor.call(this, parentColor); var node = this._node; if (node._labelType === _ccsg.Label.Type.TTF || node._labelType === _ccsg.Label.Type.SystemFont) { - this._rebuildLabelSkin(); + this._updateTexture(); + this._prepareQuad(); } }; - proto._calculateLabelFont = function(canvasSize) { + proto._calculateLabelFont = function() { var node = this._node; var paragraphedStrings = node._string.split('\n'); @@ -253,9 +255,9 @@ if (node._isWrapText) { var totalLength = 0; for (i = 0; i < paragraphedStrings.length; ++i) { - totalLength += ((paragraphLength[i] / canvasSize.width + 1) | 0) * canvasSize.width; + totalLength += ((paragraphLength[i] / this._canvasSize.width + 1) | 0) * this._canvasSize.width; } - var scale = canvasSize.width * ((canvasSize.height / this._getLineHeight()) | 0) / totalLength; + var scale = this._canvasSize.width * ((this._canvasSize.height / this._getLineHeight()) | 0) / totalLength; this._drawFontsize = (this._drawFontsize * Math.min(Math.sqrt(scale), 1)) | 0; fontDesc = this._drawFontsize.toString() + 'px ' + fontFamily; this._labelContext.font = fontDesc; @@ -267,8 +269,8 @@ for (i = 0; i < paragraphedStrings.length; ++i) { if (maxLength < paragraphLength[i]) maxLength = paragraphLength[i]; } - var scaleX = canvasSize.width / maxLength; - var scaleY = canvasSize.height / totalHeight; + var scaleX = this._canvasSize.width / maxLength; + var scaleY = this._canvasSize.height / totalHeight; this._drawFontsize = (this._drawFontsize * Math.min(1, scaleX, scaleY)) | 0; fontDesc = this._drawFontsize.toString() + 'px ' + fontFamily; @@ -299,7 +301,7 @@ return cc.size(canvasWidth, canvasHeight); }; - proto._calculateSplitedStrings = function(canvasSize) { + proto._calculateSplitedStrings = function() { var node = this._node; var ctx = this._labelContext; @@ -309,7 +311,7 @@ if (node._isWrapText) { this._splitedStrings = []; for (i = 0; i < paragraphedStrings.length; ++i) { - this._splitedStrings = this._splitedStrings.concat(this._fragmentText(paragraphedStrings[i], canvasSize.width, ctx)); + this._splitedStrings = this._splitedStrings.concat(this._fragmentText(paragraphedStrings[i], this._canvasSize.width, ctx)); } } else { @@ -318,14 +320,15 @@ }; - proto._updateLabelDimensions = function(canvasSize, ctx) { + proto._updateLabelDimensions = function() { var node = this._node; var paragraphedStrings = node._string.split('\n'); var i; + var ctx = this._labelContext; if (_ccsg.Label.Overflow.RESIZE_HEIGHT === node._overFlow) { - canvasSize.height = this._splitedStrings.length * this._getLineHeight(); - _ccsg.Node.prototype.setContentSize.call(node, canvasSize); + this._canvasSize.height = this._splitedStrings.length * this._getLineHeight(); + _ccsg.Node.prototype.setContentSize.call(node, this._canvasSize); } else if(_ccsg.Label.Overflow.NONE === node._overFlow) { this._splitedStrings = paragraphedStrings; @@ -337,18 +340,17 @@ } canvasSizeY = this._splitedStrings.length * this._getLineHeight(); - canvasSize.width = canvasSizeX; - canvasSize.height = canvasSizeY; - _ccsg.Node.prototype.setContentSize.call(node, canvasSize); + this._canvasSize.width = parseFloat(canvasSizeX.toFixed(2)); + this._canvasSize.height = parseFloat(canvasSizeY.toFixed(2)); + _ccsg.Node.prototype.setContentSize.call(node, this._canvasSize); } - this._labelCanvas.width = canvasSize.width; - this._labelCanvas.height = canvasSize.height; + this._labelCanvas.width = this._canvasSize.width; + this._labelCanvas.height = this._canvasSize.height; - this._labelContext.clearRect(0, 0, this._labelCanvas.width, this._labelCanvas.height); }; - proto._calculateFillTextStartPosition = function(canvasSize) { + proto._calculateFillTextStartPosition = function() { var node = this._node; var lineHeight = this._getLineHeight(); var lineCount = this._splitedStrings.length; @@ -356,10 +358,10 @@ var firstLinelabelY; if (cc.TextAlignment.RIGHT === node._hAlign) { - labelX = canvasSize.width; + labelX = this._canvasSize.width; } else if (cc.TextAlignment.CENTER === node._hAlign) { - labelX = canvasSize.width / 2; + labelX = this._canvasSize.width / 2; } else { labelX = 0; @@ -369,10 +371,10 @@ firstLinelabelY = 0; } else if (cc.VerticalTextAlignment.CENTER === node._vAlign) { - firstLinelabelY = canvasSize.height / 2 - lineHeight * (lineCount - 1) / 2; + firstLinelabelY = this._canvasSize.height / 2 - lineHeight * (lineCount - 1) / 2; } else { - firstLinelabelY = canvasSize.height - lineHeight * (lineCount - 1); + firstLinelabelY = this._canvasSize.height - lineHeight * (lineCount - 1); } return cc.p(labelX, firstLinelabelY); @@ -410,24 +412,34 @@ var node = this._node; this._drawFontsize = node._fontSize; - var canvasSize = this._calculateCanvasSize(); + this._canvasSize = this._calculateCanvasSize(); //Note: don't change the calling order of the following 3 statements - var fontDesc = this._calculateLabelFont(canvasSize); - this._calculateSplitedStrings(canvasSize); - this._updateLabelDimensions(canvasSize, this._labelContext); - - //this._labelContext.fillStyle = "rgb(128,128,128)"; - //this._labelContext.fillRect(0,0,this._labelCanvas.width,this._labelCanvas.height); - var color = this._displayedColor; - this._labelContext.fillStyle = 'rgb(' + color.r + ',' + color.g + ',' + color.b + ')'; + this._fontDesc = this._calculateLabelFont(); + this._calculateSplitedStrings(); + this._updateLabelDimensions(); this._calculateTextBaseline(); - var startPosition = this._calculateFillTextStartPosition(canvasSize); + this._updateTexture(); + + }; + + proto._updateTexture = function() { + this._canvasSize = this._calculateCanvasSize(); + + this._labelContext.clearRect(0, 0, this._labelCanvas.width, this._labelCanvas.height); + + + this._fontDesc = this._calculateLabelFont(); + this._labelContext.font = this._fontDesc; + + var startPosition = this._calculateFillTextStartPosition(); var lineHeight = this._getLineHeight(); - this._labelContext.font = fontDesc; + var color = this._displayedColor; + this._labelContext.fillStyle = 'rgb(' + color.r + ',' + color.g + ',' + color.b + ')'; + //do real rendering for (var i = 0; i < this._splitedStrings.length; ++i) { this._labelContext.fillText(this._splitedStrings[i], startPosition.x, startPosition.y + i * lineHeight); From f1a066807fd7d76c231b03323f8cbd85d6b2580c Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 16 Mar 2016 15:29:12 +0800 Subject: [PATCH 08/20] Improve scheduler logic, avoid using key in web & native --- cocos2d/core/CCScheduler.js | 96 +++++++++----------------- cocos2d/core/components/CCComponent.js | 2 +- jsb/jsb-etc.js | 3 + 3 files changed, 36 insertions(+), 65 deletions(-) diff --git a/cocos2d/core/CCScheduler.js b/cocos2d/core/CCScheduler.js index d0bde63af3..a268bba740 100644 --- a/cocos2d/core/CCScheduler.js +++ b/cocos2d/core/CCScheduler.js @@ -172,7 +172,7 @@ var TimerTargetSelector = Timer.extend({ this._selector = null; }, - initWithSelector: function(scheduler, selector, target, seconds, repeat, delay){ + initWithSelector: function(scheduler, target, selector, seconds, repeat, delay){ this._scheduler = scheduler; this._target = target; this._selector = selector; @@ -186,8 +186,8 @@ var TimerTargetSelector = Timer.extend({ trigger: function(){ //override - if (this._target && this._selector){ - this._target.call(this._selector, this._elapsed); + if (this._selector && this._target){ + this._selector.call(this._target, this._elapsed); } }, @@ -512,7 +512,7 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ */ scheduleCallbackForTarget: function(target, callback_fn, interval, repeat, delay, paused){ //cc.log("scheduleCallbackForTarget is deprecated. Please use schedule."); - this.schedule(callback_fn, target, interval, repeat, delay, paused, getTargetId(target) + ""); + this.schedule(callback_fn, target, interval, repeat, delay, paused); }, /** @@ -524,36 +524,21 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ * @param {Number} repeat * @param {Number} delay * @param {Boolean} paused - * @param {Number} key * @example {@link utils/api/engine/docs/cocos2d/core/CCScheduler/schedule.js} */ - schedule: function(callback, target, interval, repeat, delay, paused, key){ - var isSelector = false; - if(typeof callback !== "function"){ - var selector = callback; - isSelector = true; + schedule: function (callback, target, interval, repeat, delay, paused) { + 'use strict'; + if (typeof callback !== 'function') { + var tmp = callback; + callback = target; + target = tmp; } - - if(isSelector === false){ - //callback, target, interval, repeat, delay, paused, key - //callback, target, interval, paused, key - if(arguments.length === 4 || arguments.length === 5){ - key = delay; - paused = repeat; - delay = 0; - repeat = cc.macro.REPEAT_FOREVER; - } - }else{ - //selector, target, interval, repeat, delay, paused - //selector, target, interval, paused - if(arguments.length === 4){ - paused = repeat; - repeat = cc.macro.REPEAT_FOREVER; - delay = 0; - } - } - if (key === undefined) { - key = target.__instanceId + ""; + //selector, target, interval, repeat, delay, paused + //selector, target, interval, paused + if(arguments.length === 4 || arguments.length === 5){ + paused = repeat; + repeat = cc.macro.REPEAT_FOREVER; + delay = 0; } cc.assert(target, cc._LogInfos.Scheduler_scheduleCallbackForTarget_3); @@ -567,42 +552,27 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ this._arrayForTimers.push(element); this._hashForTimers[instanceId] = element; }else{ - cc.assert(element.paused === paused, ""); + cc.assert(element.paused === paused, ''); } var timer, i; if (element.timers == null) { element.timers = []; - } else if(isSelector === false) { - for (i = 0; i < element.timers.length; i++) { - timer = element.timers[i]; - if (callback === timer._callback) { - cc.log(cc._LogInfos.Scheduler.scheduleCallbackForTarget, timer.getInterval().toFixed(4), interval.toFixed(4)); - timer._interval = interval; - return; - } - } - }else{ + } + else { for (i = 0; i < element.timers.length; ++i){ timer =element.timers[i]; - if (timer && selector === timer.getSelector()){ - cc.log("CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: %.4f to %.4f", timer.getInterval(), interval); + if (timer && callback === timer.getSelector()){ + cc.log('CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: %.4f to %.4f', timer.getInterval(), interval); timer.setInterval(interval); return; } } - //ccArrayEnsureExtraCapacity(element->timers, 1); - } - - if(isSelector === false){ - timer = new TimerTargetCallback(); - timer.initWithCallback(this, callback, target, key, interval, repeat, delay); - element.timers.push(timer); - }else{ - timer = new TimerTargetSelector(); - timer.initWithSelector(this, selector, target, interval, repeat, delay); - element.timers.push(timer); } + + timer = new TimerTargetSelector(); + timer.initWithSelector(this, target, callback, interval, repeat, delay); + element.timers.push(timer); }, scheduleUpdate: function(target, priority, paused, updateFunc){ @@ -617,24 +587,22 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ case "string": return key === timer.getKey(); case "function": - return key === timer._callback; - default: - return key === timer.getSelector(); + return key === timer._callback || timer.getSelector(); } }, /** Unschedules a callback for a callback and a given target. * If you want to unschedule the "update", use `unscheudleUpdate()` - * @param {Function|String} key The callback to be unscheduled + * @param {Function} callback The callback to be unscheduled * @param {Object} target The target bound to the callback. */ - unschedule: function(key, target){ + unschedule: function(callback, target){ //key, target //selector, target //callback, target - This is in order to increase compatibility // explicity handle nil arguments when removing an object - if (!target || !key) + if (!target || !callback) return; var self = this, element = self._hashForTimers[getTargetId(target)]; @@ -642,7 +610,7 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ var timers = element.timers; for(var i = 0, li = timers.length; i < li; i++){ var timer = timers[i]; - if (this._getUnscheduleMark(key, timer)) { + if (this._getUnscheduleMark(callback, timer)) { if ((timer === element.currentTimer) && (!element.currentTimerSalvaged)) { element.currentTimerSalvaged = true; } @@ -1017,12 +985,12 @@ cc.Scheduler = cc._Class.extend(/** @lends cc.Scheduler# */{ /** * Unschedules all function callbacks for a given target. This also includes the "update" callback function. * @method unscheduleAllCallbacksForTarget - * @deprecated since v3.4 please use .unscheduleAll + * @deprecated since v3.4 please use unscheduleAllForTarget * @param {Object} target */ unscheduleAllCallbacksForTarget: function(target){ //cc.log("unscheduleAllCallbacksForTarget is deprecated. Please use unscheduleAll."); - this.unschedule(getTargetId(target) + "", target); + this.unscheduleAllForTarget(target); }, /** diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index f0524786d0..19e33fc5ef 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -701,7 +701,7 @@ var Component = cc.Class({ repeat = isNaN(repeat) ? cc.macro.REPEAT_FOREVER : repeat; delay = delay || 0; - cc.director.getScheduler().scheduleCallbackForTarget(this, callback, interval, repeat, delay, !this.enabledInHierarchy); + cc.director.getScheduler().schedule(callback, this, interval, repeat, delay, !this.enabledInHierarchy); }, /** diff --git a/jsb/jsb-etc.js b/jsb/jsb-etc.js index 79ffb37a8e..6b1b770d16 100644 --- a/jsb/jsb-etc.js +++ b/jsb/jsb-etc.js @@ -70,6 +70,9 @@ cc.js.mixin(cc.path, { }); // cc.Scheduler +cc.Scheduler.prototype.schedule = function (callback, target, interval, repeat, delay, paused) { + this.scheduleCallbackForTarget(target, callback, interval, repeat, delay, paused); +}; cc.Scheduler.prototype.scheduleUpdate = cc.Scheduler.prototype.scheduleUpdateForTarget; cc.Scheduler.prototype._unschedule = cc.Scheduler.prototype.unschedule; cc.Scheduler.prototype.unschedule = function (callback, target) { From bb8481e184119229f489b369c092c9e51586e3da Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 16 Mar 2016 16:42:12 +0800 Subject: [PATCH 09/20] fix label color opacity issue --- .../core/label/CCSGLabelCanvasRenderCmd.js | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index 44ccd44a3f..5a8a8b263b 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -218,23 +218,6 @@ return wrappedWords; }; - proto._updateDisplayColor = function (parentColor) { - _ccsg.Node.RenderCmd.prototype._updateDisplayColor.call(this, parentColor); - var node = this._node; - if (node._labelType === _ccsg.Label.Type.TTF || node._labelType === _ccsg.Label.Type.SystemFont) { - this._updateTexture(); - this._prepareQuad(); - } - }; - - proto._syncDisplayColor = function (parentColor) { - _ccsg.Node.RenderCmd.prototype._syncDisplayColor.call(this, parentColor); - var node = this._node; - if (node._labelType === _ccsg.Label.Type.TTF || node._labelType === _ccsg.Label.Type.SystemFont) { - this._updateTexture(); - this._prepareQuad(); - } - }; proto._calculateLabelFont = function() { var node = this._node; @@ -425,12 +408,13 @@ }; + proto._updateColor = function() { + this._rebuildLabelSkin(); + }; + proto._updateTexture = function() { - this._canvasSize = this._calculateCanvasSize(); - this._labelContext.clearRect(0, 0, this._labelCanvas.width, this._labelCanvas.height); - this._fontDesc = this._calculateLabelFont(); this._labelContext.font = this._fontDesc; From 963dad915281290e698aeac16ce841a62de91afb Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 16 Mar 2016 17:04:11 +0800 Subject: [PATCH 10/20] we should update editbox size first. --- cocos2d/core/components/CCEditBox.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/components/CCEditBox.js b/cocos2d/core/components/CCEditBox.js index c8a82ce559..7f43a21dea 100644 --- a/cocos2d/core/components/CCEditBox.js +++ b/cocos2d/core/components/CCEditBox.js @@ -276,6 +276,9 @@ var EditBox = cc.Class({ this._createBackgroundSprite(); + if (!this._useOriginalSize) { + sgNode.setContentSize(this.node.getContentSize()); + } sgNode.inputMode = this.inputMode; sgNode.maxLength = this.maxLength; @@ -290,9 +293,6 @@ var EditBox = cc.Class({ sgNode.returnType = this.returnType; sgNode.setLineHeight(this.lineHeight); - if (!this._useOriginalSize) { - sgNode.setContentSize(this.node.getContentSize()); - } sgNode.setDelegate(this); }, From e6336209c9ecb0f280f7574a629a1f0f89abee25 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 16 Mar 2016 18:11:30 +0800 Subject: [PATCH 11/20] don't excluse color,opacity and textDirty logic --- cocos2d/core/label/CCSGLabelCanvasRenderCmd.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index 5a8a8b263b..28a557549b 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -42,9 +42,7 @@ if (opacityDirty) this._updateDisplayOpacity(); - if(colorDirty || opacityDirty){ - this._updateColor(); - }else if(locFlag & flags.textDirty) { + if(colorDirty || opacityDirty || (locFlag & flags.textDirty)){ this._rebuildLabelSkin(); } @@ -77,9 +75,7 @@ if (opacityDirty) this._syncDisplayOpacity(); - if(colorDirty || opacityDirty){ - this._updateColor(); - } else if(locFlag & flags.textDirty) { + if(colorDirty || opacityDirty || (locFlag & flags.textDirty)){ this._rebuildLabelSkin(); } @@ -408,9 +404,9 @@ }; - proto._updateColor = function() { - this._rebuildLabelSkin(); - }; + // proto._updateColor = function() { + // this._rebuildLabelSkin(); + // }; proto._updateTexture = function() { this._labelContext.clearRect(0, 0, this._labelCanvas.width, this._labelCanvas.height); From 3d14f13d1eaadb39f55b8877e68f8ab195f0c1fa Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 10 Mar 2016 16:15:00 +0800 Subject: [PATCH 12/20] Use id to identify persist root node --- cocos2d/core/CCDirector.js | 10 +++++----- cocos2d/core/CCGame.js | 20 +++++++++++--------- jsb/jsb-director.js | 4 ++-- jsb/jsb-game.js | 20 +++++++++++--------- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index 6e52fad9b6..105dae4a01 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -471,7 +471,7 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ * @param {Function} [onLaunched] - The function invoked at the scene after launch. */ runSceneImmediate: function (scene, onBeforeLoadScene, onLaunched) { - var i, node, game = cc.game; + var id, node, game = cc.game; var persistNodes = game._persistRootNodes; if (scene instanceof cc.Scene) { @@ -480,8 +480,8 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ } // detach persist nodes - for (i = persistNodes.length - 1; i >= 0; --i) { - node = persistNodes[i]; + for (id in persistNodes) { + node = persistNodes[id]; game._ignoreRemovePersistNode = node; node.parent = null; game._ignoreRemovePersistNode = null; @@ -511,8 +511,8 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ sgScene = scene._sgNode; // Re-attach persist nodes - for (i = 0; i < persistNodes.length; ++i) { - node = persistNodes[i]; + for (id in persistNodes) { + node = persistNodes[id]; node.parent = scene; } scene._activate(); diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index 3ea76d1500..b08a0b4a77 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -47,7 +47,7 @@ var game = /** @lends cc.game# */{ _eventHide: null, _eventShow: null, - _persistRootNodes: [], + _persistRootNodes: {}, _ignoreRemovePersistNode: null, /** @@ -343,10 +343,12 @@ var game = /** @lends cc.game# */{ * @param {Node} node - The node to be made persistent */ addPersistRootNode: function (node) { - if (!(node instanceof cc.Node)) + if (!(node instanceof cc.Node) || !node.uuid) { + cc.warn('The target can not be made persist because it\'s not a cc.Node or it doesn\'t have _id property.'); return; - var index = this._persistRootNodes.indexOf(node); - if (index === -1) { + } + var id = node.uuid; + if (!this._persistRootNodes[id]) { var scene = cc.director._scene; if (cc.isValid(scene)) { if (!node.parent) { @@ -360,7 +362,7 @@ var game = /** @lends cc.game# */{ cc.warn('The node can not be made persist because it\'s not in current scene.'); return; } - this._persistRootNodes.push(node); + this._persistRootNodes[id] = node; node._persistNode = true; } } @@ -373,11 +375,11 @@ var game = /** @lends cc.game# */{ */ removePersistRootNode: function (node) { if (node !== this._ignoreRemovePersistNode) { - var index = this._persistRootNodes.indexOf(node); - if (index !== -1) { - this._persistRootNodes.splice(index, 1); + var id = node.uuid || ''; + if (node === this._persistRootNodes[id]) { + delete this._persistRootNodes[id]; + node._persistNode = false; } - node._persistNode = false; } }, diff --git a/jsb/jsb-director.js b/jsb/jsb-director.js index 13c1e036a9..6850f895ad 100644 --- a/jsb/jsb-director.js +++ b/jsb/jsb-director.js @@ -105,7 +105,7 @@ cc.js.mixin(cc.director, { scene._load(); } - for (i = persistNodes.length - 1; i >= 0; --i) { + for (i in persistNodes) { node = persistNodes[i]; game._ignoreRemovePersistNode = node; node.parent = null; @@ -134,7 +134,7 @@ cc.js.mixin(cc.director, { sgScene = scene._sgNode; // Re-attach persist nodes - for (i = 0; i < persistNodes.length; ++i) { + for (i in persistNodes) { node = persistNodes[i]; node.parent = scene; } diff --git a/jsb/jsb-game.js b/jsb/jsb-game.js index 28c0a781ed..0893bb38e6 100644 --- a/jsb/jsb-game.js +++ b/jsb/jsb-game.js @@ -34,7 +34,7 @@ cc.js.mixin(cc.game, { // Scenes list _sceneInfos: [], - _persistRootNodes: [], + _persistRootNodes: {}, _ignoreRemovePersistNode: null, CONFIG_KEY: { @@ -145,10 +145,12 @@ cc.js.mixin(cc.game, { * @param {ENode} node - The node to be made persistent */ addPersistRootNode: function (node) { - if (!(node instanceof cc.Node)) + if (!(node instanceof cc.Node) || !node._id) { + cc.warn('The target can not be made persist because it\'s not a cc.Node or it doesn\'t have _id property.'); return; - var index = this._persistRootNodes.indexOf(node); - if (index === -1) { + } + var id = node._id; + if (!this._persistRootNodes[id]) { var scene = cc.director._scene; if (cc.isValid(scene)) { if (!node.parent) { @@ -162,7 +164,7 @@ cc.js.mixin(cc.game, { cc.warn('The node can not be made persist because it\'s not in current scene.'); return; } - this._persistRootNodes.push(node); + this._persistRootNodes[id] = node; node._persistNode = true; } } @@ -175,11 +177,11 @@ cc.js.mixin(cc.game, { */ removePersistRootNode: function (node) { if (node !== this._ignoreRemovePersistNode) { - var index = this._persistRootNodes.indexOf(node); - if (index !== -1) { - this._persistRootNodes.splice(index, 1); + var id = node._id || ''; + if (node === this._persistRootNodes[id]) { + this._persistRootNodes[id] = null; + node._persistNode = false; } - node._persistNode = false; } }, From 46913e7d87cbb08a935502479a8b4aa5fb9313b0 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 17 Mar 2016 09:47:34 +0800 Subject: [PATCH 13/20] fix webgl label rendering position --- cocos2d/core/label/CCSGLabel.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/label/CCSGLabel.js b/cocos2d/core/label/CCSGLabel.js index 0367f9df57..cad94feee8 100644 --- a/cocos2d/core/label/CCSGLabel.js +++ b/cocos2d/core/label/CCSGLabel.js @@ -449,19 +449,19 @@ _ccsg.Label = _ccsg.Node.extend({ getContentSize: function() { if (!CC_EDITOR && !cc.sizeEqualToSize(this._contentSize, this._renderCmd._realRenderingSize)) { - this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.textDirty); + this._updateLabel(); } return _ccsg.Node.prototype.getContentSize.call(this); }, _getWidth: function () { if (!CC_EDITOR && !cc.sizeEqualToSize(this._contentSize, this._renderCmd._realRenderingSize)) { - this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.textDirty); + this._updateLabel(); } return _ccsg.Node.prototype._getWidth.call(this); }, _getHeight: function () { if (!CC_EDITOR && !cc.sizeEqualToSize(this._contentSize, this._renderCmd._realRenderingSize)) { - this._renderCmd.setDirtyFlag(_ccsg.Node._dirtyFlags.textDirty); + this._updateLabel(); } return _ccsg.Node.prototype._getHeight.call(this); }, From fc172e086a6d52619c36ff95cccbed34c0438ed8 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 17 Mar 2016 10:49:01 +0800 Subject: [PATCH 14/20] Replace instead of reattach when scene contains the persist node --- cocos2d/core/CCDirector.js | 12 ++++++++++-- jsb/jsb-director.js | 20 ++++++++++++++------ jsb/jsb-game.js | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index 105dae4a01..f501ba9975 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -510,10 +510,18 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ this._scene = scene; sgScene = scene._sgNode; - // Re-attach persist nodes + // Re-attach or replace persist nodes for (id in persistNodes) { node = persistNodes[id]; - node.parent = scene; + var existNode = scene.getChildByName(node.name); + // Scene contains the persist node, should not reattach, should update the persist node + if (existNode) { + persistNodes[id] = existNode; + existNode._persistNode = true; + } + else { + node.parent = scene; + } } scene._activate(); } diff --git a/jsb/jsb-director.js b/jsb/jsb-director.js index 6850f895ad..60dad7be5b 100644 --- a/jsb/jsb-director.js +++ b/jsb/jsb-director.js @@ -96,7 +96,7 @@ cc.js.mixin(cc.director, { */ runSceneImmediate: function (scene, onBeforeLoadScene, onLaunched) { // detach persist nodes - var i, node, game = cc.game; + var id, node, game = cc.game; var persistNodes = game._persistRootNodes; // Run an Entity Scene @@ -105,8 +105,8 @@ cc.js.mixin(cc.director, { scene._load(); } - for (i in persistNodes) { - node = persistNodes[i]; + for (id in persistNodes) { + node = persistNodes[id]; game._ignoreRemovePersistNode = node; node.parent = null; game._ignoreRemovePersistNode = null; @@ -134,9 +134,17 @@ cc.js.mixin(cc.director, { sgScene = scene._sgNode; // Re-attach persist nodes - for (i in persistNodes) { - node = persistNodes[i]; - node.parent = scene; + for (id in persistNodes) { + node = persistNodes[id]; + var existNode = scene.getChildByName(node.name); + // Scene contains the persist node, should not reattach, should update the persist node + if (existNode) { + persistNodes[id] = existNode; + existNode._persistNode = true; + } + else { + node.parent = scene; + } } scene._activate(); } diff --git a/jsb/jsb-game.js b/jsb/jsb-game.js index 0893bb38e6..74d782c544 100644 --- a/jsb/jsb-game.js +++ b/jsb/jsb-game.js @@ -179,7 +179,7 @@ cc.js.mixin(cc.game, { if (node !== this._ignoreRemovePersistNode) { var id = node._id || ''; if (node === this._persistRootNodes[id]) { - this._persistRootNodes[id] = null; + delete this._persistRootNodes[id]; node._persistNode = false; } } From ceff5e8c9efcf136ec82132f284a9ed8521c1c1b Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 17 Mar 2016 10:59:17 +0800 Subject: [PATCH 15/20] fix syncStatus transform recursive dirty --- cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js index b7facce48e..0193f3c083 100644 --- a/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js +++ b/cocos2d/core/base-nodes/CCSGNodeCanvasRenderCmd.js @@ -375,7 +375,7 @@ _ccsg.Node.RenderCmd.prototype = { if (transformDirty) //update the transform - this.transform(parentCmd, true); + this.transform(parentCmd); }, visitChildren: function(){ From e009eb0fc2788d6662b8bca4b8556e5ec435f7ed Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 17 Mar 2016 11:34:29 +0800 Subject: [PATCH 16/20] Detect existing persist node by uuid --- cocos2d/core/CCDirector.js | 2 +- cocos2d/core/utils/base-node.js | 20 ++++++++++++++++++++ jsb/jsb-director.js | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index f501ba9975..6d219df7ea 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -513,7 +513,7 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ // Re-attach or replace persist nodes for (id in persistNodes) { node = persistNodes[id]; - var existNode = scene.getChildByName(node.name); + var existNode = scene.getChildByUuid(id); // Scene contains the persist node, should not reattach, should update the persist node if (existNode) { persistNodes[id] = existNode; diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index c455dc313b..60e27474a2 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -976,6 +976,26 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ return null; }, + /** + * Returns a child from the container given its uuid + * @method getChildByUuid + * @param {String} uuid - The uuid to find the child node. + * @return {Node} a Node whose uuid equals to the input parameter + */ + getChildByUuid: function(uuid){ + if(!uuid){ + cc.log("Invalid uuid"); + return null; + } + + var locChildren = this._children; + for(var i = 0, len = locChildren.length; i < len; i++){ + if(locChildren[i]._id === uuid) + return locChildren[i]; + } + return null; + }, + /** * Returns a child from the container given its name * @method getChildByName diff --git a/jsb/jsb-director.js b/jsb/jsb-director.js index 60dad7be5b..3b4abbe1ab 100644 --- a/jsb/jsb-director.js +++ b/jsb/jsb-director.js @@ -136,7 +136,7 @@ cc.js.mixin(cc.director, { // Re-attach persist nodes for (id in persistNodes) { node = persistNodes[id]; - var existNode = scene.getChildByName(node.name); + var existNode = scene.getChildByUuid(id); // Scene contains the persist node, should not reattach, should update the persist node if (existNode) { persistNodes[id] = existNode; From cc18e4652c64e647fcf7183941118fcaa8f7afee Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Thu, 17 Mar 2016 16:03:06 +0800 Subject: [PATCH 17/20] fixed animation onEnable/onDisable --- cocos2d/core/components/CCAnimation.js | 6 ++++- test/qunit/unit/test-animation.js | 35 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCAnimation.js b/cocos2d/core/components/CCAnimation.js index fe47e40078..f0091467cb 100644 --- a/cocos2d/core/components/CCAnimation.js +++ b/cocos2d/core/components/CCAnimation.js @@ -156,8 +156,12 @@ var Animation = cc.Class({ } }, + onEnable: function () { + this.resume(); + }, + onDisable: function () { - this.stop(); + this.pause(); }, /////////////////////////////////////////////////////////////////////////////// diff --git a/test/qunit/unit/test-animation.js b/test/qunit/unit/test-animation.js index f6ff512092..c8ed6bf5a6 100644 --- a/test/qunit/unit/test-animation.js +++ b/test/qunit/unit/test-animation.js @@ -1203,3 +1203,38 @@ test('play Animation', function () { strictEqual(moveState.isPlaying, false, 'move animation state should not be playing'); strictEqual(rotateState.isPlaying, false, 'rotate animation state should be playing'); }); + +test('animation enabled/disabled', function () { + var scene = cc.director.getScene(); + var entity = new cc.Node(); + var animation = entity.addComponent(cc.Animation); + + entity.parent = scene; + + var clip = new cc.AnimationClip(); + clip._name = 'move'; + clip._duration = 1; + clip.curveData = { + props: { + x: [ + {frame: 0, value: 0}, + {frame: 1, value: 100} + ] + } + }; + animation.addClip(clip); + animation.play('move'); + + animation.enabled = false; + + strictEqual(animation._animator.isPlaying, true, 'move animation should be playing'); + strictEqual(animation._animator.isPaused, true, 'move animation should be paused'); + + animation.enabled = true; + + strictEqual(animation._animator.isPlaying, true, 'move animation should be playing'); + strictEqual(animation._animator.isPaused, false, 'move animation should be resumed'); + + entity.parent = null; +}); + From 965c0bffbd1036c2270fb91decd082468dc8c26e Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Fri, 18 Mar 2016 10:38:36 +0800 Subject: [PATCH 18/20] fix removeSgNode on JSB --- cocos2d/core/components/CCRendererInSG.js | 2 +- cocos2d/core/utils/base-node.js | 2 +- cocos2d/core/utils/scene-graph-helper.js | 5 +++-- jsb/jsb-etc.js | 6 ++++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/components/CCRendererInSG.js b/cocos2d/core/components/CCRendererInSG.js index d896cdef78..9e37e363c4 100644 --- a/cocos2d/core/components/CCRendererInSG.js +++ b/cocos2d/core/components/CCRendererInSG.js @@ -105,7 +105,7 @@ var RendererInSG = cc.Class({ parentNode.removeChild(replaced); parentNode.addChild(sgNode); sgNode.arrivalOrder = replaced.arrivalOrder; - if (cc.renderer) { + if ( !CC_JSB ) { cc.renderer.childrenOrderDirty = parentNode._reorderChildDirty = true; } diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 60e27474a2..271a2c031f 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -1493,7 +1493,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ var sibling = siblings[i]; sibling._sgNode.arrivalOrder = i; } - if (cc.renderer) { + if ( !CC_JSB ) { cc.renderer.childrenOrderDirty = this._parent._sgNode._reorderChildDirty = true; } this._parent.emit(CHILD_REORDER); diff --git a/cocos2d/core/utils/scene-graph-helper.js b/cocos2d/core/utils/scene-graph-helper.js index 68fc30e985..f291350edf 100644 --- a/cocos2d/core/utils/scene-graph-helper.js +++ b/cocos2d/core/utils/scene-graph-helper.js @@ -30,8 +30,9 @@ var SceneGraphUtils = { var node = this._sgNode; if (node) { node.release(); - if (node._parent) { - node._parent.removeChild(node); + var parent = node._parent; + if (parent) { + parent.removeChild(node); } } }, diff --git a/jsb/jsb-etc.js b/jsb/jsb-etc.js index 6b1b770d16..b7a114f41b 100644 --- a/jsb/jsb-etc.js +++ b/jsb/jsb-etc.js @@ -84,8 +84,10 @@ cc.Scheduler.prototype.unschedule = function (callback, target) { this._unschedule(target, callback); }; -// Node arrivalOrder -cc.defineGetterSetter(cc.Node.prototype, "arrivalOrder", cc.Node.prototype.getOrderOfArrival, cc.Node.prototype.setOrderOfArrival); +// Node +var nodeProto = cc.Node.prototype; +cc.defineGetterSetter(nodeProto, "arrivalOrder", nodeProto.getOrderOfArrival, nodeProto.setOrderOfArrival); +cc.defineGetterSetter(nodeProto, "_parent", nodeProto.getParent, nodeProto.setParent); // TextureCache addImage if (!cc.TextureCache.prototype._addImageAsync) { From ddbe66feb53e6d6f38693e35c1ae627ed7357c1f Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 18 Mar 2016 11:53:38 +0800 Subject: [PATCH 19/20] Fix child of inactive node can receive event issue --- cocos2d/core/CCNode.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 18b1de4e4c..dc5a533e5b 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1115,7 +1115,7 @@ var Node = cc.Class({ }, isRunning: function () { - return this.active; + return this._activeInHierarchy; }, // ACTIONS From 3039a216c3983548f25c739f61c726381130681c Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 18 Mar 2016 11:59:29 +0800 Subject: [PATCH 20/20] Fix EaseAction issue in JSB --- jsb/jsb-action.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jsb/jsb-action.js b/jsb/jsb-action.js index de14281367..b37c620b31 100644 --- a/jsb/jsb-action.js +++ b/jsb/jsb-action.js @@ -107,7 +107,9 @@ for (var i = 0; i < actionArr.length; ++i) { continue; var proto = type.prototype; setCtorReplacer(proto); - setAliasReplacer(name, type); + if (name.indexOf('Ease') === -1) { + setAliasReplacer(name, type); + } } function setChainFuncReplacer (proto, name) {