Skip to content

Commit 65548e9

Browse files
Add blocks for loading
1 parent 9b0fcf5 commit 65548e9

File tree

2 files changed

+126
-9
lines changed

2 files changed

+126
-9
lines changed

inhouseLoadingAnimations.ts

+118-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace SpriteKind {
44

55
//% color="#FF9F00"
66
//% icon="\uf0ae"
7-
//% groups="['Splash']"
7+
//% groups="['Splash', 'Loading']"
88
namespace LoadingAnimations {
99
class InhouseAnimation {
1010
private running: boolean;
@@ -302,7 +302,7 @@ namespace LoadingAnimations {
302302
}
303303
}
304304

305-
export class Loading extends InhouseAnimation {
305+
class Loading extends InhouseAnimation {
306306
private _current: number;
307307
private _minimum: number;
308308
private _maximum: number;
@@ -494,7 +494,7 @@ namespace LoadingAnimations {
494494
bar.image.fillRect(
495495
2,
496496
2,
497-
this.scale(this._current, this._minimum, this._maximum, 0, inner_width + 1),
497+
this.scale(this._current, this._minimum, this._maximum, 0, inner_width),
498498
inner_height,
499499
1
500500
);
@@ -546,4 +546,119 @@ namespace LoadingAnimations {
546546
_splash.stop();
547547
}
548548
}
549+
550+
let _loading: Loading | undefined = undefined;
551+
552+
/**
553+
* Show and start the loading screen animation.
554+
*
555+
* @param z_index The Z index of the sprites for the animation.
556+
*/
557+
//% block="show loading || at z %z_index"
558+
//% group="Loading"
559+
//% expandableArgumentMode="toggle"
560+
//% weight=80
561+
export function show_loading(z_index: number = 0): void {
562+
if (_loading) {
563+
hide_loading();
564+
}
565+
_loading = new Loading(z_index);
566+
_loading.start();
567+
}
568+
569+
/**
570+
* Hide and destroy the loading screen animation.
571+
*/
572+
//% block="hide loading"
573+
//% group="Loading"
574+
//% expandableArgumentMode="toggle"
575+
//% weight=70
576+
export function hide_loading(): void {
577+
if (_loading) {
578+
_loading.stop();
579+
}
580+
}
581+
582+
export enum LoadingValue {
583+
//% block="minimum"
584+
Minimum,
585+
//% block="current"
586+
Current,
587+
//% block="maximum"
588+
Maximum
589+
}
590+
591+
/**
592+
* Set a value on the loading animation.
593+
*
594+
* @param option A property from the LoadingValue enum.
595+
* @param value The value to set it to.
596+
*/
597+
//% block="set loading %option to %value"
598+
//% group="Loading"
599+
//% weight=60
600+
export function set_loading_value(option: LoadingValue, value: number): void {
601+
if (!_loading) {
602+
return;
603+
}
604+
switch (option) {
605+
case (LoadingValue.Minimum): {
606+
_loading.minimum = value;
607+
break;
608+
}
609+
case (LoadingValue.Current): {
610+
_loading.current = value;
611+
break;
612+
}
613+
case (LoadingValue.Maximum): {
614+
_loading.maximum = value;
615+
break;
616+
}
617+
default: {
618+
throw "Not a valid property to set on the loading bar";
619+
}
620+
}
621+
}
622+
623+
/**
624+
* Get a value on the loading animation.
625+
*
626+
* @param option A property from the LoadingValue enum.
627+
* @return A number. 0 if the loading screen hasn't started yet.
628+
*/
629+
//% block="get loading %option"
630+
//% group="Loading"
631+
//% weight=50
632+
export function get_loading_value(option: LoadingValue): number {
633+
if (!_loading) {
634+
return 0;
635+
}
636+
switch (option) {
637+
case (LoadingValue.Minimum): {
638+
return _loading.minimum;
639+
}
640+
case (LoadingValue.Current): {
641+
return _loading.current;
642+
}
643+
case (LoadingValue.Maximum): {
644+
return _loading.maximum;
645+
}
646+
default: {
647+
throw "Not a valid property to get on the loading bar";
648+
}
649+
}
650+
}
651+
652+
/**
653+
* Change a value on the loading animation.
654+
*
655+
* @param option A property from the LoadingValue enum.
656+
* @param value The value to set it to.
657+
*/
658+
//% block="change loading %option by %value"
659+
//% group="Loading"
660+
//% weight=40
661+
export function change_loading_value(option: LoadingValue, value: number): void {
662+
set_loading_value(option, get_loading_value(option) + value);
663+
}
549664
}

test.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
// LoadingAnimations.show_splash();
33
// pause(5000);
44
// LoadingAnimations.hide_splash();
5-
game.consoleOverlay.setVisible(true);
65

7-
const loading: LoadingAnimations.Loading = new LoadingAnimations.Loading(0);
8-
loading.start();
6+
LoadingAnimations.show_splash();
7+
pause(3000);
8+
LoadingAnimations.hide_splash();
9+
10+
LoadingAnimations.show_loading();
911
for (let i = 0; i < 100; i++) {
10-
loading.current = i;
11-
pause(100);
12+
LoadingAnimations.change_loading_value(LoadingAnimations.LoadingValue.Current, 1);
13+
pause(50);
1214
}
1315
pause(1000);
14-
loading.stop();
16+
LoadingAnimations.hide_loading();
1517

1618
scene.setBackgroundImage(img`
1719
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

0 commit comments

Comments
 (0)