From bb5a2974135a7789e05ca603b0348d54c42620a0 Mon Sep 17 00:00:00 2001 From: Nan Wang Date: Sun, 20 Dec 2015 23:22:13 +0800 Subject: [PATCH 1/4] update scrollview menu path. --- cocos2d/core/components/CCScrollBar.js | 2 +- cocos2d/core/components/CCScrollView.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/components/CCScrollBar.js b/cocos2d/core/components/CCScrollBar.js index bb7ae30e37..422456e4d8 100644 --- a/cocos2d/core/components/CCScrollBar.js +++ b/cocos2d/core/components/CCScrollBar.js @@ -51,7 +51,7 @@ var Scrollbar = cc.Class({ extends: require('./CCComponent'), editor: CC_EDITOR && { - menu: 'UI/Scrollbar' + menu: 'i18n:MAIN_MENU.component.ui/ScrollBar' }, properties: { diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index e6eca59b7c..29ee154f50 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -52,7 +52,7 @@ var ScrollView = cc.Class({ mixins: [EventTarget], editor: CC_EDITOR && { - menu: 'UI/ScrollView', + menu: 'i18n:MAIN_MENU.component.ui/ScrollView', executeInEditMode: true }, From 58fcbc1f8a0d1dd04e7883dd3c9d90459e079c2b Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Sun, 20 Dec 2015 23:55:14 +0800 Subject: [PATCH 2/4] callbacks invoker should supports remove operation during invoking, like event-listeners --- cocos2d/core/CCNode.js | 8 ++-- cocos2d/core/event/event-listeners.js | 48 +++++++++++++-------- cocos2d/core/platform/callbacks-invoker.js | 43 ++++++++++++++++--- test/qunit/unit/test-callbacks-invoker.js | 50 +++++++++++++++++++++- test/qunit/unit/test-spriteRenderer.js | 13 ++---- 5 files changed, 121 insertions(+), 41 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 1ee9f7c159..3f706b3ee1 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -346,11 +346,11 @@ var Node = cc.Class({ */ _addComponentAt: CC_EDITOR && function (comp, index) { if (this._objFlags & Destroying) { - return cc.error('isDestroying');; + return cc.error('isDestroying'); + } + if ( !(comp instanceof cc.Component) ) { + return cc.error("_addComponentAt: The component to add must be a constructor"); } - // if (typeof comp !== 'function') { - // return cc.error("_addComponentAt: The component to add must be a constructor"); - // } if (index > this._components.length) { return cc.error("_addComponentAt: Index out of range"); } diff --git a/cocos2d/core/event/event-listeners.js b/cocos2d/core/event/event-listeners.js index fa33d92d20..399e247b69 100644 --- a/cocos2d/core/event/event-listeners.js +++ b/cocos2d/core/event/event-listeners.js @@ -8,46 +8,56 @@ function EventListeners () { JS.extend(EventListeners, CallbacksHandler); EventListeners.prototype.invoke = function (event) { - var list = this._callbackTable[event.type], - i, endIndex, offset, valid, + var list = this._callbackTable[event.type], + i, endIndex, expectedLastFunc, callingFunc, target, hasTarget; - if (list && list.length > 0) { + if (list) { if (list.length === 1) { list[0].call(event.currentTarget, event); return; } endIndex = list.length - 1; + expectedLastFunc = list[endIndex]; for (i = 0; i <= endIndex;) { callingFunc = list[i]; target = list[i+1]; - if (target && typeof target === 'object') { - hasTarget = true; + hasTarget = target && typeof target === 'object'; + var increment; + if (hasTarget) { callingFunc.call(target, event); + increment = 2; } else { - hasTarget = false; callingFunc.call(event.currentTarget, event); + increment = 1; } + if (event._propagationImmediateStopped || i === endIndex) { break; } // 为了不每次触发消息时都创建一份回调数组的拷贝,这里需要对消息的反注册做检查和限制 // check last one to see if any one removed - offset = endIndex - (list.length-1); - valid = true; - if (offset <= 2) { // 只删一个item - if (list[i] !== callingFunc) { // 如果删了后面的回调,索引不变 - i -= offset; + if (list[endIndex] !== expectedLastFunc) { // 如果变短 + var removedCount = 1; + var secToLast = list[endIndex - removedCount]; + if (secToLast && typeof secToLast === 'object') { + // is target + removedCount = 2; + secToLast = list[endIndex - removedCount]; + } + if (secToLast === expectedLastFunc) { // 只支持删一个 + endIndex -= removedCount; + if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加 + continue; + } + } + else { + // 只允许在一个回调里面移除一个回调。如果要移除很多,只能用 event.stopPropagationImmediate() + return cc.error('Call event.stopPropagationImmediate() when you remove more than one callbacks in a event callback.'); } - endIndex -= offset; } - else if (offset !== 0) { - // 只允许在一个回调里面移除一个回调。如果要移除很多,只能用 event.stopPropagationImmediate() - cc.error('Call event.stopPropagationImmediate() when you remove more than one callbacks in a event callback.'); - return; - } - // Increment i - hasTarget ? i += 2 : ++i; + + i += increment; } } }; diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index 6bfa11c310..335b809745 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -165,18 +165,47 @@ if (CC_TEST) { * @param {any} [p5] */ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { - var list = this._callbackTable[key], i, l, target; + var list = this._callbackTable[key]; if (list) { - for (i = 0, l = list.length; i < l;) { - target = list[i+1]; - if (target && typeof target === 'object') { + var endIndex = list.length - 1; + var expectedLastFunc = list[endIndex]; + for (var i = 0; i <= endIndex;) { + var callingFunc = list[i]; + var target = list[i + 1]; + var hasTarget = target && typeof target === 'object'; + var increment; + if (hasTarget) { list[i].call(target, p1, p2, p3, p4, p5); - i += 2; + increment = 2; } else { - list[i](p1, p2, p3, p4, p5); - ++i; + callingFunc(p1, p2, p3, p4, p5); + increment = 1; } + + // 为了不每次触发消息时都创建一份回调数组的拷贝,这里需要对消息的反注册做检查和限制 + // check last one to see if any one removed + if (list[endIndex] !== expectedLastFunc && i !== endIndex) { // 如果变短 + var removedCount = 1; + var secToLast = list[endIndex - removedCount]; + if (secToLast && typeof secToLast === 'object') { + // is target + removedCount = 2; + secToLast = list[endIndex - removedCount]; + } + if (secToLast === expectedLastFunc) { // 只支持删一个 + endIndex -= removedCount; + if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加 + continue; + } + } + else { + // 只允许在一个回调里面移除一个回调。如果要移除很多,只能用 event.stopPropagationImmediate() + return cc.error('Call event.stopPropagationImmediate() when you remove more than one callbacks in a event callback.'); + } + } + + i += increment; } } }; diff --git a/test/qunit/unit/test-callbacks-invoker.js b/test/qunit/unit/test-callbacks-invoker.js index ea095a07a9..431455d972 100644 --- a/test/qunit/unit/test-callbacks-invoker.js +++ b/test/qunit/unit/test-callbacks-invoker.js @@ -1,4 +1,6 @@ -test('CallbacksInvoker', function () { +module('CallbacksInvoker'); + +test('test', function () { var ci = new cc._Test.CallbacksInvoker(); var cb1 = new Callback(); @@ -41,6 +43,52 @@ test('CallbacksInvoker', function () { ci.invoke('a'); }); +test('remove self during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback(function () { + ci.remove('eve', cb1); + }).enable(); + var cb2 = new Callback().enable(); + + ci.add('eve', cb1); + ci.add('eve', cb2); + ci.invoke('eve'); + cb2.once('it should be called correctly if previous callback deregistered itself'); + + cb1.disable('should not call after removed'); + ci.invoke('eve'); +}); + +test('remove previous during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback().enable(); + var cb2 = new Callback(function () { + ci.remove('eve', cb1); + }).enable(); + + ci.add('eve', cb1); + ci.add('eve', cb2); + ci.invoke('eve'); + + strictEqual(ci.has('eve', cb1), false, 'previous callback should be removed'); + strictEqual(ci.has('eve', cb2), true, 'self callback should not be removed'); +}); + +test('remove last during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback().enable(); + var cb2 = new Callback(function () { + ci.remove('eve', cb2); + }).enable(); + + ci.add('eve', cb1); + ci.add('eve', cb2); + ci.invoke('eve'); + + strictEqual(ci.has('eve', cb1), true, 'previous callback should not be removed'); + strictEqual(ci.has('eve', cb2), false, 'self callback should be removed'); +}); + test('CallbacksInvoker support target', function () { var ci = new cc._Test.CallbacksInvoker(); var cb1 = function () { diff --git a/test/qunit/unit/test-spriteRenderer.js b/test/qunit/unit/test-spriteRenderer.js index 7b4e5da381..96da914761 100644 --- a/test/qunit/unit/test-spriteRenderer.js +++ b/test/qunit/unit/test-spriteRenderer.js @@ -11,18 +11,11 @@ asyncTest('basic test', function () { deepEqual(render._sgNode.color, Color.RED, 'color set success'); - var texture = new cc.Texture2D(); - texture.url = url; - cc.loader.load(texture.url, function (err) { - texture.handleLoadedTexture(cc.path.extname(url) === '.png'); - }); - cc.textureCache.cacheImage(url, texture); - var newSprite = new cc.SpriteFrame(); - newSprite.setTexture(texture); + newSprite.initWithTexture(url, cc.rect(0, 0, 10, 10)); render.sprite = newSprite; - - render.sprite.on('load', function () { + var tex = newSprite.getTexture(); + tex.once('load', function () { strictEqual(render._sgNode._scale9Image.texture.url, url, 'texture set success'); start(); }); From 3cb0582920d423a3e3be3e6e21bf19f520d86b4f Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Mon, 21 Dec 2015 01:39:03 +0800 Subject: [PATCH 3/4] callbacks invoker should supports remove operation during invoking, like event-listeners --- cocos2d/core/event/event-listeners.js | 28 ++++++------- cocos2d/core/platform/callbacks-invoker.js | 30 +++++++------- test/qunit/unit/test-callbacks-invoker.js | 48 ++++++++++++++++++++++ 3 files changed, 75 insertions(+), 31 deletions(-) diff --git a/cocos2d/core/event/event-listeners.js b/cocos2d/core/event/event-listeners.js index 399e247b69..e27b02b7e7 100644 --- a/cocos2d/core/event/event-listeners.js +++ b/cocos2d/core/event/event-listeners.js @@ -9,7 +9,7 @@ JS.extend(EventListeners, CallbacksHandler); EventListeners.prototype.invoke = function (event) { var list = this._callbackTable[event.type], - i, endIndex, expectedLastFunc, + i, endIndex, lastItem, callingFunc, target, hasTarget; if (list) { if (list.length === 1) { @@ -17,7 +17,7 @@ EventListeners.prototype.invoke = function (event) { return; } endIndex = list.length - 1; - expectedLastFunc = list[endIndex]; + lastItem = list[endIndex]; for (i = 0; i <= endIndex;) { callingFunc = list[i]; target = list[i+1]; @@ -32,29 +32,27 @@ EventListeners.prototype.invoke = function (event) { increment = 1; } - if (event._propagationImmediateStopped || i === endIndex) { + if (event._propagationImmediateStopped || i + increment > endIndex) { break; } // 为了不每次触发消息时都创建一份回调数组的拷贝,这里需要对消息的反注册做检查和限制 // check last one to see if any one removed - if (list[endIndex] !== expectedLastFunc) { // 如果变短 - var removedCount = 1; - var secToLast = list[endIndex - removedCount]; - if (secToLast && typeof secToLast === 'object') { - // is target - removedCount = 2; - secToLast = list[endIndex - removedCount]; + if (list[endIndex] !== lastItem) { // 如果变短 + if (list[endIndex - 1] === lastItem) { + // 只支持删一个 + endIndex -= 1; } - if (secToLast === expectedLastFunc) { // 只支持删一个 - endIndex -= removedCount; - if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加 - continue; - } + else if (list[endIndex - 2] === lastItem) { + // 只支持删一个 + target + endIndex -= 2; } else { // 只允许在一个回调里面移除一个回调。如果要移除很多,只能用 event.stopPropagationImmediate() return cc.error('Call event.stopPropagationImmediate() when you remove more than one callbacks in a event callback.'); } + if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加 + continue; + } } i += increment; diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index 335b809745..17e62e82ee 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -168,7 +168,7 @@ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { var list = this._callbackTable[key]; if (list) { var endIndex = list.length - 1; - var expectedLastFunc = list[endIndex]; + var lastItem = list[endIndex]; for (var i = 0; i <= endIndex;) { var callingFunc = list[i]; var target = list[i + 1]; @@ -183,25 +183,23 @@ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { increment = 1; } - // 为了不每次触发消息时都创建一份回调数组的拷贝,这里需要对消息的反注册做检查和限制 + // TODO: check in remove // check last one to see if any one removed - if (list[endIndex] !== expectedLastFunc && i !== endIndex) { // 如果变短 - var removedCount = 1; - var secToLast = list[endIndex - removedCount]; - if (secToLast && typeof secToLast === 'object') { - // is target - removedCount = 2; - secToLast = list[endIndex - removedCount]; + if (list[endIndex] !== lastItem && i + increment <= endIndex) { // 如果变短 + if (list[endIndex - 1] === lastItem) { + // 只支持删一个 + endIndex -= 1; } - if (secToLast === expectedLastFunc) { // 只支持删一个 - endIndex -= removedCount; - if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加 - continue; - } + else if (list[endIndex - 2] === lastItem) { + // 只支持删一个 + target + endIndex -= 2; } else { - // 只允许在一个回调里面移除一个回调。如果要移除很多,只能用 event.stopPropagationImmediate() - return cc.error('Call event.stopPropagationImmediate() when you remove more than one callbacks in a event callback.'); + // 只允许在一个回调里面移除一个回调 + return cc.error('Can remove only a callback at a time.'); + } + if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加 + continue; } } diff --git a/test/qunit/unit/test-callbacks-invoker.js b/test/qunit/unit/test-callbacks-invoker.js index 431455d972..727ae748bb 100644 --- a/test/qunit/unit/test-callbacks-invoker.js +++ b/test/qunit/unit/test-callbacks-invoker.js @@ -59,6 +59,23 @@ test('remove self during invoking', function () { ci.invoke('eve'); }); +test('remove self with target during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback(function () { + ci.remove('eve', cb1, target); + }).enable(); + var cb2 = new Callback().enable(); + var target = {}; + + ci.add('eve', cb1, target); + ci.add('eve', cb2, target); + ci.invoke('eve'); + cb2.once('it should be called correctly if previous callback deregistered itself'); + + cb1.disable('should not call after removed'); + ci.invoke('eve'); +}); + test('remove previous during invoking', function () { var ci = new cc._Test.CallbacksInvoker(); var cb1 = new Callback().enable(); @@ -74,6 +91,22 @@ test('remove previous during invoking', function () { strictEqual(ci.has('eve', cb2), true, 'self callback should not be removed'); }); +test('remove previous with target during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback().enable(); + var cb2 = new Callback(function () { + ci.remove('eve', cb1, target); + }).enable(); + var target = {}; + + ci.add('eve', cb1, target); + ci.add('eve', cb2, target); + ci.invoke('eve'); + + strictEqual(ci.has('eve', cb1, target), false, 'previous callback should be removed'); + strictEqual(ci.has('eve', cb2, target), true, 'self callback should not be removed'); +}); + test('remove last during invoking', function () { var ci = new cc._Test.CallbacksInvoker(); var cb1 = new Callback().enable(); @@ -89,6 +122,21 @@ test('remove last during invoking', function () { strictEqual(ci.has('eve', cb2), false, 'self callback should be removed'); }); +test('remove last with target during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback().enable(); + var cb2 = new Callback(function () { + ci.remove('eve', cb2, target); + }).enable(); + var target = {}; + ci.add('eve', cb1, target); + ci.add('eve', cb2, target); + ci.invoke('eve'); + + strictEqual(ci.has('eve', cb1, target), true, 'previous callback should not be removed'); + strictEqual(ci.has('eve', cb2, target), false, 'self callback should be removed'); +}); + test('CallbacksInvoker support target', function () { var ci = new cc._Test.CallbacksInvoker(); var cb1 = function () { From 60e0622755253092ad41d3f3ab37162ca16a2dde Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Mon, 21 Dec 2015 02:03:06 +0800 Subject: [PATCH 4/4] should still check images loading state --- cocos2d/core/platform/CCAssetLibrary.js | 27 ++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 16ccd127e3..596bccdd1d 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -368,12 +368,37 @@ var AssetLibrary = { return; } else if (isRawAsset) { - cc.loader.load(dependsUrl, function (err) { + cc.loader.load(dependsUrl, function (err, assets) { if (err) { cc.error('[AssetLibrary] Failed to load "%s"', dependsUrl); obj[prop] = ''; } else { + var img = assets[0]; + if (typeof Image !== 'undefined' && img instanceof Image) { + // HACK - https://github.com/fireball-x/fireball/issues/668 + var loaded = img.width > 0 || img.height > 0; + if ( !loaded ) { + function loadCallback () { + img.removeEventListener('load', loadCallback, false); + img.removeEventListener('error', errorCallback, false); + + obj[prop] = dependsUrl; + --pendingCount; + if (callback && pendingCount === 0) { + callback(); + callback = null; + } + } + function errorCallback () { + img.removeEventListener('load', loadCallback, false); + img.removeEventListener('error', errorCallback, false); + } + img.addEventListener('load', loadCallback); + img.addEventListener('error', errorCallback); + return; + } + } obj[prop] = dependsUrl; } --pendingCount;