move trffic signal score into the top-center panel, put a button there to launch the full histogram

This commit is contained in:
Dustin Carlino 2020-01-30 11:28:39 -08:00
parent cc489ae03f
commit 2cc536608a
5 changed files with 84 additions and 98 deletions

View File

@ -25,6 +25,7 @@ impl CreateGridlock {
ctx,
GameplayMode::CreateGridlock,
"Gridlock Challenge",
Vec::new(),
),
})
}

View File

@ -26,6 +26,7 @@ impl FasterTrips {
ctx,
GameplayMode::FasterTrips(trip_mode),
&format!("Faster {} Trips Challenge", trip_mode),
Vec::new(),
),
})
}

View File

@ -2,11 +2,9 @@ use crate::common::Overlays;
use crate::game::{msg, Transition};
use crate::helpers::cmp_duration_shorter;
use crate::managed::{WrappedComposite, WrappedOutcome};
use crate::sandbox::gameplay::{
challenge_controller, manage_overlays, GameplayMode, GameplayState,
};
use crate::sandbox::gameplay::{challenge_controller, GameplayMode, GameplayState};
use crate::ui::UI;
use ezgui::{hotkey, layout, EventCtx, GfxCtx, Key, Line, ModalMenu, Text};
use ezgui::{hotkey, layout, EventCtx, GfxCtx, Key, Line, ManagedWidget, ModalMenu, Text};
use geom::{Duration, Statistic, Time};
use map_model::{IntersectionID, Map};
use sim::{BorderSpawnOverTime, OriginDestination, Scenario, TripMode};
@ -16,24 +14,19 @@ pub struct FixTrafficSignals {
once: bool,
menu: ModalMenu,
top_center: WrappedComposite,
// TODO Keeping a copy in here seems redundant?
mode: GameplayMode,
}
impl FixTrafficSignals {
pub fn new(ctx: &mut EventCtx, mode: GameplayMode) -> Box<dyn GameplayState> {
pub fn new(ctx: &mut EventCtx, ui: &UI, mode: GameplayMode) -> Box<dyn GameplayState> {
Box::new(FixTrafficSignals {
time: Time::START_OF_DAY,
once: true,
menu: ModalMenu::new(
"",
vec![
(hotkey(Key::F), "find slowest traffic signals"),
(hotkey(Key::D), "hide finished trip distribution"),
(hotkey(Key::S), "final score"),
],
ctx,
)
.set_standalone_layout(layout::ContainerOrientation::TopLeftButDownABit(150.0)),
top_center: challenge_controller(ctx, mode, "Traffic Signals Challenge"),
menu: ModalMenu::new("", vec![(hotkey(Key::S), "final score")], ctx)
.set_standalone_layout(layout::ContainerOrientation::TopLeftButDownABit(150.0)),
top_center: make_top_center(ctx, ui, mode.clone()),
mode,
})
}
}
@ -53,54 +46,12 @@ impl GameplayState for FixTrafficSignals {
Some(WrappedOutcome::Clicked(_)) => unreachable!(),
None => {}
}
self.menu.event(ctx);
// Technically this shows stop signs too, but mostly the bottlenecks are signals.
if manage_overlays(
&mut self.menu,
ctx,
ui,
"find slowest traffic signals",
"hide slowest traffic signals",
match ui.overlay {
Overlays::IntersectionDelay(_, _) => true,
_ => false,
},
) {
ui.overlay = Overlays::intersection_delay(ctx, ui);
}
if manage_overlays(
&mut self.menu,
ctx,
ui,
"show finished trip distribution",
"hide finished trip distribution",
match ui.overlay {
Overlays::FinishedTripsHistogram(_, _) => true,
_ => false,
},
) {
ui.overlay = Overlays::finished_trips_histogram(ctx, ui);
}
if self.time != ui.primary.sim.time() {
self.time = ui.primary.sim.time();
// TODO Put this in the top-center panel
let mut txt = Text::new();
let (now, _, _) = ui.primary.sim.get_analytics().all_finished_trips(self.time);
let (baseline, _, _) = ui.prebaked().all_finished_trips(self.time);
txt.add(Line("Average trip time: "));
if now.count() > 0 && baseline.count() > 0 {
txt.append_all(cmp_duration_shorter(
now.select(Statistic::Mean),
baseline.select(Statistic::Mean),
));
} else {
txt.append(Line("same as baseline"));
}
self.menu.set_info(ctx, txt);
self.top_center = make_top_center(ctx, ui, self.mode.clone());
}
self.menu.event(ctx);
if self.menu.action("final score") {
return Some(Transition::Push(msg("Final score", final_score(ui))));
}
@ -119,6 +70,43 @@ impl GameplayState for FixTrafficSignals {
}
}
fn make_top_center(ctx: &mut EventCtx, ui: &UI, mode: GameplayMode) -> WrappedComposite {
let mut txt = Text::new();
let (now, _, _) = ui
.primary
.sim
.get_analytics()
.all_finished_trips(ui.primary.sim.time());
let (baseline, _, _) = ui.prebaked().all_finished_trips(ui.primary.sim.time());
txt.add(Line("Average trip time: "));
if now.count() > 0 && baseline.count() > 0 {
txt.append_all(cmp_duration_shorter(
now.select(Statistic::Mean),
baseline.select(Statistic::Mean),
));
} else {
txt.append(Line("same as baseline"));
}
challenge_controller(
ctx,
mode,
"Traffic Signals Challenge",
vec![ManagedWidget::row(vec![
ManagedWidget::draw_text(ctx, txt).margin(5),
WrappedComposite::text_button(ctx, "details", None).margin(5),
])
.centered()],
)
.cb(
"details",
Box::new(|ctx, ui| {
ui.overlay = Overlays::finished_trips_histogram(ctx, ui);
None
}),
)
}
fn final_score(ui: &UI) -> Vec<String> {
let time = ui.primary.sim.time();
let now = ui

View File

@ -128,21 +128,6 @@ impl GameplayMode {
}
pub fn initialize(&self, ui: &mut UI, ctx: &mut EventCtx) -> Box<dyn GameplayState> {
let state = match self {
GameplayMode::Freeform => freeform::Freeform::new(ctx, ui),
GameplayMode::PlayScenario(ref scenario) => {
play_scenario::PlayScenario::new(scenario, ctx, ui)
}
GameplayMode::OptimizeBus(ref route_name) => {
optimize_bus::OptimizeBus::new(route_name, ctx, ui)
}
GameplayMode::CreateGridlock => create_gridlock::CreateGridlock::new(ctx),
GameplayMode::FasterTrips(trip_mode) => faster_trips::FasterTrips::new(*trip_mode, ctx),
GameplayMode::FixTrafficSignals | GameplayMode::FixTrafficSignalsTutorial(_) => {
fix_traffic_signals::FixTrafficSignals::new(ctx, self.clone())
}
GameplayMode::Tutorial(_, _) => unreachable!(),
};
ctx.loading_screen("instantiate scenario", |_, timer| {
if let Some(scenario) =
self.scenario(&ui.primary.map, ui.primary.current_flags.num_agents, timer)
@ -177,7 +162,21 @@ impl GameplayMode {
}
}
});
state
match self {
GameplayMode::Freeform => freeform::Freeform::new(ctx, ui),
GameplayMode::PlayScenario(ref scenario) => {
play_scenario::PlayScenario::new(scenario, ctx, ui)
}
GameplayMode::OptimizeBus(ref route_name) => {
optimize_bus::OptimizeBus::new(route_name, ctx, ui)
}
GameplayMode::CreateGridlock => create_gridlock::CreateGridlock::new(ctx),
GameplayMode::FasterTrips(trip_mode) => faster_trips::FasterTrips::new(*trip_mode, ctx),
GameplayMode::FixTrafficSignals | GameplayMode::FixTrafficSignalsTutorial(_) => {
fix_traffic_signals::FixTrafficSignals::new(ctx, ui, self.clone())
}
GameplayMode::Tutorial(_, _) => unreachable!(),
}
}
}
@ -273,6 +272,7 @@ fn challenge_controller(
ctx: &mut EventCtx,
gameplay: GameplayMode,
title: &str,
extra_rows: Vec<ManagedWidget>,
) -> WrappedComposite {
// Scrape the description
let mut description = Vec::new();
@ -285,29 +285,24 @@ fn challenge_controller(
}
}
WrappedComposite::new(
Composite::new(
ManagedWidget::row(vec![
ManagedWidget::draw_text(ctx, Text::from(Line(title).size(26))).margin(5),
WrappedComposite::svg_button(ctx, "assets/tools/info.svg", "info", None).margin(5),
ManagedWidget::draw_batch(
ctx,
GeomBatch::from(vec![(Color::WHITE, Polygon::rectangle(2.0, 50.0))]),
)
.margin(5),
WrappedComposite::svg_button(
ctx,
"assets/tools/edit_map.svg",
"edit map",
lctrl(Key::E),
)
.margin(5),
])
.centered()
.bg(Color::grey(0.4)),
let mut rows = vec![ManagedWidget::row(vec![
ManagedWidget::draw_text(ctx, Text::from(Line(title).size(26))).margin(5),
WrappedComposite::svg_button(ctx, "assets/tools/info.svg", "info", None).margin(5),
ManagedWidget::draw_batch(
ctx,
GeomBatch::from(vec![(Color::WHITE, Polygon::rectangle(2.0, 50.0))]),
)
.aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
.build(ctx),
.margin(5),
WrappedComposite::svg_button(ctx, "assets/tools/edit_map.svg", "edit map", lctrl(Key::E))
.margin(5),
])
.centered()];
rows.extend(extra_rows);
WrappedComposite::new(
Composite::new(ManagedWidget::col(rows).bg(Color::grey(0.4)))
.aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
.build(ctx),
)
.cb(
"edit map",

View File

@ -41,6 +41,7 @@ impl OptimizeBus {
ctx,
GameplayMode::OptimizeBus(route_name.to_string()),
&format!("Optimize {} Challenge", route_name),
Vec::new(),
),
})
}