Skip to content

Commit

Permalink
Added possibility to handle progress during load pixijs-userland#142
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyFlasher committed Sep 20, 2023
1 parent 8311ddb commit 82cc943
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/animate/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand All @@ -18,6 +19,10 @@ export interface LoadOptions
* Callback for load completion.
*/
complete?: Complete;
/**
* Callback for load progress.
*/
progress?: Progress;
/**
* Base root directory
*/
Expand Down Expand Up @@ -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
* ```
Expand Down Expand Up @@ -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<any>[] = [];
// assetBaseDir can accept either with trailing slash or not

Expand All @@ -152,6 +161,8 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt
}
for (const id in assets)
{
totalAssets++;

let data = null;

if (metadata)
Expand All @@ -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)
{
Expand Down

0 comments on commit 82cc943

Please sign in to comment.