abstreet/santa/src/meters.rs

40 lines
1.2 KiB
Rust
Raw Normal View History

2020-12-02 23:37:59 +03:00
use abstutil::prettyprint_usize;
2020-12-04 02:09:54 +03:00
use geom::Polygon;
2021-03-20 02:02:43 +03:00
use widgetry::{Color, EventCtx, GeomBatch, Text, Widget};
2020-12-02 23:37:59 +03:00
2020-12-04 07:36:35 +03:00
pub fn custom_bar(ctx: &mut EventCtx, filled_color: Color, pct_full: f64, txt: Text) -> Widget {
2020-12-02 23:37:59 +03:00
let total_width = 300.0;
let height = 32.0;
let radius = 4.0;
2020-12-02 23:37:59 +03:00
let mut batch = GeomBatch::new();
2020-12-04 02:09:54 +03:00
// Background
2020-12-02 23:37:59 +03:00
batch.push(
2020-12-04 02:09:54 +03:00
Color::hex("#666666"),
Polygon::rounded_rectangle(total_width, height, radius),
2020-12-02 23:37:59 +03:00
);
2020-12-04 02:09:54 +03:00
// 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);
2020-12-04 02:09:54 +03:00
let dims = label.get_dims();
batch.append(label.translate(10.0, height / 2.0 - dims.height / 2.0));
batch.into_widget(ctx)
2020-12-02 23:37:59 +03:00
}
2020-12-04 07:36:35 +03:00
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)
};
2021-03-20 02:02:43 +03:00
let txt = Text::from(format!(
2020-12-04 07:36:35 +03:00
"{} / {}",
prettyprint_usize(value),
prettyprint_usize(max)
2021-03-20 02:02:43 +03:00
));
2020-12-04 07:36:35 +03:00
custom_bar(ctx, filled_color, pct_full, txt)
}