瀏覽代碼

Improved Keyframes (#364)

Pascal Peridont 7 年之前
父節點
當前提交
6a5a8d2c27
共有 2 個文件被更改,包括 68 次插入15 次删除
  1. 66 15
      h2d/KeyFrames.hx
  2. 2 0
      hxd/fmt/kframes/Data.hx

+ 66 - 15
h2d/KeyFrames.hx

@@ -2,9 +2,13 @@ package h2d;
 import hxd.fmt.kframes.Data;
 
 typedef KeyframesLayer = {
+	var id : Int;
 	var name : String;
 	var spr : Sprite;
+	var tiles : Array<h2d.Tile>;
 	var animations : Array<KFAnimation>;
+	var from : Int;
+	var to : Int;
 }
 
 /**
@@ -12,7 +16,7 @@ typedef KeyframesLayer = {
 **/
 class KeyFrames extends Mask {
 
-	var layers : Map<String, KeyframesLayer>;
+	var layers : Array<KeyframesLayer>;
 	var filePrefix : String;
 	var curFrame : Float;
 
@@ -23,6 +27,8 @@ class KeyFrames extends Mask {
 	public var pause : Bool = false;
 	public var loop : Bool = false;
 
+	public var smooth(default,set) = true;
+
 	public function new( file : KeyframesFile, ?filePrefix : String, ?parent ) {
 		super(0,0,parent);
 		if( file.formatVersion == null )
@@ -36,22 +42,36 @@ class KeyFrames extends Mask {
 		this.frameRate = file.frame_rate;
 		this.curFrame = 0;
 
-		layers = new Map();
+		layers = [];
 		for( f in file.features ) {
-			var spr;
+			var spr, tiles = null;
 			if( f.backed_image == null ) {
 				spr = new h2d.Sprite(this);
 			} else {
-				var bmp = new h2d.Bitmap(loadTile(f.backed_image), this);
-				bmp.tile.scaleToSize(f.size.x, f.size.y);
-				bmp.smooth = true;
+				var reg = ~/(.*?)\[([0-9]+)-([0-9]+)\](.*)/;
+				if( reg.match(f.backed_image) ){
+					var from = Std.parseInt(reg.matched(2));
+					var to = Std.parseInt(reg.matched(3));
+					var l = reg.matched(2).length;
+					tiles = [for( i in from...to+1) loadTile(reg.matched(1)+StringTools.lpad(Std.string(i), "0", l)+reg.matched(4))];
+				}else{
+					tiles = [loadTile(f.backed_image)];
+				}
+				
+				for( t in tiles ) t.scaleToSize(f.size.x, f.size.y);
+				var bmp = new h2d.Bitmap(tiles[0], this);
+				bmp.smooth = smooth;
 				if( f.name.toLowerCase().indexOf("(add)") >= 0 )
 					bmp.blendMode = Add;
 				spr = bmp;
 			}
 			var l : KeyframesLayer = {
-				name : f.name,
-				spr : spr,
+				id: f.feature_id,
+				name: f.name,
+				from: f.from_frame == null ? 0 : f.from_frame,
+				to: f.to_frame == null ? file.animation_frame_count : f.to_frame,
+				tiles: tiles,
+				spr: spr,
 				animations : [],
 			};
 			for( f in f.feature_animations ) {
@@ -64,8 +84,17 @@ class KeyFrames extends Mask {
 					l.animations.push(f);
 				}
 			}
-			layers.set(l.name, l);
+			layers.push(l);
+		}
+	}
+
+	public function set_smooth( v : Bool ) : Bool {
+		for( l in layers ){
+			var bmp = Std.instance(l.spr, h2d.Bitmap);
+			if( bmp != null )
+				bmp.smooth = v;
 		}
+		return smooth = v;
 	}
 
 	public function play( speed : Float = 1., startFrame = 0 ) {
@@ -103,7 +132,7 @@ class KeyFrames extends Mask {
 				return u * u * u * 0 + c1 * 3 * t * u * u + c2 * 3 * t * t * u + t * t * t * 1;
 			}
 
-			var curves = f.timing_curves[index];
+			var curves = f.timing_curves[0];
 			var c1x = curves[0].x;
 			var c2x = curves[1].x;
 			var c1y = curves[0].y;
@@ -166,7 +195,8 @@ class KeyFrames extends Mask {
 		case YPosition:
 			l.spr.y = calcValue(0);
 		case Scale:
-			l.spr.setScale(calcValue(0) / 100.);
+			l.spr.scaleX = calcValue(0) / 100.;
+			l.spr.scaleY = calcValue(1) / 100.;
 		case Opacity:
 			l.spr.alpha = calcValue(0) / 100.;
 		case Rotation:
@@ -189,19 +219,40 @@ class KeyFrames extends Mask {
 	}
 
 	public function getLayer( name : String ) {
-		var l = layers.get(name);
-		return l == null ? null : l.spr;
+		var layer = null;
+		for( l in layers ) {
+			if( l.name == name ){
+				layer = l;
+				break;
+			}
+		}
+		return layer == null ? null : layer.spr;
 	}
 
 	override function sync( ctx : RenderContext ) {
 		super.sync(ctx);
 		var prev = curFrame;
-		if( !pause )
+		if( !pause ){
 			curFrame += speed * frameRate * ctx.elapsedTime;
+			if( curFrame > frameCount )
+				curFrame = frameCount;
+		}
 
-		for( l in layers )
+		for( l in layers ){
+			l.spr.visible = curFrame >= l.from && curFrame <= l.to;
+			if( l.spr.visible && l.tiles != null && l.tiles.length > 1 ){
+				var bmp : h2d.Bitmap = cast l.spr;
+				var curTile = hxd.Math.iclamp( Std.int( (curFrame - l.from) * l.tiles.length / (l.to - l.from) ), 0, l.tiles.length );
+				var newTile = l.tiles[curTile];
+				if( bmp.tile != newTile ){
+					newTile.dx = bmp.tile.dx;
+					newTile.dy = bmp.tile.dy;
+					bmp.tile = newTile;
+				}
+			}
 			for( a in l.animations )
 				apply(l, a);
+		}
 
 		if( curFrame < frameCount )
 			return;

+ 2 - 0
hxd/fmt/kframes/Data.hx

@@ -37,6 +37,8 @@ typedef KFFeature = {
 	var size : KFSize<Int>;
 	var feature_animations : Array<KFAnimation>;
 	@:optional var backed_image : String;
+	@:optional var from_frame : Int;
+	@:optional var to_frame : Int;
 }
 
 typedef KeyframesFile = {