share agent following and route tools also

This commit is contained in:
Dustin Carlino 2019-06-24 14:22:15 -07:00
parent b9c9a77f40
commit 36b3558db5
6 changed files with 102 additions and 53 deletions

View File

@ -1,7 +1,7 @@
mod score;
pub mod setup;
use crate::common::{time_controls, CommonState, SpeedControls};
use crate::common::{time_controls, AgentTools, CommonState, RouteExplorer, SpeedControls};
use crate::game::{State, Transition};
use crate::render::MIN_ZOOM_FOR_DETAIL;
use crate::ui::{PerMapUI, UI};
@ -14,9 +14,10 @@ use sim::{Sim, TripID};
pub struct ABTestMode {
menu: ModalMenu,
speed: SpeedControls,
primary_agent_tools: AgentTools,
secondary_agent_tools: AgentTools,
diff_trip: Option<DiffOneTrip>,
diff_all: Option<DiffAllTrips>,
// TODO Not present in Setup state.
common: CommonState,
test_name: String,
}
@ -40,6 +41,8 @@ impl ABTestMode {
(hotkey(Key::S), "swap"),
(hotkey(Key::D), "diff all trips"),
(hotkey(Key::A), "stop diffing trips"),
(hotkey(Key::F), "stop following agent"),
(hotkey(Key::R), "stop showing agent's route"),
(hotkey(Key::Q), "scoreboard"),
(hotkey(Key::O), "save state"),
],
@ -49,6 +52,8 @@ impl ABTestMode {
ctx,
),
speed: SpeedControls::new(ctx, None),
primary_agent_tools: AgentTools::new(),
secondary_agent_tools: AgentTools::new(),
diff_trip: None,
diff_all: None,
common: CommonState::new(),
@ -70,6 +75,7 @@ impl State for ABTestMode {
diff.lines.len()
));
}
self.primary_agent_tools.update_menu_info(&mut txt);
txt.add_line(ui.primary.sim.summary());
self.menu.handle_event(ctx, Some(txt));
@ -90,6 +96,11 @@ impl State for ABTestMode {
let primary = std::mem::replace(&mut ui.primary, secondary);
ui.secondary = Some(primary);
self.recalculate_stuff(ui, ctx);
std::mem::swap(
&mut self.primary_agent_tools,
&mut self.secondary_agent_tools,
);
}
if self.menu.action("scoreboard") {
@ -100,6 +111,12 @@ impl State for ABTestMode {
)));
}
if let Some(explorer) = RouteExplorer::new(ctx, ui) {
return Transition::Push(Box::new(explorer));
}
self.primary_agent_tools.event(ctx, ui, &mut self.menu);
if self.menu.action("save state") {
self.savestate(ui);
}
@ -165,6 +182,7 @@ impl State for ABTestMode {
}
self.menu.draw(g);
self.speed.draw(g);
self.primary_agent_tools.draw(g, ui);
}
fn on_suspend(&mut self, _: &mut UI) {

View File

@ -0,0 +1,69 @@
use crate::common::route_viewer::RouteViewer;
use crate::ui::UI;
use ezgui::{EventCtx, GfxCtx, Key, ModalMenu, Text};
use sim::TripID;
pub struct AgentTools {
following: Option<TripID>,
route_viewer: RouteViewer,
}
impl AgentTools {
pub fn new() -> AgentTools {
AgentTools {
following: None,
route_viewer: RouteViewer::Inactive,
}
}
pub fn update_menu_info(&self, txt: &mut Text) {
if let Some(trip) = self.following {
txt.add_line(format!("Following {}", trip));
}
match self.route_viewer {
RouteViewer::Active(_, trip, _) => {
txt.add_line(format!("Showing {}'s route", trip));
}
RouteViewer::DebugAllRoutes(_, _) => {
txt.add_line("Showing all routes".to_string());
}
_ => {}
}
}
pub fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI, menu: &mut ModalMenu) {
if self.following.is_none() {
if let Some(agent) = ui.primary.current_selection.and_then(|id| id.agent_id()) {
if let Some(trip) = ui.primary.sim.agent_to_trip(agent) {
if ctx
.input
.contextual_action(Key::F, &format!("follow {}", agent))
{
self.following = Some(trip);
}
}
}
}
if let Some(trip) = self.following {
if let Some(pt) = ui
.primary
.sim
.get_canonical_pt_per_trip(trip, &ui.primary.map)
{
ctx.canvas.center_on_map_pt(pt);
} else {
// TODO ideally they wouldnt vanish for so long according to
// get_canonical_point_for_trip
println!("{} is gone... temporarily or not?", trip);
}
if menu.action("stop following agent") {
self.following = None;
}
}
self.route_viewer.event(ctx, ui, menu);
}
pub fn draw(&self, g: &mut GfxCtx, ui: &UI) {
self.route_viewer.draw(g, ui);
}
}

