From 82cc943a810bb209c662e40aaf717776fc820f2f Mon Sep 17 00:00:00 2001 From: CrazyFlasher Date: Wed, 20 Sep 2023 09:54:05 +0300 Subject: [PATCH 1/2] Added possibility to handle progress during load https://github.com/pixijs/animate/issues/142 --- src/animate/load.ts | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/animate/load.ts b/src/animate/load.ts index 524ca5c..4d8899c 100644 --- a/src/animate/load.ts +++ b/src/animate/load.ts @@ -8,6 +8,7 @@ import { Texture } from '@pixi/core'; import { Spritesheet } from '@pixi/spritesheet'; type Complete = (instance: MovieClip | null) => void; +type Progress = (value: number) => void; export interface LoadOptions { /** @@ -18,6 +19,10 @@ export interface LoadOptions * Callback for load completion. */ complete?: Complete; + /** + * Callback for load progress. + */ + progress?: Progress; /** * Base root directory */ @@ -51,9 +56,10 @@ const EXPECTED_ASSET_VERSION = 2; * ``` * @param scene - Reference to the scene data. * @param complete - The callback function when complete. + * @param progress - The callback function when progressed. * @return instance of PIXI resource loader */ -export function load(scene: AnimateAsset, complete?: Complete): void; +export function load(scene: AnimateAsset, complete?: Complete, progress?: Progress): void; /** * Load the stage class and preload any assets * ``` @@ -143,6 +149,9 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt if (assets && Object.keys(assets).length) { + let totalAssets = 0; + let loadedAssets = 0; + const promises: Promise[] = []; // assetBaseDir can accept either with trailing slash or not @@ -152,6 +161,8 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt } for (const id in assets) { + totalAssets++; + let data = null; if (metadata) @@ -167,11 +178,19 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt data = metadata.default; } } - promises.push(Assets.load({ alias: [id], src: basePath + assets[id], data }).then((loadedAsset) => + promises.push(Assets.load({ alias: [id], src: basePath + assets[id], data }, progress => { + if (optionsOrComplete && typeof optionsOrComplete !== 'function') + { + if (optionsOrComplete.progress) optionsOrComplete.progress(progress * loadedAssets / totalAssets); + } + }).then((loadedAsset) => + { + loadedAssets++; + if (!loadedAsset) { - return; // not sure if this can evet happen + return; // not sure if this can event happen } if (loadedAsset instanceof Spritesheet) { From 2dfc44492bd9fb0404e23435617bff37b91733af Mon Sep 17 00:00:00 2001 From: CrazyFlasher Date: Sat, 30 Sep 2023 19:25:47 +0300 Subject: [PATCH 2/2] Fixes according pull-request comments --- src/animate/load.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/animate/load.ts b/src/animate/load.ts index 4d8899c..d7c44c7 100644 --- a/src/animate/load.ts +++ b/src/animate/load.ts @@ -56,10 +56,9 @@ const EXPECTED_ASSET_VERSION = 2; * ``` * @param scene - Reference to the scene data. * @param complete - The callback function when complete. - * @param progress - The callback function when progressed. * @return instance of PIXI resource loader */ -export function load(scene: AnimateAsset, complete?: Complete, progress?: Progress): void; +export function load(scene: AnimateAsset, complete?: Complete): void; /** * Load the stage class and preload any assets * ``` @@ -100,6 +99,8 @@ export function load(scene: AnimateAsset, options: LoadOptions): void; export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOptions): void { const complete: Complete = typeof optionsOrComplete === 'function' ? optionsOrComplete : optionsOrComplete?.complete; + const progress: Progress | undefined = typeof optionsOrComplete === 'function' ? undefined : optionsOrComplete?.progress; + let basePath = ''; let parent: Container = null; let metadata: any; @@ -161,7 +162,7 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt } for (const id in assets) { - totalAssets++; + if (progress) totalAssets++; let data = null; @@ -178,19 +179,18 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt data = metadata.default; } } - promises.push(Assets.load({ alias: [id], src: basePath + assets[id], data }, progress => + promises.push(Assets.load({ alias: [id], src: basePath + assets[id], data }).then((loadedAsset) => { - if (optionsOrComplete && typeof optionsOrComplete !== 'function') + if (progress) { - if (optionsOrComplete.progress) optionsOrComplete.progress(progress * loadedAssets / totalAssets); + loadedAssets++; + + progress(loadedAssets / totalAssets); } - }).then((loadedAsset) => - { - loadedAssets++; if (!loadedAsset) { - return; // not sure if this can event happen + return; // not sure if this can ever happen } if (loadedAsset instanceof Spritesheet) {