Jelajahi Sumber

FX2D : Add Atlas component

Tom Spira 6 tahun lalu
induk
melakukan
5541e39393
3 mengubah file dengan 203 tambahan dan 1 penghapusan
  1. 1 1
      hide/view/FXEditor.hx
  2. 104 0
      hrt/prefab/Anim2D.hx
  3. 98 0
      hrt/prefab/Atlas.hx

+ 1 - 1
hide/view/FXEditor.hx

@@ -74,7 +74,7 @@ private class FXSceneEditor extends hide.comp.SceneEditor {
 
 		var menu = [];
 		if (@:privateAccess parent.is2D) {
-			for(name in ["Group 2D", "Bitmap", "Shaders", "Shader Graph"]) {
+			for(name in ["Group 2D", "Bitmap", "Anim2D", "Atlas", "Shaders", "Shader Graph"]) {
 				var item = allTypes.find(i -> i.label == name);
 				if(item == null) continue;
 				allTypes.remove(item);

+ 104 - 0
hrt/prefab/Anim2D.hx

@@ -0,0 +1,104 @@
+package hrt.prefab;
+
+class Anim2D extends Object2D {
+
+	// parameters
+	var src : String;
+
+	var widthFrame : Int = 10;
+	var heightFrame : Int = 10;
+	var fpsAnimation : Int = 30;
+	var nbFrames : Int = 30;
+	
+	var loop : Bool = false;
+
+	var h2dAnim : h2d.Anim;
+	var tex : h3d.mat.Texture;
+
+	override public function load(v:Dynamic) {
+		super.load(v);
+		this.src = v.src;
+		this.widthFrame = v.widthFrame;
+		this.heightFrame = v.heightFrame;
+		this.fpsAnimation = v.fpsAnimation;
+		this.nbFrames = v.nbFrames;
+		this.loop = v.loop;
+	}
+
+	override function save() {
+		var o : Dynamic = super.save();
+		o.src = src;
+		o.widthFrame = widthFrame;
+		o.heightFrame = heightFrame;
+		o.fpsAnimation = fpsAnimation;
+		o.nbFrames = nbFrames;
+		o.loop = loop;
+		return o;
+	}
+
+	override function updateInstance( ctx: Context, ?propName : String ) {
+		super.updateInstance(ctx, propName);
+		
+		if (propName == null || (propName == "src" || propName == "widthFrame" || propName == "heightFrame" || propName == "nbFrames")) {
+			if (tex != null) {
+				tex = null;
+			}
+			if (src != null) {
+				tex = ctx.loadTexture(src);
+				var t = h2d.Tile.fromTexture(tex);
+				var tiles = [];
+				var nbFrameRow = Std.int(t.width / widthFrame);
+				for( y in 0...Std.int(t.height / heightFrame) )
+					for( x in 0...nbFrameRow)
+						if (y * nbFrameRow + x <= nbFrames)
+							tiles.push( t.sub(x * widthFrame, y * heightFrame, widthFrame, heightFrame, -(widthFrame / 2), -(heightFrame / 2)) );
+				h2dAnim.play(tiles);
+			} else {
+				h2dAnim.play([]);
+			}
+		}
+		if (propName == null || propName == "fpsAnimation") {
+			h2dAnim.speed = fpsAnimation;
+		}
+		if (propName == null || propName == "loop") {
+			h2dAnim.loop = loop;
+		}
+		h2dAnim.blendMode = blendMode;
+	}
+
+	override function makeInstance(ctx:Context):Context {
+		ctx = ctx.clone(this);
+		h2dAnim = new h2d.Anim([], fpsAnimation, ctx.local2d);
+		ctx.local2d = h2dAnim;
+		ctx.local2d.name = name;
+		ctx.cleanup = function() { h2dAnim = null; }
+		updateInstance(ctx);
+		return ctx;
+	}
+
+	#if editor
+	override function edit( ctx : EditContext ) {
+		super.edit(ctx);
+
+		ctx.properties.add(new hide.Element('<div class="group" name="Frames">
+			<dl>
+				<dt>Background</dt><dd><input type="texturepath" field="src" style="width:165px"/></dd>
+				<dt>Width Frame</dt><dd><input type="range" min="0" max="100" step="1" field="widthFrame"/></dd>
+				<dt>Height Frame</dt><dd><input type="range" min="0" max="100" step="1" field="heightFrame"/></dd>
+				<dt>FPS</dt><dd><input type="range" min="0" max="60" step="1" field="fpsAnimation"/></dd>
+				<dt>nbFrames</dt><dd><input type="range" min="0" max="120" step="1" field="nbFrames"/></dd>
+				<dt>Loop</dt><dd><input type="checkbox" field="loop"/></dd>
+			</dl></div>'), this, function(pname) {
+			ctx.onChange(this, pname);
+		});
+	}
+
+	override function getHideProps() : HideProps {
+		return { icon : "square", name : "Anim2D" };
+	}
+
+	#end
+
+	static var _ = Library.register("anim2D", Anim2D);
+
+}

+ 98 - 0
hrt/prefab/Atlas.hx

@@ -0,0 +1,98 @@
+package hrt.prefab;
+
+class Atlas extends Object2D {
+
+	// parameters
+	var src : String;
+
+	var fpsAnimation : Int = 30;
+	
+	var loop : Bool = false;
+
+	var h2dAnim : h2d.Anim;
+
+	override public function load(v:Dynamic) {
+		super.load(v);
+		this.src = v.src;
+		this.fpsAnimation = v.fpsAnimation;
+		this.loop = v.loop;
+	}
+
+	override function save() {
+		var o : Dynamic = super.save();
+		o.src = src;
+		o.fpsAnimation = fpsAnimation;
+		o.loop = loop;
+		return o;
+	}
+
+	override function updateInstance( ctx: Context, ?propName : String ) {
+		super.updateInstance(ctx, propName);
+		
+		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([]);
+				}
+			} else {
+				h2dAnim.play([]);
+			}
+		}
+		if (propName == null || propName == "fpsAnimation") {
+			h2dAnim.speed = fpsAnimation;
+		}
+		if (propName == null || propName == "loop") {
+			h2dAnim.loop = loop;
+		}
+		h2dAnim.blendMode = blendMode;
+	}
+
+	override function makeInstance(ctx:Context):Context {
+		ctx = ctx.clone(this);
+		h2dAnim = new h2d.Anim([], fpsAnimation, ctx.local2d);
+		ctx.local2d = h2dAnim;
+		ctx.local2d.name = name;
+		ctx.cleanup = function() { h2dAnim = null; }
+		updateInstance(ctx);
+		return ctx;
+	}
+
+	#if editor
+	override function edit( ctx : EditContext ) {
+		super.edit(ctx);
+
+		var parameters = new hide.Element('<div class="group" name="Parameters"></div>');
+
+		var gr = new hide.Element('<dl></dl>').appendTo(parameters);
+
+		new hide.Element('<dt>Background</dt>').appendTo(gr);
+		var element = new hide.Element('<dd></dd>').appendTo(gr);
+		var fileInput = new hide.Element('<input type="text" field="src" style="width:165px" />').appendTo(element);
+
+		var tfile = new hide.comp.FileSelect(["atlas"], null, fileInput);
+		if (this.src != null && this.src.length > 0) tfile.path = this.src;
+		tfile.onChange = function() {
+			this.src = tfile.path;
+			updateInstance(ctx.getContext(this), "src");
+		}
+		new hide.Element('<dt>FPS</dt><dd><input type="range" min="0" max="60" step="1" field="fpsAnimation"/></dd>').appendTo(gr);
+		new hide.Element('<dt>Loop</dt><dd><input type="checkbox" field="loop"/></dd>').appendTo(gr);
+		
+
+		ctx.properties.add(parameters, this, function(pname) {
+			ctx.onChange(this, pname);
+		});
+	}
+
+	override function getHideProps() : HideProps {
+		return { icon : "square", name : "Atlas" };
+	}
+
+	#end
+
+	static var _ = Library.register("atlas", Atlas);
+
+}