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 / styles / DecoAsset.as next >
Encoding:
Text File  |  2009-10-26  |  6.6 KB  |  199 lines

  1. package com.livebrush.styles
  2. {
  3.    import com.livebrush.data.FileManager;
  4.    import com.livebrush.ui.UI;
  5.    import flash.display.ActionScriptVersion;
  6.    import flash.display.Bitmap;
  7.    import flash.display.Loader;
  8.    import flash.display.LoaderInfo;
  9.    import flash.display.MovieClip;
  10.    import flash.display.SWFVersion;
  11.    import flash.display.Sprite;
  12.    import flash.events.Event;
  13.    import flash.events.EventDispatcher;
  14.    import flash.events.IOErrorEvent;
  15.    import flash.geom.Rectangle;
  16.    
  17.    public class DecoAsset extends EventDispatcher
  18.    {
  19.       public static const JPG:String = "image/jpeg";
  20.       
  21.       public static const GIF:String = "image/gif";
  22.       
  23.       public static const PNG:String = "image/png";
  24.       
  25.       public static const SWF:String = "application/x-shockwave-flash";
  26.       
  27.       public static var idList:Array = [];
  28.       
  29.       public var enabled:Boolean = true;
  30.       
  31.       public var loaded:Boolean;
  32.       
  33.       private var fileManager:FileManager;
  34.       
  35.       private var loader:Loader;
  36.       
  37.       public var contentType:String;
  38.       
  39.       public var id:int;
  40.       
  41.       public var fileName:String;
  42.       
  43.       public var graphic:Sprite;
  44.       
  45.       public var bitmap:Bitmap;
  46.       
  47.       public var assetPath:String;
  48.       
  49.       public var missing:Boolean = true;
  50.       
  51.       public function DecoAsset(assetPath:String, asset:Sprite = null, contentType:String = "application/x-shockwave-flash")
  52.       {
  53.          super();
  54.          this.id = getNewID();
  55.          this.loaded = false;
  56.          this.assetPath = assetPath;
  57.          this.fileName = assetPath.substr(assetPath.lastIndexOf("/") + 1);
  58.          this.fileManager = FileManager.getInstance();
  59.          if(asset == null)
  60.          {
  61.             this.graphic = new Sprite();
  62.             this.loadAsset();
  63.          }
  64.          else
  65.          {
  66.             this.graphic = asset;
  67.             this.contentType = contentType;
  68.             if(contentType != SWF)
  69.             {
  70.                this.bitmap = this.graphic.getChildAt(0) as Bitmap;
  71.             }
  72.             this.loaded = true;
  73.          }
  74.       }
  75.       
  76.       public static function getNewID() : int
  77.       {
  78.          var newID:int = 0;
  79.          var highestID:int = 0;
  80.          for(var i:int = 0; i < idList.length; i++)
  81.          {
  82.             if(idList[i] >= highestID)
  83.             {
  84.                highestID = int(idList[i]);
  85.             }
  86.          }
  87.          newID = highestID + 1;
  88.          idList.push(newID);
  89.          return newID;
  90.       }
  91.       
  92.       public function die() : void
  93.       {
  94.       }
  95.       
  96.       private function loadCompleteHandler(e:Event) : void
  97.       {
  98.          this.removeLoadHandlers(e.target as LoaderInfo);
  99.          this.loader = e.target.loader;
  100.          this.setup(this.loader,e.target as LoaderInfo);
  101.       }
  102.       
  103.       private function loadInitHandler(e:Event) : void
  104.       {
  105.       }
  106.       
  107.       private function init() : void
  108.       {
  109.       }
  110.       
  111.       private function removeLoadHandlers(loaderInfo:LoaderInfo) : void
  112.       {
  113.          loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.loadErrorHandler);
  114.          loaderInfo.removeEventListener(Event.COMPLETE,this.loadCompleteHandler);
  115.          loaderInfo.removeEventListener(Event.INIT,this.loadInitHandler);
  116.       }
  117.       
  118.       public function get value() : String
  119.       {
  120.          return this.fileName;
  121.       }
  122.       
  123.       private function loadErrorHandler(e:IOErrorEvent) : void
  124.       {
  125.          this.contentType = SWF;
  126.          this.loaded = true;
  127.          this.removeLoadHandlers(e.target as LoaderInfo);
  128.          var child:Sprite = new MissingDeco();
  129.          this.graphic.addChild(child);
  130.          dispatchEvent(new Event(Event.COMPLETE,true,false));
  131.       }
  132.       
  133.       public function loadAsset() : void
  134.       {
  135.          this.loader = this.fileManager.decoLoader(this.fileName);
  136.          this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.loadErrorHandler);
  137.          this.loader.contentLoaderInfo.addEventListener(Event.INIT,this.loadInitHandler);
  138.          this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.loadCompleteHandler);
  139.       }
  140.       
  141.       public function copy(reload:Boolean = false) : DecoAsset
  142.       {
  143.          return new DecoAsset(this.assetPath,reload ? null : this.graphic,this.contentType);
  144.       }
  145.       
  146.       private function setup(loader:Loader, info:LoaderInfo) : void
  147.       {
  148.          var swf:MovieClip = null;
  149.          var child:Sprite = null;
  150.          var bounds:Rectangle = null;
  151.          this.contentType = info.contentType;
  152.          if(this.contentType != SWF)
  153.          {
  154.             this.bitmap = loader.content as Bitmap;
  155.             this.bitmap.smoothing = true;
  156.             this.graphic.addChild(this.bitmap);
  157.             this.missing = false;
  158.          }
  159.          else if(loader.content is MovieClip && info.actionScriptVersion == ActionScriptVersion.ACTIONSCRIPT3 && info.swfVersion == SWFVersion.FLASH9)
  160.          {
  161.             if(MovieClip(loader.content).numChildren > 0)
  162.             {
  163.                swf = MovieClip(loader.content);
  164.                if(swf.getChildAt(0) is Sprite)
  165.                {
  166.                   child = Sprite(swf.getChildAt(0));
  167.                   child.x = child.y = 0;
  168.                }
  169.                else
  170.                {
  171.                   child = Sprite(this.graphic.addChild(new DefaultDeco()));
  172.                   UI.MAIN_UI.alert({
  173.                      "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.",
  174.                      "id":"decoSWFAlert"
  175.                   });
  176.                }
  177.                bounds = child.getBounds(child);
  178.                child.x -= bounds.x;
  179.                child.y -= bounds.y;
  180.                this.graphic.addChild(child);
  181.                this.missing = false;
  182.             }
  183.          }
  184.          else
  185.          {
  186.             child = Sprite(new DefaultDeco());
  187.             this.graphic.addChild(child);
  188.             UI.MAIN_UI.alert({
  189.                "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.",
  190.                "id":"decoSWFAlert"
  191.             });
  192.          }
  193.          this.loaded = true;
  194.          dispatchEvent(new Event(Event.COMPLETE,true,false));
  195.       }
  196.    }
  197. }
  198.  
  199.