package com.guru.display.externalLoader { import com.guru.display.DisplayAlign; import com.guru.utils.TimerUtil; 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 mTimeline_mc:MovieClip; private var mLoaderContainer_mc:MovieClip; private var mMoveContainer_mc:MovieClip; private var mLoading_mc: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:Bitmap; private var mDisplayLoading_bol:Boolean; private var mPath_str:String; private var mHasMask_bol:Boolean; private var mMask_mc:MovieClip; private var mFitMask_bol:Boolean; 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(pTimeline_mc:MovieClip, pLoaderAlign:uint = 4, pOnBitmap_bol:Boolean = false, pFitMask_bol:Boolean = true) { mTimeline_mc = pTimeline_mc; mLoaderAlign = pLoaderAlign; mOnBitmap_bol = pOnBitmap_bol; mDisplayLoading_bol = false; mLoaderContainer_mc = mTimeline_mc.container_mc; mLoading_mc = mTimeline_mc.loadingBar_mc; mError = mTimeline_mc.error_mc; mMask_mc = mTimeline_mc.mask_mc; if (mMask_mc) { mHasMask_bol = true; mFitMask_bol = (mMask_mc)?pFitMask_bol:false; } else { mHasMask_bol = false; } try{ mMoveContainer_mc = new MovieClip(); mLoader = new Loader(); mLoaderInfo = mLoader.contentLoaderInfo; mMoveContainer_mc.addChild(mLoader); mLoaderContainer_mc.addChild(mMoveContainer_mc); if(mLoading_mc.totalFrames == 100) { mLoadingBar = new BasicLoadingBar(mLoading_mc); } mFadeInOut = new LabelMovieClip(mTimeline_mc,"hide"); mLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError); 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 mTimeline_mc; } /** * 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_mc.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 { loadPhoto(pPath); } }catch (pError:Error) { trace(pError); } } public function isLoaded():Boolean { return mIsLoaded; } /** * 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_mc.removeChild(mLoader); mLoaderContainer_mc.removeChild(mMoveContainer_mc); mFadeInOut.dispose(); mFadeInOut = null; mTimeline_mc = null; mError = null; mLoaderContainer_mc = null; mMoveContainer_mc = null; mLoading_mc = null; mLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError); 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_mc.gotoAndPlay(2); } } } /** * hide the loading bar */ public function hideLoading():void { if (mDisplayLoading_bol) { mDisplayLoading_bol = false; if(mLoadingBar != null) { mLoadingBar.hide(); } else { mLoading_mc.gotoAndStop(1); } } } /** * get the object width * @return Number */ public function getWidth():Number { return mMoveContainer_mc.width; } /** * get the object height * @return Number */ public function getHeight():Number { return mMoveContainer_mc.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.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_mc.width, mMoveContainer_mc.height, true, 0x00000000); } catch (e:ArgumentError) { Logger.error("Either width or height are invalid (less than or equal to zero, greater than 2880)"); onLoadError(); return; } tmpBitmap.draw(mMoveContainer_mc); } mLoader.unload(); mBitmap = new Bitmap(tmpBitmap, "auto", true); mMoveContainer_mc.addChild(mBitmap); } this.alignContent(); mIsLoaded = true; try{ mLoadDoneCall.execute(); }catch (pError:TypeError) { } try{ mStackLoadDoneCall.execute(); }catch (pError:TypeError) { } if(mAutoFadeIn) { mFadeInOut.playTo("show",mShowDoneCall); } } protected function alignContent():void { if (mHasMask_bol && mFitMask_bol) { mMoveContainer_mc.scaleX = mMoveContainer_mc.scaleY = 1; var widthRatio_num:Number = mMoveContainer_mc.width / mMask_mc.width; var heightRatio_num:Number = mMoveContainer_mc.height / mMask_mc.height; if(widthRatio_num > 1 || heightRatio_num > 1) { if (widthRatio_num > heightRatio_num) { mMoveContainer_mc.width /= widthRatio_num; mMoveContainer_mc.height /= widthRatio_num; } else { mMoveContainer_mc.width /= heightRatio_num; mMoveContainer_mc.height /= heightRatio_num; } } } mMoveContainer_mc.width = Math.floor(mMoveContainer_mc.width); mMoveContainer_mc.height = Math.floor(mMoveContainer_mc.height); mMoveContainer_mc.x -= mMoveContainer_mc.width * 0.5; mMoveContainer_mc.y -= mMoveContainer_mc.height * 0.5; if (mHasMask_bol) { mMoveContainer_mc.x += (mMask_mc.width - mMoveContainer_mc.width) * 0.5 * (mLoaderAlign % 3 - 1); mMoveContainer_mc.y += (mMask_mc.height - mMoveContainer_mc.height) * 0.5 * (Math.floor(mLoaderAlign / 3) - 1); } } 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 { trace(pEvent); try{ mLoadFailCall.execute(); }catch (pError:TypeError) { } try{ mStackLoadFailCall.execute(); }catch(pError:TypeError) {} if(mLoadingBar != null) { mLoadingBar.hide(); } else { mLoading_mc.stop(); mLoading_mc.gotoAndStop(1); } mError.gotoAndStop(2); } private function disposeBitmap():void { try { mBitmap.bitmapData.dispose(); mBitmap.bitmapData = null; mMoveContainer_mc.removeChild(mBitmap); mBitmap = null; }catch (e:Error) { } } } }