home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 2010 Software/Programs / PCGuia_programas.iso / Software / Utils / Livebrush / Install-LivebrushLite.air / livebrush.swf / scripts / com / livebrush / graphics / Deco.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  9.3 KB  |  309 lines

  1. package com.livebrush.graphics
  2. {
  3.    import com.livebrush.data.FileManager;
  4.    import com.livebrush.data.Settings;
  5.    import com.livebrush.data.Storable;
  6.    import com.livebrush.styles.DecoAsset;
  7.    import com.livebrush.styles.DecoStyle;
  8.    import com.livebrush.transform.TransformSprite;
  9.    import flash.display.Bitmap;
  10.    import flash.display.BitmapData;
  11.    import flash.display.PixelSnapping;
  12.    import flash.display.Sprite;
  13.    import flash.events.Event;
  14.    import flash.events.EventDispatcher;
  15.    import flash.geom.ColorTransform;
  16.    import flash.geom.Matrix;
  17.    import flash.geom.Point;
  18.    import flash.geom.Rectangle;
  19.    import org.casalib.math.Percent;
  20.    import org.casalib.util.ColorUtil;
  21.    
  22.    public class Deco extends EventDispatcher implements Storable
  23.    {
  24.       public static const STATIC:String = "static";
  25.       
  26.       public static const DYNAMIC:String = "dynamic";
  27.       
  28.       public static var idList:Array = [];
  29.       
  30.       public var decoMC:TransformSprite;
  31.       
  32.       public var align:String;
  33.       
  34.       private var emptyCTF:ColorTransform;
  35.       
  36.       private var bmp:BitmapData;
  37.       
  38.       public var offset:Object;
  39.       
  40.       private var ctf:ColorTransform;
  41.       
  42.       public var id:int;
  43.       
  44.       private var drawQueue:Object = null;
  45.       
  46.       private var drawn:Boolean = false;
  47.       
  48.       public var type:String = "static";
  49.       
  50.       public var decoAsset:DecoAsset;
  51.       
  52.       public var initObj:Settings;
  53.       
  54.       public var scale:Object;
  55.       
  56.       private var decoBitmap:Bitmap = null;
  57.       
  58.       private var decoSprite:Sprite;
  59.       
  60.       public var angle:Number;
  61.       
  62.       public var pos:Number;
  63.       
  64.       public var alpha:Number;
  65.       
  66.       private var _matrix:Matrix;
  67.       
  68.       public var colorPercent:Number = 1;
  69.       
  70.       public var color:Number = -1;
  71.       
  72.       private var brushState:Object = null;
  73.       
  74.       public var y:Number;
  75.       
  76.       public var x:Number;
  77.       
  78.       public function Deco(_decoAsset:DecoAsset, initObj:Settings = null)
  79.       {
  80.          super();
  81.          this.id = getNewID();
  82.          this.brushState = {};
  83.          this.decoAsset = _decoAsset;
  84.          this.initObj = initObj;
  85.          this.init();
  86.       }
  87.       
  88.       public static function getNewID() : int
  89.       {
  90.          var newID:int = 0;
  91.          var highestID:int = 0;
  92.          for(var i:int = 0; i < idList.length; i++)
  93.          {
  94.             if(idList[i] >= highestID)
  95.             {
  96.                highestID = int(idList[i]);
  97.             }
  98.          }
  99.          newID = highestID + 1;
  100.          idList.push(newID);
  101.          return newID;
  102.       }
  103.       
  104.       public static function xmlToDeco(xml:String) : Deco
  105.       {
  106.          var decoXML:XML = new XML(xml);
  107.          var decoAsset:* = FileManager.getInstance().getDecoAsset(decoXML.@asset);
  108.          var initObj:Settings = new Settings();
  109.          initObj.pos = Number(decoXML.@pos);
  110.          initObj.offset = {
  111.             "x":Number(decoXML.@offsetX),
  112.             "y":Number(decoXML.@offsetY)
  113.          };
  114.          initObj.scale = {
  115.             "x":Number(decoXML.@scaleX),
  116.             "y":Number(decoXML.@scaleY)
  117.          };
  118.          initObj.angle = Number(decoXML.@angle);
  119.          initObj.color = Number(decoXML.@color);
  120.          if(decoXML.@colorPercent != null)
  121.          {
  122.             initObj.colorPercent = Number(decoXML.@colorPercent);
  123.          }
  124.          initObj.alpha = Number(decoXML.@alpha);
  125.          initObj.align = String(decoXML.@align);
  126.          return new Deco(decoAsset,initObj);
  127.       }
  128.       
  129.       public static function stringToMatrix(str:String) : Matrix
  130.       {
  131.          var mat:Matrix = new Matrix();
  132.          var list:Array = str.split(",");
  133.          mat.a = Number(list[0]);
  134.          mat.b = Number(list[1]);
  135.          mat.c = Number(list[2]);
  136.          mat.d = Number(list[3]);
  137.          mat.tx = Number(list[4]);
  138.          mat.ty = Number(list[5]);
  139.          return mat;
  140.       }
  141.       
  142.       public function die() : void
  143.       {
  144.          this.removeDeco();
  145.       }
  146.       
  147.       public function get loaded() : Boolean
  148.       {
  149.          return this.decoAsset.loaded;
  150.       }
  151.       
  152.       private function init() : void
  153.       {
  154.          this.decoMC = new TransformSprite();
  155.          this.decoSprite = new Sprite();
  156.          this.offset = {
  157.             "x":0,
  158.             "y":0
  159.          };
  160.          this.applyProps();
  161.          this.brushState.id = this.id;
  162.          this.brushState.color = this.color;
  163.          this.brushState.alpha = this.alpha;
  164.          this.emptyCTF = new ColorTransform();
  165.          this.decoAsset.addEventListener(Event.COMPLETE,this.decoAssetLoad);
  166.       }
  167.       
  168.       protected function setColor() : void
  169.       {
  170.          var newCTF:ColorTransform = null;
  171.          if(this.color != -1)
  172.          {
  173.             newCTF = new ColorTransform();
  174.             newCTF.color = this.color;
  175.             this.ctf = this.decoBitmap.transform.colorTransform = ColorUtil.interpolateColor(new ColorTransform(),newCTF,new Percent(this.colorPercent));
  176.          }
  177.       }
  178.       
  179.       private function applyProps() : void
  180.       {
  181.          var prop:String = null;
  182.          for(prop in this.initObj)
  183.          {
  184.             this[prop] = this.initObj[prop];
  185.          }
  186.       }
  187.       
  188.       private function decoAssetLoad(e:Event) : void
  189.       {
  190.          if(this.drawQueue != null)
  191.          {
  192.             this.draw(this.drawQueue.target);
  193.             this.drawQueue = null;
  194.          }
  195.          dispatchEvent(new Event(Event.COMPLETE,true,false));
  196.       }
  197.       
  198.       public function removeDeco() : void
  199.       {
  200.          if(this.decoSprite.numChildren > 0)
  201.          {
  202.             this.decoSprite.removeChild(this.decoSprite.getChildAt(0));
  203.          }
  204.          if(this.decoMC.numChildren > 0)
  205.          {
  206.             this.decoMC.removeChild(this.decoMC.getChildAt(0));
  207.          }
  208.          if(this.decoBitmap != null)
  209.          {
  210.             this.bmp.dispose();
  211.             this.decoBitmap.bitmapData = null;
  212.             this.decoBitmap = null;
  213.             this.bmp = null;
  214.          }
  215.          this.decoMC = null;
  216.       }
  217.       
  218.       public function setXML(xml:String) : void
  219.       {
  220.       }
  221.       
  222.       public function draw(decos:Sprite) : void
  223.       {
  224.          var bounds:Rectangle = null;
  225.          var b:Point = null;
  226.          var mat:Matrix = null;
  227.          if(this.loaded)
  228.          {
  229.             this.removeDeco();
  230.             this.decoMC = new TransformSprite();
  231.             this.decoMC.addChild(this.decoAsset.graphic);
  232.             if(this.align == DecoStyle.ALIGN_CENTER)
  233.             {
  234.                this.decoMC.setRegistration(this.decoMC.width / 2,this.decoMC.height / 2);
  235.             }
  236.             else if(this.align == DecoStyle.ALIGN_TL)
  237.             {
  238.                this.decoMC.setRegistration(0,0);
  239.             }
  240.             else
  241.             {
  242.                this.decoMC.setRegistration(0,this.decoMC.height);
  243.             }
  244.             decos.addChild(this.decoMC);
  245.             if(this.scale.y == 0)
  246.             {
  247.                this.scale.y = this.scale.x;
  248.             }
  249.             this.decoMC.scale(this.scale.x,this.scale.y);
  250.             this.decoMC.rotation2 = this.angle;
  251.             this.decoMC.translate(this.x,this.y);
  252.             bounds = this.decoMC.getBounds(decos);
  253.             b = new Point(int(Math.max(2,bounds.width)),int(Math.max(2,this.decoMC.height)));
  254.             try
  255.             {
  256.                this.bmp = new BitmapData(b.x,b.y,true,16711680);
  257.             }
  258.             catch(e:Error)
  259.             {
  260.                bmp = new BitmapData(b.x,b.x,true,16711680);
  261.             }
  262.             catch(e:Error)
  263.             {
  264.                bmp = new BitmapData(b.y,b.y,true,16711680);
  265.             }
  266.             catch(e:Error)
  267.             {
  268.                bmp = new BitmapData(50,50,true,16711680);
  269.             }
  270.             catch(e:Error)
  271.             {
  272.                return;
  273.             }
  274.             this.decoBitmap = new Bitmap(this.bmp,PixelSnapping.AUTO,true);
  275.             this.decoBitmap.x = bounds.x;
  276.             this.decoBitmap.y = bounds.y;
  277.             decos.addChild(this.decoBitmap);
  278.             this.decoSprite = new Sprite();
  279.             this.decoSprite.transform.matrix = this.decoBitmap.transform.matrix;
  280.             this.decoSprite.addChild(this.decoMC);
  281.             decos.addChild(this.decoSprite);
  282.             bounds = this.decoSprite.getBounds(this.decoSprite);
  283.             mat = this.decoSprite.transform.matrix;
  284.             mat.tx = -bounds.x;
  285.             mat.ty = -bounds.y;
  286.             this.bmp.draw(this.decoSprite,mat);
  287.             decos.removeChild(this.decoSprite);
  288.             this.setColor();
  289.             this.decoBitmap.alpha = this.alpha;
  290.          }
  291.          else
  292.          {
  293.             this.drawQueue = {"target":decos};
  294.          }
  295.       }
  296.       
  297.       public function getXML() : XML
  298.       {
  299.          return new XML(<deco id={this.id} asset={this.decoAsset.fileName} pos={this.pos} offsetX={this.offset.x} offsetY={this.offset.y} scaleX={this.scale.x} scaleY={this.scale.y} angle={this.angle} color={this.color} colorPercent={this.colorPercent} alpha={this.alpha} align={this.align}/>);
  300.       }
  301.       
  302.       public function copy() : Deco
  303.       {
  304.          return new Deco(this.decoAsset.copy(),this.initObj);
  305.       }
  306.    }
  307. }
  308.  
  309.