Browse Source

FX2D: Fix reload atlas, handle loop anim2d

Tom Spira 6 years ago
parent
commit
0a2f4c6fad
3 changed files with 36 additions and 15 deletions
  1. 6 5
      hide/Ide.hx
  2. 6 7
      hrt/prefab/Atlas.hx
  3. 24 3
      hrt/prefab/fx/FX2D.hx

+ 6 - 5
hide/Ide.hx

@@ -993,16 +993,17 @@ class CustomLoader extends hxd.res.Loader {
 	}
 
 	override function loadCache<T:hxd.res.Resource>( path : String, c : Class<T> ) : T {
-		if( (c:Dynamic) == (hxd.res.Image:Dynamic) )
-			return cast loadImage(path);
+		if( (c:Dynamic) == (hxd.res.Image:Dynamic) || (c:Dynamic) == (hxd.res.Atlas:Dynamic))
+			return cast loadEngineCache(path, c);
 		return super.loadCache(path, c);
 	}
 
-	function loadImage( path : String ) {
+	function loadEngineCache<T:hxd.res.Resource>( path : String, c : Class<T>) : T {
 		var engine = h3d.Engine.getCurrent();
-		var i : hxd.res.Image = @:privateAccess engine.resCache.get(getKey(path));
+		var i = Std.downcast(@:privateAccess engine.resCache.get(getKey(path)), c);
 		if( i == null ) {
-			i = new hxd.res.Image(fs.get(path));
+			i = Type.createInstance(c, [fs.get(path)]);
+			// i = new hxd.res.Image(fs.get(path));
 			@:privateAccess engine.resCache.set(getKey(path), i);
 		}
 		return i;

+ 6 - 7
hrt/prefab/Atlas.hx

@@ -10,6 +10,7 @@ class Atlas extends Object2D {
 	var loop : Bool = false;
 
 	var h2dAnim : h2d.Anim;
+	var atlas : hxd.res.Atlas;
 
 	override public function load(v:Dynamic) {
 		super.load(v);
@@ -31,12 +32,8 @@ class Atlas extends Object2D {
 		
 		if (propName == null || propName == "src") {
 			if (src != null) {
-				var atlas = hxd.res.Loader.currentInstance.load(src).to(hxd.res.Atlas);
-				try {
-					h2dAnim.play(atlas.getAnim());
-				} catch (e : Dynamic) {
-					h2dAnim.play([]);
-				}
+				atlas = hxd.res.Loader.currentInstance.load(src).to(hxd.res.Atlas);
+				h2dAnim.play(atlas.getAnim());
 			} else {
 				h2dAnim.play([]);
 			}
@@ -55,7 +52,9 @@ class Atlas extends Object2D {
 		h2dAnim = new h2d.Anim([], fpsAnimation, ctx.local2d);
 		ctx.local2d = h2dAnim;
 		ctx.local2d.name = name;
-		ctx.cleanup = function() { h2dAnim = null; }
+		ctx.cleanup = function() {
+			h2dAnim = null;
+		}
 		updateInstance(ctx);
 		return ctx;
 	}

+ 24 - 3
hrt/prefab/fx/FX2D.hx

@@ -51,10 +51,14 @@ class FX2DAnimation extends h2d.Object {
 	}
 
 	public function setTime( time : Float ) {
-		this.localTime = time;
+
+		var newLoop = false;
+
+		if (loop && this.localTime > time)
+			newLoop = true;
 		
-		if (loop)
-			time = this.localTime % duration;
+		this.localTime = time;
+
 		for(anim in objects) {
 			if(anim.scale != null) {
 				var scale = evaluator.getVector(anim.scale, time);
@@ -86,6 +90,20 @@ class FX2DAnimation extends h2d.Object {
 							drawable.color = evaluator.getVector(anim.color, time);
 				}
 			}
+			
+			var atlas = Std.downcast(anim.elt2d, hrt.prefab.Atlas);
+			if (atlas != null) {
+				@:privateAccess if (!atlas.loop && newLoop)  {
+					atlas.h2dAnim.currentFrame = 0;
+				}
+			} else {
+				var atlas = Std.downcast(anim.elt2d, hrt.prefab.Anim2D);
+				if (atlas != null) {
+					@:privateAccess if (!atlas.loop && newLoop)  {
+						atlas.h2dAnim.currentFrame = 0;
+					}
+				}
+			}
 
 			if(anim.events != null) {
 				for(evt in anim.events) {
@@ -190,6 +208,9 @@ class FX2D extends BaseFX {
 			anyFound = true;
 		}
 
+		if (Std.is(elt, hrt.prefab.Anim2D) || Std.is(elt, hrt.prefab.Atlas))
+			anyFound = true;
+
 		if(anyFound)
 			anims.push(anim);
 	}