View File

@ -1,10 +1,15 @@
mod agent;
mod associated;
mod navigate;
mod route_explorer;
mod route_viewer;
mod speed;
mod time;
mod turn_cycler;
mod warp;
pub use self::agent::AgentTools;
pub use self::route_explorer::RouteExplorer;
pub use self::speed::SpeedControls;
pub use self::time::time_controls;
use crate::game::Transition;

View File

@ -1,23 +1,20 @@
mod route_explorer;
mod route_viewer;
mod score;
mod show_activity;
mod spawner;
mod time_travel;
use crate::common::{time_controls, CommonState, SpeedControls};
use crate::common::{time_controls, AgentTools, CommonState, RouteExplorer, SpeedControls};
use crate::debug::DebugMode;
use crate::edit::EditMode;
use crate::game::{State, Transition};
use crate::ui::{ShowEverything, UI};
use ezgui::{hotkey, lctrl, EventCtx, EventLoopMode, GfxCtx, Key, ModalMenu, Text};
use geom::Duration;
use sim::{Sim, TripID};
use sim::Sim;
pub struct SandboxMode {
speed: SpeedControls,
following: Option<TripID>,
route_viewer: route_viewer::RouteViewer,
agent_tools: AgentTools,
show_activity: show_activity::ShowActivity,
pub time_travel: time_travel::InactiveTimeTravel,
common: CommonState,
@ -28,8 +25,7 @@ impl SandboxMode {
pub fn new(ctx: &mut EventCtx) -> SandboxMode {
SandboxMode {
speed: SpeedControls::new(ctx, None),
following: None,
route_viewer: route_viewer::RouteViewer::Inactive,
agent_tools: AgentTools::new(),
show_activity: show_activity::ShowActivity::Inactive,
time_travel: time_travel::InactiveTimeTravel::new(),
common: CommonState::new(),
@ -75,18 +71,7 @@ impl State for SandboxMode {
let mut txt = Text::prompt("Sandbox Mode");
txt.add_line(ui.primary.sim.summary());
if let Some(trip) = self.following {
txt.add_line(format!("Following {}", trip));
}
match self.route_viewer {
route_viewer::RouteViewer::Active(_, trip, _) => {
txt.add_line(format!("Showing {}'s route", trip));
}
route_viewer::RouteViewer::DebugAllRoutes(_, _) => {
txt.add_line("Showing all routes".to_string());
}
_ => {}
}
self.agent_tools.update_menu_info(&mut txt);
match self.show_activity {
show_activity::ShowActivity::Inactive => {}
_ => {
@ -106,39 +91,11 @@ impl State for SandboxMode {
if let Some(spawner) = spawner::AgentSpawner::new(ctx, ui, &mut self.menu) {
return Transition::Push(Box::new(spawner));
}
if let Some(explorer) = route_explorer::RouteExplorer::new(ctx, ui) {
if let Some(explorer) = RouteExplorer::new(ctx, ui) {
return Transition::Push(Box::new(explorer));
}
if self.following.is_none() {
if let Some(agent) = ui.primary.current_selection.and_then(|id| id.agent_id()) {
if let Some(trip) = ui.primary.sim.agent_to_trip(agent) {
if ctx
.input
.contextual_action(Key::F, &format!("follow {}", agent))
{
self.following = Some(trip);
}
}
}
}
if let Some(trip) = self.following {
if let Some(pt) = ui
.primary
.sim
.get_canonical_pt_per_trip(trip, &ui.primary.map)
{
ctx.canvas.center_on_map_pt(pt);
} else {
// TODO ideally they wouldnt vanish for so long according to
// get_canonical_point_for_trip
println!("{} is gone... temporarily or not?", trip);
}
if self.menu.action("stop following agent") {
self.following = None;
}
}
self.route_viewer.event(ctx, ui, &mut self.menu);
self.agent_tools.event(ctx, ui, &mut self.menu);
self.show_activity.event(ctx, ui, &mut self.menu);
if self.menu.action("start time traveling") {
return self.time_travel.start(ctx, ui);
@ -227,7 +184,7 @@ impl State for SandboxMode {
&ShowEverything::new(),
);
self.common.draw(g, ui);
self.route_viewer.draw(g, ui);
self.agent_tools.draw(g, ui);
self.show_activity.draw(g, ui);
self.menu.draw(g);
self.speed.draw(g);