Skip to content

Commit

Permalink
Start all UI apps except for game by loading a map using the async in…
Browse files Browse the repository at this point in the history
…terface. This lets someone on the web immediately start with any map based on URL params. And it's one step towards removing the montlake file from being hardcoded in the wasm. #344

Demo: http://0.0.0.0:8000/?salzburg/maps/south.bin when running the 15m
tool locally
  • Loading branch information
dabreegster committed Jan 9, 2021
1 parent fa9565e commit b1dda36
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 51 deletions.
1 change: 0 additions & 1 deletion abstutil/src/cli.rs
Expand Up @@ -158,7 +158,6 @@ fn parse_args() -> anyhow::Result<Vec<String>> {
.split("&")
.map(|x| x.replace("%20", " ").to_string())
.collect::<Vec<_>>();
log::info!("parsed out url parts: {:?}", parts);
Ok(parts)
}

Expand Down
6 changes: 3 additions & 3 deletions fifteen_min/src/lib.rs
Expand Up @@ -11,9 +11,9 @@ pub fn main() {
widgetry::run(
widgetry::Settings::new("15-minute neighborhoods").read_svg(Box::new(abstio::slurp_bytes)),
|ctx| {
let app = map_gui::SimpleApp::new(ctx, abstutil::CmdArgs::new(), ());
let states = vec![viewer::Viewer::random_start(ctx, &app)];
(app, states)
map_gui::SimpleApp::new(ctx, map_gui::options::Options::default(), (), |ctx, app| {
vec![viewer::Viewer::random_start(ctx, app)]
})
},
);
}
Expand Down
1 change: 0 additions & 1 deletion map_gui/src/load.rs
Expand Up @@ -40,7 +40,6 @@ impl MapLoader {
});
}

