add progress callback for loadRes

This commit is contained in:
Jare Guo
2017-01-13 15:56:58 +08:00
parent 69da1c1968
commit 4351f7c82c
5 changed files with 93 additions and 58 deletions
-1
View File
@@ -370,7 +370,6 @@ if (CC_DEV) {
//AutoReleaseUtils: 4800
"4800": "unknown asset type %s", //getKey
//Loader: 4900
"4900": "Sorry, the 'resources://' protocol is obsoleted, use cc.loader.loadRes instead please.", //load
"4901": "loadRes: should not specify the extname in %s %s", //_getResUuid
"4902": "No need to release non-cached asset.", //setAutoRelease
"4903": "Can not get class '%s'", //uuid-loader.loadUuid
-4
View File
@@ -1308,10 +1308,6 @@ The dom control is not created!
unknown asset type
### 4900
Sorry, the 'resources://' protocol is obsoleted, use cc.loader.loadRes instead please.
### 4901
loadRes: should not specify the extname in %s %s
+61 -29
View File
@@ -182,14 +182,6 @@ JS.mixin(CCLoader.prototype, {
* @param {Function} completeCallback - Callback invoked when all resources loaded
*/
load: function(resources, progressCallback, completeCallback) {
// COMPATIBLE WITH 0.X
if (CC_DEV && typeof resources === 'string' && resources.startsWith('resources://')) {
cc.warnID(4900);
this.loadRes(resources.slice('resources://'.length), progressCallback, completeCallback);
return;
}
if (completeCallback === undefined) {
completeCallback = progressCallback;
progressCallback = this.onProgress || null;
@@ -347,6 +339,41 @@ JS.mixin(CCLoader.prototype, {
});
},
/**
* @param {Function} [type]
* @param {Function} [onProgress]
* @param {Function} onComplete
* @returns {Object} arguments
* @returns {Function} arguments.type
* @returns {Function} arguments.onProgress
* @returns {Function} arguments.onComplete
*/
_parseLoadResArgs: function (type, onProgress, onComplete) {
if (onComplete === undefined) {
var isValidType = cc.isChildClassOf(type, cc.RawAsset);
if (onProgress) {
onComplete = onProgress;
if (isValidType) {
onProgress = this.onProgress || null;
}
}
else if (onProgress === undefined && !isValidType) {
onComplete = type;
onProgress = this.onProgress || null;
type = null;
}
if (onProgress !== undefined && !isValidType) {
onProgress = type;
type = null;
}
}
return {
type: type,
onProgress: onProgress,
onComplete: onComplete,
};
},
/**
* Load resources from the "resources" folder inside the "assets" folder of your project.<br>
* <br>
@@ -356,6 +383,7 @@ JS.mixin(CCLoader.prototype, {
* @param {String} url - Url of the target resource.
* The url is relative to the "resources" folder, extensions must be omitted.
* @param {Function} [type] - Only asset of type will be loaded if this argument is supplied.
* @param {Function} [progressCallback] - Callback invoked when progression change.
* @param {Function} completeCallback - Callback invoked when the resource loaded.
* @param {Error} completeCallback.error - The error info or null if loaded successfully.
* @param {Object} completeCallback.resource - The loaded resource if it can be found otherwise returns null.
@@ -380,11 +408,11 @@ JS.mixin(CCLoader.prototype, {
* cc.log('Result should be a sprite frame: ' + (spriteFrame instanceof cc.SpriteFrame));
* });
*/
loadRes: function (url, type, completeCallback) {
if (!completeCallback && type && !cc.isChildClassOf(type, cc.RawAsset)) {
completeCallback = type;
type = null;
}
loadRes: function (url, type, progressCallback, completeCallback) {
var args = this._parseLoadResArgs(type, progressCallback, completeCallback);
type = args.type;
progressCallback = args.onProgress;
completeCallback = args.onComplete;
var self = this;
var uuid = self._getResUuid(url, type);
if (uuid) {
@@ -393,6 +421,7 @@ JS.mixin(CCLoader.prototype, {
type: 'uuid',
uuid: uuid
},
progressCallback,
function (err, asset) {
if (asset) {
// should not release these assets, even if they are static referenced in the scene.
@@ -409,9 +438,8 @@ JS.mixin(CCLoader.prototype, {
}
},
_loadResUuids: function (uuids, completeCallback) {
var remain = uuids.length;
if (remain > 0) {
_loadResUuids: function (uuids, progressCallback, completeCallback) {
if (uuids.length > 0) {
var self = this;
var res = uuids.map(function (uuid) {
return {
@@ -419,7 +447,7 @@ JS.mixin(CCLoader.prototype, {
uuid: uuid
}
});
this.load(res, function (errors, items) {
this.load(res, progressCallback, function (errors, items) {
var results = [];
for (var i = 0; i < res.length; ++i) {
var uuid = res[i].uuid;
@@ -452,6 +480,7 @@ JS.mixin(CCLoader.prototype, {
* @param {String[]} urls - Array of urls of the target resource.
* The url is relative to the "resources" folder, extensions must be omitted.
* @param {Function} [type] - Only asset of type will be loaded if this argument is supplied.
* @param {Function} [progressCallback] - Callback invoked when progression change.
* @param {Function} completeCallback - A callback which is called when all assets have been loaded, or an error occurs.
* @param {Error} completeCallback.error - If one of the asset failed, the complete callback is immediately called with the error. If all assets are loaded successfully, error will be null.
* @param {Array} completeCallback.assets - An array of all loaded assets. If nothing to load, assets will be an empty array.
@@ -469,11 +498,12 @@ JS.mixin(CCLoader.prototype, {
* // ...
* });
*/
loadResArray: function (urls, type, completeCallback) {
if (!completeCallback && type && !cc.isChildClassOf(type, cc.RawAsset)) {
completeCallback = type;
type = null;
}
loadResArray: function (urls, type, progressCallback, completeCallback) {
var args = this._parseLoadResArgs(type, progressCallback, completeCallback);
type = args.type;
progressCallback = args.onProgress;
completeCallback = args.onComplete;
var uuids = [];
for (var i = 0; i < urls.length; i++) {
var url = urls[i];
@@ -486,7 +516,7 @@ JS.mixin(CCLoader.prototype, {
return;
}
}
this._loadResUuids(uuids, completeCallback);
this._loadResUuids(uuids, progressCallback, completeCallback);
},
/**
@@ -498,6 +528,7 @@ JS.mixin(CCLoader.prototype, {
* @param {String} url - Url of the target folder.
* The url is relative to the "resources" folder, extensions must be omitted.
* @param {Function} [type] - Only asset of type will be loaded if this argument is supplied.
* @param {Function} [progressCallback] - Callback invoked when progression change.
* @param {Function} completeCallback - A callback which is called when all assets have been loaded, or an error occurs.
* @param {Error} completeCallback.error - If one of the asset failed, the complete callback is immediately called with the error. If all assets are loaded successfully, error will be null.
* @param {Object[]} completeCallback.assets - An array of all loaded assets. If nothing to load, assets will be an empty array.
@@ -524,13 +555,14 @@ JS.mixin(CCLoader.prototype, {
* var texture2 = textures[1];
* });
*/
loadResDir: function (url, type, completeCallback) {
if (!completeCallback && type && !cc.isChildClassOf(type, cc.RawAsset)) {
completeCallback = type;
type = null;
}
loadResDir: function (url, type, progressCallback, completeCallback) {
var args = this._parseLoadResArgs(type, progressCallback, completeCallback);
type = args.type;
progressCallback = args.onProgress;
completeCallback = args.onComplete;
var uuids = resources.getUuidArray(url, type);
this._loadResUuids(uuids, completeCallback);
this._loadResUuids(uuids, progressCallback, completeCallback);
},
/**
-24
View File
@@ -31,14 +31,8 @@ asyncTest('Load', function () {
loader.releaseAll();
strictEqual(Object.keys(loader._cache).length, 0, 'should clear loading items after releaseAll called');
clearTimeout(timeoutId);
start();
});
var timeoutId = setTimeout(function () {
ok(false, 'time out!');
start();
}, 5000);
});
asyncTest('Load single file', function () {
@@ -56,14 +50,8 @@ asyncTest('Load single file', function () {
ok(texture instanceof cc.Texture2D, 'the single result should be Texture2D');
loader.releaseAll();
clearTimeout(timeoutId);
start();
});
var timeoutId = setTimeout(function () {
ok(false, 'time out!');
start();
}, 5000);
});
asyncTest('Load with dependencies', function () {
@@ -134,14 +122,8 @@ asyncTest('Load with dependencies', function () {
progressCallback.expect(total, 'should call ' + total + ' times progress callback for ' + total + ' resources');
loader.releaseAll();
clearTimeout(timeoutId);
start();
});
var timeoutId = setTimeout(function () {
ok(false, 'time out!');
start();
}, 5000);
});
asyncTest('Loading font', function () {
@@ -174,12 +156,6 @@ asyncTest('Loading font', function () {
ok(items.isCompleted(), 'be able to load all resources');
progressCallback.expect(total, 'should call ' + total + ' times progress callback for ' + total + ' resources');
clearTimeout(timeoutId);
start();
});
var timeoutId = setTimeout(function () {
ok(false, 'time out!');
start();
}, 5000);
});
+32
View File
@@ -112,4 +112,36 @@
start();
});
});
test('parse loadRes arguments', function () {
var args;
function onProgress () {}
function onComplete () {}
function assert (args, type, onProgress, onComplete, testName) {
strictEqual(args.type, type, 'type of ' + testName + ' should be correct');
equal(args.onProgress, onProgress, 'progress of ' + testName + ' should be correct');
equal(args.onComplete, onComplete, 'complete of ' + testName + ' should be correct');
}
args = cc.loader._parseLoadResArgs(TestSprite, onProgress, onComplete);
assert(args, TestSprite, onProgress, onComplete, 'case 1');
args = cc.loader._parseLoadResArgs(TestSprite, onProgress, null);
assert(args, TestSprite, onProgress, null, 'case 2');
args = cc.loader._parseLoadResArgs(TestSprite, null, onComplete);
assert(args, TestSprite, null, onComplete, 'case 3');
args = cc.loader._parseLoadResArgs(TestSprite, onComplete);
assert(args, TestSprite, null, onComplete, 'case 4');
args = cc.loader._parseLoadResArgs(TestSprite);
assert(args, TestSprite, null, null, 'case 5');
args = cc.loader._parseLoadResArgs(onProgress, onComplete);
assert(args, null, onProgress, onComplete, 'case 6');
args = cc.loader._parseLoadResArgs(null, onComplete);
assert(args, null, null, onComplete, 'case 7');
args = cc.loader._parseLoadResArgs(onProgress, null);
assert(args, null, onProgress, null, 'case 8');
args = cc.loader._parseLoadResArgs(onComplete);
assert(args, null, null, onComplete, 'case 9');
});
})();