Merge branch 'master' of github.com:fireball-x/engine

This commit is contained in:
2youyouo2
2015-12-21 09:25:02 +08:00
8 changed files with 194 additions and 45 deletions
+4 -4
View File
@@ -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");
}
+1 -1
View File
@@ -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: {
+1 -1
View File
@@ -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
},
+28 -20
View File
@@ -8,46 +8,54 @@ 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, lastItem,
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;
lastItem = 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) {
if (event._propagationImmediateStopped || i + increment > 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] !== lastItem) { // 如果变短
if (list[endIndex - 1] === lastItem) {
// 只支持删一个
endIndex -= 1;
}
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;
}
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;
}
}
};
+26 -1
View File
@@ -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;
+34 -7
View File
@@ -165,18 +165,45 @@ 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 lastItem = 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;
}
// TODO: check in remove
// check last one to see if any one removed
if (list[endIndex] !== lastItem && i + increment <= endIndex) { // 如果变短
if (list[endIndex - 1] === lastItem) {
// 只支持删一个
endIndex -= 1;
}
else if (list[endIndex - 2] === lastItem) {
// 只支持删一个 + target
endIndex -= 2;
}
else {
// 只允许在一个回调里面移除一个回调
return cc.error('Can remove only a callback at a time.');
}
if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加
continue;
}
}
i += increment;
}
}
};
+97 -1
View File
@@ -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,100 @@ 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 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();
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 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();
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('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 () {
+3 -10
View File
@@ -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();
});