home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2011 May / ME_2011_05.iso / Galileo-Video-Tutorial / system / player.swf / scripts / com / je / model / AssetManager.as < prev    next >
Encoding:
Text File  |  2010-11-30  |  4.5 KB  |  143 lines

  1. package com.je.model
  2. {
  3.    import com.je.data.Queue;
  4.    import com.je.data.QueueItem;
  5.    import com.je.data.loader.ILoader;
  6.    import com.je.data.loader.LoaderFactory;
  7.    import com.je.events.AssetLoaderEvent;
  8.    import com.je.events.AssetManagerEventFactory;
  9.    import com.je.utils.FileUtils;
  10.    import controller.LoadingController;
  11.    import controller.VersionController;
  12.    import mdm.Application;
  13.    import org.robotlegs.mvcs.Actor;
  14.    
  15.    public class AssetManager extends Actor
  16.    {
  17.       [Inject]
  18.       public var assets:Assets;
  19.       
  20.       private var _queue:Queue;
  21.       
  22.       private var _currentItem:QueueItem;
  23.       
  24.       private var _loaderFactory:LoaderFactory;
  25.       
  26.       private var _eventFactory:AssetManagerEventFactory;
  27.       
  28.       private var counter:int = 0;
  29.       
  30.       private var lc:LoadingController;
  31.       
  32.       private var isDone:Boolean = false;
  33.       
  34.       public function AssetManager()
  35.       {
  36.          super();
  37.          this._loaderFactory = new LoaderFactory();
  38.          this._eventFactory = new AssetManagerEventFactory();
  39.          this.lc = LoadingController.getInstance();
  40.       }
  41.       
  42.       public function addQueue(param1:Queue) : void
  43.       {
  44.          this._queue = param1;
  45.          this._currentItem = this._queue.getItemAt(0);
  46.       }
  47.       
  48.       public function load() : void
  49.       {
  50.          var _loc1_:String = this._currentItem.path;
  51.          var _loc2_:String = FileUtils.getFileExtension(_loc1_);
  52.          var _loc3_:ILoader = this._loaderFactory.getLoader(_loc2_);
  53.          _loc3_.addEventListener(AssetLoaderEvent.COMPLETE,this.completeHandler);
  54.          _loc3_.addEventListener(AssetLoaderEvent.IO_ERROR,this.errorHandler);
  55.          if(VersionController.getInstance("").version == "linux")
  56.          {
  57.             _loc3_.loadUrl(VersionController.getInstance("").path + _loc1_);
  58.          }
  59.          else
  60.          {
  61.             _loc3_.loadUrl(Application.path + _loc1_);
  62.          }
  63.       }
  64.       
  65.       private function completeHandler(param1:AssetLoaderEvent) : void
  66.       {
  67.          ++this.counter;
  68.          var _loc2_:ILoader = ILoader(param1.target);
  69.          _loc2_.removeEventListener(AssetLoaderEvent.COMPLETE,this.completeHandler);
  70.          _loc2_.removeEventListener(AssetLoaderEvent.IO_ERROR,this.errorHandler);
  71.          this.assets.storeAsset(this._currentItem.linkedId,_loc2_.getContent());
  72.          _loc2_.destroy();
  73.          dispatch(this._eventFactory.getItemCompleteEvent(this._currentItem.itemLoadedEvent));
  74.          this.lc.total = Number(this._queue.getQueueLenght() + this._queue.getQueueLenght());
  75.          this.lc.loaded = this.counter;
  76.          if(this.counter < this._queue.getQueueLenght())
  77.          {
  78.             this.loadNextItem();
  79.          }
  80.          else
  81.          {
  82.             this.done();
  83.          }
  84.       }
  85.       
  86.       private function errorHandler(param1:AssetLoaderEvent) : void
  87.       {
  88.          ++this.counter;
  89.          var _loc2_:ILoader = ILoader(param1.target);
  90.          _loc2_.removeEventListener(AssetLoaderEvent.COMPLETE,this.completeHandler);
  91.          _loc2_.removeEventListener(AssetLoaderEvent.IO_ERROR,this.errorHandler);
  92.          _loc2_.destroy();
  93.          if(this.counter < this._queue.getQueueLenght())
  94.          {
  95.             this.loadNextItem();
  96.          }
  97.          else
  98.          {
  99.             this.done();
  100.          }
  101.          var _loc3_:AssetLoaderEvent = new AssetLoaderEvent(AssetLoaderEvent.IO_ERROR);
  102.          _loc3_.message = param1.message;
  103.          dispatch(_loc3_);
  104.       }
  105.       
  106.       private function done() : void
  107.       {
  108.          if(this.isDone)
  109.          {
  110.          }
  111.          this.isDone = true;
  112.          dispatch(this._eventFactory.getCompleteEvent(this._queue.commpleteEvent));
  113.          this.counter = 0;
  114.       }
  115.       
  116.       private function loadNextItem() : void
  117.       {
  118.          this._currentItem = this._queue.getNextItem();
  119.          if(this._currentItem != null || this._currentItem.path != null || this._currentItem.path != "")
  120.          {
  121.             this.load();
  122.          }
  123.          else if(this._currentItem == null || this._currentItem.path == null || this._currentItem.path == "")
  124.          {
  125.             this.loadNextItem();
  126.             ++this.counter;
  127.          }
  128.          else if(this.counter < this._queue.getQueueLenght())
  129.          {
  130.             ++this.counter;
  131.             this.loadNextItem();
  132.          }
  133.       }
  134.       
  135.       private function reset() : void
  136.       {
  137.          this._queue.clearQueue();
  138.          this._queue = null;
  139.       }
  140.    }
  141. }
  142.  
  143.