Improve Preloader

This commit is contained in:
Joshua Granick
2015-08-18 17:36:56 -07:00
parent 788e8c10e9
commit e02fa7c2c6
5 changed files with 116 additions and 26 deletions

View File

@@ -25,6 +25,7 @@ class Application extends Module {
public var config (default, null):Config;
public var frameRate (get, set):Float;
public var modules (default, null):Array<IModule>;
public var preloader (default, null):Preloader;
/**
* Update events are dispatched each frame (usually just before rendering)
@@ -69,9 +70,19 @@ class Application extends Module {
modules.push (module);
if (initialized && renderer != null) {
if (initialized) {
module.init (renderer.context);
if (renderer != null) {
module.init (renderer.context);
}
if (preloader == null) {
module.onPreloadComplete ();
}
}
@@ -179,6 +190,12 @@ class Application extends Module {
initialized = true;
if (preloader == null || preloader.complete) {
onPreloadComplete ();
}
}
@@ -325,6 +342,28 @@ class Application extends Module {
}
public override function onPreloadComplete ():Void {
for (module in modules) {
module.onPreloadComplete ();
}
}
public override function onPreloadProgress (loaded:Int, total:Int):Void {
for (module in modules) {
module.onPreloadProgress (loaded, total);
}
}
public override function onRenderContextLost ():Void {
for (module in modules) {
@@ -592,6 +631,23 @@ class Application extends Module {
}
private function setPreloader (preloader:Preloader):Void {
if (preloader != null) {
preloader.onProgress.remove (onPreloadProgress);
preloader.onComplete.remove (onPreloadComplete);
}
this.preloader = preloader;
preloader.onProgress.add (onPreloadProgress);
preloader.onComplete.add (onPreloadComplete);
}
public override function update (deltaTime:Int):Void {
for (module in modules) {

View File

@@ -126,6 +126,20 @@ interface IModule {
public function onMouseWheel (deltaX:Float, deltaY:Float):Void;
/**
* Called when a preload complete event is fired
*/
public function onPreloadComplete ():Void;
/**
* Called when a preload progress event is fired
* @param loaded The number of items that are loaded
* @param total The total number of items will be loaded
*/
public function onPreloadProgress (loaded:Int, total:Int):Void;
/**
* Called when a render context is lost
*/

View File

@@ -111,6 +111,20 @@ class Module implements IModule {
public function onMouseWheel (deltaX:Float, deltaY:Float):Void { }
/**
* Called when a preload complete event is fired
*/
public function onPreloadComplete ():Void { }
/**
* Called when a preload progress event is fired
* @param loaded The number of items that are loaded
* @param total The total number of items will be loaded
*/
public function onPreloadProgress (loaded:Int, total:Int):Void { }
/**
* Called when a render context is lost
*/

View File

@@ -1,6 +1,7 @@
package lime.app;
import lime.app.Event;
import lime.Assets;
#if (js && html5)
@@ -12,7 +13,6 @@ import lime.net.URLRequest;
#elseif flash
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.Lib;
#end
@@ -22,7 +22,8 @@ class Preloader #if flash extends Sprite #end {
public var complete:Bool;
public var onComplete:Dynamic;
public var onComplete = new Event<Void->Void> ();
public var onProgress = new Event<Int->Int->Void> ();
#if (js && html5)
public static var images = new Map<String, Image> ();
@@ -40,6 +41,8 @@ class Preloader #if flash extends Sprite #end {
#end
onProgress.add (update);
}
@@ -49,10 +52,10 @@ class Preloader #if flash extends Sprite #end {
Lib.current.addChild (this);
Lib.current.loaderInfo.addEventListener (Event.COMPLETE, loaderInfo_onComplete);
Lib.current.loaderInfo.addEventListener (Event.INIT, loaderInfo_onInit);
Lib.current.loaderInfo.addEventListener (flash.events.Event.COMPLETE, loaderInfo_onComplete);
Lib.current.loaderInfo.addEventListener (flash.events.Event.INIT, loaderInfo_onInit);
Lib.current.loaderInfo.addEventListener (ProgressEvent.PROGRESS, loaderInfo_onProgress);
Lib.current.addEventListener (Event.ENTER_FRAME, current_onEnter);
Lib.current.addEventListener (flash.events.Event.ENTER_FRAME, current_onEnter);
#end
@@ -136,7 +139,7 @@ class Preloader #if flash extends Sprite #end {
untyped (Browser.document).fonts.load ("1em '" + font + "'").then (function (_) {
loaded ++;
update (loaded, total);
onProgress.dispatch (loaded, total);
if (loaded == total) {
@@ -191,7 +194,7 @@ class Preloader #if flash extends Sprite #end {
node.parentNode.removeChild (node);
node = null;
update (loaded, total);
onProgress.dispatch (loaded, total);
if (loaded == total) {
@@ -221,6 +224,8 @@ class Preloader #if flash extends Sprite #end {
private function start ():Void {
complete = true;
#if flash
if (Lib.current.contains (this)) {
@@ -229,11 +234,7 @@ class Preloader #if flash extends Sprite #end {
}
#end
if (onComplete != null) {
onComplete ();
}
onComplete.dispatch ();
}
@@ -257,7 +258,7 @@ class Preloader #if flash extends Sprite #end {
loaded++;
update (loaded, total);
onProgress.dispatch (loaded, total);
if (loaded == total) {
@@ -272,7 +273,7 @@ class Preloader #if flash extends Sprite #end {
loaded++;
update (loaded, total);
onProgress.dispatch (loaded, total);
if (loaded == total) {
@@ -285,20 +286,20 @@ class Preloader #if flash extends Sprite #end {
#if flash
private function current_onEnter (event:Event):Void {
private function current_onEnter (event:flash.events.Event):Void {
if (!complete && Lib.current.loaderInfo.bytesLoaded == Lib.current.loaderInfo.bytesTotal) {
complete = true;
update (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
}
if (complete) {
Lib.current.removeEventListener (Event.ENTER_FRAME, current_onEnter);
Lib.current.loaderInfo.removeEventListener (Event.COMPLETE, loaderInfo_onComplete);
Lib.current.loaderInfo.removeEventListener (Event.INIT, loaderInfo_onInit);
Lib.current.removeEventListener (flash.events.Event.ENTER_FRAME, current_onEnter);
Lib.current.loaderInfo.removeEventListener (flash.events.Event.COMPLETE, loaderInfo_onComplete);
Lib.current.loaderInfo.removeEventListener (flash.events.Event.INIT, loaderInfo_onInit);
Lib.current.loaderInfo.removeEventListener (ProgressEvent.PROGRESS, loaderInfo_onProgress);
start ();
@@ -311,21 +312,21 @@ class Preloader #if flash extends Sprite #end {
private function loaderInfo_onComplete (event:flash.events.Event):Void {
complete = true;
update (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
}
private function loaderInfo_onInit (event:flash.events.Event):Void {
update (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
}
private function loaderInfo_onProgress (event:flash.events.ProgressEvent):Void {
update (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
}
#end

View File

@@ -2,6 +2,9 @@ import ::APP_MAIN::;
import lime.Assets;
@:access(lime.app.Application)
class ApplicationMain {
@@ -13,13 +16,15 @@ class ApplicationMain {
public static function create ():Void {
preloader = new ::if (PRELOADER_NAME != "")::::PRELOADER_NAME::::else::lime.app.Preloader::end:: ();
preloader.onComplete.add (start);
#if !munit
app = new ::APP_MAIN:: ();
app.setPreloader (preloader);
app.create (config);
#end
preloader = new ::if (PRELOADER_NAME != "")::::PRELOADER_NAME::::else::lime.app.Preloader::end:: ();
preloader.onComplete = start;
preloader.create (config);
#if (js && html5)