split out an agent meter with icons for the modes

This commit is contained in:
Dustin Carlino 2019-12-11 10:13:36 -08:00
parent 2afa2ef43f
commit f0af92f00e
8 changed files with 124 additions and 15 deletions

View File

@ -630,4 +630,9 @@ impl DrawBoth {
pub fn get_dims(&self) -> ScreenDims {
self.dims
}
// TODO Hack
pub(crate) fn override_bounds(&mut self, b: Bounds) {
self.dims = ScreenDims::new(b.max_x - b.min_x, b.max_y - b.min_y);
}
}

View File

@ -48,3 +48,13 @@ pub fn stack_vertically(
top_left.y += dims.height;
}
}
pub fn stack_horizontally(top_left: ScreenPt, padding: f64, widgets: Vec<&mut dyn Widget>) {
let mut x1 = top_left.x;
let y1 = top_left.y;
for w in widgets {
w.set_pos(ScreenPt::new(x1, y1));
x1 += w.get_dims().width + padding;
}
}

View File

@ -10,6 +10,13 @@ pub struct JustDraw {
}
impl JustDraw {
pub fn wrap(draw: DrawBoth) -> JustDraw {
JustDraw {
draw,
top_left: ScreenPt::new(0.0, 0.0),
}
}
pub fn image(filename: &str, ctx: &EventCtx) -> JustDraw {
let (color, rect) = ctx.canvas.texture_rect(filename);
let batch = GeomBatch::from(vec![(color, rect)]);
@ -21,9 +28,12 @@ impl JustDraw {
pub fn svg(filename: &str, ctx: &EventCtx) -> JustDraw {
let mut batch = GeomBatch::new();
svg::add_svg(&mut batch, filename);
let bounds = svg::add_svg(&mut batch, filename);
let mut draw = DrawBoth::new(ctx, batch, vec![]);
// TODO The dims will be wrong; it'll only look at geometry, not the padding in the image.
draw.override_bounds(bounds);
JustDraw {
draw: DrawBoth::new(ctx, batch, vec![]),
draw,
top_left: ScreenPt::new(0.0, 0.0),
}
}

View File

@ -0,0 +1,3 @@
<svg width="26" height="26" viewBox="0 0 26 26" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M16.5292 6.44723C17.6421 6.44723 18.5526 5.53669 18.5526 4.42381C18.5526 3.31093 17.6421 2.40039 16.5292 2.40039C15.4163 2.40039 14.5058 3.31093 14.5058 4.42381C14.5058 5.53669 15.4163 6.44723 16.5292 6.44723ZM5.90627 13.0233C3.07348 13.0233 0.847717 15.2491 0.847717 18.0819C0.847717 20.9147 3.07348 23.1404 5.90627 23.1404C8.73905 23.1404 10.9648 20.9147 10.9648 18.0819C10.9648 15.2491 8.73905 13.0233 5.90627 13.0233ZM5.90627 21.6229C3.98402 21.6229 2.36528 20.0041 2.36528 18.0819C2.36528 16.1596 3.98402 14.5409 5.90627 14.5409C7.82851 14.5409 9.44725 16.1596 9.44725 18.0819C9.44725 20.0041 7.82851 21.6229 5.90627 21.6229ZM11.7742 11.5058L14.2023 9.07767L15.0117 9.88704C16.3269 11.2023 18.0468 12.0116 20.1714 12.0116V9.98821C18.6538 9.98821 17.4398 9.38119 16.5292 8.47065L14.607 6.5484C14.1011 6.14372 13.5953 5.94137 12.9882 5.94137C12.3812 5.94137 11.8754 6.14372 11.5718 6.5484L8.73905 9.38119C8.33437 9.78587 8.13203 10.2917 8.13203 10.7976C8.13203 11.4046 8.33437 11.9105 8.73905 12.214L11.9765 15.0468V20.1053H13.9999V13.8327L11.7742 11.5058ZM20.0702 13.0233C17.2374 13.0233 15.0117 15.2491 15.0117 18.0819C15.0117 20.9147 17.2374 23.1404 20.0702 23.1404C22.903 23.1404 25.1287 20.9147 25.1287 18.0819C25.1287 15.2491 22.903 13.0233 20.0702 13.0233ZM20.0702 21.6229C18.148 21.6229 16.5292 20.0041 16.5292 18.0819C16.5292 16.1596 18.148 14.5409 20.0702 14.5409C21.9925 14.5409 23.6112 16.1596 23.6112 18.0819C23.6112 20.0041 21.9925 21.6229 20.0702 21.6229Z" fill="#F2F2F2"/>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -0,0 +1,3 @@
<svg width="25" height="26" viewBox="0 0 25 26" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M4.75641 17.0702C4.75641 17.9605 5.15098 18.7597 5.76812 19.3162V21.117C5.76812 21.6735 6.22339 22.1287 6.77983 22.1287H7.79154C8.34798 22.1287 8.80325 21.6735 8.80325 21.117V20.1053H16.8969V21.117C16.8969 21.6735 17.3522 22.1287 17.9086 22.1287H18.9203C19.4768 22.1287 19.9321 21.6735 19.9321 21.117V19.3162C20.5492 18.7597 20.9438 17.9605 20.9438 17.0702V6.95309C20.9438 3.4121 17.3218 2.90625 12.8501 2.90625C8.37833 2.90625 4.75641 3.4121 4.75641 6.95309V17.0702ZM8.29739 18.0819C7.45767 18.0819 6.77983 17.404 6.77983 16.5643C6.77983 15.7246 7.45767 15.0468 8.29739 15.0468C9.13711 15.0468 9.81496 15.7246 9.81496 16.5643C9.81496 17.404 9.13711 18.0819 8.29739 18.0819ZM17.4028 18.0819C16.5631 18.0819 15.8852 17.404 15.8852 16.5643C15.8852 15.7246 16.5631 15.0468 17.4028 15.0468C18.2425 15.0468 18.9203 15.7246 18.9203 16.5643C18.9203 17.404 18.2425 18.0819 17.4028 18.0819ZM18.9203 12.0116H6.77983V6.95309H18.9203V12.0116Z" fill="#F2F2F2"/>
</svg>

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -0,0 +1,3 @@
<svg width="26" height="26" viewBox="0 0 26 26" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M19.9026 6.96348C19.7003 6.36657 19.1337 5.94165 18.466 5.94165H7.33719C6.66946 5.94165 6.11302 6.36657 5.90056 6.96348L3.7962 13.0236V21.1173C3.7962 21.6737 4.25147 22.129 4.80791 22.129H5.81962C6.37606 22.129 6.83133 21.6737 6.83133 21.1173V20.1056H18.9718V21.1173C18.9718 21.6737 19.4271 22.129 19.9836 22.129H20.9953C21.5517 22.129 22.007 21.6737 22.007 21.1173V13.0236L19.9026 6.96348ZM7.33719 17.0705C6.49747 17.0705 5.81962 16.3926 5.81962 15.5529C5.81962 14.7132 6.49747 14.0353 7.33719 14.0353C8.17691 14.0353 8.85475 14.7132 8.85475 15.5529C8.85475 16.3926 8.17691 17.0705 7.33719 17.0705ZM18.466 17.0705C17.6263 17.0705 16.9484 16.3926 16.9484 15.5529C16.9484 14.7132 17.6263 14.0353 18.466 14.0353C19.3057 14.0353 19.9836 14.7132 19.9836 15.5529C19.9836 16.3926 19.3057 17.0705 18.466 17.0705ZM5.81962 12.0119L7.33719 7.45921H18.466L19.9836 12.0119H5.81962Z" fill="#F2F2F2"/>
</svg>

After

Width:  |  Height:  |  Size: 1000 B

View File

@ -0,0 +1,3 @@
<svg width="25" height="26" viewBox="0 0 25 26" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M13.8688 6.44723C14.9817 6.44723 15.8922 5.53669 15.8922 4.42381C15.8922 3.31093 14.9817 2.40039 13.8688 2.40039C12.7559 2.40039 11.8454 3.31093 11.8454 4.42381C11.8454 5.53669 12.7559 6.44723 13.8688 6.44723ZM10.1255 9.88704L7.29272 24.1521H9.41731L11.2384 16.0585L13.363 18.0819V24.1521H15.3864V16.5643L13.2618 14.5409L13.8688 11.5058C15.1841 13.0233 17.2075 14.0351 19.4332 14.0351V12.0116C17.511 12.0116 15.8922 10.9999 15.0829 9.58353L14.0712 7.96479C13.6665 7.35777 13.0595 6.95308 12.3513 6.95308C12.0478 6.95308 11.8454 7.05425 11.5419 7.05425L6.28101 9.28002V14.0351H8.30443V10.5952L10.1255 9.88704Z" fill="#F2F2F2"/>
</svg>

After

Width:  |  Height:  |  Size: 739 B

View File

@ -16,17 +16,18 @@ use crate::pregame::main_menu;
use crate::ui::{ShowEverything, UI};
use abstutil::Timer;
use ezgui::{
hotkey, layout, lctrl, Choice, EventCtx, EventLoopMode, GfxCtx, Key, Line, MenuUnderButton,
ModalMenu, Text,
hotkey, layout, lctrl, Choice, Color, DrawBoth, EventCtx, EventLoopMode, GeomBatch, GfxCtx,
JustDraw, Key, Line, MenuUnderButton, ModalMenu, ScreenDims, ScreenPt, ScreenRectangle, Text,
};
pub use gameplay::spawner::spawn_agents_around;
pub use gameplay::GameplayMode;
use geom::Duration;
use geom::{Distance, Duration, Polygon, Time};
use map_model::MapEdits;
use sim::TripMode;
pub struct SandboxMode {
speed: speed::SpeedControls,
agent_meter: AgentMeter,
info_tools: MenuUnderButton,
general_tools: MenuUnderButton,
agent_tools: AgentTools,
@ -41,6 +42,7 @@ impl SandboxMode {
pub fn new(ctx: &mut EventCtx, ui: &mut UI, mode: GameplayMode) -> SandboxMode {
SandboxMode {
speed: speed::SpeedControls::new(ctx, ui.opts.dev),
agent_meter: AgentMeter::new(ctx, ui),
general_tools: MenuUnderButton::new(
"assets/ui/hamburger.png",
"General",
@ -88,16 +90,6 @@ impl State for SandboxMode {
fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Transition {
{
let mut txt = Text::new();
let (active, unfinished, by_mode) = ui.primary.sim.num_trips();
txt.add(Line(format!("Active trips: {}", active)));
txt.add(Line(format!("Unfinished trips: {}", unfinished)));
txt.add(Line(format!(
"Peds {}, Bikes {}, Cars {}, Buses {}",
by_mode[&TripMode::Walk],
by_mode[&TripMode::Bike],
by_mode[&TripMode::Drive],
by_mode[&TripMode::Transit]
)));
txt.add(Line(""));
{
let edits = ui.primary.map.get_edits();
@ -108,6 +100,7 @@ impl State for SandboxMode {
}
self.menu.set_info(ctx, txt);
}
self.agent_meter.event(ctx, ui);
if let Some(t) = self.gameplay.event(ctx, ui, &mut self.overlay) {
return t;
}
@ -315,6 +308,7 @@ impl State for SandboxMode {
self.info_tools.draw(g);
self.general_tools.draw(g);
self.gameplay.draw(g, ui);
self.agent_meter.draw(g);
if let Some(ref m) = self.minimap {
m.draw(g, ui);
}
@ -324,3 +318,81 @@ impl State for SandboxMode {
self.speed.pause();
}
}
// TODO Some kind of composite thing...
struct AgentMeter {
time: Time,
widgets: Vec<JustDraw>,
rect: ScreenRectangle,
}
impl AgentMeter {
pub fn new(ctx: &EventCtx, ui: &UI) -> AgentMeter {
let (active, unfinished, by_mode) = ui.primary.sim.num_trips();
let mut row1_txt = Text::new().no_bg();
row1_txt.add(Line(format!("Active trips: {}", active)));
row1_txt.add(Line(format!("Unfinished trips: {}", unfinished)));
// TODO Hardcoding guessed dims
let rect_bg = GeomBatch::from(vec![(
Color::grey(0.4),
Polygon::rounded_rectangle(
Distance::meters(290.0),
Distance::meters(100.0),
Distance::meters(5.0),
),
)]);
// TODO Rectangle behind everything
let mut widgets = vec![
JustDraw::wrap(DrawBoth::new(ctx, rect_bg, Vec::new())),
JustDraw::text(row1_txt, ctx),
JustDraw::svg("assets/meters/pedestrian.svg", ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Walk])).no_bg(), ctx),
JustDraw::svg("assets/meters/bike.svg", ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Bike])).no_bg(), ctx),
JustDraw::svg("assets/meters/car.svg", ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Drive])).no_bg(), ctx),
JustDraw::svg("assets/meters/bus.svg", ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Transit])).no_bg(), ctx),
];
// TODO A horrible experiment in manual layouting
use layout::Widget;
let top_left = ScreenPt::new(ctx.canvas.window_width - 300.0, 350.0);
widgets[0].set_pos(top_left);
widgets[1].set_pos(top_left);
let top_left = ScreenPt::new(top_left.x, top_left.y + widgets[1].get_dims().height);
layout::stack_horizontally(
top_left,
// TODO Padding is wrong, want to alternate the amount
5.0,
widgets
.iter_mut()
.skip(2)
.map(|w| w as &mut dyn Widget)
.collect(),
);
AgentMeter {
widgets,
time: ui.primary.sim.time(),
rect: ScreenRectangle::top_left(top_left, ScreenDims::new(290.0, 100.0)),
}
}
pub fn event(&mut self, ctx: &EventCtx, ui: &UI) {
// TODO Or window size changed...
if self.time != ui.primary.sim.time() {
*self = AgentMeter::new(ctx, ui);
}
}
pub fn draw(&self, g: &mut GfxCtx) {
for w in &self.widgets {
w.draw(g);
}
g.canvas.mark_covered_area(self.rect.clone());
}
}