Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created with IntelliJ IDEA.
- * User: rodrigolopez [ 7 interactive™ ] http://www.7interactive.com.ar
- * Date: 5/23/12
- * Time: 2:30 PM
- */
- /*
- Usage example:
- [Embed(source="/../assets/sky2.png")]
- private static var skyGFX: Class;
- [Embed(source="/../assets/sky.xml", mimeType = "application/octet-stream")]
- private static var skyXML: Class;
- skyAtlas = GTextureAtlasFactory.createFromBitmapDataAndXML("sky", (new skyGFX()).bitmapData, sky_xml);
- ids = GAtlasUtils.getIdsFromXML(sky_xml) ;
- GAtlasUtils.alignAtlas(butcherAtlas, ids, GTextureAlignType.TOP_LEFT)
- GAtlasUtils.disposeBitmapsAtlas(butcherAtlas, ids)
- // Trimmed Texture Atlas
- // When you change the texture's alignment, this will be overwritten, thus you need to call GAtlasUtils.adjustTrim after align the Atlas.
- // The applied offset will not affect the node's position.
- GAtlasUtils.alignAtlas(trimmedAtlas, GAtlasUtils.getIdsFromXML(trimmedXML),GTextureAlignType.TOP_LEFT);
- GAtlasUtils.adjustTrim(trimmedAtlas, trimmedXML, GTextureAlignType.TOP_LEFT )
- // create an atlas xml for the movieclip.
- var heroXML:XML = new XML(<nodo/>)
- // createAtlasFromMovie
- // fills the empty XML Atlas based on the standar xml atlas.
- var heroAtlas:GTextureAtlas = GAtlasUtils.createAtlasFromMovie("heroAtlasId",new hero_view(),null, 0x0, heroXML );
- // 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.
- var heroIds:Array = GAtlasUtils.buildFrames("w0", 1, 7 ).concat(GAtlasUtils.buildFrames("j0", 1, 5 ) ).concat("s01");
- // you can also use GAtlasUtils.getIdsFromXML( heroXML )
- //output: w01,w02,w03,w04,w05,w06,w07,j01,j02,j03,j04,j05,s01
- GAtlasUtils.disposeBitmapsAtlas( heroAtlas, heroIds );
- GAtlasUtils.alignAtlas( heroAtlas, heroIds, GTextureAlignType.TOP_LEFT );
- GAtlasUtils.adjustTrim( heroAtlas, heroXML, GTextureAlignType.TOP_LEFT );
- */
- package ar.com.rodrigolopezpeker.utils {
- import com.genome2d.textures.GTexture;
- import com.genome2d.textures.GTextureAlignType;
- import com.genome2d.textures.GTextureAtlas;
- import flash.display.BitmapData;
- import flash.display.MovieClip;
- import flash.geom.Matrix;
- import flash.geom.Rectangle;
- public class GAtlasUtils {
- public function GAtlasUtils() {
- }
- public static function getIdsFromXML(pXml:XML):Array{
- return pXml.SubTexture.@name.toXMLString().split('\n');
- }
- //@example http://devel.7interactive.com.ar/trim/
- public static function adjustTrim(pAtlas:GTextureAtlas, pXML:XML, pAlign:int = 1 ):void{
- var len:uint = pXML.SubTexture.length();
- for(var i:uint; i < len; ++ i ){
- var node:XML = pXML.SubTexture[i];
- var id:String = node.@name
- var tex:GTexture = pAtlas.getTexture(id) ;
- // apply offsets.
- if( pAlign == GTextureAlignType.TOP_LEFT ){
- tex.pivotX += int(node.@frameX)
- tex.pivotY += int(node.@frameY)
- } else {
- tex.pivotX = int(node.@frameWidth)/2-tex.width/2 + int(node.@frameX)
- tex.pivotY = int(node.@frameHeight)/2-tex.height/2 + int(node.@frameY)
- }
- }
- }
- public static function alignAtlas(pAtlas:GTextureAtlas, pIds:Array, pAlign:int):void{
- var len:uint = pIds.length;
- for(var i:uint; i < len; ++ i ){
- var tex:GTexture = pAtlas.getTexture(pIds[i]) ;
- if(tex){
- tex.alignTexture(pAlign)
- }
- }
- }
- public static function disposeBitmapsAtlas(pAtlas:GTextureAtlas, pIds:Array):void{
- var len:uint = pIds.length;
- for(var i:uint; i < len; ++ i ){
- var tex:GTexture = pAtlas.getTexture(pIds[i]) ;
- if(tex) tex.bitmapData.dispose();
- }
- }
- public static function buildFrames( base:String, startCount:uint=1, endCount:uint = 10, exclude:Array=null):Array{
- var len: uint = endCount - startCount ;
- var output:Array = [] ;
- for(var i:uint = startCount; i <= endCount; ++i){
- if(exclude && exclude.indexOf(i)!=-1){
- } else {
- output.push(base + String(i))
- }
- }
- return output ;
- }
- public static function createAtlasFromMovie(pId:String, pMovieClip:MovieClip, pExcludedFrames:Array = null, pForce:Boolean=false, pDebugColor:uint=0x0, pXMLAtlas:XML = null):GTextureAtlas{
- var i:uint = 1 ;
- var m:Matrix = new Matrix();
- var bitmaps:Vector.<BitmapData> = new Vector.<BitmapData>();
- var ids:Vector.<String> = new Vector.<String>();
- while(i <= pMovieClip.totalFrames){
- pMovieClip.gotoAndStop(i);
- var lbl:String = pMovieClip.currentLabel;
- if(!lbl){
- lbl = String(i);
- }
- if( pExcludedFrames ){
- // frame id!
- if( pExcludedFrames.indexOf(i) != -1 || pExcludedFrames.indexOf(lbl) != -1 ){
- ++i;
- continue;
- }
- }
- var w:int = pForce ? pMovieClip.width+1:pMovieClip.width;
- var h:int = pForce ? pMovieClip.height+1:pMovieClip.height;
- var bd:BitmapData = new BitmapData(w, h, true, pDebugColor );
- var rect:Rectangle = pMovieClip.getBounds(pMovieClip);
- m.identity();
- m.translate(-rect.x, -rect.y);
- if(pXMLAtlas){
- var xmlChild:XML = <SubTexture/>
- xmlChild.@name = lbl;
- xmlChild.@width = w;
- xmlChild.@height = h;
- xmlChild.@frameX = -rect.x;
- xmlChild.@frameY = -rect.y;
- pXMLAtlas.appendChild(xmlChild)
- }
- bd.draw(pMovieClip, m);
- ids.push(lbl);
- bitmaps.push(bd);
- ++i;
- }
- trace("GTextureAtlas",pId, " has frames:\n", ids);
- return GTextureAtlasFactory.createFromBitmapDatas(pId, bitmaps, ids ) ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment