package com.guru.display.externalLoader { import com.guru.display.DisplayAlign; import flash.display.*; import flash.events.*; import flash.errors.*; import flash.net.*; import com.guru.display.LabelMovieClip; import com.guru.utils.MovieClipUtil; import com.guru.display.loadingBar.*; import com.guru.call.*; public class ExternalLoader { private var mContainer:DisplayObject; private var mLoaderContainer:DisplayObject; private var mMoveContainer:DisplayObject; private var mLoading:MovieClip; private var mError:MovieClip; private var mLoader:Loader; private var mStackLoadDoneCall:ICall; private var mStackLoadFailCall:ICall; private var mLoadDoneCall:ICall; private var mLoadFailCall:ICall; private var mShowDoneCall:ICall; private var mLoaderInfo:LoaderInfo; private var mAutoFadeIn:Boolean; private var mLoadingBar:ILoadingBar; private var mFadeInOut:LabelMovieClip; private var mIsLoaded:Boolean = false; private var mLoaderAlign:uint; private var mOnBitmap_bol:Boolean; private var mBitmap_mc:DisplayObject; private var mDisplayLoading_bol:Boolean; private var mPath_str:String; private var mIsLoadDone_bol:Boolean = false; /** * create a object loader * @param pContainer the object container * @param pIsCenter the object place in the center of the container * @param pOnBitmap_bol use the bitmap cache the loaded object */ public function ExternalLoader(pContainer:DisplayObject, pLoaderAlign:uint = 0, pOnBitmap_bol:Boolean = false) { mContainer = pContainer; mLoaderAlign = pLoaderAlign; mOnBitmap_bol = pOnBitmap_bol; mDisplayLoading_bol = false; mLoaderContainer = MovieClipUtil.getMC(mContainer, "container_mc"); mLoading = MovieClip(MovieClipUtil.getMC(mContainer, "loadingBar_mc")); mError = MovieClip(MovieClipUtil.getMC(mContainer, "error_mc")); try{ mMoveContainer = DisplayObject(new MovieClip()); mLoader = new Loader(); mLoaderInfo = mLoader.contentLoaderInfo; DisplayObjectContainer(mMoveContainer).addChild(mLoader); DisplayObjectContainer(mLoaderContainer).addChild(mMoveContainer); if(mLoading.totalFrames == 100) { mLoadingBar = new BasicLoadingBar(DisplayObject(mLoading)); } mFadeInOut = new LabelMovieClip(MovieClip(mContainer),"hide"); mLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError); //mLoaderInfo.addEventListener(Event.INIT, onLoadInit); mLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgress); mLoaderInfo.addEventListener(Event.COMPLETE, onLoadInit); }catch(pError:Error) {} } public function getPath():String { return mPath_str; } /** * get the real object container * @return DisplayObject */ public function getTimeline():MovieClip { return MovieClip(mContainer); } /** * set the object path to be loaded * @param pPath_str the object path */ public function setPath(pPath_str:String):void { mPath_str = pPath_str; } /** * set the load done call * @param pCall done call */ public function setLoadDone(pCall:ICall):void { mLoadDoneCall = pCall; } /** * set the stack load done call * @param pCall done call */ public function setStackLoadDone(pCall:ICall):void { mStackLoadDoneCall = pCall; } public function setStackLoadFail(pCall:ICall):void { mStackLoadFailCall = pCall; } /** * set the load fail call * @param pCall done call */ public function getLoadFail():ICall { return mLoadFailCall; } public function setLoadFail(pCall:ICall):void { mLoadFailCall = pCall; } /** * set the fade in done call * @param pCall done call */ public function setShowDone(pCall:ICall):void { mShowDoneCall = pCall; } /** * get the object is loaded or not * @return Boolean */ public function getLoadDone():Boolean { return mIsLoadDone_bol; } /** * fade out the object * @param pBackCall done call */ public function fadeOut(pBackCall:ICall = null):void { mFadeInOut.playTo("hide",pBackCall); } /** * fade in the object without using auto fade in * @param pShowDoneCall done call */ public function fadeIn(pShowDoneCall:ICall=null):void { if(pShowDoneCall != null) { mShowDoneCall = pShowDoneCall; } mFadeInOut.playTo("show",mShowDoneCall); } /** * jump to the fade in or fade out * @param pLabel_str labelmovieclip label string */ public function jumpTo(pLabel_str:String):void { mFadeInOut.jumpTo(pLabel_str); } /** * start load the object * @param pPath object path * @param pAutoFadeIn is using auto fade in or not * @param pLoadDoneCall load done call * @param pLoadFailCall load fail call * @param pShowDoneCall show done call */ public function load(pPath:String=null,pAutoFadeIn:Boolean=true,pLoadDoneCall:ICall=null,pLoadFailCall:ICall=null,pShowDoneCall:ICall=null):void { try { mIsLoadDone_bol = false; mLoading.gotoAndStop(1); mError.gotoAndStop(1); if (pLoadDoneCall != null) { mLoadDoneCall = pLoadDoneCall; } if (pLoadFailCall != null) { mLoadFailCall = pLoadFailCall; } if (pShowDoneCall != null) { mShowDoneCall = pShowDoneCall; } mAutoFadeIn = pAutoFadeIn; showLoading(); if(mIsLoaded) { mFadeInOut.playTo("hide", new FunctionCall(this, loadPhoto, pPath)); } else { mIsLoaded = true; loadPhoto(pPath); } }catch(pError:Error) {} } /** * fade out and unload the object */ public function unload(pDoneCall:ICall = null):void { this.fadeOut(new FunctionCall(this, unloadPhoto, pDoneCall)); } /** * free all the memory */ public function dispose():void { unloadPhoto(); (mMoveContainer as MovieClip).removeChild(mLoader); (mLoaderContainer as MovieClip).removeChild(mMoveContainer); mFadeInOut.dispose(); mFadeInOut = null; mContainer = null; mError = null; mLoaderContainer = null; mMoveContainer = null; mLoading = null; mLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError); //mLoaderInfo.removeEventListener(Event.INIT, onLoadInit); mLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoadProgress); mLoaderInfo.removeEventListener(Event.COMPLETE, onLoadInit); mLoaderInfo = null; mLoader = null; try { mLoadingBar.dispose(); mLoadingBar = null; }catch (e:Error) { } mLoadDoneCall = null; mLoadFailCall = null; mShowDoneCall = null; } /** * show the loading bar without using load function */ public function showLoading():void { if (!mDisplayLoading_bol) { mDisplayLoading_bol = true; if(mLoadingBar != null) { mLoadingBar.show(); } else { mLoading.gotoAndPlay(2); } } } /** * hide the loading bar */ public function hideLoading():void { if (mDisplayLoading_bol) { mDisplayLoading_bol = false; if(mLoadingBar != null) { mLoadingBar.hide(); } else { mLoading.gotoAndStop(1); } } } /** * get the object width * @return Number */ public function getWidth():Number { return mMoveContainer.width; } /** * get the object height * @return Number */ public function getHeight():Number { return mMoveContainer.height; } public function loadBitmapData(pBitmapData:BitmapData, pAutoFadeIn:Boolean = true):void { mAutoFadeIn = pAutoFadeIn; onLoadInit(null, pBitmapData); } public function getBitmapData():BitmapData { var bitmapData:BitmapData; try { bitmapData = (mBitmap_mc as Bitmap).bitmapData; } catch (e:Error) { } return bitmapData; } protected function unloadPhoto(pDoneCall:ICall = null):void { hideLoading(); mError.gotoAndStop(1); mLoader.unload(); disposeBitmap(); try{ pDoneCall.execute(); }catch(e){ } } protected function loadPhoto(pPath:String = null):void { var tmpUrlRequest:URLRequest; if (pPath == null) { tmpUrlRequest = new URLRequest(mPath_str); } else { tmpUrlRequest = new URLRequest(pPath); } mLoader.load(tmpUrlRequest); } protected function onLoadInit(pEvent:Event = null, pBitmapData:BitmapData = null):void { this.hideLoading(); mIsLoadDone_bol = true; if (mOnBitmap_bol) { disposeBitmap(); var tmpBitmap:BitmapData; if (pBitmapData) { tmpBitmap = pBitmapData; } else { try { tmpBitmap = new BitmapData(mMoveContainer.width, mMoveContainer.height, true, 0x00000000); } catch (e:ArgumentError) { Logger.error("Either width or height are invalid (less than or equal to zero, greater than 2880)"); mLoader.unload(); onLoadError(); return; } tmpBitmap.draw(mMoveContainer); } var bm:Bitmap = new Bitmap(tmpBitmap, "auto", true); mBitmap_mc = DisplayObjectContainer(mMoveContainer).addChild(DisplayObject(bm)); mLoader.unload(); } if (mLoaderAlign != DisplayAlign.TOP_LEFT) { mMoveContainer.x = -mMoveContainer.width * 0.5 * (mLoaderAlign % 3); mMoveContainer.y = -mMoveContainer.height * 0.5 * Math.floor(mLoaderAlign / 3); } try{ mLoadDoneCall.execute(); }catch (pError:TypeError) { } try{ mStackLoadDoneCall.execute(); }catch (pError:TypeError) { } if(mAutoFadeIn) { mFadeInOut.playTo("show",mShowDoneCall); } } protected function onLoadProgress(pEvent:ProgressEvent):void { try{ mLoadingBar.setPercent(Math.floor(pEvent.bytesLoaded / pEvent.bytesTotal * 100)); } catch (pError) { } } protected function onLoadError(pEvent:IOErrorEvent = null):void { try{ mLoadFailCall.execute(); }catch (pError:TypeError) { } try{ mStackLoadFailCall.execute(); }catch(pError:TypeError) {} if(mLoadingBar != null) { mLoadingBar.hide(); } else { mLoading.stop(); mLoading.gotoAndStop(1); } mError.gotoAndStop(2); } private function disposeBitmap():void { try { (mBitmap_mc as Bitmap).bitmapData.dispose(); (mBitmap_mc as Bitmap).bitmapData = null; (mMoveContainer as MovieClip).removeChild(mBitmap_mc); mBitmap_mc = null; }catch (e:Error) { } } } }