fixed: html5 async asset loading

This commit is contained in:
ProPuke
2015-06-13 15:15:36 +01:00
parent 3c98ca61ac
commit 182e010424

View File

@@ -17,7 +17,10 @@ import lime.Assets;
import sys.FileSystem; import sys.FileSystem;
#end #end
#if flash #if (js && html5)
import lime.net.URLLoader;
import lime.net.URLRequest;
#elseif flash
import flash.display.Bitmap; import flash.display.Bitmap;
import flash.display.BitmapData; import flash.display.BitmapData;
import flash.display.Loader; import flash.display.Loader;
@@ -516,6 +519,25 @@ class DefaultAssetLibrary extends AssetLibrary {
handler (getBytes (id)); handler (getBytes (id));
} }
#elseif html5
if (path.exists (id)) {
var loader = new URLLoader ();
loader.dataFormat = BINARY;
loader.onComplete.add (function (_):Void {
handler(loader.data);
});
loader.load (new URLRequest (path.get (id)));
} else {
handler (getBytes (id));
}
#else #else
@@ -547,6 +569,24 @@ class DefaultAssetLibrary extends AssetLibrary {
} }
#elseif html5
if (path.exists (id)) {
var image = new js.html.Image ();
image.onload = function (_):Void {
handler (Image.fromImageElement (image));
}
image.src = id;
} else {
handler (getImage (id));
}
#else #else
handler (getImage (id)); handler (getImage (id));
@@ -653,15 +693,15 @@ class DefaultAssetLibrary extends AssetLibrary {
public override function loadText (id:String, handler:String -> Void):Void { public override function loadText (id:String, handler:String -> Void):Void {
//#if html5 #if html5
/*if (path.exists (id)) { if (path.exists (id)) {
var loader = new URLLoader (); var loader = new URLLoader ();
loader.addEventListener (Event.COMPLETE, function (event:Event) { loader.onComplete.add (function (_):Void {
handler (event.currentTarget.data); handler(loader.data);
}); });
loader.load (new URLRequest (path.get (id))); loader.load (new URLRequest (path.get (id)));
@@ -669,9 +709,9 @@ class DefaultAssetLibrary extends AssetLibrary {
handler (getText (id)); handler (getText (id));
}*/ }
//#else #else
var callback = function (bytes:ByteArray):Void { var callback = function (bytes:ByteArray):Void {
@@ -689,7 +729,7 @@ class DefaultAssetLibrary extends AssetLibrary {
loadBytes (id, callback); loadBytes (id, callback);
//#end #end
} }