santa/
meters.rs

1use abstutil::prettyprint_usize;
2use geom::Polygon;
3use widgetry::{Color, EventCtx, GeomBatch, Text, Widget};
4
5pub fn custom_bar(ctx: &mut EventCtx, filled_color: Color, pct_full: f64, txt: Text) -> Widget {
6    let total_width = 300.0;
7    let height = 32.0;
8    let radius = 4.0;
9
10    let mut batch = GeomBatch::new();
11    // Background
12    batch.push(
13        Color::hex("#666666"),
14        Polygon::rounded_rectangle(total_width, height, radius),
15    );
16    // Foreground
17    if let Some(poly) = Polygon::maybe_rounded_rectangle(pct_full * total_width, height, radius) {
18        batch.push(filled_color, poly);
19    }
20    // Text
21    let label = txt.render_autocropped(ctx);
22    let dims = label.get_dims();
23    batch.append(label.translate(10.0, height / 2.0 - dims.height / 2.0));
24    batch.into_widget(ctx)
25}
26
27pub fn make_bar(ctx: &mut EventCtx, filled_color: Color, value: usize, max: usize) -> Widget {
28    let pct_full = if max == 0 {
29        0.0
30    } else {
31        (value as f64) / (max as f64)
32    };
33    let txt = Text::from(format!(
34        "{} / {}",
35        prettyprint_usize(value),
36        prettyprint_usize(max)
37    ));
38    custom_bar(ctx, filled_color, pct_full, txt)
39}