From 16392a461fd0d01c428e79b06e301e5be547a1b2 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 15 Dec 2020 11:44:58 -0800 Subject: [PATCH] Refactor: GeomBatch::load_svg directly takes EventCtx or GfxCtx. The prerender struct should ultimately not be exposed outside of widgetry. --- experiment/src/animation.rs | 3 +-- experiment/src/title.rs | 4 +--- fifteen_min/src/viewer.rs | 2 +- game/src/challenges/cutscene.rs | 6 +++--- game/src/devtools/story.rs | 11 +++++------ game/src/edit/select.rs | 2 +- game/src/info/person.rs | 4 ++-- game/src/info/trip.rs | 16 +++++++--------- .../src/sandbox/dashboards/generic_trip_table.rs | 4 ++-- game/src/sandbox/gameplay/mod.rs | 2 +- game/src/sandbox/speed.rs | 2 +- map_gui/src/render/building.rs | 2 +- map_gui/src/render/intersection.rs | 2 +- map_gui/src/render/lane.rs | 6 +++--- map_gui/src/render/parking_lot.rs | 2 +- widgetry/src/drawing.rs | 6 ++++++ widgetry/src/event_ctx.rs | 3 +-- widgetry/src/geom.rs | 4 ++-- widgetry/src/text.rs | 5 ++--- widgetry/src/widgets/button.rs | 2 +- widgetry/src/widgets/checkbox.rs | 8 ++++---- widgetry_demo/src/lib.rs | 6 ++---- 22 files changed, 49 insertions(+), 53 deletions(-) diff --git a/experiment/src/animation.rs b/experiment/src/animation.rs index f4be196ecb..544238f399 100644 --- a/experiment/src/animation.rs +++ b/experiment/src/animation.rs @@ -160,8 +160,7 @@ impl SnowEffect { } pub fn event(&mut self, ctx: &mut EventCtx, now: Time) { - let shape = - GeomBatch::load_svg(ctx.prerender, "system/assets/map/snowflake.svg").scale(0.1); + let shape = GeomBatch::load_svg(ctx, "system/assets/map/snowflake.svg").scale(0.1); let mut batch = GeomBatch::new(); let prev_flakes = std::mem::replace(&mut self.flakes, Vec::new()); diff --git a/experiment/src/title.rs b/experiment/src/title.rs index b2f7bc330f..eebb909620 100644 --- a/experiment/src/title.rs +++ b/experiment/src/title.rs @@ -111,9 +111,7 @@ fn locked_level(ctx: &mut EventCtx, app: &App, level: &Level, idx: usize) -> Wid let hitbox = batch.get_bounds().get_rectangle(); let center = hitbox.center(); batch.push(app.cs.fade_map_dark, hitbox); - batch.append( - GeomBatch::load_svg(ctx.prerender, "system/assets/tools/locked.svg").centered_on(center), - ); + batch.append(GeomBatch::load_svg(ctx, "system/assets/tools/locked.svg").centered_on(center)); Widget::draw_batch(ctx, batch) } diff --git a/fifteen_min/src/viewer.rs b/fifteen_min/src/viewer.rs index a3e34bf7d6..a39188e3a4 100644 --- a/fifteen_min/src/viewer.rs +++ b/fifteen_min/src/viewer.rs @@ -168,7 +168,7 @@ impl State for Viewer { /// Draw a star on the start building. fn draw_star(ctx: &mut EventCtx, center: Pt2D) -> Drawable { ctx.upload( - GeomBatch::load_svg(ctx.prerender, "system/assets/tools/star.svg") + GeomBatch::load_svg(ctx, "system/assets/tools/star.svg") .centered_on(center) .color(RewriteColor::ChangeAll(Color::BLACK)), ) diff --git a/game/src/challenges/cutscene.rs b/game/src/challenges/cutscene.rs index 5638cce753..39e372990b 100644 --- a/game/src/challenges/cutscene.rs +++ b/game/src/challenges/cutscene.rs @@ -205,7 +205,7 @@ fn make_panel( Layout::PlayerSpeaking => Widget::custom_row(vec![ Widget::draw_batch( ctx, - GeomBatch::load_svg(ctx.prerender, "system/assets/characters/boss.svg") + GeomBatch::load_svg(ctx, "system/assets/characters/boss.svg") .scale(0.75) .autocrop(), ), @@ -218,7 +218,7 @@ fn make_panel( Layout::BossSpeaking => Widget::custom_row(vec![ Widget::draw_batch( ctx, - GeomBatch::load_svg(ctx.prerender, "system/assets/characters/boss.svg") + GeomBatch::load_svg(ctx, "system/assets/characters/boss.svg") .scale(0.75) .autocrop(), ), @@ -228,7 +228,7 @@ fn make_panel( Layout::Extra(name, scale) => Widget::custom_row(vec![ Widget::draw_batch( ctx, - GeomBatch::load_svg(ctx.prerender, "system/assets/characters/boss.svg") + GeomBatch::load_svg(ctx, "system/assets/characters/boss.svg") .scale(0.75) .autocrop(), ), diff --git a/game/src/devtools/story.rs b/game/src/devtools/story.rs index baa544d4a6..1338529186 100644 --- a/game/src/devtools/story.rs +++ b/game/src/devtools/story.rs @@ -277,10 +277,9 @@ impl State for StoryMapEditor { match self.mode { Mode::PlacingMarker => { if g.canvas.get_cursor_in_map_space().is_some() { - let batch = - GeomBatch::load_svg(g.prerender, "system/assets/timeline/goal_pos.svg") - .centered_on(g.canvas.get_cursor().to_pt()) - .color(RewriteColor::Change(Color::hex("#5B5B5B"), Color::GREEN)); + let batch = GeomBatch::load_svg(g, "system/assets/timeline/goal_pos.svg") + .centered_on(g.canvas.get_cursor().to_pt()) + .color(RewriteColor::Change(Color::hex("#5B5B5B"), Color::GREEN)); g.fork_screenspace(); batch.draw(g); g.unfork(); @@ -428,7 +427,7 @@ impl Marker { let hitbox = if pts.len() == 1 { batch.append( - GeomBatch::load_svg(ctx.prerender, "system/assets/timeline/goal_pos.svg") + GeomBatch::load_svg(ctx, "system/assets/timeline/goal_pos.svg") .scale(2.0) .centered_on(pts[0]) .color(RewriteColor::Change( @@ -472,7 +471,7 @@ impl Marker { let mut batch = GeomBatch::new(); if self.pts.len() == 1 { batch.append( - GeomBatch::load_svg(g.prerender, "system/assets/timeline/goal_pos.svg") + GeomBatch::load_svg(g, "system/assets/timeline/goal_pos.svg") .scale(2.0) .centered_on(self.pts[0]) .color(RewriteColor::Change(Color::hex("#5B5B5B"), app.cs.hovering)), diff --git a/game/src/edit/select.rs b/game/src/edit/select.rs index 6029bac8ac..0ebca00ef2 100644 --- a/game/src/edit/select.rs +++ b/game/src/edit/select.rs @@ -283,7 +283,7 @@ impl RoadSelector { } { let mut batch = GeomBatch::new(); batch.append( - GeomBatch::load_svg(g.prerender, cursor) + GeomBatch::load_svg(g, cursor) .centered_on(g.canvas.get_cursor().to_pt()) .color(RewriteColor::ChangeAll(Color::GREEN)), ); diff --git a/game/src/info/person.rs b/game/src/info/person.rs index c21999888c..ea3aa75fae 100644 --- a/game/src/info/person.rs +++ b/game/src/info/person.rs @@ -492,7 +492,7 @@ pub fn parked_car( ) } else { // TODO Blink - GeomBatch::load_svg(ctx.prerender, "system/assets/tools/location.svg") + GeomBatch::load_svg(ctx, "system/assets/tools/location.svg") .color(RewriteColor::ChangeAll(Color::hex("#7FFA4D"))) .to_btn(ctx) .build(ctx, "unfollow (pause the simulation)", Key::F) @@ -612,7 +612,7 @@ fn header( ) } else { // TODO Blink - GeomBatch::load_svg(ctx.prerender, "system/assets/tools/location.svg") + GeomBatch::load_svg(ctx, "system/assets/tools/location.svg") .color(RewriteColor::ChangeAll(Color::hex("#7FFA4D"))) .to_btn(ctx) .build(ctx, "unfollow (pause the simulation)", Key::F) diff --git a/game/src/info/trip.rs b/game/src/info/trip.rs index b25a20fe62..3e28d39732 100644 --- a/game/src/info/trip.rs +++ b/game/src/info/trip.rs @@ -529,11 +529,9 @@ fn make_timeline( // Show where we are in the trip currently if let Some(p) = progress_along_path { batch.append( - GeomBatch::load_svg(ctx.prerender, "system/assets/timeline/current_pos.svg") - .centered_on(Pt2D::new( - x1 + p * phase_width, - icon_height + (rectangle_height / 2.0), - )), + GeomBatch::load_svg(ctx, "system/assets/timeline/current_pos.svg").centered_on( + Pt2D::new(x1 + p * phase_width, icon_height + (rectangle_height / 2.0)), + ), ); } } @@ -666,7 +664,7 @@ fn make_trip_details( .insert(format!("jump to start of {}", trip_id), id); details.unzoomed.append( - GeomBatch::load_svg(ctx.prerender, "system/assets/timeline/start_pos.svg") + GeomBatch::load_svg(ctx, "system/assets/timeline/start_pos.svg") .scale(3.0) .color(RewriteColor::Change(Color::WHITE, Color::BLACK)) .color(RewriteColor::Change( @@ -676,7 +674,7 @@ fn make_trip_details( .centered_on(center), ); details.zoomed.append( - GeomBatch::load_svg(ctx.prerender, "system/assets/timeline/start_pos.svg") + GeomBatch::load_svg(ctx, "system/assets/timeline/start_pos.svg") .color(RewriteColor::Change(Color::WHITE, Color::BLACK)) .color(RewriteColor::Change( Color::hex("#5B5B5B"), @@ -700,7 +698,7 @@ fn make_trip_details( .insert(format!("jump to goal of {}", trip_id), id); details.unzoomed.append( - GeomBatch::load_svg(ctx.prerender, "system/assets/timeline/goal_pos.svg") + GeomBatch::load_svg(ctx, "system/assets/timeline/goal_pos.svg") .scale(3.0) .color(RewriteColor::Change(Color::WHITE, Color::BLACK)) .color(RewriteColor::Change( @@ -710,7 +708,7 @@ fn make_trip_details( .centered_on(center), ); details.zoomed.append( - GeomBatch::load_svg(ctx.prerender, "system/assets/timeline/goal_pos.svg") + GeomBatch::load_svg(ctx, "system/assets/timeline/goal_pos.svg") .color(RewriteColor::Change(Color::WHITE, Color::BLACK)) .color(RewriteColor::Change( Color::hex("#5B5B5B"), diff --git a/game/src/sandbox/dashboards/generic_trip_table.rs b/game/src/sandbox/dashboards/generic_trip_table.rs index be8b289eb7..3013edfffc 100644 --- a/game/src/sandbox/dashboards/generic_trip_table.rs +++ b/game/src/sandbox/dashboards/generic_trip_table.rs @@ -158,7 +158,7 @@ fn preview_route(g: &mut GfxCtx, app: &App, id: TripID) -> GeomBatch { let trip = app.primary.sim.trip_info(id); batch.append( - GeomBatch::load_svg(g.prerender, "system/assets/timeline/start_pos.svg") + GeomBatch::load_svg(g, "system/assets/timeline/start_pos.svg") .scale(10.0) .color(RewriteColor::Change(Color::WHITE, Color::BLACK)) .color(RewriteColor::Change( @@ -172,7 +172,7 @@ fn preview_route(g: &mut GfxCtx, app: &App, id: TripID) -> GeomBatch { }), ); batch.append( - GeomBatch::load_svg(g.prerender, "system/assets/timeline/goal_pos.svg") + GeomBatch::load_svg(g, "system/assets/timeline/goal_pos.svg") .scale(10.0) .color(RewriteColor::Change(Color::WHITE, Color::BLACK)) .color(RewriteColor::Change( diff --git a/game/src/sandbox/gameplay/mod.rs b/game/src/sandbox/gameplay/mod.rs index 9a84e71a3c..6f901dbc68 100644 --- a/game/src/sandbox/gameplay/mod.rs +++ b/game/src/sandbox/gameplay/mod.rs @@ -228,7 +228,7 @@ impl FinalScore { Widget::custom_row(vec![ Widget::draw_batch( ctx, - GeomBatch::load_svg(ctx.prerender, "system/assets/characters/boss.svg") + GeomBatch::load_svg(ctx, "system/assets/characters/boss.svg") .scale(0.75) .autocrop(), ) diff --git a/game/src/sandbox/speed.rs b/game/src/sandbox/speed.rs index c9e833dc32..17f56d0855 100644 --- a/game/src/sandbox/speed.rs +++ b/game/src/sandbox/speed.rs @@ -70,7 +70,7 @@ impl SpeedControls { txt.extend(Text::tooltip(ctx, Key::LeftArrow, "slow down")); txt.extend(Text::tooltip(ctx, Key::RightArrow, "speed up")); - GeomBatch::load_svg(ctx.prerender, "system/assets/speed/triangle.svg") + GeomBatch::load_svg(ctx, "system/assets/speed/triangle.svg") .color(if self.setting >= s { RewriteColor::NoOp } else { diff --git a/map_gui/src/render/building.rs b/map_gui/src/render/building.rs index 71ee8b9c80..e71c2ea5ff 100644 --- a/map_gui/src/render/building.rs +++ b/map_gui/src/render/building.rs @@ -57,7 +57,7 @@ impl DrawBuilding { // Might need to scale down more for some buildings, but so far, this works // everywhere. bldg_batch.append( - GeomBatch::load_svg(ctx.prerender, "system/assets/map/parking.svg") + GeomBatch::load_svg(ctx, "system/assets/map/parking.svg") .scale(0.1) .centered_on(bldg.label_center), ); diff --git a/map_gui/src/render/intersection.rs b/map_gui/src/render/intersection.rs index cb12173469..30fccd7622 100644 --- a/map_gui/src/render/intersection.rs +++ b/map_gui/src/render/intersection.rs @@ -94,7 +94,7 @@ impl DrawIntersection { IntersectionType::Construction => { // TODO Centering seems weird default_geom.append( - GeomBatch::load_svg(g.prerender, "system/assets/map/under_construction.svg") + GeomBatch::load_svg(g, "system/assets/map/under_construction.svg") .scale(0.08) .centered_on(i.polygon.center()), ); diff --git a/map_gui/src/render/lane.rs b/map_gui/src/render/lane.rs index b851abb97e..9fe7238b9f 100644 --- a/map_gui/src/render/lane.rs +++ b/map_gui/src/render/lane.rs @@ -139,21 +139,21 @@ impl DrawLane { let (pt, angle) = lane.lane_center_pts.must_dist_along(dist); if lane.is_bus() { draw.append( - GeomBatch::load_svg(g.prerender, "system/assets/map/bus_only.svg") + GeomBatch::load_svg(g, "system/assets/map/bus_only.svg") .scale(0.06) .centered_on(pt) .rotate(angle.shortest_rotation_towards(Angle::degrees(-90.0))), ); } else if lane.is_biking() { draw.append( - GeomBatch::load_svg(g.prerender, "system/assets/meters/bike.svg") + GeomBatch::load_svg(g, "system/assets/meters/bike.svg") .scale(0.06) .centered_on(pt) .rotate(angle.shortest_rotation_towards(Angle::degrees(-90.0))), ); } else if lane.lane_type == LaneType::SharedLeftTurn { draw.append( - GeomBatch::load_svg(g.prerender, "system/assets/map/shared_left_turn.svg") + GeomBatch::load_svg(g, "system/assets/map/shared_left_turn.svg") .autocrop() .scale(0.003) .centered_on(pt) diff --git a/map_gui/src/render/parking_lot.rs b/map_gui/src/render/parking_lot.rs index a1bcf13510..bc846984a5 100644 --- a/map_gui/src/render/parking_lot.rs +++ b/map_gui/src/render/parking_lot.rs @@ -31,7 +31,7 @@ impl DrawParkingLot { ); } unzoomed_batch.append( - GeomBatch::load_svg(ctx.prerender, "system/assets/map/parking.svg") + GeomBatch::load_svg(ctx, "system/assets/map/parking.svg") .scale(0.05) .centered_on(lot.polygon.polylabel()), ); diff --git a/widgetry/src/drawing.rs b/widgetry/src/drawing.rs index f3bd654152..fad9c22f64 100644 --- a/widgetry/src/drawing.rs +++ b/widgetry/src/drawing.rs @@ -297,3 +297,9 @@ impl std::convert::AsRef for EventCtx<'_> { &self.prerender } } + +impl std::convert::AsRef for Prerender { + fn as_ref(&self) -> &Prerender { + self + } +} diff --git a/widgetry/src/event_ctx.rs b/widgetry/src/event_ctx.rs index de5eccec75..51f0ba3adb 100644 --- a/widgetry/src/event_ctx.rs +++ b/widgetry/src/event_ctx.rs @@ -140,8 +140,7 @@ impl<'a> EventCtx<'a> { Widget::custom_col(vec![ Widget::draw_batch( self, - GeomBatch::load_svg(self.prerender, "system/assets/map/dont_walk.svg") - .scale(5.0), + GeomBatch::load_svg(self, "system/assets/map/dont_walk.svg").scale(5.0), ) .container() .bg(Color::BLACK) diff --git a/widgetry/src/geom.rs b/widgetry/src/geom.rs index 5872ea861f..9175a2fb8c 100644 --- a/widgetry/src/geom.rs +++ b/widgetry/src/geom.rs @@ -139,8 +139,8 @@ impl GeomBatch { } /// Returns a batch containing an SVG from a file. - pub fn load_svg(prerender: &Prerender, filename: &str) -> GeomBatch { - svg::load_svg(prerender, filename).0 + pub fn load_svg>(prerender: &P, filename: &str) -> GeomBatch { + svg::load_svg(prerender.as_ref(), filename).0 } /// Transforms all colors in a batch. diff --git a/widgetry/src/text.rs b/widgetry/src/text.rs index b530ae8d7f..f63ffb68b7 100644 --- a/widgetry/src/text.rs +++ b/widgetry/src/text.rs @@ -273,7 +273,7 @@ impl Text { } /// Render the text, without any autocropping. You can pass in an `EventCtx` or `GfxCtx`. - pub fn render<'a, A: AsRef>(self, assets: &A) -> GeomBatch { + pub fn render>(self, assets: &A) -> GeomBatch { let assets: &Assets = assets.as_ref(); self.inner_render(assets, svg::HIGH_QUALITY) } @@ -331,8 +331,7 @@ impl Text { /// Render the text, autocropping blank space out of the result. You can pass in an `EventCtx` /// or `GfxCtx`. - // TODO How is this different than render()? - pub fn render_autocropped<'a, A: AsRef>(self, assets: &A) -> GeomBatch { + pub fn render_autocropped>(self, assets: &A) -> GeomBatch { let mut batch = self.render(assets); batch.autocrop_dims = true; batch.autocrop() diff --git a/widgetry/src/widgets/button.rs b/widgetry/src/widgets/button.rs index 48f362c704..2c53e30a55 100644 --- a/widgetry/src/widgets/button.rs +++ b/widgetry/src/widgets/button.rs @@ -202,7 +202,7 @@ impl Btn { icon_batch.push(Color::INVISIBLE, icon_container); let path = "system/assets/widgetry/arrow_drop_down.svg"; - let icon = GeomBatch::load_svg(ctx.prerender, &path) + let icon = GeomBatch::load_svg(ctx, &path) .color(RewriteColor::ChangeAll(ctx.style().outline_color)) .autocrop() .centered_on(icon_batch.get_bounds().center()); diff --git a/widgetry/src/widgets/checkbox.rs b/widgetry/src/widgets/checkbox.rs index c468e4645f..5741280928 100644 --- a/widgetry/src/widgets/checkbox.rs +++ b/widgetry/src/widgets/checkbox.rs @@ -35,14 +35,14 @@ impl Checkbox { ) -> Widget { let label = label.into(); let (off, hitbox) = Widget::row(vec![ - GeomBatch::load_svg(ctx.prerender, "system/assets/widgetry/toggle_off.svg") + GeomBatch::load_svg(ctx, "system/assets/widgetry/toggle_off.svg") .batch() .centered_vert(), label.clone().batch_text(ctx), ]) .to_geom(ctx, None); let (on, _) = Widget::row(vec![ - GeomBatch::load_svg(ctx.prerender, "system/assets/widgetry/toggle_on.svg") + GeomBatch::load_svg(ctx, "system/assets/widgetry/toggle_on.svg") .batch() .centered_vert(), label.clone().batch_text(ctx), @@ -118,7 +118,7 @@ impl Checkbox { pub fn colored(ctx: &EventCtx, label: &str, color: Color, enabled: bool) -> Widget { let (off, hitbox) = Widget::row(vec![ - GeomBatch::load_svg(ctx.prerender, "system/assets/widgetry/checkbox.svg") + GeomBatch::load_svg(ctx, "system/assets/widgetry/checkbox.svg") .color(RewriteColor::ChangeAll(color.alpha(0.3))) .batch() .centered_vert(), @@ -126,7 +126,7 @@ impl Checkbox { ]) .to_geom(ctx, None); let (on, _) = Widget::row(vec![ - GeomBatch::load_svg(ctx.prerender, "system/assets/widgetry/checkbox.svg") + GeomBatch::load_svg(ctx, "system/assets/widgetry/checkbox.svg") .color(RewriteColor::Change(Color::BLACK, color)) .batch() .centered_vert(), diff --git a/widgetry_demo/src/lib.rs b/widgetry_demo/src/lib.rs index ac88c70eea..b60d15b047 100644 --- a/widgetry_demo/src/lib.rs +++ b/widgetry_demo/src/lib.rs @@ -260,10 +260,8 @@ fn setup_scrollable_canvas(ctx: &mut EventCtx) -> Drawable { Polygon::rounded_rectangle(5000.0, 5000.0, Some(25.0)), ); // SVG support using lyon and usvg. Map-space means don't scale for high DPI monitors. - batch.append( - GeomBatch::load_svg(&ctx.prerender, "system/assets/pregame/logo.svg") - .translate(300.0, 300.0), - ); + batch + .append(GeomBatch::load_svg(ctx, "system/assets/pregame/logo.svg").translate(300.0, 300.0)); // Text rendering also goes through lyon and usvg. batch.append( Text::from(Line("Awesome vector text thanks to usvg and lyon").fg(Color::hex("#DF8C3D")))