PageRenderTime 33ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/pixelrevision/textureAtlas/TextureLayout.as

https://github.com/99lives/texture_atlas_generator
ActionScript | 211 lines | 164 code | 38 blank | 9 comment | 20 complexity | 8b6a568bc271efa6a27a93cfc7f7c103 MD5 | raw file
  1. package com.pixelrevision.textureAtlas{
  2. import com.adobe.images.PNGEncoder;
  3. import com.adobe.serialization.json.JSON;
  4. import com.adobe.serialization.json.JSONEncoder;
  5. import com.pixelrevision.textureAtlas.events.TextureAtlasEvent;
  6. import deng.fzip.FZip;
  7. import flash.display.Bitmap;
  8. import flash.display.BitmapData;
  9. import flash.display.IBitmapDrawable;
  10. import flash.display.MovieClip;
  11. import flash.display.Sprite;
  12. import flash.geom.Matrix;
  13. import flash.geom.Rectangle;
  14. import flash.net.FileReference;
  15. import flash.utils.ByteArray;
  16. import uk.co.ninety9lives.TextureAtlas.AnimationProcessor;
  17. public class TextureLayout extends Sprite{
  18. protected var _settings:Settings;
  19. protected var _currentLab:String = "";
  20. protected var _items:Array = [];
  21. public function TextureLayout(){
  22. super();
  23. _settings = Settings.sharedInstance;
  24. _settings.addEventListener(TextureAtlasEvent.CANVAS_SIZE_CHANGED, drawBounds);
  25. drawBounds(null);
  26. }
  27. public function layoutChildren():void{
  28. var xPos:Number = 0;
  29. var yPos:Number = 0;
  30. var maxY:Number = 0;
  31. for(var i:uint=0; i<_items.length; i++){
  32. if( (xPos + _items[i].width) > _settings.canvasWidth){
  33. xPos = 0;
  34. yPos += maxY;
  35. maxY = 0;
  36. }
  37. if(_items[i].height + 1 > maxY){
  38. maxY = _items[i].height + 1;
  39. }
  40. _items[i].x = xPos;
  41. _items[i].y = yPos;
  42. xPos += _items[i].width + 1;
  43. }
  44. }
  45. protected function drawBounds(e:TextureAtlasEvent):void{
  46. graphics.clear();
  47. graphics.lineStyle(1, 0x000000);
  48. graphics.drawRect(0, 0, _settings.canvasWidth-1, _settings.canvasHeight-1);
  49. layoutChildren();
  50. dispatchEvent(new TextureAtlasEvent(TextureAtlasEvent.CANVAS_SIZE_CHANGED) );
  51. }
  52. public function get withinBounds():Boolean{
  53. //trace(this.width, _settings.canvasWidth, this.height, _settings.canvasHeight);
  54. //Math.floor corrects for a rare rounding error
  55. return (Math.floor(this.width) <= _settings.canvasWidth && Math.floor(this.height) <= _settings.canvasHeight);
  56. }
  57. public function addItem(item:TextureItem):void{
  58. _items.push(item);
  59. addChild(item);
  60. }
  61. public function clear():void{
  62. for(var i:uint=0; i<_items.length; i++){
  63. removeChild(_items[i]);
  64. }
  65. _currentLab = "";
  66. _items = [];
  67. }
  68. public function processSWF(swf:MovieClip):void{
  69. clear();
  70. var parseFrame:Boolean = false;
  71. var selected:MovieClip;
  72. var itemW:Number;
  73. var itemH:Number;
  74. var bounds:Rectangle;
  75. for(var i:uint=0; i<swf.numChildren; i++){
  76. if (swf.getChildAt(i) is MovieClip) {
  77. selected = MovieClip( swf.getChildAt(i) );
  78. if (!AnimationProcessor.isAnimation(selected)) {
  79. selected.gotoAndStop(1); //reset
  80. // check for frames
  81. if(selected.totalFrames > 1){
  82. for(var m:uint=0; m<selected.totalFrames; m++){
  83. selected.gotoAndStop(m+1);
  84. selected.gotoAndStop(1);
  85. trace(selected.currentFrame);
  86. drawItem(selected, selected.name + "_" + appendIntToString(m, 5), selected.name);
  87. }
  88. selected.gotoAndStop(1); //reset
  89. }else{
  90. drawItem(selected, selected.name, selected.name);
  91. }
  92. }
  93. }
  94. }
  95. layoutChildren();
  96. }
  97. protected function appendIntToString(num:int, numOfPlaces:int):String{
  98. var numString:String = num.toString();
  99. var outString:String = "";
  100. for(var i:int=0; i<numOfPlaces - numString.length; i++){
  101. outString += "0";
  102. }
  103. return outString + numString;
  104. }
  105. protected function drawItem(clip:MovieClip, name:String = "", baseName:String =""):TextureItem{
  106. var label:String = "";
  107. var bounds:Rectangle = clip.getBounds(clip.parent);
  108. var itemW:Number = Math.ceil(bounds.x + bounds.width);
  109. var itemH:Number = bounds.height;//Math.ceil(bounds.y + bounds.height);
  110. var bmd:BitmapData = new BitmapData(itemW, itemH, true, 0x00000000);
  111. bmd.draw(clip);
  112. if(clip.currentLabel != _currentLab && clip.currentLabel != null){
  113. _currentLab = clip.currentLabel;
  114. label = _currentLab;
  115. }
  116. var item:TextureItem = new TextureItem(bmd, name, label, baseName);
  117. addItem(item);
  118. return item;
  119. }
  120. public function save():void{
  121. graphics.clear();
  122. // prepare files
  123. var bmd:BitmapData = new BitmapData(_settings.canvasWidth, _settings.canvasHeight, true, 0x000000);
  124. var json:Object = new Object();
  125. json.textures = [];
  126. json.imagePath = _settings.textureName + ".png";
  127. var xml:XML = new XML(<TextureAtlas></TextureAtlas>);
  128. xml.@imagePath = _settings.textureName + ".png";
  129. for(var i:uint=0; i<_items.length; i++){
  130. var matrix:Matrix = new Matrix();
  131. matrix.tx = _items[i].x;
  132. matrix.ty = _items[i].y;
  133. bmd.draw(_items[i], matrix);
  134. // xml
  135. var subText:XML = new XML(<SubTexture />);
  136. subText.@x = _items[i].x;
  137. subText.@y = _items[i].y;
  138. subText.@width = _items[i].width;
  139. subText.@height = _items[i].height;
  140. subText.@name = _items[i].textureName;
  141. if(_items[i].frameName != "") subText.@frameLabel = _items[i].frameName;
  142. xml.appendChild(subText);
  143. // json
  144. var textureData:Object = new Object();
  145. textureData.x = _items[i].x;
  146. textureData.y = _items[i].y;
  147. textureData.width = _items[i].width;
  148. textureData.height = _items[i].height;
  149. textureData.name = _items[i].textureName;
  150. if(_items[i].frameName != "") textureData.frameLabel = _items[i].frameName;
  151. json.textures.push(textureData);
  152. }
  153. var luaGenerator:LUAGenerator = new LUAGenerator();
  154. var lua:String = luaGenerator.generate(_items);
  155. // trace(lua);
  156. // now setup zip
  157. var img:ByteArray = PNGEncoder.encode(bmd);
  158. var xmlString:String = xml.toString();
  159. var jsonString:String = JSON.encode(json);
  160. var zip:FZip = new FZip();
  161. zip.addFile(_settings.textureName + ".png", img);
  162. zip.addFileFromString(_settings.textureName + ".xml", xmlString);
  163. zip.addFileFromString(_settings.textureName + ".json", jsonString);
  164. zip.addFileFromString(_settings.textureName + ".lua", lua);
  165. // save
  166. var zipArray:ByteArray = new ByteArray();
  167. zip.serialize(zipArray, true);
  168. var fr:FileReference = new FileReference();
  169. fr.save(zipArray, _settings.textureName + ".zip");
  170. drawBounds(null);
  171. }
  172. }
  173. }