Created
November 10, 2011 11:00
-
-
Save vrobel/1354606 to your computer and use it in GitHub Desktop.
BaseApplicationPreloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by IntelliJ IDEA. | |
* User: wrobel221 | |
* Date: 08.12.10 | |
* Time: 13:07 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
package com.blackmoondev.preloader { | |
import flash.display.DisplayObject; | |
import flash.display.Graphics; | |
import flash.display.MovieClip; | |
import flash.display.Sprite; | |
import flash.display.Stage; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.events.IOErrorEvent; | |
import flash.events.ProgressEvent; | |
import flash.geom.Rectangle; | |
import flash.net.URLRequest; | |
import flash.system.ApplicationDomain; | |
import flash.system.LoaderContext; | |
import flash.utils.getDefinitionByName; | |
import org.casalib.events.LoadEvent; | |
import org.casalib.load.GroupLoad; | |
import org.casalib.load.SwfLoad; | |
import org.casalib.util.LibraryManager; | |
import utils.align.alignToRectangleCenterMiddle; | |
import utils.display.removeTargetFromParent; | |
public class BaseApplicationPreloader extends MovieClip { | |
/** Bubble this event up to the stage to remove the preloader. **/ | |
public static const REMOVE_PRELOADER:String = "removePreloader"; | |
/** The fully-qualified name of the class we should instantiate after preloading. **/ | |
protected var applicationClassName:String; | |
/** Container for our preloader graphics/animation. **/ | |
protected var preloader:Sprite; | |
/** The application once it's instantiated. **/ | |
protected var application:DisplayObject; | |
/** List of RSLs to download right after the application **/ | |
protected var rsls:Array; | |
/** Constructor. **/ | |
public function BaseApplicationPreloader(applicationClassName:String = "", rsls:Array = null) { | |
this.applicationClassName = applicationClassName; | |
this.rsls = rsls; | |
stop(); | |
if(stage) init(); | |
else addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
protected function init(e:Event = null):void { | |
loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress); | |
loaderInfo.addEventListener(Event.COMPLETE, onComplete); | |
setupStage(); | |
addPreloader(); | |
} | |
/** Set any stage properties or global playback settings, e.g. framerate. **/ | |
protected function setupStage(e:Event = null):void { | |
stage.align = StageAlign.TOP_LEFT; | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
stage.addEventListener(Event.RESIZE, onResize); | |
} | |
protected function onResize(e:Event):void { | |
if(preloader) | |
alignToRectangleCenterMiddle(preloader, new Rectangle(0,0,stage.stageWidth, stage.stageHeight)); | |
} | |
/** Add preloader -- override this to add your own animated elements. **/ | |
protected function addPreloader():void { | |
preloader = new Sprite(); | |
addChild(preloader); | |
} | |
/** Update preload progress. **/ | |
protected function onProgress(event:ProgressEvent):void { | |
updatePreloader(event.bytesLoaded / event.bytesTotal); | |
} | |
/** Update preloader -- override this to update your animated elements. **/ | |
protected function updatePreloader(progress:Number):void { | |
// update the preloader with the loading progress | |
var g:Graphics = preloader.graphics; | |
g.clear(); | |
// draw a solid background so we can't see the app as it loads in the background | |
g.beginFill(0x000000); | |
g.drawRect(0, 0, stage.stageWidth, stage.stageHeight); | |
g.endFill(); | |
// draw the outline of a progress bar | |
g.lineStyle(3, 0xffffff, 1, true); | |
g.drawRoundRect(100, 290, 600, 20, 10, 10); | |
// fill the progress bar based on how many of our bytes have been loaded | |
g.beginFill(0xffffff); | |
g.drawRoundRect(100, 290, 600 * progress, 20, 10, 10); | |
g.endFill(); | |
} | |
/** Preload complete. **/ | |
protected function onComplete(event:Event):void { | |
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress); | |
loaderInfo.removeEventListener(Event.COMPLETE, onComplete); | |
//load RSLs if any | |
if (rsls && rsls.length > 0) { | |
loadRSLs() | |
} else { | |
nextFrame(); | |
createApplication(); | |
} | |
} | |
protected function loadRSLs():void { | |
var groupLoad:GroupLoad = new GroupLoad(); | |
groupLoad.threads = rsls.length; | |
for each (var url:String in rsls) { | |
var swfLoad:SwfLoad = new SwfLoad(new URLRequest(url), new LoaderContext(false, ApplicationDomain.currentDomain)); | |
groupLoad.addLoad(swfLoad); | |
LibraryManager.addSwfLoad(swfLoad, 'rsl'); | |
} | |
groupLoad.addEventListener(LoadEvent.COMPLETE, onRSLComplete); | |
groupLoad.addEventListener(LoadEvent.PROGRESS, onRSLProgress); | |
groupLoad.addEventListener(IOErrorEvent.IO_ERROR, onRSLError); | |
groupLoad.start(); | |
} | |
private function onRSLProgress(e:LoadEvent):void { | |
onProgress(new ProgressEvent(ProgressEvent.PROGRESS, false, false, e.progress.percentage, 100)); | |
} | |
private function onRSLComplete(e:LoadEvent):void { | |
var groupLoad:GroupLoad = e.currentTarget as GroupLoad; | |
groupLoad.removeEventListeners(); | |
nextFrame(); | |
createApplication(); | |
} | |
private function onRSLError(e:IOErrorEvent):void { | |
throw new Error('RSL Libraries unable to load.'); | |
} | |
/** Create the application. **/ | |
protected function createApplication(autoRemovePreloader:Boolean = true):void { | |
// we assume the current label's name is the name of the class, | |
// unless overridden in the constructor | |
var className:String = applicationClassName || currentLabel; | |
// must create the application by name so we don't have any static linkage to this class | |
var appClass:Class = Class(getDefinitionByName(className)); | |
application = new appClass(); | |
// the application will dispatch a "removePreloader" event when it's ready | |
application.addEventListener(REMOVE_PRELOADER, onRemovePreloader); | |
// we add the application underneath our preloader | |
// this allows us to keep the preloader displayed until the app is ready to go | |
stage.addChildAt(application, 0); | |
if(autoRemovePreloader) | |
onRemovePreloader(); | |
} | |
/** Remove preloader. **/ | |
protected function onRemovePreloader(event:Event = null):void { | |
application.removeEventListener(REMOVE_PRELOADER, onRemovePreloader); | |
stage.removeEventListener(Event.RESIZE, onResize); | |
removePreloader(); | |
removeTargetFromParent(this); | |
} | |
/** Remove preloader -- override this to remove the elements added in addPreloader(). **/ | |
protected function removePreloader():void { | |
removeChild(preloader); | |
preloader = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment