diff --git a/cocos2d/actions/CCActionInterval.js b/cocos2d/actions/CCActionInterval.js index af705aef10..f293913dfa 100644 --- a/cocos2d/actions/CCActionInterval.js +++ b/cocos2d/actions/CCActionInterval.js @@ -214,7 +214,7 @@ cc.ActionInterval = cc.FiniteTimeAction.extend({ * !#zh * 改变一个动作的速度,使它的执行使用更长的时间(speed > 1)
* 或更少(speed < 1)可以有效得模拟“慢动作”或“快进”的效果。 - * @param speed + * @param {Number} speed * @returns {Action} */ speed: function(speed){ @@ -252,7 +252,7 @@ cc.ActionInterval = cc.FiniteTimeAction.extend({ * To repeat an action forever use the CCRepeatForever action. * !#zh 重复动作可以按一定次数重复一个动作,使用 RepeatForever 动作来永远重复一个动作。 * @method repeat - * @param times + * @param {Number} times * @returns {ActionInterval} */ repeat: function(times){ @@ -306,6 +306,7 @@ cc.Sequence = cc.ActionInterval.extend({ _actions:null, _split:null, _last:0, + _reversed:false, ctor:function (tempArray) { cc.ActionInterval.prototype.ctor.call(this); @@ -379,7 +380,7 @@ cc.Sequence = cc.ActionInterval.extend({ // action[0] new_t = (locSplit !== 0) ? dt / locSplit : 1; - if (found === 0 && locLast === 1) { + if (found === 0 && locLast === 1 && this._reversed) { // Reverse mode ? // XXX: Bug. this case doesn't contemplate when _last==-1, found=0 and in "reverse mode" // since it will require a hack to know if an action is on reverse mode or not. @@ -398,7 +399,7 @@ cc.Sequence = cc.ActionInterval.extend({ locActions[0].update(1); locActions[0].stop(); } - if (!locLast) { + if (locLast === 0) { // switching to action 1. stop action 0. locActions[0].update(1); locActions[0].stop(); @@ -410,7 +411,7 @@ cc.Sequence = cc.ActionInterval.extend({ if (locLast === found && actionFound.isDone()) return; - // Last action found and it is done + // Last action not found if (locLast !== found) actionFound.startWithTarget(this.target); @@ -423,6 +424,7 @@ cc.Sequence = cc.ActionInterval.extend({ var action = cc.Sequence._actionOneTwo(this._actions[1].reverse(), this._actions[0].reverse()); this._cloneDecoration(action); this._reverseEaseList(action); + action._reversed = true; return action; } }); diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index c92ec4ded7..a62b732a6f 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -195,7 +195,7 @@ Audio.State = { } catch (error) {} this._element.pause(); // remove touchPlayList - for (var i=0; i + * Note:
+ * 1.Scaling and rotation of child nodes are not considered.
+ * 2.After setting the Layout, the results need to be updated until the next frame, * unless you manually call {{#crossLink "Layout/updateLayout:method"}}{{/crossLink}}。 * !#zh - * Layout 组件相当于一个容器,能自动对它的所有子节点进行统一排版。 - * 但是对 Layout 设置后结果需要到下一帧才会更新,除非你设置完以后手动调用 {{#crossLink "Layout/updateLayout:method"}}{{/crossLink}}。 + * Layout 组件相当于一个容器,能自动对它的所有子节点进行统一排版。
+ * 注意:
+ * 1.不会考虑子节点的缩放和旋转。
+ * 2.对 Layout 设置后结果需要到下一帧才会更新,除非你设置完以后手动调用 {{#crossLink "Layout/updateLayout:method"}}{{/crossLink}}。 * @class Layout * @extends Component */ diff --git a/cocos2d/core/components/CCRichText.js b/cocos2d/core/components/CCRichText.js index 05c8592d78..2b1e981efb 100644 --- a/cocos2d/core/components/CCRichText.js +++ b/cocos2d/core/components/CCRichText.js @@ -58,7 +58,7 @@ var RichText = cc.Class({ name: 'cc.RichText', extends: cc._RendererUnderSG, - ctor: function() { + ctor: function () { this._textArray = null; this._labelSegments = []; this._labelSegmentsCache = []; @@ -66,9 +66,10 @@ var RichText = cc.Class({ this._resetState(); - if(CC_EDITOR) { + if (CC_EDITOR) { this._updateRichTextStatus = debounce(this._updateRichText, 200); - } else { + } + else { this._updateRichTextStatus = this._updateRichText; } }, @@ -105,7 +106,7 @@ var RichText = cc.Class({ tooltip: CC_DEV && 'i18n:COMPONENT.richtext.horizontal_align', animatable: false, notify: function (oldValue) { - if(this.horizontalAlign === oldValue) return; + if (this.horizontalAlign === oldValue) return; this._layoutDirty = true; this._updateRichTextStatus(); @@ -121,7 +122,7 @@ var RichText = cc.Class({ default: 40, tooltip: CC_DEV && 'i18n:COMPONENT.richtext.font_size', notify: function (oldValue) { - if(this.fontSize === oldValue) return; + if (this.fontSize === oldValue) return; this._layoutDirty = true; this._updateRichTextStatus(); @@ -138,10 +139,10 @@ var RichText = cc.Class({ type: cc.TTFFont, tooltip: CC_DEV && 'i18n:COMPONENT.richtext.font', notify: function (oldValue) { - if(this.font === oldValue) return; + if (this.font === oldValue) return; this._layoutDirty = true; - if(!CC_JSB && this.font) { + if (!CC_JSB && this.font) { this._onTTFLoaded(); } this._updateRichTextStatus(); @@ -157,7 +158,7 @@ var RichText = cc.Class({ default: 0, tooltip: CC_DEV && 'i18n:COMPONENT.richtext.max_width', notify: function (oldValue) { - if(this.maxWidth === oldValue) return; + if (this.maxWidth === oldValue) return; this._layoutDirty = true; this._updateRichTextStatus(); @@ -173,7 +174,7 @@ var RichText = cc.Class({ default: 40, tooltip: CC_DEV && 'i18n:COMPONENT.richtext.line_height', notify: function (oldValue) { - if(this.lineHeight === oldValue) return; + if (this.lineHeight === oldValue) return; this._layoutDirty = true; this._updateRichTextStatus(); @@ -189,12 +190,32 @@ var RichText = cc.Class({ default: null, type: cc.SpriteAtlas, tooltip: CC_DEV && 'i18n:COMPONENT.richtext.image_atlas', - notify: function(oldValue) { - if(this.imageAtlas === oldValue) return; + notify: function (oldValue) { + if (this.imageAtlas === oldValue) return; this._layoutDirty = true; this._updateRichTextStatus(); } + }, + + /** + * !#en + * Once checked, the RichText will block all input events (mouse and touch) within + * the bounding box of the node, preventing the input from penetrating into the underlying node. + * !#zh + * 选中此选项后,RichText 将阻止节点边界框中的所有输入事件(鼠标和触摸),从而防止输入事件穿透到底层节点。 + * @property {Boolean} handleTouchEvent + * @default true + */ + handleTouchEvent: { + default: true, + tooltip: CC_DEV && 'i18n:COMPONENT.richtext.handleTouchEvent', + notify: function (oldValue) { + if (this.handleTouchEvent === oldValue) return; + if (this.enabledInHierarchy) { + this.handleTouchEvent ? this._addEventListeners() : this._removeEventListeners(); + } + } } }, @@ -205,11 +226,23 @@ var RichText = cc.Class({ onEnable: function () { this._super(); - this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this); + if (this.handleTouchEvent) { + this._addEventListeners(); + } }, onDisable: function () { this._super(); + if (this.handleTouchEvent) { + this._removeEventListeners(); + } + }, + + _addEventListeners: function () { + this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this); + }, + + _removeEventListeners: function () { this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this); }, @@ -228,24 +261,24 @@ var RichText = cc.Class({ return sgNode; }, - _updateLabelSegmentTextAttributes: function() { - this._labelSegments.forEach(function(item) { + _updateLabelSegmentTextAttributes: function () { + this._labelSegments.forEach(function (item) { this._applyTextAttribute(item); }.bind(this)); }, _initSgNode: function () { this._updateRichText(); - if(!CC_JSB) { + if (!CC_JSB) { this._onTTFLoaded(); } }, _createFontLabel: function (string) { - return _ccsg.Label.pool.get(string, this.font, null, this.fontSize); + return _ccsg.Label.pool.get(string, this.font, null, this.fontSize); }, - _getFontRawUrl: function() { + _getFontRawUrl: function () { var isAsset = this.font instanceof cc.TTFFont; var fntRawUrl = isAsset ? this.font.rawUrl : ''; return fntRawUrl; @@ -253,7 +286,7 @@ var RichText = cc.Class({ _onTTFLoaded: function () { var rawUrl = this._getFontRawUrl(); - if(!rawUrl) return; + if (!rawUrl) return; var self = this; @@ -269,10 +302,11 @@ var RichText = cc.Class({ var self = this; var func = function (string) { var label; - if(self._labelSegmentsCache.length === 0) { + if (self._labelSegmentsCache.length === 0) { label = self._createFontLabel(string); self._labelSegmentsCache.push(label); - } else { + } + else { label = self._labelSegmentsCache[0]; label.setString(string); } @@ -281,41 +315,40 @@ var RichText = cc.Class({ var labelSize = label.getContentSize(); return labelSize.width; }; - if(string) { + if (string) { return func(string); - } else { + } + else { return func; } }, _onTouchEnded: function (event) { - if(!this.enabledInHierarchy) return; - var components = this.node.getComponents(cc.Component); - for (var i = 0; i < this._labelSegments.length; ++i ) { + for (var i = 0; i < this._labelSegments.length; ++i) { var labelSegment = this._labelSegments[i]; var clickHandler = labelSegment._clickHandler; if (clickHandler && this._containsTouchLocation(labelSegment, event.touch.getLocation())) { - components.forEach(function(component) { - if(component.enabledInHierarchy && component[clickHandler]) { + components.forEach(function (component) { + if (component.enabledInHierarchy && component[clickHandler]) { component[clickHandler](event); } }); + event.stopPropagation(); } } }, - _containsTouchLocation:function (label, point) { + _containsTouchLocation: function (label, point) { var myRect = label.getBoundingBoxToWorld(); - return cc.rectContainsPoint(myRect, point); }, _resetState: function () { var sgNode = this._sgNode; - if(sgNode) { + if (sgNode) { sgNode.removeAllChildren(); } @@ -329,11 +362,12 @@ var RichText = cc.Class({ this._layoutDirty = true; }, - _addLabelSegment: function(stringToken, styleIndex) { + _addLabelSegment: function (stringToken, styleIndex) { var labelSegment; - if(this._labelSegmentsCache.length === 0) { + if (this._labelSegmentsCache.length === 0) { labelSegment = this._createFontLabel(stringToken); - } else { + } + else { labelSegment = this._labelSegmentsCache.pop(); labelSegment.setString(stringToken); } @@ -350,7 +384,7 @@ var RichText = cc.Class({ //in Web platform, the extra pixels will be trimed. //so we need to set the overflow to clamp in JSB //FIXME: label in jsb should be refactored to keep the behavior the same as web platform. - if(CC_JSB) { + if (CC_JSB) { labelSegment.setOverflow(1); var size = labelSegment.getContentSize(); labelSegment.enableWrap(false); @@ -369,17 +403,18 @@ var RichText = cc.Class({ var checkStartIndex = 0; while (this._lineOffsetX <= this.maxWidth) { var checkEndIndex = this._getFirstWordLen(labelString, - checkStartIndex, - labelString.length); + checkStartIndex, + labelString.length); var checkString = labelString.substr(checkStartIndex, checkEndIndex); var checkStringWidth = this._measureText(styleIndex, checkString); - if(this._lineOffsetX + checkStringWidth <= this.maxWidth) { + if (this._lineOffsetX + checkStringWidth <= this.maxWidth) { this._lineOffsetX += checkStringWidth; checkStartIndex += checkEndIndex; - } else { + } + else { - if(checkStartIndex > 0) { + if (checkStartIndex > 0) { var remainingString = labelString.substr(0, checkStartIndex); this._addLabelSegment(remainingString, styleIndex); @@ -392,33 +427,34 @@ var RichText = cc.Class({ } } } - if(fragmentWidth > this.maxWidth) { + if (fragmentWidth > this.maxWidth) { var fragments = cc.TextUtils.fragmentText(labelString, - fragmentWidth, - this.maxWidth, - this._measureText(styleIndex)); - for(var k = 0; k < fragments.length; ++k) { + fragmentWidth, + this.maxWidth, + this._measureText(styleIndex)); + for (var k = 0; k < fragments.length; ++k) { var splitString = fragments[k]; - labelSegment = this._addLabelSegment(splitString, styleIndex); + labelSegment = this._addLabelSegment(splitString, styleIndex); var labelSize = labelSegment.getContentSize(); this._lineOffsetX += labelSize.width; - if(fragments.length > 1 && k < fragments.length - 1) { + if (fragments.length > 1 && k < fragments.length - 1) { this._updateLineInfo(); } } - } else { + } + else { this._lineOffsetX += fragmentWidth; this._addLabelSegment(labelString, styleIndex); } }, - _isLastComponentCR: function(stringToken) { - return stringToken.length -1 === stringToken.lastIndexOf("\n"); + _isLastComponentCR: function (stringToken) { + return stringToken.length - 1 === stringToken.lastIndexOf("\n"); }, _updateLineInfo: function () { @@ -428,40 +464,43 @@ var RichText = cc.Class({ }, _needsUpdateTextLayout: function (newTextArray) { - if(this._layoutDirty || !this._textArray || !newTextArray) { + if (this._layoutDirty || !this._textArray || !newTextArray) { return true; } - if(this._textArray.length !== newTextArray.length) { + if (this._textArray.length !== newTextArray.length) { return true; } - for(var i = 0; i < this._textArray.length; ++i) { + for (var i = 0; i < this._textArray.length; ++i) { var oldItem = this._textArray[i]; var newItem = newTextArray[i]; - if(oldItem.text != newItem.text) { + if (oldItem.text != newItem.text) { return true; - } else { + } + else { if (oldItem.style) { if (newItem.style) { - if(oldItem.style.size !== newItem.style.size - || oldItem.style.italic !== newItem.style.italic - || oldItem.style.isImage !== newItem.style.isImage) { + if (oldItem.style.size !== newItem.style.size + || oldItem.style.italic !== newItem.style.italic + || oldItem.style.isImage !== newItem.style.isImage) { return true; } - if(oldItem.style.isImage === newItem.style.isImage) { - if(oldItem.style.src !== newItem.style.src) { + if (oldItem.style.isImage === newItem.style.isImage) { + if (oldItem.style.src !== newItem.style.src) { return true; } } - } else { - if(oldItem.style.size || oldItem.style.italic || oldItem.style.isImage) { + } + else { + if (oldItem.style.size || oldItem.style.italic || oldItem.style.isImage) { return true; } } - } else { + } + else { if (newItem.style) { - if(newItem.style.size || newItem.style.italic || newItem.style.isImage) { + if (newItem.style.size || newItem.style.italic || newItem.style.isImage) { return true; } } @@ -473,9 +512,10 @@ var RichText = cc.Class({ _onSpriteFrameLoaded: function (event, spriteFrame) { var newSpriteFrame; - if(spriteFrame) { + if (spriteFrame) { newSpriteFrame = spriteFrame; - } else { + } + else { newSpriteFrame = event.target; } var sprite = newSpriteFrame.__sprite; @@ -483,10 +523,11 @@ var RichText = cc.Class({ }, _applySpriteFrame: function (spriteFrame) { - if(spriteFrame) { - if(spriteFrame.textureLoaded()) { + if (spriteFrame) { + if (spriteFrame.textureLoaded()) { this._onSpriteFrameLoaded(null, spriteFrame); - } else { + } + else { spriteFrame.once('load', this._onSpriteFrameLoaded, this); spriteFrame.ensureLoadTexture(); } @@ -496,7 +537,7 @@ var RichText = cc.Class({ _addRichTextImageElement: function (richTextElement) { var spriteFrameName = richTextElement.style.src; var spriteFrame = this.imageAtlas.getSpriteFrame(spriteFrameName); - if(spriteFrame) { + if (spriteFrame) { var sprite = new cc.Scale9Sprite(); sprite.setAnchorPoint(0, 0); spriteFrame.__sprite = sprite; @@ -511,27 +552,29 @@ var RichText = cc.Class({ var expectHeight = richTextElement.style.imageHeight; //follow the original rule, expectHeight must less then lineHeight - if(expectHeight > 0 && expectHeight < this.lineHeight ) { + if (expectHeight > 0 && expectHeight < this.lineHeight) { scaleFactor = expectHeight / spriteHeight; spriteWidth = spriteWidth * scaleFactor; spriteHeight = spriteHeight * scaleFactor; - } else { + } + else { scaleFactor = this.lineHeight / spriteHeight; spriteWidth = spriteWidth * scaleFactor; spriteHeight = spriteHeight * scaleFactor; } - if(expectWidth > 0) spriteWidth = expectWidth; + if (expectWidth > 0) spriteWidth = expectWidth; - if(this.maxWidth > 0) { - if(this._lineOffsetX + spriteWidth > this.maxWidth) { + if (this.maxWidth > 0) { + if (this._lineOffsetX + spriteWidth > this.maxWidth) { this._updateLineInfo(); } this._lineOffsetX += spriteWidth; - } else { + } + else { this._lineOffsetX += spriteWidth; - if(this._lineOffsetX > this._labelWidth) { + if (this._lineOffsetX > this._labelWidth) { this._labelWidth = this._lineOffsetX; } } @@ -539,12 +582,13 @@ var RichText = cc.Class({ sprite.setContentSize(spriteWidth, spriteHeight); sprite._lineCount = this._lineCount; - if(richTextElement.style.event) { - if(richTextElement.style.event.click) { + if (richTextElement.style.event) { + if (richTextElement.style.event.click) { sprite._clickHandler = richTextElement.style.event.click; } } - } else { + } + else { cc.warnID(4400); } }, @@ -553,14 +597,13 @@ var RichText = cc.Class({ if (!this.enabled) return; var newTextArray = cc.htmlTextParser.parse(this.string); - if(!this._needsUpdateTextLayout(newTextArray)) { + if (!this._needsUpdateTextLayout(newTextArray)) { this._textArray = newTextArray; this._updateLabelSegmentTextAttributes(); return; } this._textArray = newTextArray; - var sgNode = this._sgNode; this._resetState(); var lastEmptyLine = false; @@ -571,12 +614,12 @@ var RichText = cc.Class({ var richTextElement = this._textArray[i]; var text = richTextElement.text; //handle
tag - if(text === "") { - if(richTextElement.style && richTextElement.style.newline) { + if (text === "") { + if (richTextElement.style && richTextElement.style.newline) { this._updateLineInfo(); continue; } - if(richTextElement.style && richTextElement.style.isImage && this.imageAtlas) { + if (richTextElement.style && richTextElement.style.isImage && this.imageAtlas) { this._addRichTextImageElement(richTextElement); continue; } @@ -585,10 +628,10 @@ var RichText = cc.Class({ for (var j = 0; j < multilineTexts.length; ++j) { var labelString = multilineTexts[j]; - if(labelString === "") { + if (labelString === "") { //for continues \n - if(this._isLastComponentCR(text) - && j == multilineTexts.length - 1) { + if (this._isLastComponentCR(text) + && j == multilineTexts.length - 1) { continue; } this._updateLineInfo(); @@ -597,46 +640,49 @@ var RichText = cc.Class({ } lastEmptyLine = false; - if(this.maxWidth > 0) { + if (this.maxWidth > 0) { var labelWidth = this._measureText(i, labelString); this._updateRichTextWithMaxWidth(labelString, labelWidth, i); - if(multilineTexts.length > 1 && j < multilineTexts.length - 1) { + if (multilineTexts.length > 1 && j < multilineTexts.length - 1) { this._updateLineInfo(); } - } else { + } + else { label = this._addLabelSegment(labelString, i); labelSize = label.getContentSize(); this._lineOffsetX += labelSize.width; - if(this._lineOffsetX > this._labelWidth) { + if (this._lineOffsetX > this._labelWidth) { this._labelWidth = this._lineOffsetX; } - if(multilineTexts.length > 1 && j < multilineTexts.length - 1) { + if (multilineTexts.length > 1 && j < multilineTexts.length - 1) { this._updateLineInfo(); } } } } - if(!lastEmptyLine) { + if (!lastEmptyLine) { this._linesWidth.push(this._lineOffsetX); } - if(this.maxWidth > 0) { + if (this.maxWidth > 0) { this._labelWidth = this.maxWidth; } - this._labelHeight =this._lineCount * this.lineHeight; + this._labelHeight = this._lineCount * this.lineHeight; - sgNode._setContentSize(this._labelWidth, this._labelHeight); - this.node.emit('size-changed'); + // trigger "size-changed" event + this.node.setContentSize(this._labelWidth, this._labelHeight); + // use _setContentSize because sgNode.setContentSize is banned. + this._sgNode._setContentSize(this._labelWidth, this._labelHeight); this._updateRichTextPosition(); this._layoutDirty = false; }, - _getFirstWordLen: function(text, startIndex, textLen) { + _getFirstWordLen: function (text, startIndex, textLen) { var character = text.charAt(startIndex); if (cc.TextUtils.isUnicodeCJK(character) || cc.TextUtils.isUnicodeSpace(character)) { @@ -661,7 +707,7 @@ var RichText = cc.Class({ var nextTokenX = 0; var nextLineIndex = 1; var totalLineCount = this._lineCount; - for(var i = 0; i < this._labelSegments.length; ++i) { + for (var i = 0; i < this._labelSegments.length; ++i) { var label = this._labelSegments[i]; var lineCount = label._lineCount; if (lineCount > nextLineIndex) { @@ -670,17 +716,17 @@ var RichText = cc.Class({ } var lineOffsetX = 0; switch (this.horizontalAlign) { - case cc.TextAlignment.LEFT: - lineOffsetX = 0; - break; - case cc.TextAlignment.CENTER: - lineOffsetX = (this._labelWidth - this._linesWidth[lineCount - 1]) / 2; - break; - case cc.TextAlignment.RIGHT: - lineOffsetX = this._labelWidth - this._linesWidth[lineCount - 1]; - break; - default: - break; + case cc.TextAlignment.LEFT: + lineOffsetX = 0; + break; + case cc.TextAlignment.CENTER: + lineOffsetX = (this._labelWidth - this._linesWidth[lineCount - 1]) / 2; + break; + case cc.TextAlignment.RIGHT: + lineOffsetX = this._labelWidth - this._linesWidth[lineCount - 1]; + break; + default: + break; } label.setPositionX(nextTokenX + lineOffsetX); @@ -688,7 +734,7 @@ var RichText = cc.Class({ var positionY = (totalLineCount - lineCount) * this.lineHeight; - if(label instanceof cc.Scale9Sprite) { + if (label instanceof cc.Scale9Sprite) { positionY += (this.lineHeight - label.getContentSize().height) / 2; } @@ -705,13 +751,14 @@ var RichText = cc.Class({ var colorValue = color.toUpperCase(); if (cc.Color[colorValue]) { return cc.Color[colorValue]; - } else { + } + else { return cc.hexToColor(color); } }, _applyTextAttribute: function (label) { - if(label instanceof cc.Scale9Sprite) return; + if (label instanceof cc.Scale9Sprite) return; var index = label._styleIndex; label.setLineHeight(this.lineHeight); @@ -723,25 +770,29 @@ var RichText = cc.Class({ } if (textStyle && textStyle.color) { label.setColor(this._convertLiteralColorValue(textStyle.color)); - } else { + } + else { label.setColor(this.node.color); } if (textStyle && textStyle.bold) { label.enableBold(true); - } else { + } + else { label.enableBold(false); } if (textStyle && textStyle.italic) { label.enableItalics(true); - } else { + } + else { label.enableItalics(false); } if (textStyle && textStyle.underline) { label.enableUnderline(true); - } else { + } + else { label.enableUnderline(false); } @@ -750,14 +801,16 @@ var RichText = cc.Class({ label.setOutlineColor(this._convertLiteralColorValue(textStyle.outline.color)); label.setOutlineWidth(textStyle.outline.width); label.setMargin(textStyle.outline.width); - } else { + } + else { label.setOutlined(false); label.setMargin(0); } if (textStyle && textStyle.size) { label.setFontSize(textStyle.size); - } else { + } + else { label.setFontSize(this.fontSize); } @@ -776,6 +829,6 @@ var RichText = cc.Class({ } this._resetState(); } - }); +}); cc.RichText = module.exports = RichText; diff --git a/cocos2d/core/components/CCWebView.js b/cocos2d/core/components/CCWebView.js index aebea9cbb1..04943678dd 100644 --- a/cocos2d/core/components/CCWebView.js +++ b/cocos2d/core/components/CCWebView.js @@ -47,6 +47,9 @@ var EventType = _ccsg.WebView.EventType; * @property {String} ERROR */ +// +function emptyCallback () { } + /** * !#en cc.WebView is a component for display web pages in the game * !#zh WebView 组件,用于在游戏中显示网页 @@ -124,12 +127,6 @@ var WebView = cc.Class({ sgNode.createDomElementIfNeeded(); } - if (!CC_EDITOR) { - sgNode.setEventListener(EventType.LOADED, this._onWebViewLoaded.bind(this)); - sgNode.setEventListener(EventType.LOADING, this._onWebViewLoading.bind(this)); - sgNode.setEventListener(EventType.ERROR, this._onWebViewLoadError.bind(this)); - } - sgNode.loadURL(this._url); if (CC_EDITOR && this._useOriginalSize) { @@ -140,6 +137,26 @@ var WebView = cc.Class({ } }, + onEnable: function () { + this._super(); + if (!CC_EDITOR) { + var sgNode = this._sgNode; + sgNode.setEventListener(EventType.LOADED, this._onWebViewLoaded.bind(this)); + sgNode.setEventListener(EventType.LOADING, this._onWebViewLoading.bind(this)); + sgNode.setEventListener(EventType.ERROR, this._onWebViewLoadError.bind(this)); + } + }, + + onDisable: function () { + this._super(); + if (!CC_EDITOR) { + var sgNode = this._sgNode; + sgNode.setEventListener(EventType.LOADED, emptyCallback); + sgNode.setEventListener(EventType.LOADING, emptyCallback); + sgNode.setEventListener(EventType.ERROR, emptyCallback); + } + }, + _onWebViewLoaded: function () { cc.Component.EventHandler.emitEvents(this.webviewEvents, this, EventType.LOADED); this.node.emit('loaded', this); diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 3d12b4cb25..1dd914b28f 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -28,8 +28,10 @@ // https://segmentfault.com/q/1010000002914610 var SCROLLY = 40; -var TIMER_NAME = 400; var LEFT_PADDING = 2; +var DELAY_TIME = 400; +var FOCUS_DELAY_UC = 400; +var FOCUS_DELAY_FIREFOX = 0; var Utils = require('../platform/utils'); function adjustEditBoxPosition (editBox) { @@ -47,7 +49,7 @@ function adjustEditBoxPosition (editBox) { if (scrollOffset > 320) scrollOffset = 320; window.scrollTo(scrollOffset, scrollOffset); } - }, TIMER_NAME); + }, DELAY_TIME); } var capitalize = function(string) { @@ -688,6 +690,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; tmpEdTxt.style.position = "absolute"; tmpEdTxt.style.bottom = "0px"; tmpEdTxt.style.left = LEFT_PADDING + "px"; + tmpEdTxt.style['-moz-appearance'] = 'textfield'; tmpEdTxt.style.className = "cocosEditBox"; tmpEdTxt.addEventListener('input', function () { @@ -965,25 +968,33 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; this._updateLabelStringStyle(); }; - proto._beginEditing = function() { - if (!this._editBox._alwaysOnTop) { - if (this._edTxt.style.display === 'none') { - this._edTxt.style.display = ''; + proto._beginEditing = function () { + var self = this; + if (!self._editBox._alwaysOnTop) { + if (self._edTxt.style.display === 'none') { + self._edTxt.style.display = ''; + + function startFocus () { + self._edTxt.focus(); + } + if (cc.sys.browserType === cc.sys.BROWSER_TYPE_UC) { - setTimeout(function () { - this._edTxt.focus(); - }.bind(this), TIMER_NAME); - } else { - this._edTxt.focus(); + setTimeout(startFocus, FOCUS_DELAY_UC); + } + else if (cc.sys.browserType === cc.sys.BROWSER_TYPE_FIREFOX) { + setTimeout(startFocus, FOCUS_DELAY_FIREFOX); + } + else { + startFocus(); } } } - if (cc.sys.isMobile && !this._editingMode) { + if (cc.sys.isMobile && !self._editingMode) { // Pre adaptation and - this._beginEditingOnMobile(this._editBox); + self._beginEditingOnMobile(self._editBox); } - this._editingMode = true; + self._editingMode = true; }; proto._endEditing = function() { @@ -996,7 +1007,7 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; // Delay end editing adaptation to ensure virtual keyboard is disapeared setTimeout(function () { self._endEditingOnMobile(); - }, TIMER_NAME); + }, DELAY_TIME); } this._editingMode = false; }; diff --git a/cocos2d/core/graphics/graphics-webgl-cmd.js b/cocos2d/core/graphics/graphics-webgl-cmd.js index e1ae212e7b..dd0f08f17e 100644 --- a/cocos2d/core/graphics/graphics-webgl-cmd.js +++ b/cocos2d/core/graphics/graphics-webgl-cmd.js @@ -743,9 +743,9 @@ _p._expandFill = function () { if (path.complex) { var data = []; var start = offset*VERTS_FLOAT_LENGTH, end = buffer.vertsOffset*VERTS_FLOAT_LENGTH; - for (var i = start; i < end; i+=VERTS_FLOAT_LENGTH) { - data.push(buffer.vertsBuffer[i]); - data.push(buffer.vertsBuffer[i+1]); + for (var j = start; j < end; j+=VERTS_FLOAT_LENGTH) { + data.push(buffer.vertsBuffer[j]); + data.push(buffer.vertsBuffer[j+1]); } var newIndices = Earcut(data, null, 2); diff --git a/cocos2d/core/platform/CCClassDecorator.js b/cocos2d/core/platform/CCClassDecorator.js index 71cc94c678..c307c610a9 100644 --- a/cocos2d/core/platform/CCClassDecorator.js +++ b/cocos2d/core/platform/CCClassDecorator.js @@ -280,13 +280,14 @@ var ccclass = checkCtorArgument(function (ctor, name) { // validate methods if (CC_DEV) { - var methods = Object.getOwnPropertyNames(ctor.prototype); - for (var i = 0; i < methods.length; ++i) { - var methodName = methods[i]; - if (methodName !== 'constructor') { - var func = ctor.prototype[methodName]; + var propNames = Object.getOwnPropertyNames(ctor.prototype); + for (var i = 0; i < propNames.length; ++i) { + var prop = propNames[i]; + if (prop !== 'constructor') { + var desc = Object.getOwnPropertyDescriptor(ctor.prototype, prop); + var func = desc && desc.value; if (typeof func === 'function') { - Preprocess.doValidateMethodWithProps_DEV(func, methodName, JS.getClassName(ctor), ctor, base); + Preprocess.doValidateMethodWithProps_DEV(func, prop, JS.getClassName(ctor), ctor, base); } } } diff --git a/cocos2d/shape-nodes/CCDrawNode.js b/cocos2d/shape-nodes/CCDrawNode.js index a887eacf78..5868552521 100644 --- a/cocos2d/shape-nodes/CCDrawNode.js +++ b/cocos2d/shape-nodes/CCDrawNode.js @@ -743,43 +743,37 @@ cc.game.once(cc.game.EVENT_RENDERER_INITED, function () { var triangleBytesLen = cc.V2F_C4B_T2F_Triangle.BYTES_PER_ELEMENT, trianglesBuffer = this._trianglesArrayBuffer; var locBuffer = this._buffer; - var inset = (outline == false ? 0.5 : 0.0); + + var va, vb, vc; for (i = 0; i < count - 2; i++) { - v0 = cc.pSub(cc.v2(verts[0]), cc.pMult(extrude[0].offset, inset)); - v1 = cc.pSub(cc.v2(verts[i + 1]), cc.pMult(extrude[i + 1].offset, inset)); - v2 = cc.pSub(cc.v2(verts[i + 2]), cc.pMult(extrude[i + 2].offset, inset)); - locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: v0, colors: c4bFillColor, texCoords: __t(cc.v2())}, - {vertices: v1, colors: c4bFillColor, texCoords: __t(cc.v2())}, {vertices: v2, colors: c4bFillColor, texCoords: __t(cc.v2())}, - trianglesBuffer, locBuffer.length * triangleBytesLen)); + va = {vertices: verts[0], colors: c4bFillColor, texCoords: __t(cc.v2())}; + vb = {vertices: verts[i + 1], colors: c4bFillColor, texCoords: __t(cc.v2())}; + vc = {vertices: verts[i + 2], colors: c4bFillColor, texCoords: __t(cc.v2())}; + locBuffer.push(new cc.V2F_C4B_T2F_Triangle(va, vb, vc, trianglesBuffer, locBuffer.length * triangleBytesLen)); } + if (outline) { + for (i = 0; i < count; i++) { + var j = (i + 1) % count; + v0 = cc.v2(verts[i]); + v1 = cc.v2(verts[j]); - for (i = 0; i < count; i++) { - var j = (i + 1) % count; - v0 = cc.v2(verts[i]); - v1 = cc.v2(verts[j]); + var n0 = extrude[i].n; + var offset0 = extrude[i].offset; + var offset1 = extrude[j].offset; + var inner0 = cc.pSub(v0, cc.pMult(offset0, borderWidth)); + var inner1 = cc.pSub(v1, cc.pMult(offset1, borderWidth)); + var outer0 = cc.pAdd(v0, cc.pMult(offset0, borderWidth)); + var outer1 = cc.pAdd(v1, cc.pMult(offset1, borderWidth)); - var n0 = extrude[i].n; - var offset0 = extrude[i].offset; - var offset1 = extrude[j].offset; - var inner0 = outline ? cc.pSub(v0, cc.pMult(offset0, borderWidth)) : cc.pSub(v0, cc.pMult(offset0, 0.5)); - var inner1 = outline ? cc.pSub(v1, cc.pMult(offset1, borderWidth)) : cc.pSub(v1, cc.pMult(offset1, 0.5)); - var outer0 = outline ? cc.pAdd(v0, cc.pMult(offset0, borderWidth)) : cc.pAdd(v0, cc.pMult(offset0, 0.5)); - var outer1 = outline ? cc.pAdd(v1, cc.pMult(offset1, borderWidth)) : cc.pAdd(v1, cc.pMult(offset1, 0.5)); + va = { vertices: inner0, colors: c4bBorderColor, texCoords: __t(cc.pNeg(n0)) }; + vb = { vertices: inner1, colors: c4bBorderColor, texCoords: __t(cc.pNeg(n0)) }; + vc = { vertices: outer1, colors: c4bBorderColor, texCoords: __t(n0) }; + locBuffer.push(new cc.V2F_C4B_T2F_Triangle(va, vb, vc, trianglesBuffer, locBuffer.length * triangleBytesLen)); - if (outline) { - locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bBorderColor, texCoords: __t(cc.pNeg(n0))}, - {vertices: inner1, colors: c4bBorderColor, texCoords: __t(cc.pNeg(n0))}, {vertices: outer1, colors: c4bBorderColor, texCoords: __t(n0)}, - trianglesBuffer, locBuffer.length * triangleBytesLen)); - locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bBorderColor, texCoords: __t(cc.pNeg(n0))}, - {vertices: outer0, colors: c4bBorderColor, texCoords: __t(n0)}, {vertices: outer1, colors: c4bBorderColor, texCoords: __t(n0)}, - trianglesBuffer, locBuffer.length * triangleBytesLen)); - } else { - locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bFillColor, texCoords: __t(cc.v2())}, - {vertices: inner1, colors: c4bFillColor, texCoords: __t(cc.v2())}, {vertices: outer1, colors: c4bFillColor, texCoords: __t(n0)}, - trianglesBuffer, locBuffer.length * triangleBytesLen)); - locBuffer.push(new cc.V2F_C4B_T2F_Triangle({vertices: inner0, colors: c4bFillColor, texCoords: __t(cc.v2())}, - {vertices: outer0, colors: c4bFillColor, texCoords: __t(n0)}, {vertices: outer1, colors: c4bFillColor, texCoords: __t(n0)}, - trianglesBuffer, locBuffer.length * triangleBytesLen)); + va = { vertices: inner0, colors: c4bBorderColor, texCoords: __t(cc.pNeg(n0)) }; + vb = { vertices: outer0, colors: c4bBorderColor, texCoords: __t(n0) }; + vc = { vertices: outer1, colors: c4bBorderColor, texCoords: __t(n0) }; + locBuffer.push(new cc.V2F_C4B_T2F_Triangle(va, vb, vc, trianglesBuffer, locBuffer.length * triangleBytesLen)); } } extrude = null; diff --git a/cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js b/cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js index 8b536b81a9..143ce3f522 100644 --- a/cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js +++ b/cocos2d/tilemap/CCTMXLayerCanvasRenderCmd.js @@ -45,7 +45,7 @@ var proto = _ccsg.TMXLayer.CanvasRenderCmd.prototype = Object.create(_ccsg.Node. proto.constructor = _ccsg.TMXLayer.CanvasRenderCmd; proto.rendering = function (ctx, scaleX, scaleY) { - var node = this._node, hasRotation = (node._rotationX || node._rotationY), + var node = this._node, layerOrientation = node.layerOrientation, tiles = node.tiles, alpha = node._opacity / 255; @@ -80,7 +80,7 @@ proto.rendering = function (ctx, scaleX, scaleY) { maxCol = cols, maxRow = rows; if (cc.macro.ENABLE_TILEDMAP_CULLING) { - if (!hasRotation && layerOrientation === Orientation.ORTHO) { + if (layerOrientation === Orientation.ORTHO) { startCol = Math.floor(-(mapx - extw * a) / (maptw * a)); startRow = Math.floor((mapy - exth * d + mapth * rows * d - winh) / (mapth * d)); maxCol = Math.ceil((winw - mapx + extw * a) / (maptw * a)); @@ -171,7 +171,7 @@ proto.rendering = function (ctx, scaleX, scaleY) { right = left + tilew; top = bottom - tileh; // TMX_ORIENTATION_ISO trim - if (!hasRotation && layerOrientation === Orientation.ISO) { + if (layerOrientation === Orientation.ISO) { gb = -mapy + bottom*d; if (gb < -winh-h) { col += Math.floor((-winh - gb)*2/h) - 1; diff --git a/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js b/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js index 7253514b75..1dc0b82bb7 100644 --- a/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js +++ b/cocos2d/tilemap/CCTMXLayerWebGLRenderCmd.js @@ -70,7 +70,7 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset) { // enable Depth Test cc.renderer.setDepthTest(true); - var node = this._node, hasRotation = (node._rotationX || node._rotationY), + var node = this._node, layerOrientation = node.layerOrientation, tiles = node.tiles; @@ -121,7 +121,7 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset) { if (enabledCulling) { if (this._cameraFlag > 0) { - var tmpt = cc.affineTransformConcat(cc.Camera.main.viewMatrix, wt); + var tmpt = cc.affineTransformConcat(wt, cc.Camera.main.viewMatrix); cullingA = tmpt.a; cullingD = tmpt.d; cullingMapx = ox * cullingA + oy * tmpt.c + tmpt.tx; @@ -130,7 +130,7 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset) { cullingH = tileh * cullingD; } - if (!hasRotation && layerOrientation === Orientation.ORTHO) { + if (layerOrientation === Orientation.ORTHO) { startCol = Math.floor(-(cullingMapx - extw * cullingA) / (maptw * cullingA)); startRow = Math.floor((cullingMapy - exth * cullingD + mapth * rows * cullingD - winh) / (mapth * cullingD)); maxCol = Math.ceil((winw - cullingMapx + extw * cullingA) / (maptw * cullingA)); @@ -211,7 +211,7 @@ proto.uploadData = function (f32buffer, ui32buffer, vertexDataOffset) { top = bottom + tileh; // TMX_ORIENTATION_ISO trim - if (enabledCulling && !hasRotation && layerOrientation === Orientation.ISO) { + if (enabledCulling && layerOrientation === Orientation.ISO) { gb = cullingMapy + bottom*cullingD; if (gb > winh+cullingH) { col += Math.floor((gb-winh)*2/cullingH) - 1; diff --git a/gulp/util/utils.js b/gulp/util/utils.js index a96c5c6d31..a41839f33d 100644 --- a/gulp/util/utils.js +++ b/gulp/util/utils.js @@ -21,56 +21,74 @@ exports.getMacros = function (platform, isJSB, isDebugBuild) { exports.getUglifyOptions = function (platform, isJSB, isDebugBuild) { var global_defs = exports.getMacros(platform, isJSB, isDebugBuild); - if (!global_defs['CC_DEBUG']) { + var releaseMode = !global_defs['CC_DEBUG']; + + var optimizeForJSC = releaseMode && isJSB; + if (optimizeForJSC) { return { + output: { + beautify: true, // really preserve_lines + indent_level: 0, // reduce jsc file size + }, compress: { global_defs: global_defs, - // keep_infinity: true + sequences: false, + keep_infinity: true, // reduce jsc file size + typeofs: false, } }; } - return { - mangle: false, - //preserveComments: 'all', - output: { - // http://lisperator.net/uglifyjs/codegen - beautify: true, - bracketize: true, - }, - compress: { - // https://github.com/mishoo/UglifyJS2#compressor-options - global_defs: global_defs, - sequences: false, // join consecutive statements with the “comma operator” - properties: false, // optimize property access: a["foo"] → a.foo - //dead_code: true, // discard unreachable code - drop_debugger: false, // discard “debugger” statements - unsafe: false, // some unsafe optimizations (see below) - //conditionals: false, // optimize if-s and conditional expressions - comparisons: false, // optimize comparisons - //evaluate: true, // evaluate constant expressions - booleans: false, // optimize boolean expressions - typeofs: false, // Transforms typeof foo == "undefined" into foo === void 0. Note: recommend to set this value to false for IE10 and earlier versions due to known issues. - loops: false, // optimize loops - unused: false, // drop unused variables/functions - hoist_funs: false, // hoist function declarations - hoist_vars: false, // hoist variable declarations - if_return: false, // optimize if-s followed by return/continue - inline: false, // embed simple functions - join_vars: false, // join var declarations - cascade: false, // try to cascade `right` into `left` in sequences - collapse_vars: false, // Collapse single-use non-constant variables - side effects permitting. - reduce_vars: false, // Improve optimization on variables assigned with and used as constant values. - //warnings: true, - negate_iife: false, - pure_getters: false, - pure_funcs: null, - drop_console: false, - // expression: false, // Pass true to preserve completion values from terminal statements without return, e.g. in bookmarklets. - keep_fargs: true, - keep_fnames: true, - // keep_infinity: false, // Pass true to prevent Infinity from being compressed into 1/0, which may cause performance issues on Chrome. - side_effects: false // drop side-effect-free statements - } - }; + if (releaseMode) { + return { + compress: { + global_defs: global_defs, + } + }; + } + else { + return { + mangle: false, + //preserveComments: 'all', + output: { + // http://lisperator.net/uglifyjs/codegen + beautify: true, + indent_level: 2, + }, + compress: { + // https://github.com/mishoo/UglifyJS2#compressor-options + global_defs: global_defs, + sequences: false, // join consecutive statements with the “comma operator” + properties: false, // optimize property access: a["foo"] → a.foo + //dead_code: true, // discard unreachable code + drop_debugger: false, // discard “debugger” statements + unsafe: false, // some unsafe optimizations (see below) + //conditionals: false, // optimize if-s and conditional expressions + comparisons: false, // optimize comparisons + //evaluate: true, // evaluate constant expressions + booleans: false, // optimize boolean expressions + typeofs: false, // Transforms typeof foo == "undefined" into foo === void 0. Note: recommend to set this value to false for IE10 and earlier versions due to known issues. + loops: false, // optimize loops + unused: false, // drop unused variables/functions + hoist_funs: false, // hoist function declarations + hoist_vars: false, // hoist variable declarations + if_return: false, // optimize if-s followed by return/continue + inline: false, // embed simple functions + join_vars: false, // join var declarations + cascade: false, // try to cascade `right` into `left` in sequences + collapse_vars: false, // Collapse single-use non-constant variables - side effects permitting. + reduce_vars: false, // Improve optimization on variables assigned with and used as constant values. + //warnings: true, + negate_iife: false, + pure_getters: false, + pure_funcs: null, + drop_console: false, + // expression: false, // Pass true to preserve completion values from terminal statements without return, e.g. in bookmarklets. + keep_fargs: true, + keep_fnames: true, + keep_infinity: true, // Pass true to prevent Infinity from being compressed into 1/0, which may cause performance issues on Chrome. + side_effects: false // drop side-effect-free statements + } + }; + } }; diff --git a/jsb/jsb-etc.js b/jsb/jsb-etc.js index 9e39eec177..bed29cb65b 100644 --- a/jsb/jsb-etc.js +++ b/jsb/jsb-etc.js @@ -174,8 +174,8 @@ if (cc.Image && cc.Image.setPNGPremultipliedAlphaEnabled) { } // __errorHandler -window.__errorHandler = function (filename, lineno, message) { -}; +// window.__errorHandler = function (filename, lineno, message, stack) { +// }; // global cleanup. Dangerous!!! please do not invoke this function, it's used internally by the restart process window.__cleanup = function () { diff --git a/test/qunit/unit-es5/test-animation.js b/test/qunit/unit-es5/test-animation.js index a9c2b14f4a..d7bd89aebd 100644 --- a/test/qunit/unit-es5/test-animation.js +++ b/test/qunit/unit-es5/test-animation.js @@ -1358,3 +1358,35 @@ test('animation pause/resume should remove animation-actor from animation manage strictEqual(manager._anims.array.length, 0, 'should remove animation from animation manager'); }); + + +test('animation play on load', function () { + var entity = new cc.Node(); + + var animation = entity.addComponent(cc.Animation); + animation.playOnLoad = true; + + var clip1 = new cc.AnimationClip(); + clip1._name = 'clip1'; + clip1._duration = 1; + + var clip2 = new cc.AnimationClip(); + clip2._name = 'clip2'; + clip2._duration = 1; + + + animation.addClip(clip1); + animation.addClip(clip2); + + animation._defaultClip = clip1; + + animation.play('clip2'); + + entity.parent = cc.director.getScene(); + animation.start(); + + strictEqual(animation.getAnimationState('clip1').isPlaying, false, 'default clip should not be played if there is playing animation'); + strictEqual(animation.getAnimationState('clip2').isPlaying, true, 'should play the specified animation'); + + entity.parent = null; +});