progress bars

This commit is contained in:
Vinzenz Schroeter 2025-07-05 13:36:21 +02:00
commit fe4543ae41

View file

@ -1,4 +1,4 @@
use crate::command_queue::PacketQueue;
use crate::queue::PacketQueue;
use crate::{Currency, GenerateCommands, Progressable};
use servicepoint::{Bitmap, BitmapCommand, Grid, Origin, TILE_SIZE, TILE_WIDTH, Tiles};
use std::time::Duration;
@ -13,15 +13,26 @@ pub struct Bar {
}
impl Bar {
pub(crate) fn new(factor: f64, origin: Origin<Tiles>) -> Bar {
pub(crate) fn new(factor: f64, origin: Origin<Tiles>) -> Self {
Self {
factor,
progress: 0f64,
speed: 0.01f64,
speed: 0f64,
origin,
width_tiles: TILE_WIDTH / 2, // TODO: param
}
}
pub(crate) fn add_speed(&self, speed: f64) -> Self {
Self {
speed: self.speed + speed,
..*self
}
}
pub(crate) fn is_enabled(&self) -> bool {
self.speed > 0.0
}
}
impl Progressable for Bar {
@ -40,25 +51,30 @@ impl GenerateCommands for Bar {
fn generate_commands(&self, q: &mut impl PacketQueue) {
let mut bitmap = Bitmap::new(self.width_tiles * TILE_SIZE, TILE_SIZE).unwrap();
// border top
let last_row = bitmap.height() - 1;
for x in 0..bitmap.width() {
bitmap.set(x, 0, true);
let margin = 1;
let bar_height = bitmap.height() - 2 * margin;
let bar_width = bitmap.width() - 2 * margin;
let border_thickness = 1;
// border top + bottom
let last_row = bitmap.height() - margin - border_thickness;
for x in margin..bar_width {
bitmap.set(x, margin, true);
bitmap.set(x, last_row, true);
}
// border bottom
let last_col = bitmap.width() - 1;
for y in 0..bitmap.height() {
bitmap.set(0, y, true);
// border left + right
let last_col = bitmap.width() - margin - border_thickness;
for y in 1..=bar_height {
bitmap.set(1, y, true);
bitmap.set(last_col, y, true);
}
// progress fill
let fill_to = (bitmap.width() as f64 * self.progress) as usize;
for y in 0..bitmap.height() {
for x in 0..fill_to {
bitmap.set(x, y, true);
let fill_to = ((bar_width - 2) as f64 * self.progress) as usize;
for y in 2..bar_height {
for x in 2..=fill_to {
bitmap.set(x, y, (x + y) % 2 == 0);
}
}