improve debug warp tool. closes #222

This commit is contained in:
Dustin Carlino 2020-07-22 12:05:05 -07:00
parent e10be9019e
commit 61837f93ba
2 changed files with 96 additions and 21 deletions

View File

@ -52,7 +52,7 @@ impl CommonState {
app.opts.dev = !app.opts.dev;
}
if app.opts.dev && ctx.input.new_was_pressed(&lctrl(Key::J).unwrap()) {
return Some(Transition::Push(warp::EnteringWarp::new()));
return Some(Transition::Push(warp::DebugWarp::new(ctx)));
}
if let Some(id) = app.primary.current_selection.clone() {
@ -99,6 +99,12 @@ impl CommonState {
};
let mut osd = if let Some(ref id) = app.primary.current_selection {
CommonState::osd_for(app, id.clone())
} else if app.opts.dev {
Text::from_all(vec![
Line("Nothing selected. Hint: "),
Line("Ctrl+J").fg(g.style().hotkey_color),
Line(" to warp"),
])
} else {
Text::from(Line("..."))
};
@ -223,6 +229,12 @@ impl CommonState {
pub fn draw_osd(g: &mut GfxCtx, app: &App) {
let osd = if let Some(ref id) = app.primary.current_selection {
CommonState::osd_for(app, id.clone())
} else if app.opts.dev {
Text::from_all(vec![
Line("Nothing selected. Hint: "),
Line("Ctrl+J").fg(g.style().hotkey_color),
Line(" to warp"),
])
} else {
Text::from(Line("..."))
};

View File

@ -1,10 +1,12 @@
use crate::app::{App, PerMap};
use crate::common::Tab;
use crate::game::{msg, State, Transition, WizardState};
use crate::game::{msg, State, Transition};
use crate::helpers::ID;
use crate::info::OpenTrip;
use crate::sandbox::SandboxMode;
use ezgui::{EventCtx, GfxCtx, Warper, Wizard};
use ezgui::{
hotkey, Btn, Composite, EventCtx, GfxCtx, Key, Line, Outcome, Text, TextExt, Warper, Widget,
};
use geom::Pt2D;
use map_model::{AreaID, BuildingID, IntersectionID, LaneID, RoadID};
use sim::{PedestrianID, PersonID, TripID};
@ -12,13 +14,6 @@ use std::collections::BTreeMap;
const WARP_TO_CAM_ZOOM: f64 = 10.0;
pub struct EnteringWarp;
impl EnteringWarp {
pub fn new() -> Box<dyn State> {
WizardState::new(Box::new(warp_to))
}
}
pub struct Warping {
warper: Warper,
id: Option<ID>,
@ -67,20 +62,88 @@ impl State for Warping {
fn draw(&self, _: &mut GfxCtx, _: &App) {}
}
fn warp_to(wiz: &mut Wizard, ctx: &mut EventCtx, app: &mut App) -> Option<Transition> {
let mut wizard = wiz.wrap(ctx);
let to = wizard.input_string("Warp to what?")?;
if let Some(t) = inner_warp(ctx, app, &to) {
Some(t)
} else {
Some(Transition::Replace(msg(
"Bad warp ID",
vec![format!("{} isn't a valid ID", to)],
)))
pub struct DebugWarp {
composite: Composite,
}
impl DebugWarp {
pub fn new(ctx: &mut EventCtx) -> Box<dyn State> {
let c = ctx.style().hotkey_color;
Box::new(DebugWarp {
composite: Composite::new(Widget::col(vec![
Widget::row(vec![
Line("Warp to an object by ID").small_heading().draw(ctx),
Btn::text_fg("X")
.build(ctx, "close", hotkey(Key::Escape))
.align_right(),
]),
"Example: r42 is Road #42".draw_text(ctx),
// T
// his
//
// i
// s
//
// d
// isorienting...
Text::from_all(vec![
Line("r").fg(c),
Line("oad, "),
Line("l").fg(c),
Line("ane, "),
Line("i").fg(c),
Line("ntersection, "),
Line("b").fg(c),
Line("uilding, "),
Line("p").fg(c),
Line("edestrian, "),
Line("c").fg(c),
Line("ar, "),
Line("t").fg(c),
Line("rip, "),
Line("P").fg(c),
Line("erson"),
])
.draw(ctx),
Widget::text_entry(ctx, String::new(), true).named("input"),
Btn::text_fg("Go!").build_def(ctx, hotkey(Key::Enter)),
]))
.build(ctx),
})
}
}
fn inner_warp(ctx: &mut EventCtx, app: &mut App, line: &str) -> Option<Transition> {
impl State for DebugWarp {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
match self.composite.event(ctx) {
Some(Outcome::Clicked(x)) => match x.as_ref() {
"close" => {
return Transition::Pop;
}
"Go!" => {
let input = self.composite.text_box("input");
if let Some(t) = warp_to_id(ctx, app, &input) {
t
} else {
Transition::Replace(msg(
"Bad warp ID",
vec![format!("{} isn't a valid ID", input)],
))
}
}
_ => unreachable!(),
},
None => Transition::Keep,
}
}
fn draw(&self, g: &mut GfxCtx, app: &App) {
State::grey_out_map(g, app);
self.composite.draw(g);
}
}
fn warp_to_id(ctx: &mut EventCtx, app: &mut App, line: &str) -> Option<Transition> {
if line.is_empty() {
return None;
}