Improve loadFromFile to support URLs on all platforms, deprecate AudioBuffer.fromURL and onload/onerror callbacks in Image

This commit is contained in:
Joshua Granick
2017-02-15 13:48:32 -08:00
parent da2d0b09a9
commit b36aa5ff91
3 changed files with 46 additions and 8 deletions

View File

@@ -21,6 +21,7 @@ import lime.math.color.RGBA;
import lime.math.ColorMatrix;
import lime.math.Rectangle;
import lime.math.Vector2;
import lime.net.HTTPRequest;
import lime.system.CFFI;
import lime.utils.ArrayBuffer;
import lime.utils.UInt8Array;
@@ -477,7 +478,7 @@ class Image {
}
public static function fromBase64 (base64:String, type:String, onload:Image->Void):Image {
public static function fromBase64 (base64:String, type:String #if (lime < "4.0.0"), onload:Image->Void #end):Image {
if (base64 == null) return null;
var image = new Image ();
@@ -505,7 +506,7 @@ class Image {
}
public static function fromBytes (bytes:Bytes, onload:Image->Void = null):Image {
public static function fromBytes (bytes:Bytes #if (lime < "4.0.0"), onload:Image->Void = null #end):Image {
if (bytes == null) return null;
var image = new Image ();
@@ -531,7 +532,7 @@ class Image {
}
public static function fromFile (path:String, onload:Image -> Void = null, onerror:Void -> Void = null):Image {
public static function fromFile (path:String #if (lime < "4.0.0"), onload:Image -> Void = null, onerror:Void -> Void = null #end):Image {
if (path == null) return null;
var image = new Image ();
@@ -912,7 +913,20 @@ class Image {
#else
return new Future<Image> (function () return fromFile (path), true);
var request = new HTTPRequest<Image> ();
return request.load (path).then (function (image) {
if (image != null) {
return Future.withValue (image);
} else {
return cast Future.withError ("");
}
});
#end

View File

@@ -235,6 +235,7 @@ class AudioBuffer {
}
#if (lime < "4.0.0")
public static function fromURL (url:String, handler:AudioBuffer->Void):Void {
#if (js && html5 && howlerjs)
@@ -285,6 +286,7 @@ class AudioBuffer {
#end
}
#end
#if lime_vorbis
@@ -380,10 +382,18 @@ class AudioBuffer {
// TODO: Streaming
var request = new HTTPRequest<Bytes> ();
return request.load (path).then (function (bytes) {
var request = new HTTPRequest<AudioBuffer> ();
return request.load (path).then (function (buffer) {
return Future.withValue (AudioBuffer.fromBytes (bytes));
if (buffer != null) {
return Future.withValue (buffer);
} else {
return cast Future.withError ("");
}
});

View File

@@ -8,6 +8,7 @@ import lime.app.Promise;
import lime.graphics.Image;
import lime.graphics.ImageBuffer;
import lime.math.Vector2;
import lime.net.HTTPRequest;
import lime.system.System;
import lime.utils.UInt8Array;
@@ -129,7 +130,20 @@ class Font {
public static function loadFromFile (path:String):Future<Font> {
return Future.withValue (fromFile (path));
var request = new HTTPRequest<Font> ();
return request.load (path).then (function (font) {
if (font != null) {
return Future.withValue (font);
} else {
return cast Future.withError ("");
}
});
}