Files
OpenFrontIO/src/client/graphics/ProgressBar.ts
T
Vivacious Box 6a1e34b16a Add progress bars to show loading time and healthbars (#1107)
## 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)


![buildcity](https://github.com/user-attachments/assets/7181642a-742d-4996-8ca9-748b55c04a58)

![launchNuke](https://github.com/user-attachments/assets/85fbed8f-3d91-4d7e-9c01-737ee5868992)

![ships2](https://github.com/user-attachments/assets/9fd53e6a-b2c7-4044-8b65-6f61231775b1)


## 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>
2025-06-10 20:50:31 +00:00

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;
}
}