package com.zehfernando.data { import br.com.stimuli.loading.BulkLoader; import br.com.stimuli.loading.BulkProgressEvent; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IEventDispatcher; import flash.events.ProgressEvent; /** * @author Zeh Fernando */ public class DataAssetManager extends EventDispatcher { // A sort of a proxy for BulkLoader protected static var instance:DataAssetManager; // Properties protected var loader:BulkLoader; // ================================================================================================================ // CONSTRUCTOR ---------------------------------------------------------------------------------------------------- public function DataAssetManager(target:IEventDispatcher = null) { super(target); setInstance(this); loader = new BulkLoader("dataAssetManagerMain"); loader.addEventListener(BulkLoader.ERROR, onLoadingError); loader.addEventListener(BulkLoader.PROGRESS, onLoadingProgress); loader.addEventListener(BulkLoader.COMPLETE, onLoadingComplete); } // ================================================================================================================ // INSTANCE functions --------------------------------------------------------------------------------------------- public function addAsset(__url:String, __id:String, __weight:Number = 1): void { loader.add(__url, {id:__id, weight:__weight}); } public function start(): void { if (Boolean(loader)) loader.start(); } public function getXML(__id:String): XML { return loader.getXML(__id); } // ================================================================================================================ // EVENT functions ------------------------------------------------------------------------------------------------ protected function onLoadingError(e:Event): void { trace ("DataAssetManager event - onLoadingError"); } protected function onLoadingProgress(e:BulkProgressEvent): void { //trace ("DataAssetManager event - onLoadingProgress = " + e.bytesLoaded + " / " + e.bytesTotal + " (" + e.bytesTotalCurrent + ")"); var npe:ProgressEvent = new ProgressEvent(ProgressEvent.PROGRESS, false, false, e.bytesLoaded, e.bytesTotal); dispatchEvent(npe); } protected function onLoadingComplete(e:Event): void { //trace ("DataAssetManager event - onLoadingComplete"); var nce:Event = new Event(Event.COMPLETE, false, false); dispatchEvent(nce); } // DataAssetManager.getInstance().addEventListener(IOErrorEvent.IO_ERROR, onAssetsError); // DataAssetManager.getInstance().addEventListener(ProgressEvent.PROGRESS, onAssetsProgress); // DataAssetManager.getInstance().addEventListener(Event.COMPLETE, onAssetsComplete); // ================================================================================================================ // STATIC functions ----------------------------------------------------------------------------------------------- public static function setInstance(__instance:DataAssetManager): void { instance = __instance; } public static function getInstance(): DataAssetManager { if (instance == null) setInstance(new DataAssetManager()); return instance; } } }