scroll through a particular route

This commit is contained in:
Dustin Carlino 2019-05-08 10:55:24 -07:00
parent 16c606c745
commit 32b4c073a1
3 changed files with 130 additions and 2 deletions

View File

@ -139,8 +139,11 @@ impl PolygonDebugger {
// True when done
pub fn event(&mut self, ctx: &mut EventCtx) -> bool {
self.menu.handle_event(ctx, None);
let mut txt = Text::prompt("Polygon Debugger");
txt.add_line(format!("Item {}/{}", self.current + 1, self.items.len()));
self.menu.handle_event(ctx, Some(txt));
ctx.canvas.handle_event(ctx.input);
if self.menu.action("quit") {
return true;
} else if self.current != self.items.len() - 1 && self.menu.action("next item") {

View File

@ -1,3 +1,4 @@
mod route_explorer;
mod route_viewer;
mod show_activity;
mod spawner;
@ -22,7 +23,7 @@ pub struct SandboxMode {
show_activity: show_activity::ShowActivity,
time_travel: time_travel::TimeTravel,
state: State,
// TODO Not while Spawning or TimeTraveling...
// TODO Not while Spawning or TimeTraveling or ExploringRoute...
common: CommonState,
menu: ModalMenu,
}
@ -36,6 +37,7 @@ enum State {
},
Spawning(spawner::AgentSpawner),
TimeTraveling,
ExploringRoute(route_explorer::RouteExplorer),
}
impl SandboxMode {
@ -95,6 +97,12 @@ impl SandboxMode {
}
return EventLoopMode::Animation;
}
if let State::ExploringRoute(ref mut explorer) = mode.state {
if explorer.event(ctx, &mut state.ui) {
mode.state = State::Paused;
}
return EventLoopMode::Animation;
}
let mut txt = Text::prompt("Sandbox Mode");
txt.add_line(state.ui.primary.sim.summary());
@ -147,6 +155,10 @@ impl SandboxMode {
mode.state = State::Spawning(spawner);
return EventLoopMode::Animation;
}
if let Some(explorer) = route_explorer::RouteExplorer::new(ctx, &state.ui) {
mode.state = State::ExploringRoute(explorer);
return EventLoopMode::Animation;
}
if mode.following.is_none() {
if let Some(agent) = state
@ -292,6 +304,7 @@ impl SandboxMode {
}
State::Spawning(_) => unreachable!(),
State::TimeTraveling => unreachable!(),
State::ExploringRoute(_) => unreachable!(),
}
}
_ => unreachable!(),
@ -313,6 +326,15 @@ impl SandboxMode {
);
mode.time_travel.draw(g);
}
State::ExploringRoute(ref explorer) => {
state.ui.draw(
g,
DrawOptions::new(),
&state.ui.primary.sim,
&ShowEverything::new(),
);
explorer.draw(g, &state.ui);
}
_ => {
state.ui.draw(
g,

View File

@ -0,0 +1,103 @@
use crate::common::Warper;
use crate::helpers::ID;
use crate::render::DrawTurn;
use crate::ui::UI;
use ezgui::{Color, EventCtx, GfxCtx, Key, ModalMenu, Text};
use map_model::Traversable;
use sim::AgentID;
pub struct RouteExplorer {
menu: ModalMenu,
agent: AgentID,
steps: Vec<Traversable>,
current: usize,
warper: Option<Warper>,
}
impl RouteExplorer {
pub fn new(ctx: &mut EventCtx, ui: &UI) -> Option<RouteExplorer> {
let agent = ui.primary.current_selection.and_then(|id| id.agent_id())?;
let path = ui.primary.sim.get_path(agent)?;
if !ctx.input.contextual_action(Key::E, "explore route") {
return None;
}
Some(RouteExplorer {
menu: ModalMenu::new(
&format!("Route Explorer for {:?}", agent),
vec![
(Some(Key::Escape), "quit"),
(Some(Key::Dot), "next step"),
(Some(Key::Comma), "prev step"),
(Some(Key::F), "first step"),
(Some(Key::L), "last step"),
],
ctx,
),
agent,
steps: path
.get_steps()
.iter()
.map(|step| step.as_traversable())
.collect(),
current: 0,
warper: None,
})
}
// True when done
pub fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> bool {
if let Some(ref warper) = self.warper {
if warper.event(ctx, ui).is_some() {
return false;
}
self.warper = None;
}
let mut txt = Text::prompt(&format!("Route Explorer for {:?}", self.agent));
txt.add_line(format!("Step {}/{}", self.current + 1, self.steps.len()));
self.menu.handle_event(ctx, Some(txt));
ctx.canvas.handle_event(ctx.input);
if self.menu.action("quit") {
return true;
} else if self.current != self.steps.len() - 1 && self.menu.action("next step") {
self.current += 1;
} else if self.current != self.steps.len() - 1 && self.menu.action("last step") {
self.current = self.steps.len() - 1;
} else if self.current != 0 && self.menu.action("prev step") {
self.current -= 1;
} else if self.current != 0 && self.menu.action("first step") {
self.current = 0;
} else {
return false;
}
self.warper = Some(Warper::new(
ctx,
self.steps[self.current]
.dist_along(
self.steps[self.current].length(&ui.primary.map) / 2.0,
&ui.primary.map,
)
.0,
match self.steps[self.current] {
Traversable::Lane(l) => ID::Lane(l),
Traversable::Turn(t) => ID::Turn(t),
},
));
false
}
pub fn draw(&self, g: &mut GfxCtx, ui: &UI) {
let color = ui.cs.get_def("current step", Color::RED);
match self.steps[self.current] {
Traversable::Lane(l) => {
g.draw_polygon(color, &ui.primary.draw_map.get_l(l).polygon);
}
Traversable::Turn(t) => {
DrawTurn::draw_full(ui.primary.map.get_t(t), g, color);
}
}
self.menu.draw(g);
}
}