rodrigolopezpeker

Genome2D > GAtlasUtils

May 29th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created with IntelliJ IDEA.
  3.  * User: rodrigolopez [ 7 interactive™ ] http://www.7interactive.com.ar
  4.  * Date: 5/23/12
  5.  * Time: 2:30 PM
  6.  */
  7.  
  8. /*
  9. Usage example:
  10.  
  11.         [Embed(source="/../assets/sky2.png")]
  12.         private static var skyGFX: Class;
  13.         [Embed(source="/../assets/sky.xml", mimeType = "application/octet-stream")]
  14.         private static var skyXML: Class;
  15.  
  16.         skyAtlas = GTextureAtlasFactory.createFromBitmapDataAndXML("sky", (new skyGFX()).bitmapData, sky_xml);
  17.         ids = GAtlasUtils.getIdsFromXML(sky_xml) ;
  18.         GAtlasUtils.alignAtlas(butcherAtlas, ids, GTextureAlignType.TOP_LEFT)
  19.         GAtlasUtils.disposeBitmapsAtlas(butcherAtlas, ids)
  20.  
  21. // Trimmed Texture Atlas
  22. // When you change the texture's alignment, this will be overwritten, thus you need to call GAtlasUtils.adjustTrim after align the Atlas.
  23. // The applied offset will not affect the node's position.
  24. GAtlasUtils.alignAtlas(trimmedAtlas, GAtlasUtils.getIdsFromXML(trimmedXML),GTextureAlignType.TOP_LEFT);
  25. GAtlasUtils.adjustTrim(trimmedAtlas, trimmedXML, GTextureAlignType.TOP_LEFT )
  26.  
  27.  
  28. // create an atlas xml for the movieclip.
  29. var heroXML:XML = new XML(<nodo/>)
  30.  
  31. // createAtlasFromMovie
  32. // fills the empty XML Atlas based on the standar xml atlas.
  33. var heroAtlas:GTextureAtlas = GAtlasUtils.createAtlasFromMovie("heroAtlasId",new hero_view(),null, 0x0, heroXML );
  34.  
  35. // atlas's nodes ids harcoded (in this case based on the frame label, createAtlasFromMovie detects if the movieclip has labels, if it doesn't uses the frame number.
  36. var heroIds:Array = GAtlasUtils.buildFrames("w0", 1, 7 ).concat(GAtlasUtils.buildFrames("j0", 1, 5 ) ).concat("s01");
  37. // you can also use GAtlasUtils.getIdsFromXML( heroXML )
  38. //output:  w01,w02,w03,w04,w05,w06,w07,j01,j02,j03,j04,j05,s01
  39. GAtlasUtils.disposeBitmapsAtlas( heroAtlas, heroIds );
  40. GAtlasUtils.alignAtlas( heroAtlas, heroIds, GTextureAlignType.TOP_LEFT );
  41. GAtlasUtils.adjustTrim( heroAtlas, heroXML, GTextureAlignType.TOP_LEFT );
  42.  
  43. */
  44. package ar.com.rodrigolopezpeker.utils {
  45.  
  46.     import com.genome2d.textures.GTexture;
  47.     import com.genome2d.textures.GTextureAlignType;
  48.     import com.genome2d.textures.GTextureAtlas;
  49.     import flash.display.BitmapData;
  50.     import flash.display.MovieClip;
  51.     import flash.geom.Matrix;
  52.     import flash.geom.Rectangle;
  53.  
  54.     public class GAtlasUtils {
  55.         public function GAtlasUtils() {
  56.         }
  57.  
  58.  
  59.         public static function getIdsFromXML(pXml:XML):Array{
  60.             return pXml.SubTexture.@name.toXMLString().split('\n');
  61.         }
  62.        
  63.         //@example http://devel.7interactive.com.ar/trim/
  64.         public static function adjustTrim(pAtlas:GTextureAtlas, pXML:XML, pAlign:int = 1 ):void{
  65.             var len:uint = pXML.SubTexture.length();
  66.             for(var i:uint; i < len; ++ i ){
  67.                 var node:XML = pXML.SubTexture[i];
  68.                 var id:String = node.@name
  69.                 var tex:GTexture = pAtlas.getTexture(id) ;
  70.                 // apply offsets.
  71.                 if( pAlign == GTextureAlignType.TOP_LEFT ){
  72.                     tex.pivotX += int(node.@frameX)
  73.                     tex.pivotY += int(node.@frameY)
  74.                 } else {
  75.                     tex.pivotX = int(node.@frameWidth)/2-tex.width/2 + int(node.@frameX)
  76.                     tex.pivotY = int(node.@frameHeight)/2-tex.height/2 + int(node.@frameY)
  77.                 }
  78.             }
  79.         }
  80.  
  81.         public static function alignAtlas(pAtlas:GTextureAtlas, pIds:Array, pAlign:int):void{
  82.             var len:uint = pIds.length;
  83.             for(var i:uint; i < len; ++ i ){
  84.                 var tex:GTexture = pAtlas.getTexture(pIds[i]) ;
  85.                 if(tex){
  86.                     tex.alignTexture(pAlign)
  87.                 }
  88.             }
  89.         }
  90.  
  91.         public static function disposeBitmapsAtlas(pAtlas:GTextureAtlas, pIds:Array):void{
  92.             var len:uint = pIds.length;
  93.             for(var i:uint; i < len; ++ i ){
  94.                 var tex:GTexture = pAtlas.getTexture(pIds[i]) ;
  95.                 if(tex) tex.bitmapData.dispose();
  96.             }
  97.         }
  98.  
  99.         public static function buildFrames( base:String, startCount:uint=1,  endCount:uint = 10, exclude:Array=null):Array{
  100.             var len: uint = endCount - startCount ;
  101.             var output:Array = [] ;
  102.             for(var i:uint = startCount; i <= endCount; ++i){
  103.                 if(exclude && exclude.indexOf(i)!=-1){
  104.  
  105.                 } else {
  106.                     output.push(base + String(i))
  107.                 }
  108.             }
  109.             return output ;
  110.         }
  111.  
  112.         public static function createAtlasFromMovie(pId:String, pMovieClip:MovieClip, pExcludedFrames:Array = null, pForce:Boolean=false, pDebugColor:uint=0x0, pXMLAtlas:XML = null):GTextureAtlas{
  113.             var i:uint = 1 ;
  114.             var m:Matrix = new Matrix();
  115.             var bitmaps:Vector.<BitmapData> = new Vector.<BitmapData>();
  116.             var ids:Vector.<String> = new Vector.<String>();
  117.  
  118.             while(i <= pMovieClip.totalFrames){
  119.                 pMovieClip.gotoAndStop(i);
  120.                 var lbl:String = pMovieClip.currentLabel;
  121.                 if(!lbl){
  122.                     lbl = String(i);
  123.                 }
  124.                 if( pExcludedFrames ){
  125.                     // frame id!
  126.                     if( pExcludedFrames.indexOf(i) != -1 || pExcludedFrames.indexOf(lbl) != -1 ){
  127.                         ++i;
  128.                         continue;
  129.                     }
  130.                 }
  131.                 var w:int = pForce ? pMovieClip.width+1:pMovieClip.width;
  132.                 var h:int = pForce ? pMovieClip.height+1:pMovieClip.height;
  133.                 var bd:BitmapData = new BitmapData(w, h, true, pDebugColor );
  134.                 var rect:Rectangle = pMovieClip.getBounds(pMovieClip);
  135.                 m.identity();
  136.                 m.translate(-rect.x,  -rect.y);
  137.                 if(pXMLAtlas){
  138.                     var xmlChild:XML = <SubTexture/>
  139.                     xmlChild.@name = lbl;
  140.                     xmlChild.@width = w;
  141.                     xmlChild.@height = h;
  142.                     xmlChild.@frameX = -rect.x;
  143.                     xmlChild.@frameY = -rect.y;
  144.                     pXMLAtlas.appendChild(xmlChild)
  145.                 }
  146.  
  147.                 bd.draw(pMovieClip, m);
  148.                 ids.push(lbl);
  149.                 bitmaps.push(bd);
  150.                 ++i;
  151.             }
  152.             trace("GTextureAtlas",pId, " has frames:\n", ids);
  153.             return GTextureAtlasFactory.createFromBitmapDatas(pId, bitmaps, ids ) ;
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment