From 8f65363fc5312008f2bc230017ee7f6e355df806 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 13 Jan 2016 09:36:00 +0800 Subject: [PATCH 01/35] Fix issue when Mask is created without being loaded --- cocos2d/core/components/CCMask.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/components/CCMask.js b/cocos2d/core/components/CCMask.js index 4943455b24..b2415453cb 100644 --- a/cocos2d/core/components/CCMask.js +++ b/cocos2d/core/components/CCMask.js @@ -58,8 +58,8 @@ var Mask = cc.Class({ onDestroy: function () { if (cc.sys.isNative) { - this._clippingStencil.release(); - this._clippingNode.release(); + this._clippingStencil && this._clippingStencil.release(); + this._clippingNode && this._clippingNode.release(); } }, From 810b70e59aade00f4c78ba0ab4c6b082cc382ab4 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 13 Jan 2016 11:00:54 +0800 Subject: [PATCH 02/35] fix empty line for label ttf --- cocos2d/core/label/CCSGLabelCanvasRenderCmd.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index 2a889f1c2c..74cb9f3017 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -82,6 +82,11 @@ //check the first character var wrappedWords = []; + //fast return if strArr is empty + if(strArr.length === 0) { + wrappedWords.push(""); + return wrappedWords; + } var text = strArr; var allWidth = ctx.measureText(text).width; while(allWidth > maxWidth && text.length > 1){ From c7e45ce19cda137ae74c76868cb701df83ae60d2 Mon Sep 17 00:00:00 2001 From: Nan Wang Date: Wed, 13 Jan 2016 12:25:23 +0800 Subject: [PATCH 03/35] move actions back to cc module. --- cocos2d/actions/CCAction.js | 8 +-- cocos2d/actions/CCActionCatmullRom.js | 10 ++-- cocos2d/actions/CCActionEase.js | 76 +++++++++++++-------------- cocos2d/actions/CCActionInstant.js | 20 +++---- cocos2d/actions/CCActionInterval.js | 58 ++++++++++---------- 5 files changed, 86 insertions(+), 86 deletions(-) diff --git a/cocos2d/actions/CCAction.js b/cocos2d/actions/CCAction.js index 0d62a26c42..b0029c878f 100644 --- a/cocos2d/actions/CCAction.js +++ b/cocos2d/actions/CCAction.js @@ -25,7 +25,7 @@ ****************************************************************************/ /** - * @module actions + * @module cc */ /** Default Action tag @@ -234,7 +234,7 @@ cc.FiniteTimeAction = cc.Action.extend({ }); /** - * @module actions + * @module cc */ /* @@ -355,7 +355,7 @@ cc.Speed = cc.Action.extend({ * * @warning This action can't be Sequenceable because it is not an cc.IntervalAction * - * @method cc.speed + * @method speed * @param {ActionInterval} action * @param {Number} speed * @return {Speed} @@ -545,7 +545,7 @@ cc.Follow = cc.Action.extend({ /** * Create a follow action which makes its target follows another node. * - * @method + * @method follow * @param {Node} followedNode * @param {Rect} rect * @return {Follow|Null} returns the cc.Follow object on success diff --git a/cocos2d/actions/CCActionCatmullRom.js b/cocos2d/actions/CCActionCatmullRom.js index c540d4d4be..4b972479c0 100644 --- a/cocos2d/actions/CCActionCatmullRom.js +++ b/cocos2d/actions/CCActionCatmullRom.js @@ -33,7 +33,7 @@ ****************************************************************************/ /** - * @module actions + * @module cc */ /* @@ -267,7 +267,7 @@ cc.CardinalSplineTo = cc.ActionInterval.extend({ /** * creates an action with a Cardinal Spline array of points and tension. * - * @method cc.cardinalSplineTo + * @method cardinalSplineTo * @param {Number} duration * @param {Array} points array of control points * @param {Number} tension @@ -378,7 +378,7 @@ cc.CardinalSplineBy = cc.CardinalSplineTo.extend({ /** * creates an action with a Cardinal Spline array of points and tension. * - * @method cc.cardinalSplineBy + * @method cardinalSplineBy * @param {Number} duration * @param {Array} points * @param {Number} tension @@ -430,7 +430,7 @@ cc.CatmullRomTo = cc.CardinalSplineTo.extend({ /** * creates an action with a Cardinal Spline array of points and tension. * - * @method cc.catmullRomTo + * @method catmullRomTo * @param {Number} dt * @param {Array} points * @return {CatmullRomTo} @@ -484,7 +484,7 @@ cc.CatmullRomBy = cc.CardinalSplineBy.extend({ /** * Creates an action with a Cardinal Spline array of points and tension - * @method cc.catmullRomBy + * @method catmullRomBy * @param {Number} dt * @param {Array} points * @return {CatmullRomBy} diff --git a/cocos2d/actions/CCActionEase.js b/cocos2d/actions/CCActionEase.js index cd198af3aa..6857ef5661 100644 --- a/cocos2d/actions/CCActionEase.js +++ b/cocos2d/actions/CCActionEase.js @@ -25,7 +25,7 @@ ****************************************************************************/ /** - * @module actions + * @module cc */ /** @@ -163,7 +163,7 @@ cc.easeRateAction = function (action, rate) { cc.EaseRateAction.create = cc.easeRateAction; /** - * @module actions + * @module cc */ /* @@ -205,7 +205,7 @@ cc.EaseIn.create = function (action, rate) { * Creates the action easing object with the rate parameter.
* From slow to fast. * - * @method cc.easeIn + * @method easeIn * @param {Number} rate * @return {Object} * @example @@ -262,7 +262,7 @@ cc.EaseOut.create = function (action, rate) { * Creates the action easing object with the rate parameter.
* From fast to slow. * - * @method cc.easeOut + * @method easeOut * @param {Number} rate * @return {Object} * @example @@ -322,7 +322,7 @@ cc.EaseInOut.create = function (action, rate) { /** * Creates the action easing object with the rate parameter.
* Slow to fast then to slow. - * @method cc.easeInOut + * @method easeInOut * @param {Number} rate * @return {Object} * @@ -394,7 +394,7 @@ cc._easeExponentialInObj = { * Creates the action easing object with the rate parameter.
* Reference easeInExpo:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeExponentialIn + * @method easeExponentialIn * @return {Object} * @example * // example @@ -452,7 +452,7 @@ cc._easeExponentialOutObj = { * creates the action easing object.
* Reference easeOutExpo:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeExponentialOut + * @method easeExponentialOut * @return {Object} * @example * // example @@ -525,7 +525,7 @@ cc._easeExponentialInOutObj = { * creates an EaseExponentialInOut action easing object.
* Reference easeInOutExpo:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeExponentialInOut + * @method easeExponentialInOut * @return {Object} * @example * // example @@ -583,7 +583,7 @@ cc._easeSineInObj = { * creates an EaseSineIn action.
* Reference easeInSine:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeSineIn + * @method easeSineIn * @return {Object} * @example * // example @@ -642,7 +642,7 @@ cc._easeSineOutObj = { * Creates an EaseSineOut action easing object.
* Reference easeOutSine:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeSineOut + * @method easeSineOut * @return {Object} * @example * // example @@ -701,7 +701,7 @@ cc._easeSineInOutObj = { * creates the action easing object.
* Reference easeInOutSine:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeSineInOut + * @method easeSineInOut * @return {Object} * @example * // example @@ -772,7 +772,7 @@ cc.EaseElastic.create = function (action, period) { }; /** - * @module actions + * @module cc */ /* @@ -836,7 +836,7 @@ cc._easeElasticInObj = { * Creates the action easing obejct with the period in radians (default is 0.3).
* Reference easeInElastic:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeElasticIn + * @method easeElasticIn * @param {Number} [period=0.3] * @return {Object} * @example @@ -918,7 +918,7 @@ cc._easeElasticOutObj = { * Creates the action easing object with the period in radians (default is 0.3).
* Reference easeOutElastic:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeElasticOut + * @method easeElasticOut * @param {Number} [period=0.3] * @return {Object} * @example @@ -996,7 +996,7 @@ cc.EaseElasticInOut.create = function (action, period) { * Creates the action easing object with the period in radians (default is 0.3).
* Reference easeInOutElastic:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeElasticInOut + * @method easeElasticInOut * @param {Number} [period=0.3] * @return {Object} * @example @@ -1073,7 +1073,7 @@ cc.EaseBounce.create = function (action) { }; /** - * @module actions + * @module cc */ /* @@ -1139,7 +1139,7 @@ cc._easeBounceInObj = { /** * Creates the action easing object.
* Eased bounce effect at the beginning. - * @method cc.easeBounceIn + * @method easeBounceIn * @return {Object} * @example * // example @@ -1197,7 +1197,7 @@ cc._easeBounceOutObj = { /** * Creates the action easing object.
* Eased bounce effect at the ending. - * @method cc.easeBounceOut + * @method easeBounceOut * @return {Object} * @example * // example @@ -1268,7 +1268,7 @@ cc._easeBounceInOutObj = { /** * Creates the action easing object.
* Eased bounce effect at the begining and ending. - * @method cc.easeBounceInOut + * @method easeBounceInOut * @return {Object} * @example * // example @@ -1328,7 +1328,7 @@ cc._easeBackInObj = { /** * Creates the action easing object.
* In the opposite direction to move slowly, and then accelerated to the right direction. - * @method cc.easeBackIn + * @method easeBackIn * @return {Object} * @example * // example @@ -1389,7 +1389,7 @@ cc._easeBackOutObj = { /** * Creates the action easing object.
* Fast moving more than the finish, and then slowly back to the finish. - * @method cc.easeBackOut + * @method easeBackOut * @return {Object} * @example * // example @@ -1460,7 +1460,7 @@ cc._easeBackInOutObj = { /** * Creates the action easing object.
* Begining of cc.EaseBackIn. Ending of cc.EaseBackOut. - * @method cc.easeBackInOut + * @method easeBackInOut * @return {Object} * @example * // example @@ -1548,7 +1548,7 @@ cc.EaseBezierAction.create = function(action){ * Creates the action easing object.
* Into the 4 reference point.
* To calculate the motion curve. - * @method cc.easeBezierAction + * @method easeBezierAction * @param {Number} p0 The first bezier parameter * @param {Number} p1 The second bezier parameter * @param {Number} p2 The third bezier parameter @@ -1621,7 +1621,7 @@ cc._easeQuadraticActionIn = { * Creates the action easing object.
* Reference easeInQuad:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeQuadraticActionIn + * @method easeQuadraticActionIn * @returns {Object} * @example * //example @@ -1681,7 +1681,7 @@ cc._easeQuadraticActionOut = { * Creates the action easing object.
* Reference easeOutQuad:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeQuadraticActionOut + * @method easeQuadraticActionOut * @returns {Object} * @example * //example @@ -1749,7 +1749,7 @@ cc._easeQuadraticActionInOut = { * Creates the action easing object.
* Reference easeInOutQuad:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeQuadraticActionInOut + * @method easeQuadraticActionInOut * @returns {Object} * @example * //example @@ -1808,7 +1808,7 @@ cc._easeQuarticActionIn = { * Creates the action easing object.
* Reference easeIntQuart:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeQuarticActionIn + * @method easeQuarticActionIn * @returns {Object} * @example * //example @@ -1869,7 +1869,7 @@ cc._easeQuarticActionOut = { * Creates the action easing object.
* Reference easeOutQuart:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeQuarticActionOut + * @method easeQuarticActionOut * @returns {Object} * @example * //example @@ -1932,7 +1932,7 @@ cc._easeQuarticActionInOut = { * Creates the action easing object.
* Reference easeInOutQuart:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeQuarticActionInOut + * @method easeQuarticActionInOut * @returns {Object} */ cc.easeQuarticActionInOut = function(){ @@ -1989,7 +1989,7 @@ cc._easeQuinticActionIn = { * Creates the action easing object.
* Reference easeInQuint:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeQuinticActionIn + * @method easeQuinticActionIn * @returns {Object} * @example * //example @@ -2050,7 +2050,7 @@ cc._easeQuinticActionOut = { * Creates the action easing object.
* Reference easeOutQuint:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeQuinticActionOut + * @method easeQuinticActionOut * @returns {Object} * @example * //example @@ -2114,7 +2114,7 @@ cc._easeQuinticActionInOut = { * Creates the action easing object.
* Reference easeInOutQuint:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeQuinticActionInOut + * @method easeQuinticActionInOut * @returns {Object} * @example * //example @@ -2174,7 +2174,7 @@ cc._easeCircleActionIn = { * Creates the action easing object.
* Reference easeInCirc:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeCircleActionIn + * @method easeCircleActionIn * @returns {Object} * @example * //example @@ -2235,7 +2235,7 @@ cc._easeCircleActionOut = { * Creates the action easing object.
* Reference easeOutCirc:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeCircleActionOut + * @method easeCircleActionOut * @returns {Object} * @exampple * //example @@ -2299,7 +2299,7 @@ cc._easeCircleActionInOut = { * Creates the action easing object.
* Reference easeInOutCirc:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeCircleActionInOut + * @method easeCircleActionInOut * @returns {Object} * @example * //example @@ -2359,7 +2359,7 @@ cc._easeCubicActionIn = { * Creates the action easing object.
* Reference easeInCubic:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeCubicActionIn + * @method easeCubicActionIn * @returns {Object} * @example * //example @@ -2420,7 +2420,7 @@ cc._easeCubicActionOut = { * Creates the action easing object.
* Reference easeOutCubic:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeCubicActionOut + * @method easeCubicActionOut * @returns {Object} * @example * //example @@ -2484,7 +2484,7 @@ cc._easeCubicActionInOut = { * Creates the action easing object.
* Reference easeInOutCubic:
* http://www.zhihu.com/question/21981571/answer/19925418 - * @method cc.easeCubicActionInOut + * @method easeCubicActionInOut * @returns {Object} */ cc.easeCubicActionInOut = function(){ diff --git a/cocos2d/actions/CCActionInstant.js b/cocos2d/actions/CCActionInstant.js index 3e492b911e..16b6c0d071 100644 --- a/cocos2d/actions/CCActionInstant.js +++ b/cocos2d/actions/CCActionInstant.js @@ -25,7 +25,7 @@ ****************************************************************************/ /** - * @module actions + * @module cc */ /** @@ -63,7 +63,7 @@ cc.ActionInstant = cc.FiniteTimeAction.extend({ }); /** - * @module actions + * @module cc */ /* @@ -88,7 +88,7 @@ cc.Show = cc.ActionInstant.extend({ /** * Show the Node. - * @method cc.show + * @method show * @return {Show} * @example * // example @@ -122,7 +122,7 @@ cc.Hide = cc.ActionInstant.extend({ /** * Hide the node. - * @method cc.hide + * @method hide * @return {Hide} * @example * // example @@ -156,7 +156,7 @@ cc.ToggleVisibility = cc.ActionInstant.extend({ /** * Toggles the visibility of a node. - * @method cc.toggleVisibility + * @method toggleVisibility * @return {ToggleVisibility} * @example * // example @@ -208,7 +208,7 @@ cc.RemoveSelf = cc.ActionInstant.extend({ /** * Create a RemoveSelf object with a flag indicate whether the target should be cleaned up while removing. * - * @method cc.removeSelf + * @method removeSelf * @param {Boolean} [isNeedCleanUp=true] * @return {RemoveSelf} * @@ -268,7 +268,7 @@ cc.FlipX = cc.ActionInstant.extend({ /** * Create a FlipX action to flip or unflip the target. * - * @method cc.flipX + * @method flipX * @param {Boolean} flip Indicate whether the target should be flipped or not * @return {FlipX} * @example @@ -326,7 +326,7 @@ cc.FlipY = cc.ActionInstant.extend({ /** * Create a FlipY action to flip or unflip the target. * - * @method cc.flipY + * @method flipY * @param {Boolean} flip * @return {FlipY} * @example @@ -391,7 +391,7 @@ cc.Place = cc.ActionInstant.extend({ /** * Creates a Place action with a position. - * @method cc.place + * @method place * @param {Vec2|Number} pos * @param {Number} [y] * @return {Place} @@ -502,7 +502,7 @@ cc.CallFunc = cc.ActionInstant.extend({ /** * Creates the action with the callback - * @method cc.callFunc + * @method callFunc * @param {function} selector * @param {object|null} [selectorTarget] * @param {*|null} [data] data for function, it accepts all data types. diff --git a/cocos2d/actions/CCActionInterval.js b/cocos2d/actions/CCActionInterval.js index 9cde873298..b86dc1523b 100644 --- a/cocos2d/actions/CCActionInterval.js +++ b/cocos2d/actions/CCActionInterval.js @@ -25,7 +25,7 @@ ****************************************************************************/ /** - * @module actions + * @module cc */ /** @@ -279,7 +279,7 @@ cc.actionInterval = function (d) { cc.ActionInterval.create = cc.actionInterval; /** - * @module actions + * @module cc */ /* @@ -418,7 +418,7 @@ cc.Sequence = cc.ActionInterval.extend({ /** * Helper constructor to create an array of sequenceable actions * The created action will run actions sequentially, one after another. - * @method cc.sequence + * @method sequence * @param {Array|FiniteTimeAction} tempArray * @return {Sequence} * @example @@ -595,7 +595,7 @@ cc.Repeat = cc.ActionInterval.extend({ /** * Creates a Repeat action. Times is an unsigned integer between 1 and pow(2,30) - * @method cc.repeat + * @method repeat * @param {FiniteTimeAction} action * @param {Number} times * @return {Repeat} @@ -699,7 +699,7 @@ cc.RepeatForever = cc.ActionInterval.extend({ /** * Create a acton which repeat forever - * @method cc.repeatForever + * @method repeatForever * @param {FiniteTimeAction} action * @return {RepeatForever} * @example @@ -810,7 +810,7 @@ cc.Spawn = cc.ActionInterval.extend({ /** * Create a spawn action which runs several actions in parallel. - * @method cc.spawn + * @method spawn * @param {Array|FiniteTimeAction}tempArray * @return {FiniteTimeAction} * @example @@ -928,7 +928,7 @@ cc.RotateTo = cc.ActionInterval.extend({ /** * Rotates a Node object to a certain angle by modifying its rotation property.
* The direction will be decided by the shortest angle. - * @method cc.rotateTo + * @method rotateTo * @param {Number} duration duration in seconds * @param {Number} deltaAngleX deltaAngleX in degrees. * @param {Number} [deltaAngleY] deltaAngleY in degrees. @@ -1015,7 +1015,7 @@ cc.RotateBy = cc.ActionInterval.extend({ /** * Rotates a Node object clockwise a number of degrees by modifying its rotation property. * Relative to its properties to modify. - * @method cc.rotateBy + * @method rotateBy * @param {Number} duration duration in seconds * @param {Number} deltaAngleX deltaAngleX in degrees * @param {Number} [deltaAngleY] deltaAngleY in degrees @@ -1136,7 +1136,7 @@ cc.MoveBy = cc.ActionInterval.extend({ * x and y are relative to the position of the object.
* Several MoveBy actions can be concurrently called, and the resulting
* movement will be the sum of individual movements. - * @method cc.moveBy + * @method moveBy * @param {Number} duration duration in seconds * @param {Vec2|Number} deltaPos * @param {Number} deltaY @@ -1213,7 +1213,7 @@ cc.MoveTo = cc.MoveBy.extend({ * Moves a Node object to the position x,y. x and y are absolute coordinates by modifying its position property.
* Several MoveTo actions can be concurrently called, and the resulting
* movement will be the sum of individual movements. - * @method cc.moveTo + * @method moveTo * @param {Number} duration duration in seconds * @param {Vec2} position * @param {Number} y @@ -1306,7 +1306,7 @@ cc.SkewTo = cc.ActionInterval.extend({ /** * Create a action which skews a Node object to given angles by modifying its skewX and skewY properties. * Changes to the specified value. - * @method cc.skewTo + * @method skewTo * @param {Number} t time in seconds * @param {Number} sx * @param {Number} sy @@ -1380,7 +1380,7 @@ cc.SkewBy = cc.SkewTo.extend({ /** * Skews a Node object by skewX and skewY degrees.
* Relative to its property modification. - * @method cc.skewBy + * @method skewBy * @param {Number} t time in seconds * @param {Number} sx sx skew in degrees for X axis * @param {Number} sy sy skew in degrees for Y axis @@ -1508,7 +1508,7 @@ cc.JumpBy = cc.ActionInterval.extend({ /** * Moves a Node object simulating a parabolic jump movement by modifying it's position property. * Relative to its movement. - * @method cc.jumpBy + * @method jumpBy * @param {Number} duration * @param {Vec2|Number} position * @param {Number} [y] @@ -1590,7 +1590,7 @@ cc.JumpTo = cc.JumpBy.extend({ /** * Moves a Node object to a parabolic position simulating a jump movement by modifying its position property.
* Jump to the specified location. - * @method cc.jumpTo + * @method jumpTo * @param {Number} duration * @param {Vec2|Number} position * @param {Number} [y] @@ -1609,7 +1609,7 @@ cc.jumpTo = function (duration, position, y, height, jumps) { cc.JumpTo.create = cc.jumpTo; /* - * @method cc.bezierAt + * @method bezierAt * @param {Number} a * @param {Number} b * @param {Number} c @@ -1736,7 +1736,7 @@ cc.BezierBy = cc.ActionInterval.extend({ /** * An action that moves the target with a cubic Bezier curve by a certain distance. * Relative to its movement. - * @method cc.bezierBy + * @method bezierBy * @param {Number} t time in seconds * @param {Array} c Array of points * @return {BezierBy} @@ -1803,7 +1803,7 @@ cc.BezierTo = cc.BezierBy.extend({ }); /** * An action that moves the target with a cubic Bezier curve to a destination point. - * @method cc.bezierTo + * @method bezierTo * @param {Number} t * @param {Array} c array of points * @return {BezierTo} @@ -1888,7 +1888,7 @@ cc.ScaleTo = cc.ActionInterval.extend({ }); /** * Scales a Node object to a zoom factor by modifying it's scale property. - * @method cc.scaleTo + * @method scaleTo * @param {Number} duration * @param {Number} sx scale parameter in X * @param {Number} [sy] scale parameter in Y, if Null equal to sx @@ -1936,7 +1936,7 @@ cc.ScaleBy = cc.ScaleTo.extend({ /** * Scales a Node object a zoom factor by modifying it's scale property. * Relative to its changes. - * @method cc.scaleBy + * @method scaleBy * @param {Number} duration duration in seconds * @param {Number} sx sx scale parameter in X * @param {Number|Null} [sy=] sy scale parameter in Y, if Null equal to sx @@ -2019,7 +2019,7 @@ cc.Blink = cc.ActionInterval.extend({ }); /** * Blinks a Node object by modifying it's visible property. - * @method cc.blink + * @method blink * @param {Number} duration duration in seconds * @param blinks blinks in times * @return {Blink} @@ -2085,7 +2085,7 @@ cc.FadeTo = cc.ActionInterval.extend({ /** * Fades an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from the current value to a custom one. - * @method cc.fadeTo + * @method fadeTo * @param {Number} duration * @param {Number} opacity 0-255, 0 is transparent * @return {FadeTo} @@ -2138,7 +2138,7 @@ cc.FadeIn = cc.FadeTo.extend({ /** * Fades In an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 0 to 255. - * @method cc.fadeIn + * @method fadeIn * @param {Number} duration duration in seconds * @return {FadeIn} * @example @@ -2185,7 +2185,7 @@ cc.FadeOut = cc.FadeTo.extend({ /** * Fades Out an object that implements the cc.RGBAProtocol protocol. It modifies the opacity from 255 to 0. - * @method cc.fadeOut + * @method fadeOut * @param {Number} d duration in seconds * @return {FadeOut} * @example @@ -2266,7 +2266,7 @@ cc.TintTo = cc.ActionInterval.extend({ /** * Tints a Node that implements the cc.NodeRGB protocol from current tint to a custom one. - * @method cc.tintTo + * @method tintTo * @param {Number} duration * @param {Number} red 0-255 * @param {Number} green 0-255 @@ -2360,7 +2360,7 @@ cc.TintBy = cc.ActionInterval.extend({ /** * Tints a Node that implements the cc.NodeRGB protocol from current tint to a custom one. * Relative to their own color change. - * @method cc.tintBy + * @method tintBy * @param {Number} duration duration in seconds * @param {Number} deltaRed * @param {Number} deltaGreen @@ -2399,7 +2399,7 @@ cc.DelayTime = cc.ActionInterval.extend({ /** * Delays the action a certain amount of seconds - * @method cc.delayTime + * @method delayTime * @param {Number} d duration in seconds * @return {DelayTime} * @example @@ -2482,7 +2482,7 @@ cc.ReverseTime = cc.ActionInterval.extend({ /** * Executes an action in reverse order, from time=duration to time=0. - * @method cc.reverseTime + * @method reverseTime * @param {FiniteTimeAction} action * @return {ReverseTime} * @example @@ -2651,7 +2651,7 @@ cc.Animate = cc.ActionInterval.extend({ /** * create the animate with animation - * @method cc.animate + * @method animate * @param {SpriteFrameAnimation} animation * @return {Animate} * @example @@ -2739,7 +2739,7 @@ cc.TargetedAction = cc.ActionInterval.extend({ /** * Create an action with the specified action and forced target - * @method cc.targetedAction + * @method targetedAction * @param {Node} target * @param {FiniteTimeAction} action * @return {TargetedAction} From 6604053da835fcb5f5c94e0ed3f7c0af29c8ceed Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 13 Jan 2016 14:05:24 +0800 Subject: [PATCH 04/35] add _initSgNode for ComponentInSG --- cocos2d/core/components/CCComponentInSG.js | 9 +++++- cocos2d/core/components/CCLabel.js | 8 +++-- cocos2d/core/components/CCSprite.js | 32 ++++++++++--------- cocos2d/particle/CCParticleSystem.js | 7 ++-- .../src/NodeEventTest/NodeEventTest.js | 1 - 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/cocos2d/core/components/CCComponentInSG.js b/cocos2d/core/components/CCComponentInSG.js index 211dbb3a56..7ae842a276 100644 --- a/cocos2d/core/components/CCComponentInSG.js +++ b/cocos2d/core/components/CCComponentInSG.js @@ -25,6 +25,8 @@ var ComponentInSG = cc.Class({ onLoad: function () { var sgNode = this._createSgNode(); + this._sgNode = sgNode; + this._initSgNode(); this._appendSgNode(sgNode); if ( !this.node._sizeProvider ) { this.node._sizeProvider = sgNode; @@ -57,6 +59,12 @@ var ComponentInSG = cc.Class({ */ _createSgNode: null, + /** + * @method _initSgNode + * @private + */ + _initSgNode: null, + _removeSgNode: SceneGraphHelper.removeSgNode, _appendSgNode: function (sgNode) { @@ -82,7 +90,6 @@ var ComponentInSG = cc.Class({ // retain immediately // will be released in SceneGraphHelper.removeSgNode sgNode.retain(); - this._sgNode = sgNode; } }); diff --git a/cocos2d/core/components/CCLabel.js b/cocos2d/core/components/CCLabel.js index cec6ccee38..293577eede 100644 --- a/cocos2d/core/components/CCLabel.js +++ b/cocos2d/core/components/CCLabel.js @@ -261,7 +261,11 @@ var Label = cc.Class({ }, _createSgNode: function () { - var sgNode = new _ccsg.Label(this.string, this.file); + return new _ccsg.Label(this.string, this.file); + }, + + _initSgNode: function () { + var sgNode = this._sgNode; // TODO // sgNode.enableRichText = this.enableRichText; @@ -276,8 +280,6 @@ var Label = cc.Class({ sgNode.setContentSize(this.node.getContentSize()); } sgNode.setColor(this.node.color); - - return sgNode; }, _resized: function () { diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index eb02f8aa3a..94d3c1ce51 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -72,7 +72,7 @@ var Sprite = cc.Class({ var lastSprite = this._spriteFrame; this._spriteFrame = value; if (this._sgNode) { - this._applySpriteFrame(this._sgNode, lastSprite); + this._applySpriteFrame(lastSprite); // color cleared after reset texture, should reapply color this._sgNode.setColor(this.node._color); this._sgNode.setOpacity(this.node._opacity); @@ -313,9 +313,9 @@ var Sprite = cc.Class({ } }, - _applyCapInset: function (sgNode) { + _applyCapInset: function () { if (this._type === SpriteType.SLICED && this._spriteFrame) { - sgNode = sgNode || this._sgNode; + var sgNode = this._sgNode; sgNode.setInsetTop(this._spriteFrame.insetTop); sgNode.setInsetBottom(this._spriteFrame.insetBottom); sgNode.setInsetRight(this._spriteFrame.insetRight); @@ -323,8 +323,7 @@ var Sprite = cc.Class({ } }, - _applySpriteSize: function (sgNode) { - sgNode = sgNode || this._sgNode; + _applySpriteSize: function () { if (this._useOriginalSize && this._spriteFrame) { var rect = this._spriteFrame.getRect(); this.node.setContentSize(cc.size(rect.width, rect.height)); @@ -334,25 +333,26 @@ var Sprite = cc.Class({ } }, - _onSpriteFrameLoaded: function (event, sgNode) { + _onSpriteFrameLoaded: function (event) { var self = this; - sgNode = sgNode || this._sgNode; + var sgNode = this._sgNode; sgNode.setSpriteFrame(self._spriteFrame); - self._applyCapInset(sgNode); + self._applyCapInset(); self._applySpriteSize(); if ( this.enabledInHierarchy && !sgNode.isVisible() ) { sgNode.setVisible(true); } }, - _applySpriteFrame: function (sgNode, oldFrame) { + _applySpriteFrame: function (oldFrame) { + var sgNode = this._sgNode; if (oldFrame && oldFrame.off) { oldFrame.off('load', this._onSpriteFrameLoaded, this); } if (this._spriteFrame) { if (this._spriteFrame.textureLoaded()) { - this._onSpriteFrameLoaded(null, sgNode); + this._onSpriteFrameLoaded(null); } else { this._spriteFrame.once('load', this._onSpriteFrameLoaded, this); @@ -369,18 +369,20 @@ var Sprite = cc.Class({ }, _createSgNode: function () { - var sgNode = new cc.Scale9Sprite(); + return new cc.Scale9Sprite(); + }, - this._applySpriteFrame(sgNode, null); + _initSgNode: function () { + var sgNode = this._sgNode; + + this._applySpriteFrame(null); // should keep the size of the sg node the same as entity, // otherwise setContentSize may not take effect sgNode.setContentSize(this.node.getContentSize(true)); - this._applySpriteSize(sgNode); + this._applySpriteSize(); sgNode.setRenderingType(this._type); - - return sgNode; }, _resized: function () { diff --git a/cocos2d/particle/CCParticleSystem.js b/cocos2d/particle/CCParticleSystem.js index a28caa9c34..80e6588421 100644 --- a/cocos2d/particle/CCParticleSystem.js +++ b/cocos2d/particle/CCParticleSystem.js @@ -646,7 +646,11 @@ var ParticleSystem = cc.Class({ // OVERRIDE METHODS _createSgNode: function () { - var sgNode = this._sgNode = new _ccsg.ParticleSystem(); + return new _ccsg.ParticleSystem(); + }, + + _initSgNode: function () { + var sgNode = this._sgNode; var loadCustomAfterFile = false; if (this._file) { @@ -660,7 +664,6 @@ var ParticleSystem = cc.Class({ // stop by default sgNode.stopSystem(); - return sgNode; }, // APIS diff --git a/test/visual-tests/src/NodeEventTest/NodeEventTest.js b/test/visual-tests/src/NodeEventTest/NodeEventTest.js index 0b0c32129b..7eb6048442 100644 --- a/test/visual-tests/src/NodeEventTest/NodeEventTest.js +++ b/test/visual-tests/src/NodeEventTest/NodeEventTest.js @@ -40,7 +40,6 @@ var createEntity = function (file, x, y) { var spriteFrame = new cc.SpriteFrame(tex, cc.rect(0, 0, tex.getPixelWidth(), tex.getPixelHeight())); var spriteRenderer = node.addComponent(cc.Sprite); spriteRenderer.spriteFrame = spriteFrame; - spriteRenderer._createSgNode(); } node.x = x; node.y = y; From 62006f4a78acfdc18ce5214ceff9a96419018a12 Mon Sep 17 00:00:00 2001 From: Nan Wang Date: Wed, 13 Jan 2016 14:27:12 +0800 Subject: [PATCH 05/35] fix cc module wrong link issue. --- cocos2d/core/platform/deserialize.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index 52848feb44..680bbd6be2 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -484,6 +484,10 @@ var _Deserializer = (function () { // FACADE +/** + * @module cc + */ + /** * !#en Deserialize json to cc.Asset * !#zh 将 JSON 反序列化为对象实例。 @@ -496,7 +500,6 @@ var _Deserializer = (function () { * @param {deserialize.Details} [result] - additional loading result * @param {Object} [options] * @return {object} the main data(asset) - * @module cc */ cc.deserialize = function (data, result, options) { var classFinder = (options && options.classFinder) || JS._getClassById; From ade6bb5362dc0ecec651bf44f73d225e6f9563f0 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 13 Jan 2016 15:26:58 +0800 Subject: [PATCH 06/35] check component conflict --- cocos2d/ToMerge/wrappers/ui/layout.js | 110 --------------------- cocos2d/ToMerge/wrappers/ui/scroll-view.js | 99 ------------------- cocos2d/core/CCNode.js | 6 ++ cocos2d/core/utils/base-node.js | 4 + 4 files changed, 10 insertions(+), 209 deletions(-) delete mode 100644 cocos2d/ToMerge/wrappers/ui/layout.js delete mode 100644 cocos2d/ToMerge/wrappers/ui/scroll-view.js diff --git a/cocos2d/ToMerge/wrappers/ui/layout.js b/cocos2d/ToMerge/wrappers/ui/layout.js deleted file mode 100644 index 7a7c20b270..0000000000 --- a/cocos2d/ToMerge/wrappers/ui/layout.js +++ /dev/null @@ -1,110 +0,0 @@ -var NodeWrapper = require('../node'); -var WidgetWrapper = require('./widget'); - -var LayoutWrapper = cc.Class({ - name: 'cc.LayoutWrapper', - extends: WidgetWrapper, - - properties: { - clippingEnabled: { - get: function () { - return this.targetN.clippingEnabled; - }, - set: function (value) { - if (typeof value === 'boolean') { - this.targetN.clippingEnabled = value; - - cc.renderer.childrenOrderDirty = true; - if (CC_EDITOR) { - cc.engine.repaintInEditMode(); - } - } - else { - cc.error('The new clippingEnabled must be boolean'); - } - } - }, - - layoutType: { - get: function () { - return this.targetN.layoutType; - }, - set: function (value) { - if (typeof value === 'number' && !isNaN(value)) { - this.targetN.layoutType = value; - this.updateChildLayoutType(); - - this.doLayout(); - } - else { - cc.error('The new layoutType must be number'); - } - }, - type: ccui.Layout.Type - }, - - _layoutType: { - default: 0 - }, - - _clippingEnabled: { - default: null - } - }, - - updateChildLayoutType: function () { - var layoutType = this.layoutType; - this.children.forEach( function (child) { - child.widgetLayoutType = layoutType; - }); - }, - - addChildN: function (child) { - var wrapper = cc.getWrapper(child); - wrapper.widgetLayoutType = this.layoutType; - NodeWrapper.prototype.addChildN.call(this, child); - }, - - canAddChildN: function (child) { - if ( !(child instanceof ccui.Widget) ) { - cc.error('Layout can only add ccui.Widget as a child'); - return false; - } - - return true; - }, - - onChildSiblingIndexChanged: function () { - this.doLayout(); - }, - - doLayout: function () { - this.targetN.requestDoLayout(); - cc.renderer.childrenOrderDirty = true; - if (CC_EDITOR) { - cc.engine.repaintInEditMode(); - } - }, - - onBeforeSerialize: function () { - WidgetWrapper.prototype.onBeforeSerialize.call(this); - - this._layoutType = this.layoutType; - this._clippingEnabled = this.clippingEnabled; - }, - - createNode: function (node) { - node = node || new ccui.Layout(); - - WidgetWrapper.prototype.createNode.call(this, node); - - node.layoutType = this._layoutType; - - if (this._clippingEnabled !== null) - node.clippingEnabled = this._clippingEnabled; - - return node; - } -}); - -cc.LayoutWrapper = module.exports = LayoutWrapper; diff --git a/cocos2d/ToMerge/wrappers/ui/scroll-view.js b/cocos2d/ToMerge/wrappers/ui/scroll-view.js deleted file mode 100644 index b89fb8d24e..0000000000 --- a/cocos2d/ToMerge/wrappers/ui/scroll-view.js +++ /dev/null @@ -1,99 +0,0 @@ - -var LayoutWrapper = require('./layout'); - -var ScrollViewWrapper = cc.Class({ - name: 'cc.ScrollViewWrapper', - extends: LayoutWrapper, - - properties: { - direction: { - get: function () { - return this.targetN.getDirection(); - }, - set: function (value) { - if (typeof value === 'number' && !isNaN(value)) { - this.targetN.setDirection(value); - - this.doLayout(); - } - else { - cc.error('The new direction must be number'); - } - }, - type: ccui.ScrollView.Dir - }, - - bounce: { - get: function () { - return this.targetN.isBounceEnabled(); - }, - set: function (value) { - if (typeof value === 'boolean') { - this.targetN.setBounceEnabled(value); - } - else { - cc.error('The new bounce must be boolean'); - } - } - }, - - innerSize: { - get: function () { - var size = this.targetN.getInnerContainerSize(); - return new cc.Vec2(size.width, size.height); - }, - set: function (value) { - if ( value instanceof cc.Vec2 ) { - this.targetN.setInnerContainerSize( cc.size(value.x, value.y) ); - - this.doLayout(); - } - else { - cc.error('The new innerSize must be cc.Vec2'); - } - } - }, - - _direction: { - default: ccui.ScrollView.Dir.NONE, - type: ccui.ScrollView.Dir - }, - - _bounce: { - default: false - }, - - _innerSize: { - default: null - } - }, - - doLayout: function () { - this.targetN._innerContainer.requestDoLayout(); - LayoutWrapper.prototype.doLayout.call(this); - }, - - onBeforeSerialize: function () { - LayoutWrapper.prototype.onBeforeSerialize.call(this); - - this._direction = this.direction; - this._bounce = this.bounce; - this._innerSize = [this.innerSize.x, this.innerSize.y]; - }, - - createNode: function (node) { - node = node || new ccui.ScrollView(); - - LayoutWrapper.prototype.createNode.call(this, node); - - node.setDirection( this._direction ); - node.setBounceEnabled( this._bounce ); - - var innerSize = this._innerSize; - if (innerSize) node.setInnerContainerSize( cc.size(innerSize[0], innerSize[1]) ); - - return node; - } -}); - -cc.ScrollViewWrapper = module.exports = ScrollViewWrapper; diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 0cb540a607..48683d23df 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -551,6 +551,12 @@ var Node = cc.Class({ } } + // check conflict + + if (CC_EDITOR && !_Scene.DetectConflict.beforeAddComponent(this, constructor)) { + return null; + } + // var component = new constructor(); diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index b9f61ae231..2e806d5c2b 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -144,6 +144,10 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ oldParent._children.splice(removeAt, 1); oldParent.emit(CHILD_REMOVED, removeAt); this._onHierarchyChanged(oldParent); + + if (CC_EDITOR) { + _Scene.DetectConflict.afterAddChild(this); + } } } else if (value) { From b09919b23ae564c6ddf1dcd59d19a75d91b10a7f Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 13 Jan 2016 22:50:31 +0800 Subject: [PATCH 07/35] Fix Android WEBGL detection issue --- cocos2d/core/platform/CCSys.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/platform/CCSys.js b/cocos2d/core/platform/CCSys.js index 51f26e7423..acf9fd35f5 100644 --- a/cocos2d/core/platform/CCSys.js +++ b/cocos2d/core/platform/CCSys.js @@ -641,8 +641,11 @@ else { _supportWebGL = true; } - // Ruled out Android 4- except for QQ Brwoser 6.2+ - if (sys.os === sys.OS_ANDROID && sys.osMainVersion < 5) { + // Accept only Android 5+ and QQ Brwoser 6.2+ + if (sys.os === sys.OS_ANDROID) { + if (!sys.osMainVersion || sys.osMainVersion < 5) { + _supportWebGL = false; + } var browserVer = parseFloat(sys.browserVersion); if (sys.browserType !== sys.BROWSER_TYPE_MOBILE_QQ || browserVer < 6.2) { _supportWebGL = false; From a21e09695a3075d6f0b890db72a25a62371b7a29 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 14 Jan 2016 00:05:51 +0800 Subject: [PATCH 08/35] fix uglify pipeline --- gulp/tasks/build.js | 46 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/gulp/tasks/build.js b/gulp/tasks/build.js index 62a89dfbae..c54b438ac8 100644 --- a/gulp/tasks/build.js +++ b/gulp/tasks/build.js @@ -17,18 +17,18 @@ var handleErrors = require('../util/handleErrors'); require('./build-cocos2d'); -function getUglifyOptions (minify, global_defs, preserveComments) { - preserveComments = preserveComments || false; +function getUglifyOptions (minify, global_defs) { if (minify) { return { compress: { - global_defs: global_defs + global_defs: global_defs, + conditionals: false, // 如果为 true, 会把 if 压缩成 bool 表达式, 但 sourcemap 的行号就会乱掉 } }; } else { - // http://lisperator.net/uglifyjs/compress var compress = { + // http://lisperator.net/uglifyjs/compress global_defs: global_defs, sequences: false, // join consecutive statements with the “comma operator” properties: false, // optimize property access: a["foo"] → a.foo @@ -49,11 +49,11 @@ function getUglifyOptions (minify, global_defs, preserveComments) { side_effects: false // drop side-effect-free statements //warnings: true // warn about potentially dangerous optimizations/code }; - // http://lisperator.net/uglifyjs/codegen return { mangle: false, - preserveComments: preserveComments, + //preserveComments: 'all', output: { + // http://lisperator.net/uglifyjs/codegen beautify: true, bracketize: true }, @@ -63,32 +63,28 @@ function getUglifyOptions (minify, global_defs, preserveComments) { } function rebundle(bundler) { - var bundle = bundler.bundle() - .on('error', handleErrors.handler) - .pipe(handleErrors()) - .pipe(source(paths.outFile)) - .pipe(buffer()); - - var dev = sourcemaps.init({loadMaps: true}) - .pipe(uglify(getUglifyOptions(false, { - CC_EDITOR: false, - CC_DEV: true, - CC_TEST: false - }))) - .pipe(sourcemaps.write('./', {sourceRoot: './', addComment: true})) - .pipe(gulp.dest(paths.outDir)); + var dev = uglify(getUglifyOptions(false, { + CC_EDITOR: false, + CC_DEV: false, + CC_TEST: false + })); var min = rename({ suffix: '-min' }); - min.pipe(sourcemaps.init({loadMaps: true})) - .pipe(uglify(getUglifyOptions(true, { + min.pipe(uglify(getUglifyOptions(true, { CC_EDITOR: false, CC_DEV: false, CC_TEST: false - }))) + }))); + + return bundler.bundle() + .on('error', handleErrors.handler) + .pipe(handleErrors()) + .pipe(source(paths.outFile)) + .pipe(buffer()) + .pipe(sourcemaps.init({loadMaps: true})) + .pipe(mirror(dev, min)) .pipe(sourcemaps.write('./', {sourceRoot: './', addComment: true})) .pipe(gulp.dest(paths.outDir)); - - return bundle.pipe(mirror(dev, min)); } function rebundle_test(bundler, suffix) { From 176925da48852022f3377823c62aff509b19681c Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 14 Jan 2016 10:17:26 +0800 Subject: [PATCH 09/35] Improve WebGL support detection --- cocos2d/core/platform/CCSys.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/platform/CCSys.js b/cocos2d/core/platform/CCSys.js index acf9fd35f5..3de3f41838 100644 --- a/cocos2d/core/platform/CCSys.js +++ b/cocos2d/core/platform/CCSys.js @@ -641,14 +641,17 @@ else { _supportWebGL = true; } - // Accept only Android 5+ and QQ Brwoser 6.2+ + // Accept only Android 5+ default browser and QQ Brwoser 6.2+ if (sys.os === sys.OS_ANDROID) { - if (!sys.osMainVersion || sys.osMainVersion < 5) { - _supportWebGL = false; - } + _supportWebGL = false; + // QQ Brwoser 6.2+ var browserVer = parseFloat(sys.browserVersion); - if (sys.browserType !== sys.BROWSER_TYPE_MOBILE_QQ || browserVer < 6.2) { - _supportWebGL = false; + if (sys.browserType === sys.BROWSER_TYPE_MOBILE_QQ && browserVer >= 6.2) { + _supportWebGL = true; + } + // Android 5+ default browser + else if (sys.osMainVersion && sys.osMainVersion >= 5 && sys.browserType === sys.BROWSER_TYPE_ANDROID) { + _supportWebGL = true; } } } From 5456aa241ef69a150465cb27413a4fe36524eedf Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 14 Jan 2016 13:55:01 +0800 Subject: [PATCH 10/35] add animatable attribute for CCClass --- cocos2d/core/platform/CCClass.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index 3086afde75..aed538dc4b 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -920,6 +920,11 @@ function parseAttributes (attrs, className, propName) { if (attrs.serializable === false) { result.push(Attr.NonSerialized); } + if (CC_EDITOR) { + if ('animatable' in attrs && !attrs.animatable) { + result.push({ animatable: false }); + } + } if (CC_DEV) { var visible = attrs.visible; From 9804d1a72299e0af578ad7fd7174b393764cd3f4 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 14 Jan 2016 00:05:51 +0800 Subject: [PATCH 11/35] fix uglify pipeline --- gulp/tasks/build.js | 46 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/gulp/tasks/build.js b/gulp/tasks/build.js index 62a89dfbae..c54b438ac8 100644 --- a/gulp/tasks/build.js +++ b/gulp/tasks/build.js @@ -17,18 +17,18 @@ var handleErrors = require('../util/handleErrors'); require('./build-cocos2d'); -function getUglifyOptions (minify, global_defs, preserveComments) { - preserveComments = preserveComments || false; +function getUglifyOptions (minify, global_defs) { if (minify) { return { compress: { - global_defs: global_defs + global_defs: global_defs, + conditionals: false, // 如果为 true, 会把 if 压缩成 bool 表达式, 但 sourcemap 的行号就会乱掉 } }; } else { - // http://lisperator.net/uglifyjs/compress var compress = { + // http://lisperator.net/uglifyjs/compress global_defs: global_defs, sequences: false, // join consecutive statements with the “comma operator” properties: false, // optimize property access: a["foo"] → a.foo @@ -49,11 +49,11 @@ function getUglifyOptions (minify, global_defs, preserveComments) { side_effects: false // drop side-effect-free statements //warnings: true // warn about potentially dangerous optimizations/code }; - // http://lisperator.net/uglifyjs/codegen return { mangle: false, - preserveComments: preserveComments, + //preserveComments: 'all', output: { + // http://lisperator.net/uglifyjs/codegen beautify: true, bracketize: true }, @@ -63,32 +63,28 @@ function getUglifyOptions (minify, global_defs, preserveComments) { } function rebundle(bundler) { - var bundle = bundler.bundle() - .on('error', handleErrors.handler) - .pipe(handleErrors()) - .pipe(source(paths.outFile)) - .pipe(buffer()); - - var dev = sourcemaps.init({loadMaps: true}) - .pipe(uglify(getUglifyOptions(false, { - CC_EDITOR: false, - CC_DEV: true, - CC_TEST: false - }))) - .pipe(sourcemaps.write('./', {sourceRoot: './', addComment: true})) - .pipe(gulp.dest(paths.outDir)); + var dev = uglify(getUglifyOptions(false, { + CC_EDITOR: false, + CC_DEV: false, + CC_TEST: false + })); var min = rename({ suffix: '-min' }); - min.pipe(sourcemaps.init({loadMaps: true})) - .pipe(uglify(getUglifyOptions(true, { + min.pipe(uglify(getUglifyOptions(true, { CC_EDITOR: false, CC_DEV: false, CC_TEST: false - }))) + }))); + + return bundler.bundle() + .on('error', handleErrors.handler) + .pipe(handleErrors()) + .pipe(source(paths.outFile)) + .pipe(buffer()) + .pipe(sourcemaps.init({loadMaps: true})) + .pipe(mirror(dev, min)) .pipe(sourcemaps.write('./', {sourceRoot: './', addComment: true})) .pipe(gulp.dest(paths.outDir)); - - return bundle.pipe(mirror(dev, min)); } function rebundle_test(bundler, suffix) { From a27a9b3528631279a21ce3fefae2921da14dddfa Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 12 Jan 2016 16:29:03 +0800 Subject: [PATCH 12/35] add grid layout and finish all the layout fature finish none mode of layout add resize none mode for horizontal and vertical refactor horizontal layout algorithm refactoring add row break logic make row break working rename margin to padding grid layout start axis works refactoring finish y axis grid layout add grid layout resize container feature. add resize container for grid vertical layout fix horizontal layout issue fix some issues when in grid resize container mode finish layout feature --- cocos2d/core/components/CCLayout.js | 425 +++++++++++++++++++++++++--- 1 file changed, 378 insertions(+), 47 deletions(-) diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index c9747ef0d2..fb0d31c9c9 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -41,6 +41,36 @@ var Type = cc.Enum({ * @property {Number} VERTICAL */ VERTICAL: 2, + /** + * @property {Number} GRID + */ + GRID: 3, +}); + +var ResizeType = cc.Enum({ + /** + * @property {Number} NONE + */ + NONE: 0, + /** + * @property {Number} CONTAINER + */ + CONTAINER: 1, + /** + * @property {Number} CHILDREN + */ + CHILDREN: 2 +}); + +var AxisDirection = cc.Enum({ + /** + * @property {Number} HORIZONTAL + */ + HORIZONTAL: 0, + /** + * @property {Number} VERTICAL + */ + VERTICAL: 1, }); /** @@ -110,28 +140,54 @@ var Layout = cc.Class({ tooltip: 'i18n:COMPONENT.layout.layout_type' }, + _resize: ResizeType.NONE, + /** * Whether allow layout to adjust size. * @property {Boolean} autoResize * @default true * @readonly */ - autoResize: { - default: true, - readonly: true, - tooltip: 'i18n:COMPONENT.layout.auto_resize' + resize: { + type: ResizeType, + tooltip: 'i18n:COMPONENT.layout.auto_resize', + get: function() { + return this._resize; + }, + set: function(value) { + if (this.layoutType === Type.NONE && value === ResizeType.CHILDREN) { + return; + } + + this._doLayoutDirty(); + this._resize = value; + } }, + cellSize: { + default: cc.size(40, 40), + type: cc.Size, + notify: function() { + this._doLayoutDirty(); + } + }, + + startAxis: { + default: AxisDirection.HORIZONTAL, + type: AxisDirection, + notify: function() { + this._doLayoutDirty(); + } + }, /** - * The margin of layout, it only effect the layout in one direction. - * @property {Number} margin + * The padding of layout, it only effect the layout in one direction. + * @property {Number} padding */ - margin: { + padding: { default: 0, notify: function() { this._doLayoutDirty(); }, - tooltip: 'i18n:COMPONENT.layout.margin' }, /** @@ -190,7 +246,9 @@ var Layout = cc.Class({ statics: { Type: Type, VerticalDirection: VerticalDirection, - HorizontalDirection: HorizontalDirection + HorizontalDirection: HorizontalDirection, + ResizeType: ResizeType, + AxisDirection: AxisDirection, }, onLoad: function() { @@ -226,71 +284,218 @@ var Layout = cc.Class({ this._doLayoutDirty(); }, - _doLayoutHorizontally: function(layoutAnchor, layoutSize, children) { - var newWidth = 0; + _doLayoutHorizontally: function(baseWidth, rowBreak, fnPositionY, applyChildren) { + var layoutAnchor = this.node.getAnchorPoint(); + var children = this.node.children; + var sign = 1; - - children.forEach(function(child) { - newWidth += child.width; - }); - - newWidth += (children.length - 1) * this.spacingX + 2 * this.margin; - this.node.setContentSize(newWidth, layoutSize.height); - - var leftBoundaryOfLayout = -layoutAnchor.x * newWidth; + var leftBoundaryOfLayout = -layoutAnchor.x * baseWidth; if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { sign = -1; - leftBoundaryOfLayout = (1 - layoutAnchor.x) * newWidth; + leftBoundaryOfLayout = (1 - layoutAnchor.x) * baseWidth; } - var nextX = leftBoundaryOfLayout + sign * this.margin - sign * this.spacingX; + var nextX = leftBoundaryOfLayout + sign * this.padding - sign * this.spacingX; + var rowMaxHeight = 0; + var tempMaxHeight = 0; + var secondMaxHeight = 0; + var row = 0; + var containerResizeBoundary = 0; + + var maxHeightChildAnchor; + + var newChildWidth = this.cellSize.width; + if (this.layoutType !== Type.GRID) { + newChildWidth = (baseWidth - 2 * this.padding - (children.length - 1) * this.spacingX) / children.length; + } children.forEach(function(child) { + //for resizing children + if (this._resize === ResizeType.CHILDREN) { + child.width = newChildWidth; + if (this.layoutType === Type.GRID) { + child.height = this.cellSize.height; + } + } + var anchorX = child.anchorX; + + if (secondMaxHeight > tempMaxHeight) { + tempMaxHeight = secondMaxHeight; + } + + if (child.height >= tempMaxHeight) { + secondMaxHeight = tempMaxHeight; + tempMaxHeight = child.height; + maxHeightChildAnchor = child.getAnchorPoint(); + } + if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { anchorX = 1 - child.anchorX; } nextX = nextX + sign * anchorX * child.width + sign * this.spacingX; + var rightBoundaryOfChild = sign * (1 - anchorX) * child.width; - child.setPosition(cc.p(nextX, child.y)); + if (rowBreak) { + var rowBreakBoundary = nextX + rightBoundaryOfChild + sign * this.padding; + var leftToRightRowBreak = this.horizontalDirection === HorizontalDirection.LEFT_TO_RIGHT && rowBreakBoundary > (1 - layoutAnchor.x) * baseWidth; + var rightToLeftRowBreak = this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT && rowBreakBoundary < -layoutAnchor.x * baseWidth; - nextX += sign * (1 - anchorX) * child.width; + if (leftToRightRowBreak || rightToLeftRowBreak) { + + if (child.height >= tempMaxHeight) { + if (secondMaxHeight === 0) { + secondMaxHeight = tempMaxHeight; + } + rowMaxHeight += secondMaxHeight; + secondMaxHeight = tempMaxHeight; + } + else { + rowMaxHeight += tempMaxHeight; + secondMaxHeight = child.height; + tempMaxHeight = 0; + } + nextX = leftBoundaryOfLayout + sign * (this.padding + anchorX * child.width); + row++; + } + } + + var finalPositionY = fnPositionY(child, rowMaxHeight, row); + if(baseWidth > (child.width + 2 * this.padding)) { + if (applyChildren) { + child.setPosition(cc.p(nextX, finalPositionY)); + } + } + + var signX = 1; + if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { + signX = -1; + } + + var topMarign = (tempMaxHeight === 0) ? child.height : tempMaxHeight; + containerResizeBoundary = finalPositionY + signX * (topMarign * maxHeightChildAnchor.y + this.padding); + + nextX += rightBoundaryOfChild; }.bind(this)); + + return containerResizeBoundary; }, - _doLayoutVertically: function(layoutAnchor, layoutSize, children) { + _getVerticalBaseHeight: function (children) { var newHeight = 0; + if (this.resize === ResizeType.CONTAINER) { + children.forEach(function(child) { + newHeight += child.height; + }); + + newHeight += (children.length - 1) * this.spacingY + 2 * this.padding; + } + else { + newHeight = this.node.getContentSize().height; + } + return newHeight; + }, + + _doLayoutVertically: function(baseHeight, columnBreak, fnPositionX, applyChildren) { + var layoutAnchor = this.node.getAnchorPoint(); + var children = this.node.children; + var sign = 1; - - children.forEach(function(child) { - newHeight += child.height; - }); - - newHeight += (children.length - 1) * this.spacingY + 2 * this.margin; - this.node.setContentSize(layoutSize.width, newHeight); - - var bottomBoundaryOfLayout = -layoutAnchor.y * newHeight; + var bottomBoundaryOfLayout = -layoutAnchor.y * baseHeight; if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { sign = -1; - bottomBoundaryOfLayout = (1 - layoutAnchor.y) * newHeight; + bottomBoundaryOfLayout = (1 - layoutAnchor.y) * baseHeight; } - var nextY = bottomBoundaryOfLayout + sign * this.margin - sign * this.spacingY; + var nextY = bottomBoundaryOfLayout + sign * this.padding - sign * this.spacingY; + var columnMaxWidth = 0; + var tempMaxWidth = 0; + var secondMaxWidth = 0; + var column = 0; + var containerResizeBoundary = 0; + var maxWidthChildAnchor; + + var newChildHeight = this.cellSize.height; + if (this.layoutType !== Type.GRID) { + newChildHeight = (baseHeight - 2 * this.padding - (children.length - 1) * this.spacingY) / children.length; + } children.forEach(function(child) { + //for resizing children + if (this.resize === ResizeType.CHILDREN) { + child.height = newChildHeight; + if (this.layoutType === Type.GRID) { + child.width = this.cellSize.width; + } + } + var anchorY = child.anchorY; + + if (secondMaxWidth > tempMaxWidth) { + tempMaxWidth = secondMaxWidth; + } + + if (child.width >= tempMaxWidth) { + secondMaxWidth = tempMaxWidth; + tempMaxWidth = child.width; + maxWidthChildAnchor = child.getAnchorPoint(); + } + if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { anchorY = 1 - child.anchorY; } nextY = nextY + sign * anchorY * child.height + sign * this.spacingY; + var topBoundaryOfChild = sign * (1 - anchorY) * child.height; - child.setPosition(cc.p(child.x, nextY)); + if (columnBreak) { + var columnBreakBoundary = nextY + topBoundaryOfChild + sign * this.padding; + var bottomToTopColumnBreak = this.verticalDirection === VerticalDirection.BOTTOM_TO_TOP && columnBreakBoundary > (1 - layoutAnchor.y) * baseHeight; + var topToBottomColumnBreak = this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM && columnBreakBoundary < -layoutAnchor.y * baseHeight; - nextY += sign * (1 - anchorY) * child.height; + if (bottomToTopColumnBreak || topToBottomColumnBreak) { + if (child.width >= tempMaxWidth) { + if (secondMaxWidth === 0) { + secondMaxWidth = tempMaxWidth; + } + columnMaxWidth += secondMaxWidth; + secondMaxWidth = tempMaxWidth; + } + else { + columnMaxWidth += tempMaxWidth; + secondMaxWidth = child.width; + tempMaxWidth = 0; + } + nextY = bottomBoundaryOfLayout + sign * (this.padding + anchorY * child.height); + column++; + } + } + + var finalPositionX = fnPositionX(child, columnMaxWidth, column); + if (baseHeight > (child.height + 2 * this.padding)) { + if (applyChildren) { + child.setPosition(cc.p(finalPositionX, nextY)); + } + } + + var signX = 1; + if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { + signX = -1; + } + + //when the item is the last column break item, the tempMaxWidth will be 0. + var rightMarign = (tempMaxWidth === 0) ? child.width : tempMaxWidth; + containerResizeBoundary = finalPositionX + signX * (rightMarign * maxWidthChildAnchor.x + this.padding); + + nextY += topBoundaryOfChild; }.bind(this)); + + return containerResizeBoundary; }, - _doLayoutBasic: function(layoutAnchor, layoutSize, children) { + _doLayoutBasic: function() { + var layoutAnchor = this.node.getAnchorPoint(); + var children = this.node.children; + var allChildrenBoundingBox = null; children.forEach(function(child){ @@ -301,7 +506,7 @@ var Layout = cc.Class({ } }); - if(allChildrenBoundingBox) { + if (allChildrenBoundingBox) { var leftBottomInParentSpace = this.node.parent.convertToNodeSpaceAR(cc.p(allChildrenBoundingBox.x, allChildrenBoundingBox.y)); var rightTopInParentSpace = this.node.parent.convertToNodeSpaceAR(cc.p(allChildrenBoundingBox.x + allChildrenBoundingBox.width, allChildrenBoundingBox.y + allChildrenBoundingBox.height)); @@ -318,17 +523,143 @@ var Layout = cc.Class({ } }, - _doLayout: function() { - var children = this.node.children; + _doLayoutGridAxisHorizontal: function (layoutAnchor, layoutSize) { + var baseWidth = layoutSize.width; + + var sign = 1; + var bottomBoundaryOfLayout = -layoutAnchor.y * layoutSize.height; + + if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { + sign = -1; + bottomBoundaryOfLayout = (1 - layoutAnchor.y) * layoutSize.height; + } + + var fnPositionY = function(child, topOffset, row) { + return bottomBoundaryOfLayout + sign * (topOffset + child.anchorY * child.height + this.padding + row * this.spacingY); + }.bind(this); + + + var newHeight = 0; + if (this.resize === ResizeType.CONTAINER) { + //calculate the new height of container, it won't change the position of it's children + var boundary = this._doLayoutHorizontally(baseWidth, true, fnPositionY, false); + newHeight = bottomBoundaryOfLayout - boundary + if (newHeight < 0) { + newHeight *= -1; + } + + bottomBoundaryOfLayout = -layoutAnchor.y * newHeight; + + if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { + sign = -1; + bottomBoundaryOfLayout = (1 - layoutAnchor.y) * newHeight; + } + } + + this._doLayoutHorizontally(baseWidth, true, fnPositionY, true); + + if (this.resize == ResizeType.CONTAINER) { + this.node.setContentSize(baseWidth, newHeight); + } + }, + + _doLayoutGridAxisVertical: function (layoutAnchor, layoutSize) { + var baseHeight = layoutSize.height; + + var sign = 1; + var leftBoundaryOfLayout = -layoutAnchor.x * layoutSize.width; + + if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { + sign = -1; + leftBoundaryOfLayout = (1 - layoutAnchor.x) * layoutSize.width; + } + + var fnPositionX = function(child, leftOffset, column) { + return leftBoundaryOfLayout + sign * (leftOffset + child.anchorX * child.width + this.padding + column * this.spacingX); + }.bind(this); + + var newWidth = 0; + if (this.resize === ResizeType.CONTAINER) { + var boundary = this._doLayoutVertically(baseHeight, true, fnPositionX, false); + newWidth = leftBoundaryOfLayout - boundary; + if (newWidth < 0) { + newWidth *= -1; + } + + leftBoundaryOfLayout = -layoutAnchor.x * newWidth; + + if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { + sign = -1; + leftBoundaryOfLayout = (1 - layoutAnchor.x) * newWidth; + } + } + + this._doLayoutVertically(baseHeight, true, fnPositionX, true); + + if (this.resize === ResizeType.CONTAINER) { + this.node.setContentSize(newWidth, baseHeight); + } + }, + + _doLayoutGrid: function() { var layoutAnchor = this.node.getAnchorPoint(); var layoutSize = this.node.getContentSize(); + if (this.startAxis === AxisDirection.HORIZONTAL) { + this._doLayoutGridAxisHorizontal(layoutAnchor, layoutSize); + + } + else if(this.startAxis === AxisDirection.VERTICAL) { + this._doLayoutGridAxisVertical(layoutAnchor, layoutSize); + } + + }, + + _getHorizontalBaseWidth: function (children) { + var newWidth = 0; + if (this.resize === ResizeType.CONTAINER) { + children.forEach(function(child) { + newWidth += child.width; + }); + newWidth += (children.length - 1) * this.spacingX + 2 * this.padding; + } + else { + newWidth = this.node.getContentSize().width; + } + return newWidth; + }, + + _doLayout: function() { + if (this.layoutType === Type.HORIZONTAL) { - this._doLayoutHorizontally(layoutAnchor, layoutSize, children); - } else if (this.layoutType === Type.VERTICAL) { - this._doLayoutVertically(layoutAnchor, layoutSize, children); - } else if (this.layoutType === Type.NONE) { - this._doLayoutBasic(layoutAnchor, layoutSize, children); + var newWidth = this._getHorizontalBaseWidth(this.node.children); + + var fnPositionY = function(child, topOffset) { + return child.y; + }; + + this._doLayoutHorizontally( newWidth, false, fnPositionY, true); + + this.node.width = newWidth; + } + else if (this.layoutType === Type.VERTICAL) { + var newHeight = this._getVerticalBaseHeight(this.node.children); + + var fnPositionX = function(child, leftOffset) { + return child.x; + }; + + this._doLayoutVertically(newHeight, false, fnPositionX, true); + + this.node.height = newHeight; + } + else if (this.layoutType === Type.NONE) { + if (this.resize === ResizeType.CONTAINER) { + this._doLayoutBasic(); + } + } + else if(this.layoutType === Type.GRID) { + this._doLayoutGrid(); } }, From 9b12b53ee7a4c2c0fb0a476f74aa9f741a390e5e Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 14 Jan 2016 19:01:19 +0800 Subject: [PATCH 13/35] Fixed indexOf issue on Baidu browser, damn you baidu browser guys ! --- cocos2d/core/platform/callbacks-invoker.js | 4 ++-- cocos2d/core/platform/js.js | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index 17e62e82ee..ae304688cc 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -76,7 +76,7 @@ CallbacksHandler.prototype.has = function (key, callback, target) { if (callbackTarget === target) { return true; } - index = list.indexOf(callback, index + 1); + index = cc.js.array.indexOf.call(list, callback, index + 1); } // callback given but not found return false; @@ -131,7 +131,7 @@ CallbacksHandler.prototype.remove = function (key, callback, target) { break; } - index = list.indexOf(callback, index + 1); + index = cc.js.array.indexOf.call(list, callback, index + 1); } return true; } diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index 1bb5289d85..25e6a43fd0 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -628,7 +628,9 @@ js.array = { for (i = 0; i < len; i += 1) arr_clone[i] = array[i]; return arr_clone; - } + }, + + indexOf: Array.prototype.indexOf, }; cc.js = js; From 7932d986e8a8c6154a636b4fea588bdbf93a9077 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 14 Jan 2016 21:49:07 +0800 Subject: [PATCH 14/35] Add documentation to accuse Baidu developer --- cocos2d/core/platform/js.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index 25e6a43fd0..b91df6bbfb 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -630,6 +630,22 @@ js.array = { return arr_clone; }, + /** + * Exact same function as Array.prototype.indexOf. + * HACK: ugliy hack for Baidu mobile browser compatibility, + * stupid Baidu guys modify Array.prototype.indexOf for all pages loaded, + * their version changes strict comparison to non-strict comparison, + * it also ignores the second parameter of the original API, + * and this will cause event handler enter infinite loop. + * Baidu developers, if you ever see this documentation, + * here is the standard: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf + * Seriously ! + * + * @method indexOf + * @param {any} searchElement Element to locate in the array. + * @param {Number} [fromIndex=0] The index to start the search at + * @return {Number} returns the first index at which a given element can be found in the array, or -1 if it is not present. + */ indexOf: Array.prototype.indexOf, }; From 60a163f42b37d3a3ff7fe441f3373772a6ec8920 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Fri, 15 Jan 2016 10:47:18 +0800 Subject: [PATCH 15/35] add missing docs --- cocos2d/core/components/CCLayout.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index fb0d31c9c9..da6bf46e66 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -47,6 +47,10 @@ var Type = cc.Enum({ GRID: 3, }); +/** + * Enum for Layout Resize Type + * @enum Layout.ResizeType + */ var ResizeType = cc.Enum({ /** * @property {Number} NONE @@ -62,6 +66,10 @@ var ResizeType = cc.Enum({ CHILDREN: 2 }); +/** + * Enum for Grid Layout start axis direction. + * @enum Layout.AxisDirection + */ var AxisDirection = cc.Enum({ /** * @property {Number} HORIZONTAL @@ -143,10 +151,10 @@ var Layout = cc.Class({ _resize: ResizeType.NONE, /** - * Whether allow layout to adjust size. - * @property {Boolean} autoResize - * @default true - * @readonly + * The are three resize types for Layout. + * None, resize Container and resize children. + * @property {Layout.ResizeType} resize + * @default ResizeType.NONE */ resize: { type: ResizeType, @@ -164,6 +172,11 @@ var Layout = cc.Class({ } }, + /** + * The cell size for grid layout. + * @property {cc.Size} cellSize + * @default cc.size(40, 40) + */ cellSize: { default: cc.size(40, 40), type: cc.Size, @@ -172,6 +185,11 @@ var Layout = cc.Class({ } }, + /** + * The start axis for grid layout. If you choose horizontal, then children will layout horizontally at first, + * and then break line on demand. Choose vertical if you want to layout vertically at first . + * @property {Layout.AxisDirection} startAxis + */ startAxis: { default: AxisDirection.HORIZONTAL, type: AxisDirection, From 5e5459af3bb4afccb1071f0b4b20728f0f449306 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 15 Jan 2016 11:10:00 +0800 Subject: [PATCH 16/35] fix label does not work on sougou browser --- .../core/label/CCSGLabelCanvasRenderCmd.js | 95 +++++++++---------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index 346bb295fc..bd0503e323 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -29,12 +29,12 @@ http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) http://www.angelcode.com/products/bmfont/ (Free, Windows only) ****************************************************************************/ -(function() { - _ccsg.Label.TTFLabelBaker = function() { +(function () { + _ccsg.Label.TTFLabelBaker = function () { }; var proto = _ccsg.Label.TTFLabelBaker.prototype = Object.create(Object.prototype); - proto._getLineHeight = function() { + proto._getLineHeight = function () { //todo refine it var nodeSpacingY = this._node.getLineHeight(); if (nodeSpacingY === 0) { @@ -45,7 +45,7 @@ return nodeSpacingY | 0; }; - proto._prepareQuad = function() { + proto._prepareQuad = function () { var quad = this._quad; var white = cc.color(255, 255, 255, this._displayedOpacity); var width = this._node._contentSize.width; @@ -84,7 +84,7 @@ var wrappedWords = []; var text = strArr; var allWidth = ctx.measureText(text).width; - while(allWidth > maxWidth && text.length > 1){ + while (allWidth > maxWidth && text.length > 1) { var fuzzyLen = text.length * ( maxWidth / allWidth ) | 0; var tmpText = text.substr(fuzzyLen); @@ -151,14 +151,14 @@ text = sLine || tmpText; allWidth = ctx.measureText(text).width; } - if(text.length > 0) { + if (text.length > 0) { wrappedWords.push(text); } - + return wrappedWords; }; - - proto._updateDisplayOpacity = function(parentOpacity) { + + proto._updateDisplayOpacity = function (parentOpacity) { _ccsg.Node.RenderCmd.prototype._updateDisplayOpacity.call(this, parentOpacity); //specify opacity to quad var color = cc.color(255, 255, 255, this._displayedOpacity); @@ -169,14 +169,14 @@ quad._tr.colors = color; this._quadDirty = true; }; - + proto._updateDisplayColor = function (parentColor) { _ccsg.Node.RenderCmd.prototype._updateDisplayColor.call(this, parentColor); var node = this._node; node._labelSkinDirty = true; }; - - proto._bakeLabel = function() { + + proto._bakeLabel = function () { var node = this._node; this._drawFontsize = node._fontSize; var ctx = this._labelContext; @@ -220,7 +220,7 @@ canvasSizeY = this._splitedStrings.length * this._getLineHeight(); node.setContentSize(cc.size(canvasSizeX, canvasSizeY)); } - } else if(_ccsg.Label.Overflow.SHRINK === node._overFlow) { + } else if (_ccsg.Label.Overflow.SHRINK === node._overFlow) { this._splitedStrings = paragraphedStrings; //shrink if (node._isWrapText) { @@ -315,14 +315,14 @@ this._labelTexture.handleLoadedTexture(); }; - proto._rebuildLabelSkin = function() { + proto._rebuildLabelSkin = function () { var node = this._node; if (node._labelSkinDirty) { - if(node._labelType === _ccsg.Label.Type.TTF || - node._labelType === _ccsg.Label.Type.SystemFont){ + if (node._labelType === _ccsg.Label.Type.TTF || + node._labelType === _ccsg.Label.Type.SystemFont) { this._bakeLabel(); this._prepareQuad(); - }else { + } else { node._updateContent(); } this._node._labelSkinDirty = false; @@ -330,8 +330,8 @@ }; })(); -(function() { - _ccsg.Label.CanvasRenderCmd = function(renderableObject) { +(function () { + _ccsg.Label.CanvasRenderCmd = function (renderableObject) { _ccsg.Node.CanvasRenderCmd.call(this, renderableObject); this._needDraw = true; this._labelTexture = new cc.Texture2D(); @@ -351,13 +351,13 @@ proto.constructor = _ccsg.Label.CanvasRenderCmd; - proto.rendering = function(ctx, scaleX, scaleY) { + proto.rendering = function (ctx, scaleX, scaleY) { this._rebuildLabelSkin(); var node = this._node; if (node._labelType === _ccsg.Label.Type.TTF || - node._labelType === _ccsg.Label.Type.SystemFont) { + node._labelType === _ccsg.Label.Type.SystemFont) { var locDisplayOpacity = this._displayedOpacity; var alpha = locDisplayOpacity / 255; //var locTexture = this._labelTexture; @@ -371,41 +371,36 @@ wrapper.setGlobalAlpha(alpha); if (this._labelTexture) { + var sx, sy, sw, sh; + var x, y, w, h; - var quad = this._quad; { - var sx, sy, sw, sh; - var x, y, w, h; + x = 0; + y = 0; + w = this._node._contentSize.width; + h = this._node._contentSize.height; - x = quad._bl.vertices.x; - y = quad._bl.vertices.y; - w = quad._tr.vertices.x - quad._bl.vertices.x; - h = quad._tr.vertices.y - quad._bl.vertices.y; - y = -y - h; + var textureWidth = this._labelTexture.getPixelWidth(); + var textureHeight = this._labelTexture.getPixelHeight(); - var textureWidth = this._labelTexture.getPixelWidth(); - var textureHeight = this._labelTexture.getPixelHeight(); + sx = 0; + sy = 0; + sw = textureWidth; + sh = textureHeight; - sx = quad._bl.texCoords.u * textureWidth; - sy = quad._bl.texCoords.v * textureHeight; - sw = (quad._tr.texCoords.u - quad._bl.texCoords.u) * textureWidth; - sh = (quad._tr.texCoords.v - quad._bl.texCoords.v) * textureHeight; + x = x * scaleX; + y = y * scaleY; + w = w * scaleX; + h = h * scaleY; - x = x * scaleX; - y = y * scaleY; - w = w * scaleX; - h = h * scaleY; - - var image = this._labelTexture._htmlElementObj; - if (this._labelTexture._pattern !== "") { - wrapper.setFillStyle(context.createPattern(image, this._labelTexture._pattern)); - context.fillRect(x, y, w, h); - } else { - context.drawImage(image, - sx, sy, sw, sh, - x, y, w, h); - } + var image = this._labelTexture._htmlElementObj; + if (this._labelTexture._pattern !== "") { + wrapper.setFillStyle(context.createPattern(image, this._labelTexture._pattern)); + context.fillRect(x, y, w, h); + } else { + context.drawImage(image, + sx, sy, sw, sh, + x, y, w, h); } - } cc.g_NumberOfDraws = cc.g_NumberOfDraws + 1; } From 0e389f2b5523bd0ef529b92cde3934b93412f146 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 15 Jan 2016 12:40:46 +0800 Subject: [PATCH 17/35] fix label and sprite canvas rendering --- cocos2d/core/label/CCSGLabelCanvasRenderCmd.js | 3 ++- cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index bd0503e323..867a60ec23 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -375,10 +375,11 @@ var x, y, w, h; x = 0; - y = 0; + y = -this._node._contentSize.height; w = this._node._contentSize.width; h = this._node._contentSize.height; + var textureWidth = this._labelTexture.getPixelWidth(); var textureHeight = this._labelTexture.getPixelHeight(); diff --git a/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js b/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js index 65b0281fe7..5e13a0b466 100644 --- a/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js +++ b/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js @@ -71,7 +71,7 @@ var wrapper = ctx || cc._renderContext, context = wrapper.getContext(); wrapper.setTransform(this._worldTransform, scaleX, scaleY); - wrapper.setCompositeOperation(node._blendFunc); + wrapper.setCompositeOperation(_ccsg.Node.CanvasRenderCmd._getCompositeOperationByBlendFunc(node._blendFunc)); wrapper.setGlobalAlpha(alpha); if(this._textureToRender) { @@ -94,10 +94,10 @@ var textureWidth = this._textureToRender.getPixelWidth(); var textureHeight = this._textureToRender.getPixelHeight(); - sx = quads[i]._bl.texCoords.u * textureWidth; - sy = quads[i]._bl.texCoords.v * textureHeight; + sx = quads[i]._tl.texCoords.u * textureWidth; + sy = quads[i]._tl.texCoords.v * textureHeight; sw = (quads[i]._tr.texCoords.u - quads[i]._bl.texCoords.u) * textureWidth; - sh = (quads[i]._tr.texCoords.v - quads[i]._bl.texCoords.v) * textureHeight; + sh = (quads[i]._bl.texCoords.v - quads[i]._tr.texCoords.v) * textureHeight; x = x * scaleX; y = y * scaleY; From b5dffdbed27dcbe63568d550bd2318d1c586ed55 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 15 Jan 2016 15:39:05 +0800 Subject: [PATCH 18/35] ...... --- cocos2d/core/label/CCSGLabelCanvasRenderCmd.js | 8 +++++--- cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index 867a60ec23..0a29de15e5 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -398,9 +398,11 @@ wrapper.setFillStyle(context.createPattern(image, this._labelTexture._pattern)); context.fillRect(x, y, w, h); } else { - context.drawImage(image, - sx, sy, sw, sh, - x, y, w, h); + if(sw !== 0 && sh !== 0 && w !== 0 && h !== 0) { + context.drawImage(image, + sx, sy, sw, sh, + x, y, w, h); + } } } cc.g_NumberOfDraws = cc.g_NumberOfDraws + 1; diff --git a/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js b/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js index 5e13a0b466..36844c4d72 100644 --- a/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js +++ b/cocos2d/core/sprites/CCScale9SpriteCanvasRenderCmd.js @@ -109,9 +109,11 @@ wrapper.setFillStyle(context.createPattern(image, this._textureToRender._pattern)); context.fillRect(x, y, w, h); } else { - context.drawImage(image, - sx, sy, sw, sh, - x, y, w, h); + if(sw !== 0 && sh !== 0 && w !== 0 && h !== 0) { + context.drawImage(image, + sx, sy, sw, sh, + x, y, w, h); + } } } From 1dbf6d3f43720c1d6fe2c219a0465d29f445f89f Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 15 Jan 2016 16:13:40 +0800 Subject: [PATCH 19/35] Improve doc and code hint --- cocos2d/core/CCDirector.js | 2 +- cocos2d/core/platform/CCEGLView.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index aadfe24eab..a398ab8e16 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -64,7 +64,7 @@ cc.g_NumberOfDraws = 0; * - Scheduled timers & drawing are synchronizes with the refresh rate of the display
* - Only supports animation intervals of 1/60 1/30 & 1/15
*

- * @class Director + * @class director */ cc.Director = Class.extend(/** @lends cc.Director# */{ //Variables diff --git a/cocos2d/core/platform/CCEGLView.js b/cocos2d/core/platform/CCEGLView.js index aa7352d783..d633b53603 100644 --- a/cocos2d/core/platform/CCEGLView.js +++ b/cocos2d/core/platform/CCEGLView.js @@ -114,7 +114,7 @@ switch(cc.__BrowserGetter.adaptationType){ * the standard way to use it is by calling:
* - cc.view.methodName();
* - * @class EGLView + * @class view * @extends _Class */ cc.EGLView = cc._Class.extend(/** @lends cc.view# */{ From 9bb7bbdea0ac866a566826500e1613981b9a32b2 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Fri, 15 Jan 2016 16:55:58 +0800 Subject: [PATCH 20/35] sprite should not display if disabled in scene --- cocos2d/core/components/CCSprite.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 686527795b..0dffcf95c3 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -372,6 +372,10 @@ var Sprite = cc.Class({ _createSgNode: function () { var sgNode = new cc.Scale9Sprite(); + if ( !this.enabledInHierarchy ) { + sgNode.setVisible(false); + } + this._applySpriteFrame(sgNode, null); // should keep the size of the sg node the same as entity, From 697d585e399e745e33f5c22efd09c2efdb473d13 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Sun, 17 Jan 2016 12:03:42 +0800 Subject: [PATCH 21/35] Fix bmfont texture not handled in Canvas --- cocos2d/core/textures/CCTexture2D.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/textures/CCTexture2D.js b/cocos2d/core/textures/CCTexture2D.js index fdc68fbcb2..8dbe25b7d7 100644 --- a/cocos2d/core/textures/CCTexture2D.js +++ b/cocos2d/core/textures/CCTexture2D.js @@ -281,12 +281,13 @@ var Texture2D = cc.Class(/** @lends cc.Texture2D# */{ */ handleLoadedTexture: function () { var self = this; - if (self._textureLoaded) return; if (!self._htmlElementObj) { var img = cc.loader.getRes(self.url); if (!img) return; self.initWithElement(img); } + if (!self._htmlElementObj.width || !self._htmlElementObj.height) + return; var locElement = self._htmlElementObj; self._contentSize.width = locElement.width; From 39d348da28ff0cc6e0644917bab8cdbd459f6212 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Sun, 17 Jan 2016 18:48:24 +0800 Subject: [PATCH 22/35] make code about CCClass more concise --- cocos2d/core/platform/CCClass.js | 306 ++++++++-------------- cocos2d/core/platform/attribute.js | 20 -- cocos2d/core/platform/preprocess-attrs.js | 18 +- test/qunit/unit/test-class.js | 12 + 4 files changed, 137 insertions(+), 219 deletions(-) diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index aed538dc4b..0bfaada657 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -49,7 +49,7 @@ var INVALID_STATICS = CC_DEV && ['name', '__ctors__', '__props__', 'arguments', // * both getter and prop must register the name into __props__ array // * @param {String} name - prop name // */ -var _appendProp = function (cls, name/*, isGetter*/) { +function appendProp (cls, name/*, isGetter*/) { if (CC_DEV) { //var JsVarReg = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/; //if (!JsVarReg.test(name)) { @@ -66,127 +66,118 @@ var _appendProp = function (cls, name/*, isGetter*/) { if (index < 0) { cls.__props__.push(name); } -}; +} -var _metaClass = { - - prop: function (name, defaultValue, attribute) { - 'use strict'; - if (CC_DEV) { - // check default object value - if (typeof defaultValue === 'object' && defaultValue) { - if (Array.isArray(defaultValue)) { - // check array empty - if (defaultValue.length > 0) { - cc.error('Default array must be empty, set default value of %s.%s to [], ' + - 'and initialize in "onLoad" or "ctor" please. (just like "this.%s = [...];")', - JS.getClassName(this), name, name); - return this; - } - } - else if (!_isPlainEmptyObj_DEV(defaultValue)) { - // check cloneable - if (!_cloneable_DEV(defaultValue)) { - cc.error('Do not set default value to non-empty object, ' + - 'unless the object defines its own "clone" function. Set default value of %s.%s to null or {}, ' + - 'and initialize in "onLoad" or "ctor" please. (just like "this.%s = {foo: bar};")', - JS.getClassName(this), name, name); - return this; - } +function defineProp (cls, className, propName, defaultValue, attrs) { + if (CC_DEV) { + // check default object value + if (typeof defaultValue === 'object' && defaultValue) { + if (Array.isArray(defaultValue)) { + // check array empty + if (defaultValue.length > 0) { + cc.error('Default array must be empty, set default value of %s.%s to [], ' + + 'and initialize in "onLoad" or "ctor" please. (just like "this.%s = [...];")', + className, propName, propName); + return; } } - - // check base prototype to avoid name collision - for (var base = this.$super; base; base = base.$super) { - // 这个循环只能检测到最上面的FireClass的父类,如果再上还有父类,将不做检测。 - if (base.prototype.hasOwnProperty(name)) { - cc.error('Can not declare %s.%s, it is already defined in the prototype of %s', - JS.getClassName(this), name, JS.getClassName(base)); + else if (!_isPlainEmptyObj_DEV(defaultValue)) { + // check cloneable + if (!_cloneable_DEV(defaultValue)) { + cc.error('Do not set default value to non-empty object, ' + + 'unless the object defines its own "clone" function. Set default value of %s.%s to null or {}, ' + + 'and initialize in "onLoad" or "ctor" please. (just like "this.%s = {foo: bar};")', + className, propName, propName); return; } } } - // set default value - Attr.attr(this, name, { 'default': defaultValue }); - - _appendProp(this, name); - - // apply attributes - if (attribute) { - var onAfterProp = null; - var AttrArgStart = 2; - for (var i = AttrArgStart; i < arguments.length; i++) { - var attr = arguments[i]; - Attr.attr(this, name, attr); - // register callback - if (attr._onAfterProp) { - onAfterProp = onAfterProp || []; - onAfterProp.push(attr._onAfterProp); - } - } - // call callback - if (onAfterProp) { - for (var c = 0; c < onAfterProp.length; c++) { - onAfterProp[c](this, name); - } + // check base prototype to avoid name collision + for (var base = cls.$super; base; base = base.$super) { + // 这个循环只能检测到最上面的FireClass的父类,如果再上还有父类,将不做检测。 + if (base.prototype.hasOwnProperty(propName)) { + cc.error('Can not declare %s.%s, it is already defined in the prototype of %s', + className, propName, className); + return; } } - return this; - }, + } - get: function (name, getter, attribute) { - 'use strict'; + // set default value + Attr.attr(cls, propName, { 'default': defaultValue }); - if (CC_DEV) { - var d = Object.getOwnPropertyDescriptor(this.prototype, name); - if (d && d.get) { - cc.error('%s: the getter of "%s" is already defined!', JS.getClassName(this), name); - return this; + appendProp(cls, propName); + + // apply attributes + if (attrs) { + var onAfterProp = null; + for (var i = 0; i < attrs.length; i++) { + var attr = attrs[i]; + Attr.attr(cls, propName, attr); + // register callback + if (attr._onAfterProp) { + onAfterProp = onAfterProp || []; + onAfterProp.push(attr._onAfterProp); } } + // call callback + if (onAfterProp) { + for (var c = 0; c < onAfterProp.length; c++) { + onAfterProp[c](cls, propName); + } + } + } +} - if (attribute) { - var AttrArgStart = 2; - for (var i = AttrArgStart; i < arguments.length; i++) { - var attr = arguments[i]; - if (CC_DEV) { - if (attr._canUsedInGetter === false) { - cc.error('Can not apply the specified attribute to the getter of "%s.%s", attribute index: %s', - JS.getClassName(this), name, (i - AttrArgStart)); - continue; - } +function defineGetSet (cls, name, propName, val, attrs) { + var getter = val.get; + var setter = val.set; + var proto = cls.prototype; + var d = Object.getOwnPropertyDescriptor(proto, propName); + + if (getter) { + if (d && d.get && CC_DEV) { + cc.error('"%s": the getter of "%s" is already defined!', name, propName); + return; + } + + if (attrs) { + for (var i = 0; i < attrs.length; ++i) { + var attr = attrs[i]; + if (attr._canUsedInGetter === false && CC_DEV) { + cc.error('Can not apply the specified attribute to the getter of "%s.%s", ' + + 'attribute index: %s', name, propName, i); + continue; } - Attr.attr(this, name, attr); + Attr.attr(cls, propName, attr); - if (CC_DEV) { - // check attributes - if (attr.serializable === false || attr.editorOnly === true) { - cc.warn('No need to use "serializable: false" or "editorOnly: true" for the getter of %s.%s, ' + - 'every getter is actually non-serialized.', - JS.getClassName(this), name); - } + // check attributes + if ((attr.serializable === false || attr.editorOnly === true) && CC_DEV) { + cc.warn('No need to use "serializable: false" or "editorOnly: true" for ' + + 'the getter of "%s.%s", every getter is actually non-serialized.', + name, propName); } } } - var forceSerializable = false; - if ( !forceSerializable ) { - Attr.attr(this, name, Attr.NonSerialized); + var ForceSerializable = false; + if (!ForceSerializable) { + Attr.attr(cls, propName, Attr.NonSerialized); } - if (forceSerializable || CC_DEV) { + if (ForceSerializable || CC_DEV) { // 不论是否 hide in inspector 都要添加到 props,否则 asset watcher 不能正常工作 - _appendProp(this, name/*, true*/); + appendProp(cls, propName/*, true*/); } - if (Object.getOwnPropertyDescriptor(this.prototype, name)) { - Object.defineProperty(this.prototype, name, { + if (d) { + Object.defineProperty(proto, propName, { get: getter }); } else { - Object.defineProperty(this.prototype, name, { + Object.defineProperty(proto, propName, { get: getter, configurable: true, enumerable: true @@ -194,57 +185,39 @@ var _metaClass = { } if (CC_DEV) { - Attr.attr(this, name, {hasGetter: true}); // 方便 editor 做判断 + Attr.attr(cls, propName, {hasGetter: true}); // 方便 editor 做判断 } - return this; - }, + } - set: function (name, setter) { + if (setter) { if (CC_DEV) { - var d = Object.getOwnPropertyDescriptor(this.prototype, name); if (d && d.set) { - cc.error('%s: the setter of "%s" is already defined!', JS.getClassName(this), name); - return this; + return cc.error('"%s": the setter of "%s" is already defined!', name, propName); } - } - if (CC_DEV) { - Object.defineProperty(this.prototype, name, { + Object.defineProperty(proto, propName, { set: setter, configurable: true, enumerable: true }); - Attr.attr(this, name, { hasSetter: true }); // 方便 editor 做判断 + Attr.attr(cls, propName, {hasSetter: true}); // 方便 editor 做判断 } else { - if (Object.getOwnPropertyDescriptor(this.prototype, name)) { - Object.defineProperty(this.prototype, name, { + if (d) { + Object.defineProperty(proto, propName, { set: setter }); } else { - Object.defineProperty(this.prototype, name, { + Object.defineProperty(proto, propName, { set: setter, configurable: true, enumerable: true }); } } - - return this; - }, - - /** - * Create a new Class that inherits from this Class - * @param {Object} options - * @return {Function} - * @deprecated - */ - extend: function (options) { - options.extends = this; - return CCClass(options); } -}; +} function getDefault (defaultVal) { if (typeof defaultVal === 'function') { @@ -336,13 +309,14 @@ cc.isChildClassOf = function (subclass, superclass) { function doDefine (className, baseClass, mixins, constructor, options) { var fireClass = _createCtor(constructor, baseClass, mixins, className, options); - // occupy some non-inherited static members - for (var staticMember in _metaClass) { - Object.defineProperty(fireClass, staticMember, { - value: _metaClass[staticMember], - writable: true - }); - } + // (NON-INHERITED) Create a new Class that inherits from this Class + Object.defineProperty(fireClass, 'extend', { + value: function (options) { + options.extends = this; + return CCClass(options); + }, + writable: true + }); if (baseClass) { // inherit @@ -443,7 +417,7 @@ function normalizeClassName (className) { var DefaultName = 'CCClass'; if (className) { className = className.replace(/\./g, '_'); - className = className.split('').filter(function (x) { return /^[a-zA-Z0-9_$]/.test(x) }).join(''); + className = className.split('').filter(function (x) { return /^[a-zA-Z0-9_$]/.test(x); }).join(''); if (/^[0-9]/.test(className[0])) { className = '_' + className; } @@ -558,8 +532,8 @@ function _createCtor (ctor, baseClass, mixins, className, options) { Object.defineProperty(fireClass, '__ctors__', { value: ctors.length > 0 ? ctors : null, - writable: false, - enumerable: false + // writable should be false, + // enumerable should be false }); if (shouldAddProtoCtor && CC_EDITOR) { @@ -695,7 +669,7 @@ function CCClass (options) { name = cc.js.getClassName(cls); } - // define properties + // define Properties var properties = options.properties; if (properties) { @@ -706,23 +680,10 @@ function CCClass (options) { var val = properties[propName]; var attrs = parseAttributes(val, name, propName); if ('default' in val) { - cls.prop.apply(cls, [propName, val.default].concat(attrs)); + defineProp(cls, name, propName, val.default, attrs); } else { - var getter = val.get; - var setter = val.set; - if (CC_DEV) { - if (!getter && !setter) { - cc.error('Property %s.%s must define at least one of "default", "get" or "set".', name, - propName); - } - } - if (getter) { - cls.get.apply(cls, [propName, getter].concat(attrs)); - } - if (setter) { - cls.set(propName, setter); - } + defineGetSet(cls, name, propName, val, attrs); } } } @@ -789,31 +750,17 @@ function CCClass (options) { * @private */ CCClass._isCCClass = function (constructor) { - return !!constructor && (constructor.prop === _metaClass.prop); + return !!constructor && typeof constructor.__ctors__ !== 'undefined'; }; -// -// @method _convertToFireClass -// @param {Function} constructor -// @private -// -//CCClass._convertToFireClass = function (constructor) { -// constructor.prop = _metaClass.prop; -//}; - -// Optimized define function only for internal base classes -// -// @param {String} className -// @param {Function} constructor -// @param {string[]} serializableFields -// @private -function fastDefine (className, constructor, serializableFields) { +// Optimized define function only for internal classes +CCClass._fastDefine = function (className, constructor, serializableFields) { JS.setClassName(className, constructor); constructor.__props__ = serializableFields; for (var i = 0; i < serializableFields.length; i++) { Attr.attr(constructor, serializableFields[i], { visible: false }); } -} +}; CCClass.attr = Attr.attr; @@ -960,38 +907,9 @@ function parseAttributes (attrs, className, propName) { } } - var nullable = attrs.nullable; - if (nullable) { - if (typeof nullable === 'object') { - var boolPropName = nullable.propName; - if (typeof boolPropName === 'string') { - var def = nullable.default; - if (typeof def === 'boolean') { - result.push(Attr.Nullable(boolPropName, def)); - } - else if (CC_DEV) { - cc.error(ERR_Type, '"default"', 'nullable object', 'boolean'); - } - } - else if (CC_DEV) { - cc.error(ERR_Type, '"propName"', 'nullable object', 'string'); - } - } - else if (CC_DEV) { - cc.error(ERR_Type, '"nullable"', className + '.' + propName, 'object'); - } - } - return result; } -/** - * @param {Object} options - * @return {Function} - * @deprecated - */ -CCClass.extend = CCClass; - cc.Class = CCClass; module.exports = { @@ -1000,5 +918,5 @@ module.exports = { defaultVal = getDefault(defaultVal); return Array.isArray(defaultVal); }, - fastDefine: fastDefine + fastDefine: CCClass._fastDefine }; diff --git a/cocos2d/core/platform/attribute.js b/cocos2d/core/platform/attribute.js index 7419870b0c..00ae84aed7 100644 --- a/cocos2d/core/platform/attribute.js +++ b/cocos2d/core/platform/attribute.js @@ -287,25 +287,6 @@ function RawType (typename) { }; } -function Nullable (boolPropName, hasValueByDefault) { - return { - nullable: boolPropName, - - _onAfterProp: function (constructor, mainPropName) { - // declare boolean - constructor.prop(boolPropName, hasValueByDefault, { visible: false }); - // copy attributes from main property - var mainPropAttr = cc.Class.attr(constructor, mainPropName) || {}; - if (mainPropAttr.serializable === false) { - cc.Class.attr(constructor, boolPropName, NonSerialized); - } - else if (mainPropAttr.editorOnly) { - cc.Class.attr(constructor, boolPropName, EditorOnly); - } - } - }; -} - function Range (min, max) { return { min: min, max: max }; } @@ -318,6 +299,5 @@ module.exports = { ObjectType: ObjectType, RawType: RawType, ScriptUuid: {}, // the value will be represented as a uuid string - Nullable: Nullable, Range: Range }; diff --git a/cocos2d/core/platform/preprocess-attrs.js b/cocos2d/core/platform/preprocess-attrs.js index 3241821cf8..ad42002404 100644 --- a/cocos2d/core/platform/preprocess-attrs.js +++ b/cocos2d/core/platform/preprocess-attrs.js @@ -193,12 +193,20 @@ module.exports = function (properties, className, cls) { properties[propName] = val; } if (val) { - if (CC_EDITOR && 'default' in val) { - if (val.get) { - cc.error('The "default" value of "%s.%s" should not be used with a "get" function.', className, propName); + if (CC_EDITOR) { + if ('default' in val) { + if (val.get) { + cc.error('The "default" value of "%s.%s" should not be used with a "get" function.', + className, propName); + } + else if (val.set) { + cc.error('The "default" value of "%s.%s" should not be used with a "set" function.', + className, propName); + } } - else if (val.set) { - cc.error('The "default" value of "%s.%s" should not be used with a "set" function.', className, propName); + else if (!val.get && !val.set) { + cc.error('Property "%s.%s" must define at least one of "default", "get" or "set".', + className, propName); } } if (!val.override && cls.__props__.indexOf(propName) !== -1 && CC_DEV) { diff --git a/test/qunit/unit/test-class.js b/test/qunit/unit/test-class.js index 8d9db2d527..ae11f5d237 100644 --- a/test/qunit/unit/test-class.js +++ b/test/qunit/unit/test-class.js @@ -411,6 +411,18 @@ test('statics', function () { cc.js.unregisterClass(Animal, Dog, Labrador); }); +test('_isCCClass', function () { + strictEqual(cc.Class._isCCClass(cc.Class({})), true, 'should be CCClass'); + + function ctor () { + this.foo = 0; + } + cc.Class._fastDefine('T', ctor, ['foo']); + strictEqual(cc.Class._isCCClass(ctor), false, 'fastDefined ctor should not recognized as CCClass'); + + cc.js.unregisterClass(ctor); +}); + test('try catch', function () { var originThrow = cc._throw; From 3c764f5d98c8cfa6b5ff29c717a739cf435a8f19 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Mon, 18 Jan 2016 10:56:39 +0800 Subject: [PATCH 23/35] scale 9 works on both trimmed content size and raw content size mode --- cocos2d/core/sprites/CCScale9Sprite.js | 49 +++++++++++++++++++++----- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/sprites/CCScale9Sprite.js b/cocos2d/core/sprites/CCScale9Sprite.js index 0f1c2483be..1003a89956 100644 --- a/cocos2d/core/sprites/CCScale9Sprite.js +++ b/cocos2d/core/sprites/CCScale9Sprite.js @@ -51,10 +51,10 @@ EventTarget = require("../cocos2d/core/event/event-target"); * @property {Number} insetBottom - The bottom inset of the 9-slice sprite */ cc.SimpleQuadGenerator = { - _rebuildQuads_base : function (spriteFrame, contentSize, colorOpacity) { + _rebuildQuads_base : function (spriteFrame, contentSize, colorOpacity, isTrimmedContentSize) { var quads = []; //build vertices - var vertices = this._calculateVertices(spriteFrame, contentSize); + var vertices = this._calculateVertices(spriteFrame, contentSize, isTrimmedContentSize); //build uvs var uvs = this._calculateUVs(spriteFrame); @@ -88,15 +88,31 @@ cc.SimpleQuadGenerator = { return quads; }, - _calculateVertices : function (spriteFrame, contentSize) { - + _calculateVertices : function (spriteFrame, contentSize, isTrimmedContentSize) { var x0,x3; var y0,y3; - x0 = 0; - x3 = contentSize.width; + if(isTrimmedContentSize) { + x0 = 0; + x3 = contentSize.width; - y0 = 0; - y3 = contentSize.height; + y0 = 0; + y3 = contentSize.height; + } else { + var originalSize = spriteFrame.getOriginalSize(); + var rect = spriteFrame.getRect(); + var offset = spriteFrame.getOffset(); + var scaleX = contentSize.width / originalSize.width; + var scaleY = contentSize.height / originalSize.height; + var trimmLeft = offset.x + (originalSize.width - rect.width) / 2; + var trimmRight = offset.x - (originalSize.width - rect.width) / 2; + var trimmedBottom = offset.y + (originalSize.height - rect.height) / 2; + var trimmedTop = offset.y - (originalSize.height - rect.height) / 2; + + x0 = trimmLeft * scaleX; + x3 = contentSize.width + trimmRight * scaleX; + y0 = trimmedBottom * scaleY; + y3 = contentSize.height + trimmedTop * scaleY; + } //apply contentscale factor x0 = x0 / cc.contentScaleFactor(); @@ -396,6 +412,8 @@ cc.Scale9Sprite = _ccsg.Node.extend({ _quads: [], _quadsDirty: true, + _isTrimmedContentSize: true, + ctor: function (textureOrSpriteFrame) { _ccsg.Node.prototype.ctor.call(this); this._renderCmd.setState(this._brightState); @@ -522,6 +540,18 @@ cc.Scale9Sprite = _ccsg.Node.extend({ this._quadsDirty = true; }, + // + enableTrimmedContentSize: function(isTrimmed) { + isTrimmed = isTrimmed || true; + if(this._isTrimmedContentSize !== isTrimmed) { + this._isTrimmedContentSize = isTrimmed; + this._quadsDirty = true; + } + }, + + isTrimmedContentSizeEnabled: function() { + return this._isTrimmedContentSize; + }, /** * Change the state of 9-slice sprite. * @see `State` @@ -634,12 +664,13 @@ cc.Scale9Sprite = _ccsg.Node.extend({ this._quads[index]._tr.colors = color; } }, + _rebuildQuads : function () { if (!this.loaded() || this._quadsDirty === false) return; var color = this.getDisplayedColor(); color.a = this.getDisplayedOpacity(); if (this._renderingType === cc.Scale9Sprite.RenderingType.SIMPLE) { - this._quads = cc.SimpleQuadGenerator._rebuildQuads_base(this._spriteFrame, this.getContentSize(), color); + this._quads = cc.SimpleQuadGenerator._rebuildQuads_base(this._spriteFrame, this.getContentSize(), color, this._isTrimmedContentSize); } else if (this._renderingType === cc.Scale9Sprite.RenderingType.SLICED) { this._quads = cc.Scale9QuadGenerator._rebuildQuads_base(this._spriteFrame, this.getContentSize(), color, this._insetLeft, this._insetRight, this._insetTop, this._insetBottom); } else if (this._renderingType === cc.Scale9Sprite.RenderingType.TILED) { From 8bdcd4d7f234be5f1159aaa4fa39bd0ce6eac3ca Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Mon, 18 Jan 2016 14:39:38 +0800 Subject: [PATCH 24/35] fix scale9 sprite bug --- cocos2d/core/sprites/CCScale9Sprite.js | 1 - 1 file changed, 1 deletion(-) diff --git a/cocos2d/core/sprites/CCScale9Sprite.js b/cocos2d/core/sprites/CCScale9Sprite.js index 1003a89956..00bab36abe 100644 --- a/cocos2d/core/sprites/CCScale9Sprite.js +++ b/cocos2d/core/sprites/CCScale9Sprite.js @@ -542,7 +542,6 @@ cc.Scale9Sprite = _ccsg.Node.extend({ // enableTrimmedContentSize: function(isTrimmed) { - isTrimmed = isTrimmed || true; if(this._isTrimmedContentSize !== isTrimmed) { this._isTrimmedContentSize = isTrimmed; this._quadsDirty = true; From d3d99c9f7c6cc4b3065c679b2fb941d244345d19 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Mon, 18 Jan 2016 14:40:32 +0800 Subject: [PATCH 25/35] add EC level code --- cocos2d/core/components/CCSprite.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 385b9f84bb..6d2153b1a5 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -45,7 +45,7 @@ var Sprite = cc.Class({ }, _type: SpriteType.SIMPLE, _useOriginalSize: true, - + _isTrimmedMode: true, /** * The Sprite Atlas. * @property _atlas @@ -100,6 +100,17 @@ var Sprite = cc.Class({ tooltip: 'i18n:COMPONENT.sprite.type', }, + isTrimmedMode: { + get: function() { + return this._isTrimmedMode; + }, + set: function(value) { + this._isTrimmedMode = value; + this._sgNode.enableTrimmedContentSize(value); + this._applySpriteSize(); + } + }, + useOriginalSize: { get: function () { return this._useOriginalSize; @@ -326,6 +337,9 @@ var Sprite = cc.Class({ _applySpriteSize: function () { if (this._useOriginalSize && this._spriteFrame) { var rect = this._spriteFrame.getRect(); + if(!this._isTrimmedMode && this._type == SpriteType.SIMPLE) { + rect = this._spriteFrame.getOriginalSize(); + } this.node.setContentSize(cc.size(rect.width, rect.height)); } else { From 88dca474c2d5f4c5cd4e3c8c04aad2e48f600375 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 18 Jan 2016 14:18:47 +0800 Subject: [PATCH 26/35] fix grid layout resize container issue --- cocos2d/core/components/CCLayout.js | 62 ++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/cocos2d/core/components/CCLayout.js b/cocos2d/core/components/CCLayout.js index da6bf46e66..7ad8c158e8 100644 --- a/cocos2d/core/components/CCLayout.js +++ b/cocos2d/core/components/CCLayout.js @@ -275,7 +275,7 @@ var Layout = cc.Class({ this.node.on('size-changed', this._resized, this); this.node.on('anchor-changed', this._doLayoutDirty, this); this.node.on('child-added', this._childrenAddOrDeleted, this); - this.node.on('child-removed', this._childrenAddOrDeleted, this);; + this.node.on('child-removed', this._childrenAddOrDeleted, this); this._updateChildrenEventListener(); }, @@ -292,7 +292,7 @@ var Layout = cc.Class({ }.bind(this)); }, - _childrenAddOrDeleted: function(event) { + _childrenAddOrDeleted: function() { this._updateChildrenEventListener(); this._doLayoutDirty(); }, @@ -318,7 +318,7 @@ var Layout = cc.Class({ var tempMaxHeight = 0; var secondMaxHeight = 0; var row = 0; - var containerResizeBoundary = 0; + var containerResizeBoundary; var maxHeightChildAnchor; @@ -386,12 +386,24 @@ var Layout = cc.Class({ } var signX = 1; - if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { - signX = -1; - } - + var tempFinalPositionY; var topMarign = (tempMaxHeight === 0) ? child.height : tempMaxHeight; - containerResizeBoundary = finalPositionY + signX * (topMarign * maxHeightChildAnchor.y + this.padding); + + if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) { + containerResizeBoundary = containerResizeBoundary || this.node._contentSize.height; + signX = -1; + tempFinalPositionY = finalPositionY + signX * (topMarign * maxHeightChildAnchor.y + this.padding); + if (tempFinalPositionY < containerResizeBoundary) { + containerResizeBoundary = tempFinalPositionY; + } + } + else { + containerResizeBoundary = containerResizeBoundary || -this.node._contentSize.height; + tempFinalPositionY = finalPositionY + signX * (topMarign * maxHeightChildAnchor.y + this.padding); + if (tempFinalPositionY > containerResizeBoundary) { + containerResizeBoundary = tempFinalPositionY; + } + } nextX += rightBoundaryOfChild; }.bind(this)); @@ -430,7 +442,7 @@ var Layout = cc.Class({ var tempMaxWidth = 0; var secondMaxWidth = 0; var column = 0; - var containerResizeBoundary = 0; + var containerResizeBoundary; var maxWidthChildAnchor; var newChildHeight = this.cellSize.height; @@ -496,13 +508,26 @@ var Layout = cc.Class({ } var signX = 1; - if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { - signX = -1; - } - + var tempFinalPositionX; //when the item is the last column break item, the tempMaxWidth will be 0. var rightMarign = (tempMaxWidth === 0) ? child.width : tempMaxWidth; - containerResizeBoundary = finalPositionX + signX * (rightMarign * maxWidthChildAnchor.x + this.padding); + + if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) { + signX = -1; + containerResizeBoundary = containerResizeBoundary || this.node._contentSize.width; + tempFinalPositionX = finalPositionX + signX * (rightMarign * maxWidthChildAnchor.x + this.padding); + if (tempFinalPositionX < containerResizeBoundary) { + containerResizeBoundary = tempFinalPositionX; + } + } + else { + containerResizeBoundary = containerResizeBoundary || -this.node._contentSize.width; + tempFinalPositionX = finalPositionX + signX * (rightMarign * maxWidthChildAnchor.x + this.padding); + if (tempFinalPositionX > containerResizeBoundary) { + containerResizeBoundary = tempFinalPositionX; + } + + } nextY += topBoundaryOfChild; }.bind(this)); @@ -511,7 +536,6 @@ var Layout = cc.Class({ }, _doLayoutBasic: function() { - var layoutAnchor = this.node.getAnchorPoint(); var children = this.node.children; var allChildrenBoundingBox = null; @@ -561,7 +585,7 @@ var Layout = cc.Class({ if (this.resize === ResizeType.CONTAINER) { //calculate the new height of container, it won't change the position of it's children var boundary = this._doLayoutHorizontally(baseWidth, true, fnPositionY, false); - newHeight = bottomBoundaryOfLayout - boundary + newHeight = bottomBoundaryOfLayout - boundary; if (newHeight < 0) { newHeight *= -1; } @@ -576,7 +600,7 @@ var Layout = cc.Class({ this._doLayoutHorizontally(baseWidth, true, fnPositionY, true); - if (this.resize == ResizeType.CONTAINER) { + if (this.resize === ResizeType.CONTAINER) { this.node.setContentSize(baseWidth, newHeight); } }, @@ -652,7 +676,7 @@ var Layout = cc.Class({ if (this.layoutType === Type.HORIZONTAL) { var newWidth = this._getHorizontalBaseWidth(this.node.children); - var fnPositionY = function(child, topOffset) { + var fnPositionY = function(child) { return child.y; }; @@ -663,7 +687,7 @@ var Layout = cc.Class({ else if (this.layoutType === Type.VERTICAL) { var newHeight = this._getVerticalBaseHeight(this.node.children); - var fnPositionX = function(child, leftOffset) { + var fnPositionX = function(child) { return child.x; }; From dd9cc99b83ba27204fc8a9f0452c8d36fe0320de Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Mon, 18 Jan 2016 15:19:33 +0800 Subject: [PATCH 27/35] CCClass now support async initialize properties via function to avoid cross reference problem --- cocos2d/core/components/CCComponent.js | 3 +- cocos2d/core/platform/CCClass.js | 130 +++++++++++++++++++------ test/qunit/unit/test-class.js | 50 ++++++++++ 3 files changed, 151 insertions(+), 32 deletions(-) diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index 7214b7a22c..8d57e7654c 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -213,7 +213,8 @@ var Component = cc.Class({ }, set: function (value) { this._name = value; - } + }, + visible: false }, _id: { diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index 0bfaada657..17d99d0551 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -45,6 +45,50 @@ var TYPO_TO_CORRECT = CC_DEV && { var INVALID_STATICS = CC_DEV && ['name', '__ctors__', '__props__', 'arguments', 'call', 'apply', 'caller', 'length', 'prototype']; +var deferredInitializer = { + + // Configs for classes which needs deferred initialization + datas: null, + + // register new class + // data - {cls: cls, cb: properties, mixins: options.mixins} + push: function (data) { + if (this.datas) { + this.datas.push(data); + } + else { + this.datas = [data]; + // start a new timer to initialize + var self = this; + setTimeout(function () { + self.init(); + }, 0); + } + }, + + init: function () { + var datas = this.datas; + if (datas) { + for (var i = 0; i < datas.length; ++i) { + var data = datas[i]; + var cls = data.cls; + var properties = data.props; + if (typeof properties === 'function') { + properties = properties(); + } + var name = JS.getClassName(cls); + if (properties) { + declareProperties(cls, name, properties, cls.$super, data.mixins); + } + else { + cc.error('Properties function of "%s" should return an object!', name); + } + } + this.datas = null; + } + } +}; + ///** // * both getter and prop must register the name into __props__ array // * @param {String} name - prop name @@ -239,6 +283,10 @@ function getDefault (defaultVal) { function instantiateProps (instance, itsClass) { var propList = itsClass.__props__; + if (propList === null) { + deferredInitializer.init(); + propList = itsClass.__props__; + } for (var i = 0; i < propList.length; i++) { var prop = propList[i]; var attrs = Attr.attr(itsClass, prop); @@ -309,22 +357,19 @@ cc.isChildClassOf = function (subclass, superclass) { function doDefine (className, baseClass, mixins, constructor, options) { var fireClass = _createCtor(constructor, baseClass, mixins, className, options); - // (NON-INHERITED) Create a new Class that inherits from this Class + // extend - (NON-INHERITED) Create a new Class that inherits from this Class Object.defineProperty(fireClass, 'extend', { value: function (options) { options.extends = this; return CCClass(options); }, - writable: true + writable: true, + configurable: true }); if (baseClass) { - // inherit JS.extend(fireClass, baseClass); // 这里会把父类的 __props__ 复制给子类 - // fireClass.$super = baseClass; - // inherit __props__ - fireClass.__props__ = baseClass.__props__ ? baseClass.__props__.slice() : []; } if (mixins) { @@ -337,20 +382,11 @@ function doDefine (className, baseClass, mixins, constructor, options) { for (var p in mixin) if (mixin.hasOwnProperty(p) && INVALID_STATICS.indexOf(p) < 0) fireClass[p] = mixin[p]; - - // mixin __props__ - fireClass.__props__ = fireClass.__props__ || []; - if (mixin.__props__) { - fireClass.__props__ = fireClass.__props__.concat(mixin.__props__.filter(function (x) { - return fireClass.__props__.indexOf(x) < 0; - })); - } } // restore constuctor overridden by mixin fireClass.prototype.constructor = fireClass; } - fireClass.__props__ = fireClass.__props__ || []; JS.setClassName(className, fireClass); return fireClass; } @@ -597,6 +633,41 @@ function boundSuperCalls (baseClass, options) { return hasSuperCall; } +function declareProperties (cls, className, properties, baseClass, mixins) { + cls.__props__ = []; + + if (baseClass && baseClass.__props__) { + cls.__props__ = baseClass.__props__.slice(); + } + + if (mixins) { + for (var m = 0; m < mixins.length; ++m) { + var mixin = mixins[m]; + if (mixin.__props__) { + cls.__props__ = cls.__props__.concat(mixin.__props__.filter(function (x) { + return cls.__props__.indexOf(x) < 0; + })); + } + } + } + + if (properties) { + // 预处理属性 + preprocessAttrs(properties, className, cls); + + for (var propName in properties) { + var val = properties[propName]; + var attrs = parseAttributes(val, className, propName); + if ('default' in val) { + defineProp(cls, className, propName, val.default, attrs); + } + else { + defineGetSet(cls, className, propName, val, attrs); + } + } + } +} + /** * !#en Defines a CCClass using the given specification, please see [Class](/en/scripting/class/) for details. * !#zh 定义一个 CCClass,传入参数必须是一个包含类型参数的字面量对象,具体用法请查阅[类型定义](/zh/scripting/class/)。 @@ -661,31 +732,28 @@ function CCClass (options) { var name = options.name; var base = options.extends/* || CCObject*/; + var mixins = options.mixins; // create constructor var cls; - cls = define(name, base, options.mixins, options.ctor, options); + cls = define(name, base, mixins, options.ctor, options); if (!name) { name = cc.js.getClassName(cls); } // define Properties var properties = options.properties; - if (properties) { - - // 预处理属性 - preprocessAttrs(properties, name, cls); - - for (var propName in properties) { - var val = properties[propName]; - var attrs = parseAttributes(val, name, propName); - if ('default' in val) { - defineProp(cls, name, propName, val.default, attrs); - } - else { - defineGetSet(cls, name, propName, val, attrs); - } - } + if (typeof properties === 'function' || + (base && base.__props__ === null) || + (mixins && mixins.some(function (x) { + return x.__props__ === null; + })) + ) { + deferredInitializer.push({cls: cls, props: properties, mixins: mixins}); + cls.__props__ = null; + } + else { + declareProperties(cls, name, properties, base, options.mixins); } // define statics diff --git a/test/qunit/unit/test-class.js b/test/qunit/unit/test-class.js index ae11f5d237..fbc0b7f51e 100644 --- a/test/qunit/unit/test-class.js +++ b/test/qunit/unit/test-class.js @@ -579,3 +579,53 @@ test('mixins', function () { strictEqual(cc.Class.attr(BigDog, 'p2').default, 'Defined by Mixin2', 'last mixin property should override previous'); strictEqual(cc.Class.attr(BigDog, 'p1').default, 'Defined by BigDog', "should override base property"); }); + +asyncTest('instantiate properties in the next frame', function () { + var Dog = cc.Class({ + extends: cc.Object, + properties: function () { + return { + like: 'shit' + }; + } + }); + var Husky = cc.Class({ + extends: Dog, + properties: { + weight: 100 + } + }); + + throws( + function () { + Husky.__props__.length; + }, + 'should raised error if accessing to props via Class' + ); + + setTimeout(function () { + deepEqual(Husky.__props__, ['like', 'weight'], 'should get properties in the correct order'); + equal(cc.Class.attr(Dog, '_name').visible, false, 'Should not override fast defined properties'); + + start(); + }, 0); +}); + +test('lazy instantiate properties', function () { + var Dog = cc.Class({ + properties: function () { + return { + like: 'shit' + }; + } + }); + var Husky = cc.Class({ + extends: Dog, + properties: { + weight: 100 + } + }); + + var dog = new Husky(); + deepEqual(Husky.__props__, ['like', 'weight'], 'could get properties in the correct order after instantiating'); +}); From dc1caef5cd85c8f2493037928e834a4c23062b95 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 18 Jan 2016 17:39:52 +0800 Subject: [PATCH 28/35] fix label component init font size issue. fix https://github.com/cocos-creator/fireball/issues/1612 fix https://github.com/cocos-creator/fireball/issues/1573 --- cocos2d/core/components/CCLabel.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/components/CCLabel.js b/cocos2d/core/components/CCLabel.js index 293577eede..9fd26f6668 100644 --- a/cocos2d/core/components/CCLabel.js +++ b/cocos2d/core/components/CCLabel.js @@ -272,10 +272,10 @@ var Label = cc.Class({ sgNode.setHorizontalAlign( this.horizontalAlign ); sgNode.setVerticalAlign( this.verticalAlign ); - sgNode.setFontSize( this.fontSize ); + sgNode.setFontSize( this._fontSize ); sgNode.setOverflow( this.overflow ); - sgNode.enableWrapText( this.enableWrapText ); - sgNode.setLineHeight(this.lineHeight); + sgNode.enableWrapText( this._enableWrapText ); + sgNode.setLineHeight(this._lineHeight); if(!this._useOriginalSize){ sgNode.setContentSize(this.node.getContentSize()); } From 657544e3c96c6307be78373266ed6fe644bc7de4 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Mon, 18 Jan 2016 17:46:29 +0800 Subject: [PATCH 29/35] add EC level code --- cocos2d/core/components/CCSprite.js | 78 ++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 6d2153b1a5..424555948b 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -23,7 +23,11 @@ ****************************************************************************/ var SpriteType = cc.SpriteType; - +var SizeMode = cc.Enum({ + Custom_Size: 0, + Trimmed_Size: 1, + Raw_Size: 2 +}); /** * Renders a sprite in the scene. * @class Sprite @@ -44,7 +48,9 @@ var Sprite = cc.Class({ type: cc.SpriteFrame }, _type: SpriteType.SIMPLE, + //FIXME:_useOriginalSize is deprecated, since v0.8, it need to be deleted _useOriginalSize: true, + _sizeMode: -1, _isTrimmedMode: true, /** * The Sprite Atlas. @@ -105,12 +111,14 @@ var Sprite = cc.Class({ return this._isTrimmedMode; }, set: function(value) { - this._isTrimmedMode = value; - this._sgNode.enableTrimmedContentSize(value); - this._applySpriteSize(); + if(this._isTrimmedMode !== value) { + this._isTrimmedMode = value; + this._sgNode.enableTrimmedContentSize(value); + } } }, + //FIXME:_useOriginalSize is deprecated, since v0.8, it need to be deleted useOriginalSize: { get: function () { return this._useOriginalSize; @@ -124,6 +132,18 @@ var Sprite = cc.Class({ tooltip: 'i18n:COMPONENT.sprite.original_size', }, + sizeMode : { + get: function() { + return this._sizeMode; + }, + set: function(value) { + this._sizeMode = value; + if(value !== SizeMode.Custom_Size) { + this._applySpriteSize(); + } + }, + type: SizeMode + }, /** * Only for editor to calculate bounding box. */ @@ -304,6 +324,17 @@ var Sprite = cc.Class({ onLoad: function () { this._super(); + //do processing + if(-1 === this._sizeMode) { + //FIXME:_useOriginalSize is deprecated, since v0.8, it need to be deleted + if(this._useOriginalSize) { + this._sizeMode = SizeMode.Trimmed_Size; + this._isTrimmedMode = true; + } else { + this._sizeMode = SizeMode.Custom_Size; + this._isTrimmedMode = true; + } + } this.node.on('size-changed', this._resized, this); }, @@ -335,14 +366,15 @@ var Sprite = cc.Class({ }, _applySpriteSize: function () { - if (this._useOriginalSize && this._spriteFrame) { + if(SizeMode.Custom_Size === this._sizeMode || !this._spriteFrame) { + this.node.setContentSize(this.node.getContentSize(true)); + } else if(SizeMode.Raw_Size == this._sizeMode) { + var size = this._spriteFrame.getOriginalSize(); + this.node.setContentSize(cc.size(size.width, size.height)); + } else if(SizeMode.Trimmed_Size == this._sizeMode) { var rect = this._spriteFrame.getRect(); - if(!this._isTrimmedMode && this._type == SpriteType.SIMPLE) { - rect = this._spriteFrame.getOriginalSize(); - } this.node.setContentSize(cc.size(rect.width, rect.height)); - } - else { + } else { this.node.setContentSize(this.node.getContentSize(true)); } }, @@ -404,13 +436,25 @@ var Sprite = cc.Class({ }, _resized: function () { - if (this._useOriginalSize && this._spriteFrame) { - var rect = this._spriteFrame.getRect(); - var expectedW = rect.width; - var expectedH = rect.height; - var actualSize = this.node.getContentSize(); - if (expectedW !== actualSize.width || expectedH !== actualSize.height) { - this.useOriginalSize = false; + if (SizeMode.Custom_Size !== this._sizeMode && this._spriteFrame) { + if(this._sizeMode === SizeMode.Raw_Size) { + var size = this._spriteFrame.getOriginalSize(); + var expectedW = size.width; + var expectedH = size.height; + var actualSize = this.node.getContentSize(); + if (expectedW !== actualSize.width || expectedH !== actualSize.height) { + this._sizeMode = SizeMode.Custom_Size; + } + } else if(this._sizeMode === SizeMode.Trimmed_Size) { + var rect = this._spriteFrame.getRect(); + var expectedW = rect.width; + var expectedH = rect.height; + var actualSize = this.node.getContentSize(); + if (expectedW !== actualSize.width || expectedH !== actualSize.height) { + this._sizeMode = SizeMode.Custom_Size; + } + } else { + //do nothing } } }, From 46556d9b3ad989da6d1875c598fadf38a8c46b7c Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Mon, 18 Jan 2016 17:46:45 +0800 Subject: [PATCH 30/35] reformat code --- cocos2d/core/components/CCSprite.js | 60 +++++++++++++------------- cocos2d/core/sprites/CCScale9Sprite.js | 54 +++++++++++------------ 2 files changed, 57 insertions(+), 57 deletions(-) diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 424555948b..e150bdf2ef 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -107,11 +107,11 @@ var Sprite = cc.Class({ }, isTrimmedMode: { - get: function() { + get: function () { return this._isTrimmedMode; }, - set: function(value) { - if(this._isTrimmedMode !== value) { + set: function (value) { + if (this._isTrimmedMode !== value) { this._isTrimmedMode = value; this._sgNode.enableTrimmedContentSize(value); } @@ -132,13 +132,13 @@ var Sprite = cc.Class({ tooltip: 'i18n:COMPONENT.sprite.original_size', }, - sizeMode : { - get: function() { + sizeMode: { + get: function () { return this._sizeMode; }, - set: function(value) { + set: function (value) { this._sizeMode = value; - if(value !== SizeMode.Custom_Size) { + if (value !== SizeMode.Custom_Size) { this._applySpriteSize(); } }, @@ -176,7 +176,7 @@ var Sprite = cc.Class({ * @method setScale9Enabled * @param {Boolean} enabled - True to enable 9-slice, false otherwise. */ - setScale9Enabled: function(enabled) { + setScale9Enabled: function (enabled) { this.type = enabled ? cc.SpriteType.SLICED : cc.SpriteType.SIMPLE; }, @@ -185,7 +185,7 @@ var Sprite = cc.Class({ * @method isScale9Enabled * @return {Boolean} True if 9-slice is enabled, false otherwise. */ - isScale9Enabled: function(){ + isScale9Enabled: function () { return this.type === cc.SpriteType.SLICED; }, @@ -255,7 +255,7 @@ var Sprite = cc.Class({ * @method setInsetLeft * @param {Number} leftInset - The values to use for the cap inset. */ - setInsetLeft: function(insetLeft){ + setInsetLeft: function (insetLeft) { this._sgNode.setInsetLeft(insetLeft); }, @@ -264,7 +264,7 @@ var Sprite = cc.Class({ * @method getInsetLeft * @return {Number} The left sprite's cap inset. */ - getInsetLeft: function(){ + getInsetLeft: function () { return this._sgNode.getInsetLeft(); }, @@ -273,7 +273,7 @@ var Sprite = cc.Class({ * @method setInsetTop * @param {Number} topInset - The values to use for the cap inset. */ - setInsetTop: function(insetTop){ + setInsetTop: function (insetTop) { this._sgNode.setInsetTop(insetTop); }, @@ -282,7 +282,7 @@ var Sprite = cc.Class({ * @method getInsetTop * @return {Number} The top sprite's cap inset. */ - getInsetTop: function(){ + getInsetTop: function () { return this._sgNode.getInsetTop(); }, @@ -291,7 +291,7 @@ var Sprite = cc.Class({ * @method setInsetRight * @param {Number} rightInset - The values to use for the cap inset. */ - setInsetRight: function(insetRight){ + setInsetRight: function (insetRight) { this._sgNode.setInsetRight(insetRight); }, @@ -300,7 +300,7 @@ var Sprite = cc.Class({ * @method getInsetRight * @return {Number} The right sprite's cap inset. */ - getInsetRight: function(){ + getInsetRight: function () { return this._sgNode.getInsetRight(); }, @@ -309,7 +309,7 @@ var Sprite = cc.Class({ * @method setInsetBottom * @param {Number} bottomInset - The values to use for the cap inset. */ - setInsetBottom: function(insetBottom) { + setInsetBottom: function (insetBottom) { this._sgNode.setInsetBottom(insetBottom); }, @@ -318,16 +318,16 @@ var Sprite = cc.Class({ * @method getInsetBottom * @return {Number} The bottom sprite's cap inset. */ - getInsetBottom: function(){ + getInsetBottom: function () { return this._sgNode.getInsetBottom(); }, onLoad: function () { this._super(); //do processing - if(-1 === this._sizeMode) { + if (-1 === this._sizeMode) { //FIXME:_useOriginalSize is deprecated, since v0.8, it need to be deleted - if(this._useOriginalSize) { + if (this._useOriginalSize) { this._sizeMode = SizeMode.Trimmed_Size; this._isTrimmedMode = true; } else { @@ -343,11 +343,11 @@ var Sprite = cc.Class({ this.node.off('size-changed', this._resized, this); }, - _applyAtlas: CC_EDITOR && function ( spriteFrame ) { + _applyAtlas: CC_EDITOR && function (spriteFrame) { // Set atlas if (spriteFrame && spriteFrame._atlasUuid) { var self = this; - cc.AssetLibrary.loadAsset(spriteFrame._atlasUuid, function(err, asset) { + cc.AssetLibrary.loadAsset(spriteFrame._atlasUuid, function (err, asset) { self._atlas = asset; }); } else { @@ -366,12 +366,12 @@ var Sprite = cc.Class({ }, _applySpriteSize: function () { - if(SizeMode.Custom_Size === this._sizeMode || !this._spriteFrame) { + if (SizeMode.Custom_Size === this._sizeMode || !this._spriteFrame) { this.node.setContentSize(this.node.getContentSize(true)); - } else if(SizeMode.Raw_Size == this._sizeMode) { + } else if (SizeMode.Raw_Size == this._sizeMode) { var size = this._spriteFrame.getOriginalSize(); this.node.setContentSize(cc.size(size.width, size.height)); - } else if(SizeMode.Trimmed_Size == this._sizeMode) { + } else if (SizeMode.Trimmed_Size == this._sizeMode) { var rect = this._spriteFrame.getRect(); this.node.setContentSize(cc.size(rect.width, rect.height)); } else { @@ -385,7 +385,7 @@ var Sprite = cc.Class({ sgNode.setSpriteFrame(self._spriteFrame); self._applyCapInset(); self._applySpriteSize(); - if ( this.enabledInHierarchy && !sgNode.isVisible() ) { + if (this.enabledInHierarchy && !sgNode.isVisible()) { sgNode.setVisible(true); } }, @@ -421,7 +421,7 @@ var Sprite = cc.Class({ _initSgNode: function () { var sgNode = this._sgNode; - if ( !this.enabledInHierarchy ) { + if (!this.enabledInHierarchy) { sgNode.setVisible(false); } @@ -431,13 +431,13 @@ var Sprite = cc.Class({ // otherwise setContentSize may not take effect sgNode.setContentSize(this.node.getContentSize(true)); this._applySpriteSize(); - + sgNode.setRenderingType(this._type); }, _resized: function () { if (SizeMode.Custom_Size !== this._sizeMode && this._spriteFrame) { - if(this._sizeMode === SizeMode.Raw_Size) { + if (this._sizeMode === SizeMode.Raw_Size) { var size = this._spriteFrame.getOriginalSize(); var expectedW = size.width; var expectedH = size.height; @@ -445,7 +445,7 @@ var Sprite = cc.Class({ if (expectedW !== actualSize.width || expectedH !== actualSize.height) { this._sizeMode = SizeMode.Custom_Size; } - } else if(this._sizeMode === SizeMode.Trimmed_Size) { + } else if (this._sizeMode === SizeMode.Trimmed_Size) { var rect = this._spriteFrame.getRect(); var expectedW = rect.width; var expectedH = rect.height; @@ -463,7 +463,7 @@ var Sprite = cc.Class({ var misc = require('../utils/misc'); var SameNameGetSets = ['atlas', 'capInsets', 'insetLeft', 'insetTop', 'insetRight', 'insetBottom']; var DiffNameGetSets = { - type: [ null, 'setRenderingType'] + type: [null, 'setRenderingType'] }; misc.propertyDefine(Sprite, SameNameGetSets, DiffNameGetSets); diff --git a/cocos2d/core/sprites/CCScale9Sprite.js b/cocos2d/core/sprites/CCScale9Sprite.js index 00bab36abe..c343daa9a2 100644 --- a/cocos2d/core/sprites/CCScale9Sprite.js +++ b/cocos2d/core/sprites/CCScale9Sprite.js @@ -51,7 +51,7 @@ EventTarget = require("../cocos2d/core/event/event-target"); * @property {Number} insetBottom - The bottom inset of the 9-slice sprite */ cc.SimpleQuadGenerator = { - _rebuildQuads_base : function (spriteFrame, contentSize, colorOpacity, isTrimmedContentSize) { + _rebuildQuads_base: function (spriteFrame, contentSize, colorOpacity, isTrimmedContentSize) { var quads = []; //build vertices var vertices = this._calculateVertices(spriteFrame, contentSize, isTrimmedContentSize); @@ -88,10 +88,10 @@ cc.SimpleQuadGenerator = { return quads; }, - _calculateVertices : function (spriteFrame, contentSize, isTrimmedContentSize) { - var x0,x3; - var y0,y3; - if(isTrimmedContentSize) { + _calculateVertices: function (spriteFrame, contentSize, isTrimmedContentSize) { + var x0, x3; + var y0, y3; + if (isTrimmedContentSize) { x0 = 0; x3 = contentSize.width; @@ -127,7 +127,7 @@ cc.SimpleQuadGenerator = { return vertices; }, - _calculateUVs : function (spriteFrame) { + _calculateUVs: function (spriteFrame) { var atlasWidth = spriteFrame._texture.getPixelWidth(); var atlasHeight = spriteFrame._texture.getPixelHeight(); @@ -161,7 +161,7 @@ cc.SimpleQuadGenerator = { }; cc.Scale9QuadGenerator = { - _rebuildQuads_base : function (spriteFrame, contentSize, colorOpacity,insetLeft, insetRight, insetTop, insetBottom) { + _rebuildQuads_base: function (spriteFrame, contentSize, colorOpacity, insetLeft, insetRight, insetTop, insetBottom) { var quads = []; //build vertices var vertices = this._calculateVertices(spriteFrame, contentSize, insetLeft, insetRight, insetTop, insetBottom); @@ -202,7 +202,7 @@ cc.Scale9QuadGenerator = { return quads; }, - _calculateVertices : function (spriteFrame, contentSize, insetLeft, insetRight, insetTop, insetBottom) { + _calculateVertices: function (spriteFrame, contentSize, insetLeft, insetRight, insetTop, insetBottom) { var leftWidth, centerWidth, rightWidth; var topHeight, centerHeight, bottomHeight; @@ -255,7 +255,7 @@ cc.Scale9QuadGenerator = { return vertices; }, - _calculateUVs : function (spriteFrame, insetLeft, insetRight, insetTop, insetBottom) { + _calculateUVs: function (spriteFrame, insetLeft, insetRight, insetTop, insetBottom) { var rect = spriteFrame._rect; var atlasWidth = spriteFrame._texture.getPixelWidth(); var atlasHeight = spriteFrame._texture.getPixelHeight(); @@ -311,7 +311,7 @@ cc.Scale9QuadGenerator = { }; cc.TiledQuadGenerator = { - _rebuildQuads_base : function (spriteFrame, contentSize, colorOpacity) { + _rebuildQuads_base: function (spriteFrame, contentSize, colorOpacity) { var quads = []; //build uvs @@ -325,8 +325,8 @@ cc.TiledQuadGenerator = { //build quads var hRepeat = contentSize.width / rectWidth; var vRepeat = contentSize.height / rectHeight; - for(var hindex = 0; hindex < Math.ceil(hRepeat); ++hindex) { - for(var vindex = 0; vindex < Math.ceil(vRepeat); ++vindex) { + for (var hindex = 0; hindex < Math.ceil(hRepeat); ++hindex) { + for (var vindex = 0; vindex < Math.ceil(vRepeat); ++vindex) { var quad; quad = new cc.V3F_C4B_T2F_Quad(); @@ -336,20 +336,20 @@ cc.TiledQuadGenerator = { quad._tr.colors = colorOpacity; quad._bl.vertices = new cc.Vertex3F(rectWidth * hindex, rectHeight * vindex, 0); - quad._br.vertices = new cc.Vertex3F(rectWidth * Math.min(hindex+1, hRepeat), rectHeight * vindex, 0); - quad._tl.vertices = new cc.Vertex3F(rectWidth * hindex, rectHeight * Math.min(vindex+1, vRepeat), 0); - quad._tr.vertices = new cc.Vertex3F(rectWidth * Math.min(hindex+1, hRepeat), rectHeight * Math.min(vindex+1, vRepeat), 0); + quad._br.vertices = new cc.Vertex3F(rectWidth * Math.min(hindex + 1, hRepeat), rectHeight * vindex, 0); + quad._tl.vertices = new cc.Vertex3F(rectWidth * hindex, rectHeight * Math.min(vindex + 1, vRepeat), 0); + quad._tr.vertices = new cc.Vertex3F(rectWidth * Math.min(hindex + 1, hRepeat), rectHeight * Math.min(vindex + 1, vRepeat), 0); if (!spriteFrame._rotated) { quad._bl.texCoords = new cc.Tex2F(u0, v0); - quad._br.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1,hRepeat - hindex), v0); - quad._tl.texCoords = new cc.Tex2F(u0, v0 + (v1 - v0) * Math.min(1,vRepeat - vindex)); - quad._tr.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1,hRepeat - hindex), v0 + (v1 - v0) * Math.min(1,vRepeat - vindex)); + quad._br.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1, hRepeat - hindex), v0); + quad._tl.texCoords = new cc.Tex2F(u0, v0 + (v1 - v0) * Math.min(1, vRepeat - vindex)); + quad._tr.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1, hRepeat - hindex), v0 + (v1 - v0) * Math.min(1, vRepeat - vindex)); } else { quad._bl.texCoords = new cc.Tex2F(u0, v1); - quad._br.texCoords = new cc.Tex2F(u0, v1 + (v0-v1) * Math.min(1,hRepeat - hindex)); - quad._tl.texCoords = new cc.Tex2F(u0 + (u1-u0) * Math.min(1,vRepeat - vindex), v1); - quad._tr.texCoords = new cc.Tex2F(u0 + (u1-u0) * Math.min(1,vRepeat - vindex), v1 + (v0-v1) * Math.min(1,hRepeat - hindex)); + quad._br.texCoords = new cc.Tex2F(u0, v1 + (v0 - v1) * Math.min(1, hRepeat - hindex)); + quad._tl.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1, vRepeat - vindex), v1); + quad._tr.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1, vRepeat - vindex), v1 + (v0 - v1) * Math.min(1, hRepeat - hindex)); } quads.push(quad); @@ -358,7 +358,7 @@ cc.TiledQuadGenerator = { return quads; }, - _calculateUVs : function (spriteFrame) { + _calculateUVs: function (spriteFrame) { var atlasWidth = spriteFrame._texture.getPixelWidth(); var atlasHeight = spriteFrame._texture.getPixelHeight(); @@ -541,14 +541,14 @@ cc.Scale9Sprite = _ccsg.Node.extend({ }, // - enableTrimmedContentSize: function(isTrimmed) { - if(this._isTrimmedContentSize !== isTrimmed) { + enableTrimmedContentSize: function (isTrimmed) { + if (this._isTrimmedContentSize !== isTrimmed) { this._isTrimmedContentSize = isTrimmed; this._quadsDirty = true; } }, - isTrimmedContentSizeEnabled: function() { + isTrimmedContentSizeEnabled: function () { return this._isTrimmedContentSize; }, /** @@ -664,7 +664,7 @@ cc.Scale9Sprite = _ccsg.Node.extend({ } }, - _rebuildQuads : function () { + _rebuildQuads: function () { if (!this.loaded() || this._quadsDirty === false) return; var color = this.getDisplayedColor(); color.a = this.getDisplayedOpacity(); @@ -755,7 +755,7 @@ cc.SpriteType = cc.Enum({ /* * @property {Number} TILED */ - TILED : 2, + TILED: 2, /* * @property {Number} FILLED */ From f990ee6e82de95c877b380b0c3c56b639f44cb1a Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Mon, 18 Jan 2016 18:14:24 +0800 Subject: [PATCH 31/35] rename enumerable Size mode --- cocos2d/core/components/CCSprite.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index e150bdf2ef..1440b6f435 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -24,9 +24,9 @@ var SpriteType = cc.SpriteType; var SizeMode = cc.Enum({ - Custom_Size: 0, - Trimmed_Size: 1, - Raw_Size: 2 + CUSTOM: 0, + TRIMMED: 1, + RAW: 2 }); /** * Renders a sprite in the scene. @@ -138,7 +138,7 @@ var Sprite = cc.Class({ }, set: function (value) { this._sizeMode = value; - if (value !== SizeMode.Custom_Size) { + if (value !== SizeMode.CUSTOM) { this._applySpriteSize(); } }, @@ -328,10 +328,10 @@ var Sprite = cc.Class({ if (-1 === this._sizeMode) { //FIXME:_useOriginalSize is deprecated, since v0.8, it need to be deleted if (this._useOriginalSize) { - this._sizeMode = SizeMode.Trimmed_Size; + this._sizeMode = SizeMode.TRIMMED; this._isTrimmedMode = true; } else { - this._sizeMode = SizeMode.Custom_Size; + this._sizeMode = SizeMode.CUSTOM; this._isTrimmedMode = true; } } @@ -366,12 +366,12 @@ var Sprite = cc.Class({ }, _applySpriteSize: function () { - if (SizeMode.Custom_Size === this._sizeMode || !this._spriteFrame) { + if (SizeMode.CUSTOM === this._sizeMode || !this._spriteFrame) { this.node.setContentSize(this.node.getContentSize(true)); - } else if (SizeMode.Raw_Size == this._sizeMode) { + } else if (SizeMode.RAW == this._sizeMode) { var size = this._spriteFrame.getOriginalSize(); this.node.setContentSize(cc.size(size.width, size.height)); - } else if (SizeMode.Trimmed_Size == this._sizeMode) { + } else if (SizeMode.TRIMMED == this._sizeMode) { var rect = this._spriteFrame.getRect(); this.node.setContentSize(cc.size(rect.width, rect.height)); } else { @@ -436,22 +436,22 @@ var Sprite = cc.Class({ }, _resized: function () { - if (SizeMode.Custom_Size !== this._sizeMode && this._spriteFrame) { - if (this._sizeMode === SizeMode.Raw_Size) { + if (SizeMode.CUSTOM !== this._sizeMode && this._spriteFrame) { + if (this._sizeMode === SizeMode.RAW) { var size = this._spriteFrame.getOriginalSize(); var expectedW = size.width; var expectedH = size.height; var actualSize = this.node.getContentSize(); if (expectedW !== actualSize.width || expectedH !== actualSize.height) { - this._sizeMode = SizeMode.Custom_Size; + this._sizeMode = SizeMode.CUSTOM; } - } else if (this._sizeMode === SizeMode.Trimmed_Size) { + } else if (this._sizeMode === SizeMode.TRIMMED) { var rect = this._spriteFrame.getRect(); var expectedW = rect.width; var expectedH = rect.height; var actualSize = this.node.getContentSize(); if (expectedW !== actualSize.width || expectedH !== actualSize.height) { - this._sizeMode = SizeMode.Custom_Size; + this._sizeMode = SizeMode.CUSTOM; } } else { //do nothing From 15df58923bb4b86cb6a80983aff17e8d7ddc5d7f Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Tue, 19 Jan 2016 09:42:32 +0800 Subject: [PATCH 32/35] fix notes error to CCCommon.js --- cocos2d/core/platform/CCCommon.js | 1 + 1 file changed, 1 insertion(+) diff --git a/cocos2d/core/platform/CCCommon.js b/cocos2d/core/platform/CCCommon.js index 52a19dc939..19548a41ec 100644 --- a/cocos2d/core/platform/CCCommon.js +++ b/cocos2d/core/platform/CCCommon.js @@ -374,6 +374,7 @@ cc.KEY = { /** * @property {Number} num4 * @readonly + */ num4:100, /** * @property {Number} num5 From fceda73a0d97982f79376d2e3104e77c4b67cb06 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Tue, 19 Jan 2016 09:59:22 +0800 Subject: [PATCH 33/35] refine code --- cocos2d/core/components/CCSprite.js | 49 ++++++++++++++++++----------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 1440b6f435..1a88afa50b 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -23,9 +23,21 @@ ****************************************************************************/ var SpriteType = cc.SpriteType; +/** + * Sprite Size can track trimmed size, raw size or none + */ var SizeMode = cc.Enum({ + /** + * @property {Number} CUSTOM + */ CUSTOM: 0, + /** + * @property {Number} TRIMMED + */ TRIMMED: 1, + /** + * @property {Number} RAW + */ RAW: 2 }); /** @@ -329,11 +341,10 @@ var Sprite = cc.Class({ //FIXME:_useOriginalSize is deprecated, since v0.8, it need to be deleted if (this._useOriginalSize) { this._sizeMode = SizeMode.TRIMMED; - this._isTrimmedMode = true; } else { this._sizeMode = SizeMode.CUSTOM; - this._isTrimmedMode = true; } + this._isTrimmedMode = true; } this.node.on('size-changed', this._resized, this); }, @@ -368,10 +379,10 @@ var Sprite = cc.Class({ _applySpriteSize: function () { if (SizeMode.CUSTOM === this._sizeMode || !this._spriteFrame) { this.node.setContentSize(this.node.getContentSize(true)); - } else if (SizeMode.RAW == this._sizeMode) { + } else if (SizeMode.RAW === this._sizeMode) { var size = this._spriteFrame.getOriginalSize(); - this.node.setContentSize(cc.size(size.width, size.height)); - } else if (SizeMode.TRIMMED == this._sizeMode) { + this.node.setContentSize(size); + } else if (SizeMode.TRIMMED === this._sizeMode) { var rect = this._spriteFrame.getRect(); this.node.setContentSize(cc.size(rect.width, rect.height)); } else { @@ -436,25 +447,25 @@ var Sprite = cc.Class({ }, _resized: function () { - if (SizeMode.CUSTOM !== this._sizeMode && this._spriteFrame) { + if (this._spriteFrame) { + var actualSize = this.node.getContentSize(); + var expectedW = actualSize.width; + var expectedH = actualSize.height; if (this._sizeMode === SizeMode.RAW) { var size = this._spriteFrame.getOriginalSize(); - var expectedW = size.width; - var expectedH = size.height; - var actualSize = this.node.getContentSize(); - if (expectedW !== actualSize.width || expectedH !== actualSize.height) { - this._sizeMode = SizeMode.CUSTOM; - } + expectedW = size.width; + expectedH = size.height; } else if (this._sizeMode === SizeMode.TRIMMED) { var rect = this._spriteFrame.getRect(); - var expectedW = rect.width; - var expectedH = rect.height; - var actualSize = this.node.getContentSize(); - if (expectedW !== actualSize.width || expectedH !== actualSize.height) { - this._sizeMode = SizeMode.CUSTOM; - } + expectedW = rect.width; + expectedH = rect.height; + } else { - //do nothing + + } + + if (expectedW !== actualSize.width || expectedH !== actualSize.height) { + this._sizeMode = SizeMode.CUSTOM; } } }, From a5ab1375e0d69811de6209b629cc6c8f4df9f9c2 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 19 Jan 2016 10:16:25 +0800 Subject: [PATCH 34/35] minor fix --- test/qunit/unit/test-class.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/qunit/unit/test-class.js b/test/qunit/unit/test-class.js index fbc0b7f51e..c0563b4dcb 100644 --- a/test/qunit/unit/test-class.js +++ b/test/qunit/unit/test-class.js @@ -582,7 +582,6 @@ test('mixins', function () { asyncTest('instantiate properties in the next frame', function () { var Dog = cc.Class({ - extends: cc.Object, properties: function () { return { like: 'shit' @@ -605,7 +604,6 @@ asyncTest('instantiate properties in the next frame', function () { setTimeout(function () { deepEqual(Husky.__props__, ['like', 'weight'], 'should get properties in the correct order'); - equal(cc.Class.attr(Dog, '_name').visible, false, 'Should not override fast defined properties'); start(); }, 0); From 9f9850e4d66a9169f2bbe83d3fc6dcb0010266ce Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Tue, 19 Jan 2016 10:37:41 +0800 Subject: [PATCH 35/35] add comments --- cocos2d/core/components/CCSprite.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 1a88afa50b..bfccda855a 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -117,7 +117,11 @@ var Sprite = cc.Class({ type: SpriteType, tooltip: 'i18n:COMPONENT.sprite.type', }, - + /** + * specify the rendering mode + * @property isTrimmedMode + * @type {Boolean} + */ isTrimmedMode: { get: function () { return this._isTrimmedMode; @@ -143,7 +147,11 @@ var Sprite = cc.Class({ }, tooltip: 'i18n:COMPONENT.sprite.original_size', }, - + /** + * specify the size tracing mode + * @property sizeMode + * @type {SizeMode} + */ sizeMode: { get: function () { return this._sizeMode; @@ -461,7 +469,7 @@ var Sprite = cc.Class({ expectedH = rect.height; } else { - + } if (expectedW !== actualSize.width || expectedH !== actualSize.height) {