Merge pull request #866 from restorer/more-precise-preloader

1. More precise preloader for html5; 2. Fix font preloading in Safari.
This commit is contained in:
Joshua Granick
2016-12-20 08:13:26 -08:00
committed by GitHub
3 changed files with 58 additions and 3 deletions

View File

@@ -38,6 +38,8 @@ class Preloader #if flash extends Sprite #end {
private var loadedLibraries:Int;
private var loadedStage:Bool;
private var itemsProgressLoaded:Int;
private var itemsProgressTotal:Int;
public function new () {
@@ -75,15 +77,32 @@ class Preloader #if flash extends Sprite #end {
public function load ():Void {
itemsProgressLoaded = 0;
itemsProgressTotal = 0;
for (library in libraries) {
itemsProgressTotal += library.computeProgressTotal ();
}
loadedLibraries = -1;
for (library in libraries) {
library.load ().onComplete (function (_) {
library.load ().onProgress (function (_, _) {
updateItemsProgress();
}).onComplete (function (_) {
loadedLibraries++;
updateProgress ();
}).onError (function (e) {
trace(e);
});
}
@@ -120,7 +139,8 @@ class Preloader #if flash extends Sprite #end {
private function updateProgress ():Void {
update (loadedLibraries, libraries.length);
update (itemsProgressLoaded, itemsProgressTotal);
// update (loadedLibraries, libraries.length);
if (#if flash loadedStage && #end loadedLibraries == libraries.length) {
@@ -130,6 +150,12 @@ class Preloader #if flash extends Sprite #end {
}
private function updateItemsProgress ():Void {
itemsProgressLoaded++;
updateProgress();
}
#if flash
private function current_onEnter (event:flash.events.Event):Void {

View File

@@ -446,8 +446,15 @@ class Font {
this.name = name;
var font = name;
var ua = Browser.navigator.userAgent.toLowerCase();
if (untyped (Browser.document).fonts && untyped (Browser.document).fonts.load) {
// When Safari reports that font is loaded, actually font IS NOT loaded.
// If you try to use this font immediately after preloader completes,
// default font will be used instead.
//
// We detect Safari, and use old font loading for it.
if (!(ua.indexOf(" safari/") >= 0 && ua.indexOf(" chrome/") < 0) && untyped (Browser.document).fonts && untyped (Browser.document).fonts.load) {
untyped (Browser.document).fonts.load ("1em '" + font + "'").then (function (_) {

View File

@@ -373,6 +373,27 @@ class AssetLibrary {
}
public function computeProgressTotal ():Int {
var result = 1;
for (id in preload.keys ()) {
switch (types.get (id)) {
case BINARY, FONT, IMAGE, MUSIC, SOUND, TEXT:
result++;
default:
}
}
return result;
}
public function load ():Future<AssetLibrary> {
@@ -575,6 +596,7 @@ class AssetLibrary {
private function updateProgressLoaded ():Void {
progressLoaded++;
promise.progress(progressLoaded, progressTotal);
if (progressLoaded == progressTotal) {