making edits manager create the new per-map state itself

This commit is contained in:
Dustin Carlino 2018-10-08 14:37:55 -07:00
parent e0eece7a55
commit dca91bcf43
2 changed files with 25 additions and 23 deletions

View File

@ -6,8 +6,10 @@ use piston::input::Key;
use plugins::road_editor::RoadEditor;
use plugins::{choose_edits, Colorizer};
use sim::{MapEdits, SimFlags};
use ui::PerMapUI;
pub struct EditsManager {
// TODO Is it weird to store this here and not in the outer PerMapUI?
current_flags: SimFlags,
state: State,
}
@ -25,14 +27,16 @@ impl EditsManager {
}
}
// May return a new PerMapUI to replace the current primary.
pub fn event(
&mut self,
input: &mut UserInput,
map: &Map,
control_map: &ControlMap,
road_editor: &RoadEditor,
new_flags: &mut Option<SimFlags>,
) -> bool {
kml: &Option<String>,
) -> (bool, Option<PerMapUI>) {
let mut new_primary: Option<PerMapUI> = None;
let mut new_state: Option<State> = None;
match self.state {
State::Inactive => {
@ -46,7 +50,8 @@ impl EditsManager {
map,
control_map,
road_editor,
new_flags,
&mut new_primary,
kml,
wizard.wrap(input),
).is_some()
{
@ -59,10 +64,11 @@ impl EditsManager {
if let Some(s) = new_state {
self.state = s;
}
match self.state {
let active = match self.state {
State::Inactive => false,
_ => true,
}
};
(active, new_primary)
}
pub fn draw(&self, g: &mut GfxCtx, canvas: &Canvas) {
@ -82,7 +88,8 @@ fn manage_edits(
map: &Map,
control_map: &ControlMap,
road_editor: &RoadEditor,
new_flags: &mut Option<SimFlags>,
new_primary: &mut Option<PerMapUI>,
kml: &Option<String>,
mut wizard: WrappedWizard,
) -> Option<()> {
// TODO Indicate how many edits are there / if there are any unsaved edits
@ -125,7 +132,9 @@ fn manage_edits(
let load_name = choose_edits(map, &mut wizard, "Load which map edits?")?;
let mut flags = current_flags.clone();
flags.edits_name = load_name;
*new_flags = Some(flags);
info!("Reloading everything...");
*new_primary = Some(PerMapUI::new(flags, kml));
Some(())
}
_ => unreachable!(),

View File

@ -242,13 +242,17 @@ impl UIWrapper {
)
}),
Box::new(|ctx| {
ctx.ui.primary.edits_manager.event(
let (active, new_primary) = ctx.ui.primary.edits_manager.event(
ctx.input,
&ctx.ui.primary.map,
&ctx.ui.primary.control_map,
&ctx.ui.primary.road_editor,
ctx.new_flags,
)
&ctx.ui.kml,
);
if new_primary.is_some() {
ctx.ui.primary = new_primary.unwrap();
}
active
}),
Box::new(|ctx| ctx.ui.ab_test_manager.event(ctx.input, &ctx.ui.primary.map)),
Box::new(|ctx| ctx.ui.logs.event(ctx.input)),
@ -260,7 +264,7 @@ impl UIWrapper {
// All of the state that's bound to a specific map+edit has to live here.
// TODO How can we arrange the code so that we statically know that we don't pass anything from UI
// to something in PerMapUI?
struct PerMapUI {
pub struct PerMapUI {
map: Map,
draw_map: DrawMap,
control_map: ControlMap,
@ -286,7 +290,7 @@ struct PerMapUI {
}
impl PerMapUI {
fn new(flags: SimFlags, kml: &Option<String>) -> PerMapUI {
pub fn new(flags: SimFlags, kml: &Option<String>) -> PerMapUI {
flame::start("setup");
let (map, control_map, sim) = sim::load(flags.clone(), Some(sim::Tick::from_seconds(30)));
let extra_shapes = if let Some(path) = kml {
@ -423,16 +427,12 @@ impl UI {
// TODO Normally we'd return InputOnly here if there was an active plugin, but actually, we
// want some keys to always be pressable (sim controller stuff, quitting the game?)
// If we should start over and load something new, fill this out.
let mut new_flags: Option<SimFlags> = None;
// If there's an active plugin, just run it.
if let Some(idx) = self.active_plugin {
if !plugins[idx](PluginCtx {
ui: self,
input: &mut input,
osd,
new_flags: &mut new_flags,
}) {
self.active_plugin = None;
}
@ -443,7 +443,6 @@ impl UI {
ui: self,
input: &mut input,
osd,
new_flags: &mut new_flags,
}) {
self.active_plugin = Some(idx);
break;
@ -451,11 +450,6 @@ impl UI {
}
}
if let Some(flags) = new_flags {
info!("Reloading everything...");
self.primary = PerMapUI::new(flags, &self.kml);
}
if input.unimportant_key_pressed(Key::Escape, ROOT_MENU, "quit") {
let state = EditorState {
map_name: self.primary.map.get_name().clone(),
@ -694,5 +688,4 @@ struct PluginCtx<'a> {
ui: &'a mut UI,
input: &'a mut UserInput,
osd: &'a mut Text,
new_flags: &'a mut Option<SimFlags>,
}