Anim2D.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package hrt.prefab.l2d;
  2. class Anim2D extends Object2D {
  3. // parameters
  4. @:s var src : String;
  5. @:s var widthFrame : Int = 10;
  6. @:s var heightFrame : Int = 10;
  7. @:s var fpsAnimation : Int = 30;
  8. @:s var nbFrames : Int = 30;
  9. @:s var delayStart : Float = 0;
  10. @:s var loop : Bool = false;
  11. var tex : h3d.mat.Texture;
  12. override function updateInstance( ctx: Context, ?propName : String ) {
  13. super.updateInstance(ctx, propName);
  14. var h2dAnim = (cast ctx.local2d : h2d.Anim);
  15. if (propName == null || (propName == "src" || propName == "widthFrame" || propName == "heightFrame" || propName == "nbFrames")) {
  16. if (tex != null) {
  17. tex = null;
  18. }
  19. if (src != null) {
  20. tex = ctx.loadTexture(src);
  21. var t = h2d.Tile.fromTexture(tex);
  22. var tiles = [];
  23. var nbFrameRow = Std.int(t.width / widthFrame);
  24. for( y in 0...Std.int(t.height / heightFrame) )
  25. for( x in 0...nbFrameRow)
  26. if (y * nbFrameRow + x <= nbFrames)
  27. tiles.push( t.sub(x * widthFrame, y * heightFrame, widthFrame, heightFrame, -(widthFrame / 2), -(heightFrame / 2)) );
  28. h2dAnim.play(tiles);
  29. } else {
  30. h2dAnim.play([]);
  31. }
  32. }
  33. if (propName == null || propName == "fpsAnimation") {
  34. h2dAnim.speed = fpsAnimation;
  35. }
  36. if (propName == null || propName == "loop") {
  37. h2dAnim.loop = loop;
  38. }
  39. h2dAnim.pause = !loop;
  40. h2dAnim.blendMode = blendMode;
  41. }
  42. override function makeInstance(ctx:Context):Context {
  43. ctx = ctx.clone(this);
  44. var h2dAnim = new h2d.Anim([], fpsAnimation, ctx.local2d);
  45. ctx.local2d = h2dAnim;
  46. ctx.local2d.name = name;
  47. updateInstance(ctx);
  48. return ctx;
  49. }
  50. #if editor
  51. override function edit( ctx : EditContext ) {
  52. super.edit(ctx);
  53. ctx.properties.add(new hide.Element('<div class="group" name="Frames">
  54. <dl>
  55. <dt>Background</dt><dd><input type="texturepath" field="src" style="width:165px"/></dd>
  56. <dt>Width Frame</dt><dd><input type="range" min="0" max="100" step="1" field="widthFrame"/></dd>
  57. <dt>Height Frame</dt><dd><input type="range" min="0" max="100" step="1" field="heightFrame"/></dd>
  58. <dt>FPS</dt><dd><input type="range" min="0" max="60" step="1" field="fpsAnimation"/></dd>
  59. <dt>nbFrames</dt><dd><input type="range" min="0" max="120" step="1" field="nbFrames"/></dd>
  60. <dt>Delay Start</dt><dd><input type="range" min="0" max="10" field="delayStart"/></dd>
  61. <dt>Loop</dt><dd><input type="checkbox" field="loop"/></dd>
  62. </dl></div>'), this, function(pname) {
  63. ctx.onChange(this, pname);
  64. });
  65. }
  66. override function getHideProps() : HideProps {
  67. return { icon : "square", name : "Anim2D" };
  68. }
  69. #end
  70. static var _ = Library.register("anim2D", Anim2D);
  71. }