moving warp into ViewMode

This commit is contained in:
Dustin Carlino 2018-12-06 09:40:52 -08:00
parent 63c80405f5
commit 6a317aea33
5 changed files with 42 additions and 28 deletions

View File

@ -152,8 +152,8 @@ and probably step 2...
thursday pick-up:
- search (sometimes ambient, sometimes blocking)
- warp (blocking)
- overlapping keys to quit stuff...
- cant edit mode when sim is actively running
and step 3...
- dismantle the plugin abstraction in UI and probably also the trait. do something different for modes.

View File

@ -7,7 +7,6 @@ pub mod search;
pub mod sim_controls;
pub mod time_travel;
pub mod view;
pub mod warp;
use abstutil;
use abstutil::WeightedUsizeChoice;

View File

@ -5,6 +5,7 @@ mod show_activity;
mod show_owner;
mod show_route;
mod turn_cycler;
mod warp;
use abstutil::Timer;
use ezgui::{Color, GfxCtx};
@ -14,12 +15,14 @@ use plugins::{Plugin, PluginCtx};
use render::DrawMap;
pub struct ViewMode {
warp: Option<Box<Plugin>>,
ambient_plugins: Vec<Box<Plugin>>,
}
impl ViewMode {
pub fn new(map: &Map, draw_map: &DrawMap, timer: &mut Timer) -> ViewMode {
ViewMode {
warp: None,
ambient_plugins: vec![
Box::new(follow::FollowState::new()),
Box::new(debug_objects::DebugObjectsState::new()),
@ -37,6 +40,19 @@ impl ViewMode {
impl Plugin for ViewMode {
fn event(&mut self, mut ctx: PluginCtx) -> bool {
if self.warp.is_some() {
if self.warp.as_mut().unwrap().new_event(&mut ctx) {
return true;
} else {
self.warp = None;
return false;
}
}
if let Some(p) = warp::WarpState::new(&mut ctx) {
self.warp = Some(Box::new(p));
return true;
}
for p in self.ambient_plugins.iter_mut() {
p.ambient_event(&mut ctx);
}
@ -44,14 +60,20 @@ impl Plugin for ViewMode {
}
fn draw(&self, g: &mut GfxCtx, mut ctx: Ctx) {
// Always draw these, even when a blocking plugin is active.
for p in &self.ambient_plugins {
p.new_draw(g, &mut ctx);
}
if let Some(ref p) = self.warp {
p.new_draw(g, &mut ctx);
}
}
fn color_for(&self, obj: ID, mut ctx: Ctx) -> Option<Color> {
// warp doesn't implement color_for.
// First one arbitrarily wins.
// TODO Maybe none of these actually do this?
for p in &self.ambient_plugins {
if let Some(c) = p.new_color_for(obj, &mut ctx) {
return Some(c);

View File

@ -14,36 +14,33 @@ use std::usize;
const ANIMATION_TIME_S: f64 = 0.5;
pub enum WarpState {
Empty,
EnteringSearch(TextBox),
Warping(Instant, Line, ID),
}
impl WarpState {
pub fn new() -> WarpState {
WarpState::Empty
pub fn new(ctx: &mut PluginCtx) -> Option<WarpState> {
if ctx.input.unimportant_key_pressed(
Key::J,
DEBUG,
"start searching for something to warp to",
) {
return Some(WarpState::EnteringSearch(TextBox::new(
"Warp to what?",
None,
)));
}
None
}
}
impl Plugin for WarpState {
fn event(&mut self, ctx: PluginCtx) -> bool {
fn new_event(&mut self, ctx: &mut PluginCtx) -> bool {
let mut new_state: Option<WarpState> = None;
match self {
WarpState::Empty => {
if ctx.input.unimportant_key_pressed(
Key::J,
DEBUG,
"start searching for something to warp to",
) {
new_state = Some(WarpState::EnteringSearch(TextBox::new(
"Warp to what?",
None,
)));
}
}
WarpState::EnteringSearch(tb) => match tb.event(ctx.input) {
InputResult::Canceled => {
new_state = Some(WarpState::Empty);
return false;
}
InputResult::Done(to, _) => {
if let Some((id, pt)) = warp_point(
@ -58,7 +55,7 @@ impl Plugin for WarpState {
id,
));
} else {
new_state = Some(WarpState::Empty);
return false;
}
}
InputResult::StillActive => {}
@ -69,7 +66,7 @@ impl Plugin for WarpState {
if percent >= 1.0 {
ctx.canvas.center_on_map_pt(line.pt2());
ctx.primary.current_selection = Some(*id);
new_state = Some(WarpState::Empty);
return false;
} else {
ctx.canvas
.center_on_map_pt(line.dist_along(percent * line.length()));
@ -79,13 +76,10 @@ impl Plugin for WarpState {
if let Some(s) = new_state {
*self = s;
}
match self {
WarpState::Empty => false,
_ => true,
}
true
}
fn draw(&self, g: &mut GfxCtx, ctx: Ctx) {
fn new_draw(&self, g: &mut GfxCtx, ctx: &mut Ctx) {
if let WarpState::EnteringSearch(tb) = self {
tb.draw(g, ctx.canvas);
}

View File

@ -252,7 +252,6 @@ impl UI {
list: vec![
Box::new(EditMode::new()),
Box::new(plugins::search::SearchState::new()),
Box::new(plugins::warp::WarpState::new()),
Box::new(logs),
Box::new(plugins::diff_all::DiffAllState::new()),
Box::new(plugins::diff_worlds::DiffWorldsState::new()),