Browse Source

replaced Texture onLoad by waitLoad

ncannasse 8 years ago
parent
commit
394ada487a
2 changed files with 19 additions and 13 deletions
  1. 13 11
      h3d/mat/Texture.hx
  2. 6 2
      hxd/res/Image.hx

+ 13 - 11
h3d/mat/Texture.hx

@@ -38,17 +38,11 @@ class Texture {
 
 	var lastFrame : Int;
 	var bits : Int;
+	var waitLoads : Array<Void -> Void>;
 	public var mipMap(default,set) : MipMap;
 	public var filter(default,set) : Filter;
 	public var wrap(default, set) : Wrap;
 
-	/**
-		Some textures might take some time to load. You can check flags.has(Loading)
-		or bind onLoaded which will get called either immediately if the texture is already loaded
-		or when loading is complete.
-	**/
-	public var onLoaded(default, set) : Void -> Void;
-
 	/**
 		If this callback is set, the texture can be re-allocated when the 3D context has been lost or when
 		it's been free because of lack of memory.
@@ -115,10 +109,18 @@ class Texture {
 		lastFrame = 0x7FFFFFFF;
 	}
 
-	function set_onLoaded(v) {
-		onLoaded = v;
-		if( v != null && !flags.has(Loading) ) v();
-		return v;
+	/**
+		Some textures might take some time to load. You can check flags.has(Loading)
+		or add a waitLoad callback which will get called either immediately if the texture is already loaded
+		or when loading is complete.
+	**/
+	public function waitLoad( f : Void -> Void ) {
+		if( !flags.has(Loading) ) {
+			f();
+			return;
+		}
+		if( waitLoads == null ) waitLoads = [];
+		waitLoads.push(f);
 	}
 
 	function toString() {

+ 6 - 2
hxd/res/Image.hx

@@ -31,7 +31,7 @@ class Image extends Resource {
 	**/
 	public static var ALLOW_NPOT = #if (flash && !flash11_8) false #else true #end;
 	public static var DEFAULT_FILTER : h3d.mat.Data.Filter = Linear;
-	
+
 	/**
 		Forces async decoding for images if available on the target platform.
 	**/
@@ -240,7 +240,11 @@ class Image extends Resource {
 				bmp.dispose();
 				tex.realloc = loadTexture;
 				tex.flags.unset(Loading);
-				if( tex.onLoaded != null ) tex.onLoaded();
+				@:privateAccess if( tex.waitLoads != null ) {
+					var arr = tex.waitLoads;
+					tex.waitLoads = null;
+					for( f in arr ) f();
+				}
 				watch(watchCallb);
 			});
 		}