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;
|
2021-01-25 02:32:28 +03:00
|
|
|
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
|
2020-12-15 22:35:40 +03:00
|
|
|
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));
|
2021-03-09 19:55:12 +03:00
|
|
|
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 {
|
2020-12-15 22:35:40 +03:00
|
|
|
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)
|
|
|
|
}
|