abstreet/editor/src/plugins/debug_objects.rs

81 lines
2.5 KiB
Rust
Raw Normal View History

2018-10-22 19:43:48 +03:00
use ezgui::{GfxCtx, Text};
use objects::{Ctx, ID};
2018-10-06 01:44:12 +03:00
use piston::input::Key;
2018-10-22 19:06:02 +03:00
use plugins::{Plugin, PluginCtx};
pub enum DebugObjectsState {
Empty,
Selected(ID),
Tooltip(ID),
}
impl DebugObjectsState {
pub fn new() -> DebugObjectsState {
DebugObjectsState::Empty
}
}
2018-10-22 19:06:02 +03:00
impl Plugin for DebugObjectsState {
fn event(&mut self, ctx: PluginCtx) -> bool {
let (selected, input, map, sim, control_map) = (
ctx.primary.current_selection,
ctx.input,
&ctx.primary.map,
&mut ctx.primary.sim,
&ctx.primary.control_map,
);
2018-09-13 20:50:59 +03:00
let new_state = if let Some(id) = selected {
// Don't break out of the tooltip state
if let DebugObjectsState::Tooltip(_) = self {
DebugObjectsState::Tooltip(id)
} else {
DebugObjectsState::Selected(id)
}
} else {
DebugObjectsState::Empty
};
*self = new_state;
let mut new_state: Option<DebugObjectsState> = None;
match self {
DebugObjectsState::Empty => {}
DebugObjectsState::Selected(id) => {
if input.key_pressed(Key::LCtrl, &format!("Hold Ctrl to show {:?}'s tooltip", id)) {
new_state = Some(DebugObjectsState::Tooltip(*id));
} else if input.key_pressed(Key::D, "debug") {
id.debug(map, control_map, sim);
}
}
DebugObjectsState::Tooltip(id) => {
2018-10-06 01:44:12 +03:00
if input.key_released(Key::LCtrl) {
new_state = Some(DebugObjectsState::Selected(*id));
}
}
};
if let Some(s) = new_state {
*self = s;
}
match self {
DebugObjectsState::Empty => false,
// TODO hmm, but when we press D to debug, we don't want other stuff to happen...
DebugObjectsState::Selected(_) => false,
DebugObjectsState::Tooltip(_) => true,
}
}
2018-10-22 19:43:48 +03:00
fn draw(&self, g: &mut GfxCtx, ctx: Ctx) {
match *self {
DebugObjectsState::Empty => {}
DebugObjectsState::Selected(_) => {}
DebugObjectsState::Tooltip(id) => {
let mut txt = Text::new();
for line in id.tooltip_lines(ctx.map, ctx.draw_map, ctx.sim) {
txt.add_line(line);
}
ctx.canvas.draw_mouse_tooltip(g, txt);
}
}
}
}