Atlas.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package hxd.res;
  2. class Atlas extends Resource {
  3. var contents : Map<String,Array<{ t : h2d.Tile, width : Int, height : Int }>>;
  4. function tileAlign( t : h2d.Tile, halign : h2d.Flow.FlowAlign, valign : h2d.Flow.FlowAlign, width : Int, height : Int ) {
  5. if( halign == null ) halign = Left;
  6. if( valign == null ) valign = Top;
  7. var dx = 0, dy = 0;
  8. switch( halign ) {
  9. case Middle:
  10. dx = width >> 1;
  11. case Right:
  12. dx = width;
  13. default:
  14. }
  15. switch( valign ) {
  16. case Middle:
  17. dy = height >> 1;
  18. case Bottom:
  19. dy = height;
  20. default:
  21. }
  22. return t.sub(0, 0, t.width, t.height, t.dx - dx, t.dy - dy);
  23. }
  24. public function get( name : String, ?horizontalAlign : h2d.Flow.FlowAlign, ?verticalAlign : h2d.Flow.FlowAlign ) : h2d.Tile {
  25. var c = getContents().get(name);
  26. if( c == null )
  27. return null;
  28. var t = c[0];
  29. if( t == null )
  30. return null;
  31. return tileAlign(t.t, horizontalAlign, verticalAlign, t.width, t.height);
  32. }
  33. public function getAnim( ?name : String, ?horizontalAlign : h2d.Flow.FlowAlign, ?verticalAlign : h2d.Flow.FlowAlign ) : Array<h2d.Tile> {
  34. if( name == null ) {
  35. var cont = getContents().keys();
  36. name = cont.next();
  37. if( cont.hasNext() )
  38. throw "Altas has several items in it " + Lambda.array( contents );
  39. }
  40. var c = getContents().get(name);
  41. if( c == null )
  42. return null;
  43. return [for( t in c ) if( t == null ) null else tileAlign(t.t, horizontalAlign, verticalAlign, t.width, t.height)];
  44. }
  45. public function getContents() {
  46. if( contents != null )
  47. return contents;
  48. contents = new Map();
  49. var lines = entry.getBytes().toString().split("\n");
  50. var basePath = entry.path.split("/");
  51. basePath.pop();
  52. var basePath = basePath.join("/");
  53. if( basePath.length > 0 ) basePath += "/";
  54. while( lines.length > 0 ) {
  55. var line = StringTools.trim(lines.shift());
  56. if ( line == "" ) continue;
  57. var scale = 1.;
  58. var file = hxd.res.Loader.currentInstance.load(basePath + line).toTile();
  59. while( lines.length > 0 ) {
  60. if( lines[0].indexOf(":") < 0 ) break;
  61. var line = StringTools.trim(lines.shift()).split(": ");
  62. switch( line[0] ) {
  63. case "size":
  64. var wh = line[1].split(",");
  65. var w = Std.parseInt(wh[0]);
  66. scale = file.width / w;
  67. default:
  68. }
  69. }
  70. while( lines.length > 0 ) {
  71. var line = StringTools.trim(lines.shift());
  72. if( line == "" ) break;
  73. var prop = line.split(": ");
  74. if( prop.length > 1 ) continue;
  75. var key = line;
  76. var tileX = 0, tileY = 0, tileW = 0, tileH = 0, tileDX = 0, tileDY = 0, origW = 0, origH = 0, index = 0;
  77. while( lines.length > 0 ) {
  78. var line = StringTools.trim(lines.shift());
  79. var prop = line.split(": ");
  80. if( prop.length == 1 ) {
  81. lines.unshift(line);
  82. break;
  83. }
  84. var v = prop[1];
  85. switch( prop[0] ) {
  86. case "rotate":
  87. if( v == "true" ) throw "Rotation not supported in atlas";
  88. case "xy":
  89. var vals = v.split(", ");
  90. tileX = Std.parseInt(vals[0]);
  91. tileY = Std.parseInt(vals[1]);
  92. case "size":
  93. var vals = v.split(", ");
  94. tileW = Std.parseInt(vals[0]);
  95. tileH = Std.parseInt(vals[1]);
  96. case "offset":
  97. var vals = v.split(", ");
  98. tileDX = Std.parseInt(vals[0]);
  99. tileDY = Std.parseInt(vals[1]);
  100. case "orig":
  101. var vals = v.split(", ");
  102. origW = Std.parseInt(vals[0]);
  103. origH = Std.parseInt(vals[1]);
  104. case "index":
  105. index = Std.parseInt(v);
  106. if( index < 0 ) index = 0;
  107. default:
  108. trace("Unknown prop " + prop[0]);
  109. }
  110. }
  111. // offset is bottom-relative
  112. tileDY = origH - (tileH + tileDY);
  113. var t = file.sub(Std.int(tileX * scale), Std.int(tileY * scale), Std.int(tileW * scale), Std.int(tileH * scale), tileDX, tileDY);
  114. if( scale != 1 ) t.scaleToSize(tileW, tileH);
  115. var tl = contents.get(key);
  116. if( tl == null ) {
  117. tl = [];
  118. contents.set(key, tl);
  119. }
  120. tl[index] = { t : t, width : origW, height : origH };
  121. }
  122. // remove first element if index started at 1 instead of 0
  123. for( tl in contents )
  124. if( tl.length > 1 && tl[0] == null ) tl.shift();
  125. }
  126. return contents;
  127. }
  128. }