From eb6934eced2fecc90242e0d0d96db9791e6dc464 Mon Sep 17 00:00:00 2001 From: restorer Date: Tue, 20 Dec 2016 13:23:10 +0300 Subject: [PATCH] 1. More precise preloader for html5; 2. Fix font preloading in Safari. --- lime/app/Preloader.hx | 30 ++++++++++++++++++++++++++++-- lime/text/Font.hx | 9 ++++++++- lime/utils/AssetLibrary.hx | 22 ++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lime/app/Preloader.hx b/lime/app/Preloader.hx index 4bebe4a79f..a1ebca52b6 100644 --- a/lime/app/Preloader.hx +++ b/lime/app/Preloader.hx @@ -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 { diff --git a/lime/text/Font.hx b/lime/text/Font.hx index 58db26bca5..843e03c7ca 100644 --- a/lime/text/Font.hx +++ b/lime/text/Font.hx @@ -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 (_) { diff --git a/lime/utils/AssetLibrary.hx b/lime/utils/AssetLibrary.hx index 61ac2f4f4a..21fef55bcb 100644 --- a/lime/utils/AssetLibrary.hx +++ b/lime/utils/AssetLibrary.hx @@ -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 { @@ -575,6 +596,7 @@ class AssetLibrary { private function updateProgressLoaded ():Void { progressLoaded++; + promise.progress(progressLoaded, progressTotal); if (progressLoaded == progressTotal) {