Refactor: GeomBatch::load_svg directly takes EventCtx or GfxCtx. The prerender struct should ultimately not be exposed outside of widgetry.

This commit is contained in:
Dustin Carlino 2020-12-15 11:44:58 -08:00
parent 02dd3613cd
commit 16392a461f
22 changed files with 49 additions and 53 deletions

View File

@ -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());

View File

@ -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)
}

View File

@ -168,7 +168,7 @@ impl State<App> 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)),
)

View File

@ -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(),
),

View File

@ -277,10 +277,9 @@ impl State<App> 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)),

View File

@ -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)),
);

View File

@ -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)

View File

@ -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"),

View File

@ -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(

View File

@ -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(),
)

View File

@ -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 {

View File

@ -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),
);

View File

@ -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()),
);

View File

@ -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)

View File

@ -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()),
);

View File

@ -297,3 +297,9 @@ impl std::convert::AsRef<Prerender> for EventCtx<'_> {
&self.prerender
}
}
impl std::convert::AsRef<Prerender> for Prerender {
fn as_ref(&self) -> &Prerender {
self
}
}

View File

@ -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)

View File

@ -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<P: AsRef<Prerender>>(prerender: &P, filename: &str) -> GeomBatch {
svg::load_svg(prerender.as_ref(), filename).0
}
/// Transforms all colors in a batch.

View File

@ -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<Assets>>(self, assets: &A) -> GeomBatch {
pub fn render<A: AsRef<Assets>>(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<Assets>>(self, assets: &A) -> GeomBatch {
pub fn render_autocropped<A: AsRef<Assets>>(self, assets: &A) -> GeomBatch {
let mut batch = self.render(assets);
batch.autocrop_dims = true;
batch.autocrop()

View File

@ -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());

View File

@ -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(),

View File

@ -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")))