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

Add loading progress bar to the creation template #6

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion templates/template-creation-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"type": "module",
"scripts": {
"build": "tsc && vite build",
"dev": "vite"
"dev": "vite",
"start": "vite"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice to have this in all the templates

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah if you want to do a pr to add this to all the templates I'm ok with adding it in

},
"dependencies": {
"@esotericsoftware/spine-pixi-v8": "^4.2.66",
Expand Down
42 changes: 24 additions & 18 deletions templates/template-creation-web/src/app/screens/LoadScreen.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
import { CircularProgressBar } from "@pixi/ui";
import { animate } from "motion";
import type { ObjectTarget } from "motion/react";
import { Container, Sprite, Text, Texture } from "pixi.js";
import { Container, Sprite, Texture } from "pixi.js";

/** Screen shown while loading assets */
export class LoadScreen extends Container {
/** Assets bundles required by this screen */
public static assetBundles = ["preload"];
/** The PixiJS logo */
private pixiLogo: Sprite;
/** LThe loading message display */
private message: Text;
/** Progress Bar */
private progressBar: CircularProgressBar;

constructor() {
super();

this.message = new Text({
text: "Loading...",
style: {
fill: "white",
fontFamily: "Verdana",
align: "center",
fontSize: 40,
},
this.progressBar = new CircularProgressBar({
backgroundColor: "#3d3d3d",
fillColor: "#e72264",
radius: 100,
lineWidth: 15,
value: 20,
backgroundAlpha: 0.5,
fillAlpha: 0.8,
cap: "round",
});
this.message.anchor.set(0.5);
this.addChild(this.message);

this.progressBar.x += this.progressBar.width / 2;
this.progressBar.y += -this.progressBar.height / 2;

this.addChild(this.progressBar);

this.pixiLogo = new Sprite({
texture: Texture.from("logo.svg"),
anchor: 0.5,
scale: 0.5,
scale: 0.2,
});
this.addChild(this.pixiLogo);
}

public onLoad(progress: number) {
this.progressBar.progress = progress;
}

/** Resize the screen, fired whenever window size changes */
public resize(width: number, height: number) {
this.message.position.set(width * 0.5, height * 0.5 + 125);
this.pixiLogo.position.set(width * 0.5, height * 0.5);
this.progressBar.position.set(width * 0.5, height * 0.5);
}

/** Show screen with animations */
Expand All @@ -47,9 +56,6 @@ export class LoadScreen extends Container {

/** Hide screen with animations */
public async hide() {
// Change then hide the loading message
this.message.text = "Ready!";

await animate(this, { alpha: 0 } as ObjectTarget<this>, {
duration: 0.3,
ease: "linear",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface AppScreen extends Container {
blur?(): void;
/** Focus the screen */
focus?(): void;
/** Method to react on assets loading progress */
onLoad?: (progress: number) => void;
}

/** Interface for app screens constructors */
Expand Down Expand Up @@ -139,7 +141,15 @@ export class Navigation {
// Load assets for the new screen, if available
if (ctor.assetBundles) {
// Load all assets required by this new screen
await Assets.loadBundle(ctor.assetBundles);
await Assets.loadBundle(ctor.assetBundles, (progress) => {
if (this.currentScreen?.onLoad) {
this.currentScreen.onLoad(progress * 100);
}
});
}

if (this.currentScreen?.onLoad) {
this.currentScreen.onLoad(100);
}

// If there is a screen already created, hide and destroy it
Expand Down
Loading