home *** CD-ROM | disk | FTP | other *** search
- package com.livebrush.graphics
- {
- import com.livebrush.data.FileManager;
- import com.livebrush.data.Settings;
- import com.livebrush.data.Storable;
- import com.livebrush.styles.DecoAsset;
- import com.livebrush.styles.DecoStyle;
- import com.livebrush.transform.TransformSprite;
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.PixelSnapping;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.EventDispatcher;
- import flash.geom.ColorTransform;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import org.casalib.math.Percent;
- import org.casalib.util.ColorUtil;
-
- public class Deco extends EventDispatcher implements Storable
- {
- public static const STATIC:String = "static";
-
- public static const DYNAMIC:String = "dynamic";
-
- public static var idList:Array = [];
-
- public var decoMC:TransformSprite;
-
- public var align:String;
-
- private var emptyCTF:ColorTransform;
-
- private var bmp:BitmapData;
-
- public var offset:Object;
-
- private var ctf:ColorTransform;
-
- public var id:int;
-
- private var drawQueue:Object = null;
-
- private var drawn:Boolean = false;
-
- public var type:String = "static";
-
- public var decoAsset:DecoAsset;
-
- public var initObj:Settings;
-
- public var scale:Object;
-
- private var decoBitmap:Bitmap = null;
-
- private var decoSprite:Sprite;
-
- public var angle:Number;
-
- public var pos:Number;
-
- public var alpha:Number;
-
- private var _matrix:Matrix;
-
- public var colorPercent:Number = 1;
-
- public var color:Number = -1;
-
- private var brushState:Object = null;
-
- public var y:Number;
-
- public var x:Number;
-
- public function Deco(_decoAsset:DecoAsset, initObj:Settings = null)
- {
- super();
- this.id = getNewID();
- this.brushState = {};
- this.decoAsset = _decoAsset;
- this.initObj = initObj;
- this.init();
- }
-
- public static function getNewID() : int
- {
- var newID:int = 0;
- var highestID:int = 0;
- for(var i:int = 0; i < idList.length; i++)
- {
- if(idList[i] >= highestID)
- {
- highestID = int(idList[i]);
- }
- }
- newID = highestID + 1;
- idList.push(newID);
- return newID;
- }
-
- public static function xmlToDeco(xml:String) : Deco
- {
- var decoXML:XML = new XML(xml);
- var decoAsset:* = FileManager.getInstance().getDecoAsset(decoXML.@asset);
- var initObj:Settings = new Settings();
- initObj.pos = Number(decoXML.@pos);
- initObj.offset = {
- "x":Number(decoXML.@offsetX),
- "y":Number(decoXML.@offsetY)
- };
- initObj.scale = {
- "x":Number(decoXML.@scaleX),
- "y":Number(decoXML.@scaleY)
- };
- initObj.angle = Number(decoXML.@angle);
- initObj.color = Number(decoXML.@color);
- if(decoXML.@colorPercent != null)
- {
- initObj.colorPercent = Number(decoXML.@colorPercent);
- }
- initObj.alpha = Number(decoXML.@alpha);
- initObj.align = String(decoXML.@align);
- return new Deco(decoAsset,initObj);
- }
-
- public static function stringToMatrix(str:String) : Matrix
- {
- var mat:Matrix = new Matrix();
- var list:Array = str.split(",");
- mat.a = Number(list[0]);
- mat.b = Number(list[1]);
- mat.c = Number(list[2]);
- mat.d = Number(list[3]);
- mat.tx = Number(list[4]);
- mat.ty = Number(list[5]);
- return mat;
- }
-
- public function die() : void
- {
- this.removeDeco();
- }
-
- public function get loaded() : Boolean
- {
- return this.decoAsset.loaded;
- }
-
- private function init() : void
- {
- this.decoMC = new TransformSprite();
- this.decoSprite = new Sprite();
- this.offset = {
- "x":0,
- "y":0
- };
- this.applyProps();
- this.brushState.id = this.id;
- this.brushState.color = this.color;
- this.brushState.alpha = this.alpha;
- this.emptyCTF = new ColorTransform();
- this.decoAsset.addEventListener(Event.COMPLETE,this.decoAssetLoad);
- }
-
- protected function setColor() : void
- {
- var newCTF:ColorTransform = null;
- if(this.color != -1)
- {
- newCTF = new ColorTransform();
- newCTF.color = this.color;
- this.ctf = this.decoBitmap.transform.colorTransform = ColorUtil.interpolateColor(new ColorTransform(),newCTF,new Percent(this.colorPercent));
- }
- }
-
- private function applyProps() : void
- {
- var prop:String = null;
- for(prop in this.initObj)
- {
- this[prop] = this.initObj[prop];
- }
- }
-
- private function decoAssetLoad(e:Event) : void
- {
- if(this.drawQueue != null)
- {
- this.draw(this.drawQueue.target);
- this.drawQueue = null;
- }
- dispatchEvent(new Event(Event.COMPLETE,true,false));
- }
-
- public function removeDeco() : void
- {
- if(this.decoSprite.numChildren > 0)
- {
- this.decoSprite.removeChild(this.decoSprite.getChildAt(0));
- }
- if(this.decoMC.numChildren > 0)
- {
- this.decoMC.removeChild(this.decoMC.getChildAt(0));
- }
- if(this.decoBitmap != null)
- {
- this.bmp.dispose();
- this.decoBitmap.bitmapData = null;
- this.decoBitmap = null;
- this.bmp = null;
- }
- this.decoMC = null;
- }
-
- public function setXML(xml:String) : void
- {
- }
-
- public function draw(decos:Sprite) : void
- {
- var bounds:Rectangle = null;
- var b:Point = null;
- var mat:Matrix = null;
- if(this.loaded)
- {
- this.removeDeco();
- this.decoMC = new TransformSprite();
- this.decoMC.addChild(this.decoAsset.graphic);
- if(this.align == DecoStyle.ALIGN_CENTER)
- {
- this.decoMC.setRegistration(this.decoMC.width / 2,this.decoMC.height / 2);
- }
- else if(this.align == DecoStyle.ALIGN_TL)
- {
- this.decoMC.setRegistration(0,0);
- }
- else
- {
- this.decoMC.setRegistration(0,this.decoMC.height);
- }
- decos.addChild(this.decoMC);
- if(this.scale.y == 0)
- {
- this.scale.y = this.scale.x;
- }
- this.decoMC.scale(this.scale.x,this.scale.y);
- this.decoMC.rotation2 = this.angle;
- this.decoMC.translate(this.x,this.y);
- bounds = this.decoMC.getBounds(decos);
- b = new Point(int(Math.max(2,bounds.width)),int(Math.max(2,this.decoMC.height)));
- try
- {
- this.bmp = new BitmapData(b.x,b.y,true,16711680);
- }
- catch(e:Error)
- {
- bmp = new BitmapData(b.x,b.x,true,16711680);
- }
- catch(e:Error)
- {
- bmp = new BitmapData(b.y,b.y,true,16711680);
- }
- catch(e:Error)
- {
- bmp = new BitmapData(50,50,true,16711680);
- }
- catch(e:Error)
- {
- return;
- }
- this.decoBitmap = new Bitmap(this.bmp,PixelSnapping.AUTO,true);
- this.decoBitmap.x = bounds.x;
- this.decoBitmap.y = bounds.y;
- decos.addChild(this.decoBitmap);
- this.decoSprite = new Sprite();
- this.decoSprite.transform.matrix = this.decoBitmap.transform.matrix;
- this.decoSprite.addChild(this.decoMC);
- decos.addChild(this.decoSprite);
- bounds = this.decoSprite.getBounds(this.decoSprite);
- mat = this.decoSprite.transform.matrix;
- mat.tx = -bounds.x;
- mat.ty = -bounds.y;
- this.bmp.draw(this.decoSprite,mat);
- decos.removeChild(this.decoSprite);
- this.setColor();
- this.decoBitmap.alpha = this.alpha;
- }
- else
- {
- this.drawQueue = {"target":decos};
- }
- }
-
- public function getXML() : XML
- {
- 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}/>);
- }
-
- public function copy() : Deco
- {
- return new Deco(this.decoAsset.copy(),this.initObj);
- }
- }
- }
-
-