// TODO If we want to load montlake on the web, just pull from bundled data.
FileLoader::<A, map_model::Map>::new(
ctx,
name.path(),
Expand Down
68 changes: 38 additions & 30 deletions map_gui/src/simple_app.rs
@@ -1,10 +1,12 @@
use abstio::MapName;
use abstutil::{CmdArgs, Timer};
use geom::{Circle, Distance, Duration, Pt2D, Time};
use map_model::{IntersectionID, Map};
use sim::Sim;
use widgetry::{Canvas, EventCtx, GfxCtx, SharedAppState, State, Transition, Warper};

use crate::colors::ColorScheme;
use crate::load::MapLoader;
use crate::options::Options;
use crate::render::DrawMap;
use crate::render::{DrawOptions, Renderable};
Expand All @@ -24,38 +26,44 @@ pub struct SimpleApp<T> {
pub time: Time,
}

impl<T> SimpleApp<T> {
pub fn new(ctx: &mut EventCtx, args: CmdArgs, session: T) -> SimpleApp<T> {
SimpleApp::new_with_opts(ctx, args, Options::default(), session)
}

pub fn new_with_opts(
impl<T: 'static> SimpleApp<T> {
pub fn new<
F: 'static + Fn(&mut EventCtx, &mut SimpleApp<T>) -> Vec<Box<dyn State<SimpleApp<T>>>>,
>(
ctx: &mut EventCtx,
mut args: CmdArgs,
mut opts: Options,
session: T,
) -> SimpleApp<T> {
ctx.loading_screen("load map", |ctx, mut timer| {
opts.update_from_args(&mut args);
let map_path = args
.optional_free()
.unwrap_or(abstio::MapName::seattle("montlake").path());
args.done();
init_states: F,
) -> (SimpleApp<T>, Vec<Box<dyn State<SimpleApp<T>>>>) {
let mut args = CmdArgs::new();
opts.update_from_args(&mut args);
let map_name = args
.optional_free()
.map(|path| MapName::from_path(&path).expect(&format!("bad map path: {}", path)))
.unwrap_or(MapName::seattle("montlake"));
args.done();

let cs = ColorScheme::new(ctx, opts.color_scheme);
let map = Map::new(map_path, &mut timer);
let draw_map = DrawMap::new(ctx, &map, &opts, &cs, timer);
CameraState::load(ctx, map.get_name());
SimpleApp {
map,
draw_map,
cs,
opts,
current_selection: None,
session,
time: Time::START_OF_DAY,
}
})
let cs = ColorScheme::new(ctx, opts.color_scheme);
// Start with a blank map
let map = Map::blank();
let draw_map = DrawMap::new(ctx, &map, &opts, &cs, &mut Timer::throwaway());
let app = SimpleApp {
map,
draw_map,
cs,
opts,
current_selection: None,
session,
time: Time::START_OF_DAY,
};

let states = vec![MapLoader::new(
ctx,
&app,
map_name,
Box::new(move |ctx, app| Transition::Clear(init_states(ctx, app))),
)];
(app, states)
}

pub fn draw_unzoomed(&self, g: &mut GfxCtx) {
Expand Down Expand Up @@ -201,7 +209,7 @@ impl<T> SimpleApp<T> {
}
}

impl<T> AppLike for SimpleApp<T> {
impl<T: 'static> AppLike for SimpleApp<T> {
#[inline]
fn map(&self) -> &Map {
&self.map
Expand Down Expand Up @@ -279,7 +287,7 @@ impl<T> AppLike for SimpleApp<T> {
}
}

impl<T> SharedAppState for SimpleApp<T> {
impl<T: 'static> SharedAppState for SimpleApp<T> {
fn draw_default(&self, g: &mut GfxCtx) {
self.draw_with_opts(g, DrawOptions::new());
}
Expand Down
6 changes: 3 additions & 3 deletions osm_viewer/src/lib.rs
Expand Up @@ -4,9 +4,9 @@ pub fn main() {
widgetry::run(
widgetry::Settings::new("OpenStreetMap viewer").read_svg(Box::new(abstio::slurp_bytes)),
|ctx| {
let app = map_gui::SimpleApp::new(ctx, abstutil::CmdArgs::new(), ());
let states = vec![viewer::Viewer::new(ctx, &app)];
(app, states)
map_gui::SimpleApp::new(ctx, map_gui::options::Options::default(), (), |ctx, app| {
vec![viewer::Viewer::new(ctx, app)]
})
},
);
}
Expand Down
9 changes: 5 additions & 4 deletions parking_mapper/src/main.rs
Expand Up @@ -4,10 +4,11 @@ fn main() {
widgetry::run(
widgetry::Settings::new("OSM parking mapper").read_svg(Box::new(abstio::slurp_bytes)),
|ctx| {
let mut app = map_gui::SimpleApp::new(ctx, abstutil::CmdArgs::new(), ());
app.opts.min_zoom_for_detail = 2.0;
let states = vec![mapper::ParkingMapper::new(ctx, &app)];
(app, states)
let mut opts = map_gui::options::Options::default();
opts.min_zoom_for_detail = 2.0;
map_gui::SimpleApp::new(ctx, opts, (), |ctx, app| {
vec![mapper::ParkingMapper::new(ctx, app)]
})
},
);
}
19 changes: 10 additions & 9 deletions santa/src/lib.rs
Expand Up @@ -28,16 +28,17 @@ pub fn main() {
opts.color_scheme = map_gui::colors::ColorSchemeChoice::NightMode;
let session = session::Session::load();
session.save();
let mut app =
map_gui::SimpleApp::new_with_opts(ctx, abstutil::CmdArgs::new(), opts, session);
if app.opts.dev {
app.session.unlock_all();
}
app.session.music = music::Music::start(ctx, app.session.play_music, "jingle_bells");
app.session.music.specify_volume(music::OUT_OF_GAME);

let states = vec![title::TitleScreen::new(ctx, &app)];
(app, states)
map_gui::SimpleApp::new(ctx, opts, session, |ctx, app| {
if app.opts.dev {
app.session.unlock_all();
}
app.session.music =
music::Music::start(ctx, app.session.play_music, "jingle_bells");
app.session.music.specify_volume(music::OUT_OF_GAME);

vec![title::TitleScreen::new(ctx, app)]
})
},
);
}
Expand Down

0 comments on commit b1dda36

Please sign in to comment.