From 44fff0d114b4fa02905228ae492d4e5f1de3547c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=AE=B6=E8=BE=89?= <35832931+ColinCollins@users.noreply.github.com> Date: Mon, 2 Jul 2018 10:35:27 +0800 Subject: [PATCH 01/12] a little modify (#2929) s --- cocos2d/core/CCGame.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index 1909710504..7af59ffe60 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -811,8 +811,8 @@ var game = { } if (CC_WECHATGAME && cc.sys.browserType !== cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB) { - wx.onShow(onShown); - wx.onHide(onHidden); + wx.onShow && wx.onShow(onShown); + wx.onHide && wx.onHide(onHidden); } if ("onpageshow" in window && "onpagehide" in window) { From ff66da2789b62da48e071538d9066ff43b9b1b97 Mon Sep 17 00:00:00 2001 From: Knox Date: Mon, 2 Jul 2018 13:13:10 +0800 Subject: [PATCH 02/12] fix audio bug for wcchatgame fireball/issues/7763 (#2931) --- cocos2d/audio/CCAudio.js | 45 ++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index 882413bf91..b9e921dcc7 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -84,18 +84,20 @@ Audio.State = { proto._bindEnded = function (callback) { callback = callback || this._onended; - if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO) { - this._element.addEventListener('ended', callback); + var elem = this._element; + if (this._src && (elem instanceof HTMLAudioElement)) { + elem.addEventListener('ended', callback); } else { - this._element.onended = callback; + elem.onended = callback; } }; proto._unbindEnded = function () { - if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO) { - this._element.removeEventListener('ended', this._onended); - } else { - this._element.onended = null; + var elem = this._element; + if (this._src && (elem instanceof HTMLAudioElement)) { + elem.removeEventListener('ended', this._onended); + } else if (elem) { + elem.onended = null; } }; @@ -107,8 +109,11 @@ Audio.State = { proto._onLoaded = function () { var elem = this._src._nativeAsset; - if (elem instanceof HTMLElement) { - this._element = document.createElement('audio'); + if (elem instanceof HTMLAudioElement) { + // Reuse dom audio element + if (!this._element) { + this._element = document.createElement('audio'); + } this._element.src = elem.src; } else { @@ -139,10 +144,11 @@ Audio.State = { this._bindEnded(); this._element.play(); - if (!(CC_QQPLAY || CC_WECHATGAME) && - this._src && this._src.loadMode === LoadMode.DOM_AUDIO && - this._element.paused) { - touchPlayList.push({ instance: this, offset: 0, audio: this._element }); + if (!CC_QQPLAY && !CC_WECHATGAME) { + if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO && + this._element.paused) { + touchPlayList.push({instance: this, offset: 0, audio: this._element}); + } } if (touchBinded) return; @@ -158,7 +164,10 @@ Audio.State = { }; proto.destroy = function () { - + if (CC_WECHATGAME) { + this._element && this._element.destroy(); + } + this._element = null; }; proto.pause = function () { @@ -264,6 +273,7 @@ Audio.State = { return this._src; }); proto.__defineSetter__('src', function (clip) { + this._unbindEnded(); if (clip) { this._src = clip; if (clip.loaded) { @@ -280,7 +290,12 @@ Audio.State = { } else { this._src = null; - this._element = null; + if (this._element instanceof HTMLAudioElement) { + this._element.src = ''; + } + else { + this._element = null; + } this._state = Audio.State.INITIALZING; } return clip; From 5125506c0297164fc7edb9f48e5e56882f81fc6c Mon Sep 17 00:00:00 2001 From: Knox Date: Tue, 3 Jul 2018 16:05:05 +0800 Subject: [PATCH 03/12] fix audio bug to wechatgame for fireball/issues/7775 (#2940) --- cocos2d/core/load-pipeline/audio-downloader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/load-pipeline/audio-downloader.js b/cocos2d/core/load-pipeline/audio-downloader.js index 07ebeee101..728e8eb479 100644 --- a/cocos2d/core/load-pipeline/audio-downloader.js +++ b/cocos2d/core/load-pipeline/audio-downloader.js @@ -38,7 +38,7 @@ function loadDomAudio (item, callback) { if (sys.platform === sys.WECHAT_GAME) { item.element = dom; - callback(null, item.url); + callback(null, item.id); return; } From e852caa44eaa1a7e0753ae591a94af1c5b61686b Mon Sep 17 00:00:00 2001 From: wangyhzzz <344204552@qq.com> Date: Wed, 4 Jul 2018 11:26:18 +0800 Subject: [PATCH 04/12] Bug: canvas measureText function would return undefined (#2937) --- cocos2d/core/label/CCSGLabelCanvasRenderCmd.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js index 030600f535..da059f1473 100644 --- a/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js +++ b/cocos2d/core/label/CCSGLabelCanvasRenderCmd.js @@ -118,6 +118,11 @@ return fontDesc; }; + proto._safeMeasureText = function (ctx, string) { + var metric = ctx.measureText(string); + return metric && metric.width || 0; + }; + proto._measureText = function (ctx) { return function(string) { return ctx.measureText(string).width; @@ -176,13 +181,13 @@ totalHeight = 0; for (i = 0; i < paragraphedStrings.length; ++i) { var j = 0; - var allWidth = this._labelContext.measureText(paragraphedStrings[i]).width; + var allWidth = this._safeMeasureText(this._labelContext, paragraphedStrings[i]); textFragment = cc.TextUtils.fragmentText(paragraphedStrings[i], allWidth, canvasWidthNoMargin, this._measureText(this._labelContext)); while(j < textFragment.length) { - var measureWidth = this._labelContext.measureText(textFragment[j]).width; + var measureWidth = this._safeMeasureText(this._labelContext, textFragment[j]); maxLength = measureWidth; totalHeight += this._getLineHeight(); ++j; @@ -228,8 +233,7 @@ var paragraphLength = []; for (var i = 0; i < paragraphedStrings.length; ++i) { - var textMetric = ctx.measureText(paragraphedStrings[i]); - paragraphLength.push(textMetric.width); + paragraphLength.push(this._safeMeasureText(ctx, paragraphedStrings[i])); } return paragraphLength; @@ -255,7 +259,7 @@ this._splitedStrings = []; var canvasWidthNoMargin = this._canvasSize.width - 2 * this._getMargin(); for (i = 0; i < paragraphedStrings.length; ++i) { - var allWidth = this._labelContext.measureText(paragraphedStrings[i]).width; + var allWidth = this._safeMeasureText(this._labelContext, paragraphedStrings[i]); var textFragment = cc.TextUtils.fragmentText(paragraphedStrings[i], allWidth, canvasWidthNoMargin, @@ -284,7 +288,7 @@ var canvasSizeX = 0; var canvasSizeY = 0; for (i = 0; i < paragraphedStrings.length; ++i) { - var paraLength = ctx.measureText(paragraphedStrings[i]).width; + var paraLength = this._safeMeasureText(ctx, paragraphedStrings[i]); canvasSizeX = canvasSizeX > paraLength ? canvasSizeX : paraLength; } canvasSizeY = this._splitedStrings.length * this._getLineHeight(); From 2d1d8543f3beafca4b69aa9e47114ba8636ef368 Mon Sep 17 00:00:00 2001 From: SijieWang Date: Fri, 6 Jul 2018 15:06:23 +0800 Subject: [PATCH 05/12] Video player current time display error (#2951) --- cocos2d/core/videoplayer/CCSGVideoPlayer.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/videoplayer/CCSGVideoPlayer.js b/cocos2d/core/videoplayer/CCSGVideoPlayer.js index 3f28bb1269..f5dd7c8d45 100644 --- a/cocos2d/core/videoplayer/CCSGVideoPlayer.js +++ b/cocos2d/core/videoplayer/CCSGVideoPlayer.js @@ -82,11 +82,13 @@ _ccsg.VideoPlayer = _ccsg.Node.extend(/** @lends _ccsg.VideoPlayer# */{ }, duration: function () { - return this._renderCmd.duration() || -1; + var duration = this._renderCmd.duration(); + return typeof duration === 'number' ? duration : -1; }, currentTime: function() { - return this._renderCmd.currentTime() || -1; + var currentTime = this._renderCmd.currentTime(); + return typeof currentTime === 'number' ? currentTime : -1; }, createDomElementIfNeeded: function () { From 2f1a1773f27fec6417b83566bde735a922d8e73f Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 11 Jul 2018 16:28:23 +0800 Subject: [PATCH 06/12] Fix RenderTexture error for Android WeChat (#2967) * Fix RenderTexture error for Android WeChat. * Fix audio clip set error and instance release safe. --- cocos2d/render-texture/CCRenderTextureWebGLRenderCmd.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos2d/render-texture/CCRenderTextureWebGLRenderCmd.js b/cocos2d/render-texture/CCRenderTextureWebGLRenderCmd.js index 445d9cc7c4..6cf19d5d55 100644 --- a/cocos2d/render-texture/CCRenderTextureWebGLRenderCmd.js +++ b/cocos2d/render-texture/CCRenderTextureWebGLRenderCmd.js @@ -236,6 +236,9 @@ proto.begin = function(){ var gl = cc._renderContext; + this._oldFBO = gl.getParameter(gl.FRAMEBUFFER_BINDING); + gl.bindFramebuffer(gl.FRAMEBUFFER, this._fBO);//Will direct drawing to the frame buffer created above + var director = cc.director; director.setProjection(director.getProjection()); @@ -259,9 +262,6 @@ proto.begin = function(){ viewport.y = (this._fullRect.y - this._rtTextureRect.y) * viewPortRectHeightRatio; gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height); - this._oldFBO = gl.getParameter(gl.FRAMEBUFFER_BINDING); - gl.bindFramebuffer(gl.FRAMEBUFFER, this._fBO);//Will direct drawing to the frame buffer created above - /* Certain Qualcomm Andreno gpu's will retain data in memory after a frame buffer switch which corrupts the render to the texture. * The solution is to clear the frame buffer before rendering to the texture. However, calling glClear has the unintended result of clearing the current texture. * Create a temporary texture to overcome this. At the end of CCRenderTexture::begin(), switch the attached texture to the second one, call glClear, From 293e31756a686e0caa89b0f6a14e9ee0eecb0b3b Mon Sep 17 00:00:00 2001 From: PP Date: Fri, 13 Jul 2018 15:36:29 +0800 Subject: [PATCH 07/12] cut debug info when build project (#2976) ensure DebugInfo ignored even if cwd not in engine --- gulp/tasks/engine.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gulp/tasks/engine.js b/gulp/tasks/engine.js index a2477e5bca..e08a916252 100644 --- a/gulp/tasks/engine.js +++ b/gulp/tasks/engine.js @@ -148,7 +148,7 @@ exports.buildCocosJsMin = function (sourceFile, outputFile, excludes, opt_macroF bundler.ignore(file); }); - bundler.ignore('./DebugInfos.json'); + bundler.ignore(Path.resolve(__dirname, '../../DebugInfos.json')); var Size = null; try { @@ -306,7 +306,7 @@ exports.buildJsbMin = function (sourceFile, outputFile, excludes, opt_macroFlags bundler.ignore(require.resolve(module)); }); - bundler.ignore('../../DebugInfos.json'); + bundler.ignore(Path.resolve(__dirname, '../../DebugInfos.json')); bundler.bundle() .on('error', HandleErrors.handler) From 1813e48491197c62b9821d425389d28752b4dd0a Mon Sep 17 00:00:00 2001 From: PP Date: Tue, 17 Jul 2018 10:28:22 +0800 Subject: [PATCH 08/12] return spriteAtlas-related spriteFrames when loadResDir (#2981) --- cocos2d/core/assets/CCSpriteAtlas.js | 11 +++++++++-- cocos2d/core/load-pipeline/CCLoader.js | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/assets/CCSpriteAtlas.js b/cocos2d/core/assets/CCSpriteAtlas.js index f4601794b7..0dde4587b7 100644 --- a/cocos2d/core/assets/CCSpriteAtlas.js +++ b/cocos2d/core/assets/CCSpriteAtlas.js @@ -62,7 +62,14 @@ var SpriteAtlas = cc.Class({ * @returns {SpriteFrame} */ getSpriteFrame: function (key) { - return this._spriteFrames[key]; + let sf = this._spriteFrames[key]; + if (!sf) { + return null; + } + if (!sf.name) { + sf.name = key; + } + return sf; }, /** @@ -75,7 +82,7 @@ var SpriteAtlas = cc.Class({ var spriteFrames = this._spriteFrames; for (var key in spriteFrames) { - frames.push(spriteFrames[key]); + frames.push(this.getSpriteFrame(key)); } return frames; diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index 8d1ed41f40..1e4851205f 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -639,7 +639,24 @@ proto.loadResDir = function (url, type, progressCallback, completeCallback) { var urls = []; var uuids = resources.getUuidArray(url, type, urls); - this._loadResUuids(uuids, progressCallback, completeCallback, urls); + this._loadResUuids(uuids, progressCallback, function (errors, assetRes, urlRes) { + // The spriteFrame url in spriteAtlas will be removed after build project + // To show users the exact structure in asset panel, we need to return the spriteFrame assets in spriteAtlas + let assetResLength = assetRes.length; + for (let i = 0; i < assetResLength; ++i) { + if (assetRes[i] instanceof cc.SpriteAtlas) { + let spriteFrames = assetRes[i].getSpriteFrames(); + for (let k in spriteFrames) { + let sf = spriteFrames[k]; + assetRes.push(sf); + if (urlRes) { + urlRes.push(`${urlRes[i]}/${sf.name}`); + } + } + } + } + completeCallback(errors, assetRes, urlRes); + }, urls); }; /** From aee8d9c0454c254af8a5945f69101018138a0503 Mon Sep 17 00:00:00 2001 From: Knox Date: Mon, 16 Jul 2018 15:13:11 +0800 Subject: [PATCH 09/12] fix load ttf error in md5 for fireball/issues/7896 (#2982) --- cocos2d/core/label/CCTextUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/label/CCTextUtils.js b/cocos2d/core/label/CCTextUtils.js index 5dd2c2cdb0..dea5b7d331 100644 --- a/cocos2d/core/label/CCTextUtils.js +++ b/cocos2d/core/label/CCTextUtils.js @@ -60,7 +60,7 @@ var CustomFontLoader = { var md5Pipe = cc.loader.md5Pipe; if (md5Pipe) { - url = md5Pipe.transformURL(url); + url = md5Pipe.transformURL(url, true); } //these platforms support window.FontFace, but it sucks sometimes. var useFontFace = (cc.sys.browserType !== cc.sys.BROWSER_TYPE_BAIDU From c42f77ab255c6c61274b1ee2be1c8b8bc2d29602 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 17 Jul 2018 16:33:34 +0800 Subject: [PATCH 10/12] Secure measureText in wechat (offscreen) --- cocos2d/core/renderer/utils/label/ttf.js | 14 +++++++------- cocos2d/core/utils/text-utils.js | 9 +++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/renderer/utils/label/ttf.js b/cocos2d/core/renderer/utils/label/ttf.js index 9a1afbb642..70ced076d2 100644 --- a/cocos2d/core/renderer/utils/label/ttf.js +++ b/cocos2d/core/renderer/utils/label/ttf.js @@ -317,7 +317,7 @@ module.exports = { let canvasSizeX = 0; let canvasSizeY = 0; for (let i = 0; i < paragraphedStrings.length; ++i) { - let paraLength = _context.measureText(paragraphedStrings[i]).width; + let paraLength = TextUtils.safeMeasureText(_context, paragraphedStrings[i]); canvasSizeX = canvasSizeX > paraLength ? canvasSizeX : paraLength; } canvasSizeY = _splitedStrings.length * this._getLineHeight(); @@ -369,7 +369,7 @@ module.exports = { _splitedStrings = []; let canvasWidthNoMargin = _canvasSize.width - 2 * _margin; for (let i = 0; i < paragraphedStrings.length; ++i) { - let allWidth = _context.measureText(paragraphedStrings[i]).width; + let allWidth = TextUtils.safeMeasureText(_context, paragraphedStrings[i]); let textFragment = TextUtils.fragmentText(paragraphedStrings[i], allWidth, canvasWidthNoMargin, @@ -408,8 +408,8 @@ module.exports = { let paragraphLength = []; for (let i = 0; i < paragraphedStrings.length; ++i) { - let textMetric = ctx.measureText(paragraphedStrings[i]); - paragraphLength.push(textMetric.width); + let width = TextUtils.safeMeasureText(ctx, paragraphedStrings[i]); + paragraphLength.push(width); } return paragraphLength; @@ -417,7 +417,7 @@ module.exports = { _measureText (ctx) { return function (string) { - return ctx.measureText(string).width; + return TextUtils.measureText(ctx, string); }; }, @@ -468,13 +468,13 @@ module.exports = { totalHeight = 0; for (i = 0; i < paragraphedStrings.length; ++i) { let j = 0; - let allWidth = _context.measureText(paragraphedStrings[i]).width; + let allWidth = TextUtils.safeMeasureText(_context, paragraphedStrings[i]); textFragment = TextUtils.fragmentText(paragraphedStrings[i], allWidth, canvasWidthNoMargin, this._measureText(_context)); while (j < textFragment.length) { - let measureWidth = _context.measureText(textFragment[j]).width; + let measureWidth = TextUtils.safeMeasureText(_context, textFragment[j]); maxLength = measureWidth; totalHeight += this._getLineHeight(); ++j; diff --git a/cocos2d/core/utils/text-utils.js b/cocos2d/core/utils/text-utils.js index 9b3147afae..09554d27cb 100644 --- a/cocos2d/core/utils/text-utils.js +++ b/cocos2d/core/utils/text-utils.js @@ -90,7 +90,7 @@ var CustomFontLoader = { } var oldWidth = this._fontWidthCache[k]; this._canvasContext.font = '40px ' + k; - var newWidth = this._canvasContext.measureText(this._testString).width; + var newWidth = TextUtils.safeMeasureText(this._canvasContext, this._testString); if(oldWidth !== newWidth) { fontDescriptor.onLoaded(); } else { @@ -169,7 +169,7 @@ var CustomFontLoader = { var fontDesc = '40px ' + fontFamilyName; this._canvasContext.font = fontDesc; - var width = this._canvasContext.measureText(this._testString).width; + var width = TextUtils.safeMeasureText(this._canvasContext, this._testString); this._fontWidthCache[fontFamilyName] = width; var self = this; @@ -227,6 +227,11 @@ var TextUtils = { return ((ch >= 9 && ch <= 13) || ch === 32 || ch === 133 || ch === 160 || ch === 5760 || (ch >= 8192 && ch <= 8202) || ch === 8232 || ch === 8233 || ch === 8239 || ch === 8287 || ch === 12288); }, + safeMeasureText = function (ctx, string) { + var metric = ctx.measureText(string); + return metric && metric.width || 0; + }, + fragmentText: function (stringToken, allWidth, maxWidth, measureText) { //check the first character var wrappedWords = []; From 83e963033328ebd8388f4fd502363bb58700feee Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 17 Jul 2018 16:33:51 +0800 Subject: [PATCH 11/12] Fix ttf loading issue --- cocos2d/core/utils/text-utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/utils/text-utils.js b/cocos2d/core/utils/text-utils.js index 09554d27cb..4be864606b 100644 --- a/cocos2d/core/utils/text-utils.js +++ b/cocos2d/core/utils/text-utils.js @@ -62,7 +62,7 @@ var CustomFontLoader = { var md5Pipe = cc.loader.md5Pipe; if (md5Pipe) { - url = md5Pipe.transformURL(url); + url = md5Pipe.transformURL(url, true); } //these platforms support window.FontFace, but it sucks sometimes. var useFontFace = (cc.sys.browserType !== cc.sys.BROWSER_TYPE_BAIDU From 69166aef92d521556a1c24bec2c46711b9457c94 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 17 Jul 2018 16:46:02 +0800 Subject: [PATCH 12/12] Fix typo --- cocos2d/core/renderer/utils/label/ttf.js | 2 +- cocos2d/core/utils/text-utils.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/renderer/utils/label/ttf.js b/cocos2d/core/renderer/utils/label/ttf.js index 70ced076d2..e52ba79304 100644 --- a/cocos2d/core/renderer/utils/label/ttf.js +++ b/cocos2d/core/renderer/utils/label/ttf.js @@ -417,7 +417,7 @@ module.exports = { _measureText (ctx) { return function (string) { - return TextUtils.measureText(ctx, string); + return TextUtils.safeMeasureText(ctx, string); }; }, diff --git a/cocos2d/core/utils/text-utils.js b/cocos2d/core/utils/text-utils.js index 4be864606b..9dbdd1cdaa 100644 --- a/cocos2d/core/utils/text-utils.js +++ b/cocos2d/core/utils/text-utils.js @@ -227,7 +227,7 @@ var TextUtils = { return ((ch >= 9 && ch <= 13) || ch === 32 || ch === 133 || ch === 160 || ch === 5760 || (ch >= 8192 && ch <= 8202) || ch === 8232 || ch === 8233 || ch === 8239 || ch === 8287 || ch === 12288); }, - safeMeasureText = function (ctx, string) { + safeMeasureText: function (ctx, string) { var metric = ctx.measureText(string); return metric && metric.width || 0; },