abstreet/santa/src/meters.rs
2021-03-19 16:48:54 -07:00

40 lines
1.2 KiB
Rust

use abstutil::prettyprint_usize;
use geom::Polygon;
use widgetry::{Color, EventCtx, GeomBatch, Text, Widget};
pub fn custom_bar(ctx: &mut EventCtx, filled_color: Color, pct_full: f64, txt: Text) -> Widget {
let total_width = 300.0;
let height = 32.0;
let radius = 4.0;
let mut batch = GeomBatch::new();
// Background
batch.push(
Color::hex("#666666"),
Polygon::rounded_rectangle(total_width, height, radius),
);
// Foreground
if let Some(poly) = Polygon::maybe_rounded_rectangle(pct_full * total_width, height, radius) {
batch.push(filled_color, poly);
}
// Text
let label = txt.render_autocropped(ctx);
let dims = label.get_dims();
batch.append(label.translate(10.0, height / 2.0 - dims.height / 2.0));
batch.into_widget(ctx)
}
pub fn make_bar(ctx: &mut EventCtx, filled_color: Color, value: usize, max: usize) -> Widget {
let pct_full = if max == 0 {
0.0
} else {
(value as f64) / (max as f64)
};
let txt = Text::from(format!(
"{} / {}",
prettyprint_usize(value),
prettyprint_usize(max)
));
custom_bar(ctx, filled_color, pct_full, txt)
}