initial commit
This commit is contained in:
commit
5817fea9ad
1 changed files with 465 additions and 0 deletions
73
src/bar.rs
Normal file
73
src/bar.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
use crate::command_queue::PacketQueue;
|
||||
use crate::{Currency, GenerateCommands, Progressable};
|
||||
use servicepoint::{Bitmap, BitmapCommand, Grid, Origin, TILE_SIZE, TILE_WIDTH, Tiles};
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Bar {
|
||||
progress: f64,
|
||||
speed: f64,
|
||||
factor: f64,
|
||||
origin: Origin<Tiles>,
|
||||
width_tiles: usize,
|
||||
}
|
||||
|
||||
impl Bar {
|
||||
pub(crate) fn new(factor: f64, origin: Origin<Tiles>) -> Bar {
|
||||
Self {
|
||||
factor,
|
||||
progress: 0f64,
|
||||
speed: 0.01f64,
|
||||
origin,
|
||||
width_tiles: TILE_WIDTH / 2, // TODO: param
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Progressable for Bar {
|
||||
fn progress(&self, delta: Duration) -> (Self, Currency) {
|
||||
let extra_progress = delta.as_secs_f64() * self.speed;
|
||||
let progress = self.progress + extra_progress;
|
||||
let completions = progress.floor();
|
||||
let progress = progress - completions;
|
||||
let currency = completions * self.factor;
|
||||
|
||||
(Self { progress, ..*self }, currency)
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// to command
|
||||
q.enqueue_command(BitmapCommand {
|
||||
bitmap,
|
||||
origin: Origin::from(&self.origin),
|
||||
compression: Default::default(),
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue