mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-23 07:30:31 +00:00
6a1e34b16a
## Description: Add progress bars to show construction time, loading time and health bars in the UI layer The progress bars always show at least one pixel of progression (better visuals)    ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors ## Please put your Discord username so you can be contacted if a bug or regression is found: Vivacious Box --------- Co-authored-by: evanpelle <evanpelle@gmail.com>
62 lines
1.4 KiB
TypeScript
62 lines
1.4 KiB
TypeScript
export class ProgressBar {
|
|
private static readonly CLEAR_PADDING = 2;
|
|
constructor(
|
|
private colors: string[] = [],
|
|
private ctx: CanvasRenderingContext2D,
|
|
private x: number,
|
|
private y: number,
|
|
private w: number,
|
|
private h: number,
|
|
private progress: number = 0, // Progress from 0 to 1
|
|
) {
|
|
this.setProgress(progress);
|
|
}
|
|
|
|
setProgress(progress: number): void {
|
|
progress = Math.max(0, Math.min(1, progress));
|
|
this.clear();
|
|
// Draw the loading bar background
|
|
this.ctx.fillStyle = "rgba(0, 0, 0, 1)";
|
|
this.ctx.fillRect(this.x - 1, this.y - 1, this.w, this.h);
|
|
|
|
// Draw the loading progress
|
|
if (this.colors.length === 0) {
|
|
this.ctx.fillStyle = "#808080"; // default gray
|
|
} else {
|
|
const idx = Math.min(
|
|
this.colors.length - 1,
|
|
Math.floor(progress * this.colors.length),
|
|
);
|
|
this.ctx.fillStyle = this.colors[idx];
|
|
}
|
|
this.ctx.fillRect(
|
|
this.x,
|
|
this.y,
|
|
Math.max(1, Math.floor(progress * (this.w - 2))),
|
|
this.h - 2,
|
|
);
|
|
this.progress = progress;
|
|
}
|
|
|
|
clear() {
|
|
this.ctx.clearRect(
|
|
this.x - ProgressBar.CLEAR_PADDING,
|
|
this.y - ProgressBar.CLEAR_PADDING,
|
|
this.w + ProgressBar.CLEAR_PADDING,
|
|
this.h + ProgressBar.CLEAR_PADDING,
|
|
);
|
|
}
|
|
|
|
getX(): number {
|
|
return this.x;
|
|
}
|
|
|
|
getY(): number {
|
|
return this.y;
|
|
}
|
|
|
|
getProgress(): number {
|
|
return this.progress;
|
|
}
|
|
}
|