48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
use servicepoint::{Bitmap, WindowMut};
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub struct Bar {
|
|
pub progress: f64,
|
|
}
|
|
|
|
impl Bar {
|
|
pub fn new() -> Self {
|
|
Self { progress: 0.0 }
|
|
}
|
|
|
|
pub fn draw(&self, mut bitmap: WindowMut<bool, Bitmap>) {
|
|
bitmap.fill(false);
|
|
|
|
let padding = 1;
|
|
let mut bitmap = bitmap
|
|
.window_mut(
|
|
padding,
|
|
padding,
|
|
bitmap.width() - 2 * padding,
|
|
bitmap.height() - 2 * padding,
|
|
)
|
|
.unwrap();
|
|
|
|
// border top + bottom
|
|
let last_row = bitmap.height() - 1;
|
|
for x in 0..bitmap.width() {
|
|
bitmap.set(x, 0, true);
|
|
bitmap.set(x, last_row, true);
|
|
}
|
|
|
|
// border left + right
|
|
let last_col = bitmap.width() - 1;
|
|
for y in 0..bitmap.height() {
|
|
bitmap.set(0, y, true);
|
|
bitmap.set(last_col, y, true);
|
|
}
|
|
|
|
// progress fill
|
|
let fill_to = (bitmap.width() as f64 * self.progress) as usize;
|
|
for y in 1..bitmap.height() - 1 {
|
|
for x in 1..bitmap.width() - 1 {
|
|
bitmap.set(x, y, x < fill_to && (x + y) % 2 == 0);
|
|
}
|
|
}
|
|
}
|
|
}
|