From e02fa7c2c6102983c6a176902abf1478de52f99a Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Tue, 18 Aug 2015 17:36:56 -0700 Subject: [PATCH] Improve Preloader --- lime/app/Application.hx | 60 +++++++++++++++++++++++++++++-- lime/app/IModule.hx | 14 ++++++++ lime/app/Module.hx | 14 ++++++++ lime/app/Preloader.hx | 45 +++++++++++------------ templates/haxe/ApplicationMain.hx | 9 +++-- 5 files changed, 116 insertions(+), 26 deletions(-) diff --git a/lime/app/Application.hx b/lime/app/Application.hx index 984d3c4e2..8a42fb456 100644 --- a/lime/app/Application.hx +++ b/lime/app/Application.hx @@ -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; + 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) { diff --git a/lime/app/IModule.hx b/lime/app/IModule.hx index 1f0634bb7..695f186c2 100644 --- a/lime/app/IModule.hx +++ b/lime/app/IModule.hx @@ -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 */ diff --git a/lime/app/Module.hx b/lime/app/Module.hx index ab0161340..1f03302cb 100644 --- a/lime/app/Module.hx +++ b/lime/app/Module.hx @@ -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 */ diff --git a/lime/app/Preloader.hx b/lime/app/Preloader.hx index 1c7cb76c5..2f2b6bc63 100644 --- a/lime/app/Preloader.hx +++ b/lime/app/Preloader.hx @@ -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 EventVoid> (); + public var onProgress = new EventInt->Void> (); #if (js && html5) public static var images = new Map (); @@ -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 diff --git a/templates/haxe/ApplicationMain.hx b/templates/haxe/ApplicationMain.hx index 5b7b84985..2828982ff 100644 --- a/templates/haxe/ApplicationMain.hx +++ b/templates/haxe/ApplicationMain.hx @@ -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)