1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)
}