home *** CD-ROM | disk | FTP | other *** search
Wrap
package com.livebrush.styles { import com.livebrush.data.FileManager; import com.livebrush.ui.UI; import flash.display.ActionScriptVersion; import flash.display.Bitmap; import flash.display.Loader; import flash.display.LoaderInfo; import flash.display.MovieClip; import flash.display.SWFVersion; import flash.display.Sprite; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.geom.Rectangle; public class DecoAsset extends EventDispatcher { public static const JPG:String = "image/jpeg"; public static const GIF:String = "image/gif"; public static const PNG:String = "image/png"; public static const SWF:String = "application/x-shockwave-flash"; public static var idList:Array = []; public var enabled:Boolean = true; public var loaded:Boolean; private var fileManager:FileManager; private var loader:Loader; public var contentType:String; public var id:int; public var fileName:String; public var graphic:Sprite; public var bitmap:Bitmap; public var assetPath:String; public var missing:Boolean = true; public function DecoAsset(assetPath:String, asset:Sprite = null, contentType:String = "application/x-shockwave-flash") { super(); this.id = getNewID(); this.loaded = false; this.assetPath = assetPath; this.fileName = assetPath.substr(assetPath.lastIndexOf("/") + 1); this.fileManager = FileManager.getInstance(); if(asset == null) { this.graphic = new Sprite(); this.loadAsset(); } else { this.graphic = asset; this.contentType = contentType; if(contentType != SWF) { this.bitmap = this.graphic.getChildAt(0) as Bitmap; } this.loaded = true; } } 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 function die() : void { } private function loadCompleteHandler(e:Event) : void { this.removeLoadHandlers(e.target as LoaderInfo); this.loader = e.target.loader; this.setup(this.loader,e.target as LoaderInfo); } private function loadInitHandler(e:Event) : void { } private function init() : void { } private function removeLoadHandlers(loaderInfo:LoaderInfo) : void { loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.loadErrorHandler); loaderInfo.removeEventListener(Event.COMPLETE,this.loadCompleteHandler); loaderInfo.removeEventListener(Event.INIT,this.loadInitHandler); } public function get value() : String { return this.fileName; } private function loadErrorHandler(e:IOErrorEvent) : void { this.contentType = SWF; this.loaded = true; this.removeLoadHandlers(e.target as LoaderInfo); var child:Sprite = new MissingDeco(); this.graphic.addChild(child); dispatchEvent(new Event(Event.COMPLETE,true,false)); } public function loadAsset() : void { this.loader = this.fileManager.decoLoader(this.fileName); this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.loadErrorHandler); this.loader.contentLoaderInfo.addEventListener(Event.INIT,this.loadInitHandler); this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.loadCompleteHandler); } public function copy(reload:Boolean = false) : DecoAsset { return new DecoAsset(this.assetPath,reload ? null : this.graphic,this.contentType); } private function setup(loader:Loader, info:LoaderInfo) : void { var swf:MovieClip = null; var child:Sprite = null; var bounds:Rectangle = null; this.contentType = info.contentType; if(this.contentType != SWF) { this.bitmap = loader.content as Bitmap; this.bitmap.smoothing = true; this.graphic.addChild(this.bitmap); this.missing = false; } else if(loader.content is MovieClip && info.actionScriptVersion == ActionScriptVersion.ACTIONSCRIPT3 && info.swfVersion == SWFVersion.FLASH9) { if(MovieClip(loader.content).numChildren > 0) { swf = MovieClip(loader.content); if(swf.getChildAt(0) is Sprite) { child = Sprite(swf.getChildAt(0)); child.x = child.y = 0; } else { child = Sprite(this.graphic.addChild(new DefaultDeco())); UI.MAIN_UI.alert({ "message":"<b>Missing SWF Asset</b>\nYour SWF should have a MovieClip on the first frame of the main timeline. <b><a href=\'http://www.Livebrush.com/help/SWF_Support.html\' target=\'_blank\'>Click here</a></b> for Livebrush Help.", "id":"decoSWFAlert" }); } bounds = child.getBounds(child); child.x -= bounds.x; child.y -= bounds.y; this.graphic.addChild(child); this.missing = false; } } else { child = Sprite(new DefaultDeco()); this.graphic.addChild(child); UI.MAIN_UI.alert({ "message":"<b>Invalid Layer Asset</b>\nSWF assets must be exported for Flash Player 9 using Actionscript 3. <b><a href=\'http://www.Livebrush.com/help/SWF_Support.html\' target=\'_blank\'>Click here</a></b> for Livebrush Help.", "id":"decoSWFAlert" }); } this.loaded = true; dispatchEvent(new Event(Event.COMPLETE,true,false)); } } }