Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to handle progress during load #143

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion 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 @@ -94,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;
Expand Down Expand Up @@ -143,6 +150,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 +162,8 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt
}
for (const id in assets)
{
if (progress) totalAssets++;

let data = null;

if (metadata)
Expand All @@ -169,9 +181,16 @@ export function load(scene: AnimateAsset, optionsOrComplete?: Complete | LoadOpt
}
promises.push(Assets.load({ alias: [id], src: basePath + assets[id], data }).then((loadedAsset) =>
{
if (progress)
{
loadedAssets++;

progress(loadedAssets / totalAssets);
}

if (!loadedAsset)
{
return; // not sure if this can evet happen
return; // not sure if this can ever happen
}
if (loadedAsset instanceof Spritesheet)
{
Expand Down