use super::{LoadingScreen, Progress};
use crate::graphics;
use crate::Result;
#[allow(missing_debug_implementations)]
pub struct ProgressBar {
font: graphics::Font,
pencil: graphics::Image,
}
impl LoadingScreen for ProgressBar {
fn new(gpu: &mut graphics::Gpu) -> Result<Self> {
Ok(Self {
font: graphics::Font::from_bytes(gpu, graphics::Font::DEFAULT)?,
pencil: graphics::Image::from_colors(
gpu,
&[graphics::Color::WHITE],
)?,
})
}
fn draw(&mut self, progress: &Progress, frame: &mut graphics::Frame<'_>) {
frame.clear(graphics::Color::BLACK);
self.pencil.draw(
graphics::Quad {
position: graphics::Point::new(
50.0,
frame.height() / 2.0 - 25.0,
),
size: (
(frame.width() - 100.0) * (progress.percentage() / 100.0),
50.0,
),
..Default::default()
},
&mut frame.as_target(),
);
if let Some(stage) = progress.stage() {
self.font.add(graphics::Text {
content: stage,
position: graphics::Point::new(
50.0,
frame.height() / 2.0 - 80.0,
),
size: 30.0,
color: graphics::Color::WHITE,
..graphics::Text::default()
});
}
self.font.add(graphics::Text {
content: &(format!("{:.0}", progress.percentage()) + "%"),
position: graphics::Point::new(50.0, frame.height() / 2.0 + 50.0),
size: 30.0,
color: graphics::Color::WHITE,
..graphics::Text::default()
});
self.font.draw(&mut frame.as_target());
}
}