migrating all callers of draw_text_at_mapspace to do something better now

This commit is contained in:
Dustin Carlino 2020-02-06 19:40:23 -08:00
parent d806d9509b
commit 7c76f2d119
9 changed files with 111 additions and 112 deletions

View File

@ -269,19 +269,6 @@ impl<'a> GfxCtx<'a> {
self.unfork();
}
pub fn draw_text_at_mapspace(&mut self, txt: &Text, map_pt: Pt2D) {
let dims = txt.dims();
let mut batch = GeomBatch::new();
txt.clone().render(
&mut batch,
ScreenPt::new(
map_pt.x() - (dims.width / 2.0),
map_pt.y() - (dims.height / 2.0),
),
);
batch.draw(self);
}
pub fn draw_mouse_tooltip(&mut self, txt: &Text) {
let dims = txt.dims();
// TODO Maybe also consider the cursor as a valid center
@ -375,7 +362,7 @@ impl GeomBatch {
}
// Sets the top-left to 0, 0. Not sure exactly when this should be used.
pub fn realign(mut self) -> GeomBatch {
pub(crate) fn realign(mut self) -> GeomBatch {
let mut bounds = Bounds::new();
for (_, poly) in &self.list {
bounds.union(poly.get_bounds());

View File

@ -237,6 +237,12 @@ impl Text {
ScreenRectangle::top_left(top_left, ScreenDims::new(max_width, y - top_left.y))
}
pub fn render_to_batch(self) -> GeomBatch {
let mut batch = GeomBatch::new();
self.render(&mut batch, ScreenPt::new(0.0, 0.0));
batch.realign()
}
}
fn render_text(spans: Vec<TextSpan>) -> GeomBatch {

View File

@ -1,17 +1,21 @@
use crate::helpers::{ColorScheme, ID};
use crate::render::{DrawCtx, DrawOptions, Renderable, OUTLINE_THICKNESS};
use ezgui::{Color, GeomBatch, GfxCtx, Line, Text};
use ezgui::{Color, Drawable, GeomBatch, GfxCtx, Line, Prerender, Text};
use geom::{Angle, Distance, Line, Polygon, Pt2D};
use map_model::{Building, BuildingID, Map, NORMAL_LANE_THICKNESS, SIDEWALK_THICKNESS};
pub struct DrawBuilding {
pub id: BuildingID,
label: Option<Text>,
label_pos: Pt2D,
label: Option<Drawable>,
}
impl DrawBuilding {
pub fn new(bldg: &Building, cs: &ColorScheme, batch: &mut GeomBatch) -> DrawBuilding {
pub fn new(
bldg: &Building,
cs: &ColorScheme,
batch: &mut GeomBatch,
prerender: &Prerender,
) -> DrawBuilding {
// Trim the front path line away from the sidewalk's center line, so that it doesn't
// overlap. For now, this cleanup is visual; it doesn't belong in the map_model layer.
let mut front_path_line = bldg.front_path.line.clone();
@ -39,11 +43,6 @@ impl DrawBuilding {
);
}
let label = bldg
.osm_tags
.get("addr:housenumber")
.map(|num| Text::from(Line(num.to_string()).fg(Color::BLACK).size(50)));
if bldg.parking.is_some() {
// Might need to scale down more for some buildings, but so far, this works everywhere.
batch.add_svg(
@ -54,16 +53,28 @@ impl DrawBuilding {
);
}
// TODO Uh oh, this is lots of work upfront. Disable by default. :(
let label = if false {
bldg.osm_tags.get("addr:housenumber").map(|num| {
let mut lbl = GeomBatch::new();
lbl.add_transformed(
Text::from(Line(num.to_string()).fg(Color::BLACK)).render_to_batch(),
bldg.label_center,
0.1,
Angle::ZERO,
);
prerender.upload(lbl)
})
} else {
None
};
// TODO Slow and looks silly, but it's a nice experiment.
/*for poly in bldg.polygon.shrink(-3.0) {
batch.push(cs.get_def("building roof", Color::rgb(150, 75, 0)), poly);
}*/
DrawBuilding {
id: bldg.id,
label,
label_pos: bldg.label_center,
}
DrawBuilding { id: bldg.id, label }
}
}
@ -74,8 +85,8 @@ impl Renderable for DrawBuilding {
fn draw(&self, g: &mut GfxCtx, opts: &DrawOptions, _: &DrawCtx) {
if opts.label_buildings {
if let Some(ref txt) = self.label {
g.draw_text_at_mapspace(txt, self.label_pos);
if let Some(ref lbl) = self.label {
g.redraw(lbl);
}
}
}

View File

@ -12,7 +12,6 @@ pub struct DrawCar {
body: PolyLine,
body_polygon: Polygon,
zorder: isize,
label: Option<Text>,
draw_default: Drawable,
}
@ -109,14 +108,22 @@ impl DrawCar {
}
}
if let Some(line) = input.label {
// TODO Would rotation make any sense? Or at least adjust position/size while turning.
// Buses are a constant length, so hardcoding this is fine.
draw_default.add_transformed(
Text::from(Line(line).fg(Color::rgb(249, 206, 24))).render_to_batch(),
input.body.dist_along(Distance::meters(9.0)).0,
0.07,
Angle::ZERO,
);
}
DrawCar {
id: input.id,
body: input.body,
body_polygon,
zorder: input.on.get_zorder(map),
label: input
.label
.map(|line| Text::from(Line(line).fg(Color::rgb(249, 206, 24)).size(20))),
draw_default: prerender.upload(draw_default),
}
}
@ -129,12 +136,6 @@ impl Renderable for DrawCar {
fn draw(&self, g: &mut GfxCtx, _: &DrawOptions, _: &DrawCtx) {
g.redraw(&self.draw_default);
if let Some(ref txt) = self.label {
// TODO Would rotation make any sense? Or at least adjust position/size while turning.
// Buses are a constant length, so hardcoding this is fine.
g.draw_text_at_mapspace(txt, self.body.dist_along(Distance::meters(9.0)).0);
}
}
fn get_outline(&self, _: &Map) -> Polygon {

View File

@ -4,7 +4,7 @@ use crate::render::{
OUTLINE_THICKNESS,
};
use abstutil::Timer;
use ezgui::{Color, Drawable, GeomBatch, GfxCtx, Line, Prerender, ScreenPt, Text};
use ezgui::{Color, Drawable, GeomBatch, GfxCtx, Line, Prerender, Text};
use geom::{Angle, Distance, Line, PolyLine, Polygon, Pt2D, Time, EPSILON_DIST};
use map_model::{
Intersection, IntersectionID, IntersectionType, Map, Road, RoadWithStopSign, Turn, TurnType,
@ -143,11 +143,8 @@ impl Renderable for DrawIntersection {
ctx,
ctx.opts.traffic_signal_style.clone(),
);
let mut txt = GeomBatch::new();
Text::from(Line(format!("{}", idx + 1)).roboto())
.render(&mut txt, ScreenPt::new(0.0, 0.0));
batch.add_transformed(
txt.realign(),
Text::from(Line(format!("{}", idx + 1)).roboto()).render_to_batch(),
ctx.map.get_i(self.id).polygon.center(),
0.1,
Angle::ZERO,

View File

@ -132,7 +132,7 @@ impl DrawMap {
timer.start_iter("make DrawBuildings", map.all_buildings().len());
for b in map.all_buildings() {
timer.next();
buildings.push(DrawBuilding::new(b, cs, &mut all_buildings));
buildings.push(DrawBuilding::new(b, cs, &mut all_buildings, ctx.prerender));
}
timer.start("upload all buildings");
let draw_all_buildings = all_buildings.upload(ctx);

View File

@ -1,7 +1,7 @@
use crate::helpers::{rotating_color_agents, ColorScheme, ID};
use crate::render::{DrawCtx, DrawOptions, Renderable, OUTLINE_THICKNESS};
use ezgui::{Color, Drawable, GeomBatch, GfxCtx, Line, Prerender, Text};
use geom::{Circle, Distance, PolyLine, Polygon};
use geom::{Angle, Circle, Distance, PolyLine, Polygon};
use map_model::{Map, SIDEWALK_THICKNESS};
use sim::{DrawPedCrowdInput, DrawPedestrianInput, PedCrowdLocation, PedestrianID};
@ -177,7 +177,6 @@ pub struct DrawPedCrowd {
zorder: isize,
draw_default: Drawable,
label: Text,
}
impl DrawPedCrowd {
@ -204,16 +203,16 @@ impl DrawPedCrowd {
.exact_slice(input.low, input.high),
};
let blob = pl_shifted.make_polygons(SIDEWALK_THICKNESS / 2.0);
let draw_default = prerender.upload_borrowed(vec![(
let mut batch = GeomBatch::new();
batch.push(
cs.get_def("pedestrian crowd", Color::rgb_f(0.2, 0.7, 0.7)),
&blob,
)]);
// Ideally "pedestrian head" color, but it looks really faded...
let label = Text::from(
Line(format!("{}", input.members.len()))
.fg(Color::BLACK)
.size(60),
blob.clone(),
);
batch.add_transformed(
Text::from(Line(format!("{}", input.members.len())).fg(Color::BLACK)).render_to_batch(),
blob.center(),
0.02,
Angle::ZERO,
);
DrawPedCrowd {
@ -224,8 +223,7 @@ impl DrawPedCrowd {
PedCrowdLocation::Sidewalk(on, _) => on.get_zorder(map),
PedCrowdLocation::FrontPath(_) => 0,
},
draw_default,
label,
draw_default: prerender.upload(batch),
}
}
}
@ -238,7 +236,6 @@ impl Renderable for DrawPedCrowd {
fn draw(&self, g: &mut GfxCtx, _: &DrawOptions, _: &DrawCtx) {
g.redraw(&self.draw_default);
g.draw_text_at_mapspace(&self.label, self.blob.center());
}
fn get_outline(&self, _: &Map) -> Polygon {

View File

@ -1,7 +1,7 @@
use crate::helpers::{ColorScheme, ID};
use crate::render::{dashed_lines, DrawCtx, DrawOptions, Renderable, OUTLINE_THICKNESS};
use ezgui::{Color, Drawable, GeomBatch, GfxCtx, Line, Prerender, Text};
use geom::{Distance, Polygon, Pt2D};
use geom::{Angle, Distance, Polygon, Pt2D};
use map_model::{LaneType, Map, Road, RoadID};
pub struct DrawRoad {
@ -9,8 +9,7 @@ pub struct DrawRoad {
zorder: isize,
draw_center_line: Drawable,
label: Text,
label_pos: Pt2D,
label: Drawable,
}
impl DrawRoad {
@ -34,15 +33,24 @@ impl DrawRoad {
);
}
let mut label = Text::new().with_bg();
label.add(Line(r.get_name()).size(50));
let mut txt = Text::new().with_bg();
txt.add(Line(r.get_name()));
let mut lbl = GeomBatch::new();
// TODO Disabled because it's slow up-front cost
if false {
lbl.add_transformed(
txt.render_to_batch(),
r.center_pts.middle(),
0.1,
Angle::ZERO,
);
}
DrawRoad {
id: r.id,
zorder: r.get_zorder(),
draw_center_line: prerender.upload(draw),
label,
label_pos: r.center_pts.middle(),
label: prerender.upload(lbl),
}
}
}
@ -55,7 +63,7 @@ impl Renderable for DrawRoad {
fn draw(&self, g: &mut GfxCtx, opts: &DrawOptions, _: &DrawCtx) {
g.redraw(&self.draw_center_line);
if opts.label_roads {
g.draw_text_at_mapspace(&self.label, self.label_pos);
g.redraw(&self.label);
}
}

View File

@ -7,7 +7,7 @@ use ezgui::{
hotkey, Canvas, Choice, Color, Drawable, EventCtx, EventLoopMode, GeomBatch, GfxCtx, Key, Line,
ModalMenu, Text, Wizard, GUI,
};
use geom::{Distance, Line, Polygon, Pt2D};
use geom::{Angle, Distance, Line, Polygon, Pt2D};
use map_model::raw::{OriginalBuilding, OriginalIntersection, OriginalRoad, RestrictionType};
use map_model::{osm, NORMAL_LANE_THICKNESS};
use model::{Model, ID};
@ -36,7 +36,7 @@ enum State {
CreatingTurnRestrictionPt1(OriginalRoad),
CreatingTurnRestrictionPt2(OriginalRoad, OriginalRoad, Wizard),
// bool is show_tooltip
PreviewIntersection(Drawable, Vec<(Text, Pt2D)>, bool),
PreviewIntersection(Drawable, bool),
EnteringWarp(Wizard),
StampingRoads(String, String, String, String),
}
@ -94,11 +94,11 @@ impl UI {
last_id: None,
};
ui.recount_parking_tags(ctx);
ui.recount_parking_tags();
ui
}
fn recount_parking_tags(&mut self, ctx: &EventCtx) {
fn recount_parking_tags(&mut self) {
let mut ways_audited = HashSet::new();
let mut ways_missing = HashSet::new();
for r in self.model.map.roads.values() {
@ -111,14 +111,11 @@ impl UI {
ways_audited.insert(r.osm_tags[osm::OSM_WAY_ID].clone());
}
}
self.menu.set_info(
ctx,
Text::from(Line(format!(
"Parking data audited: {} / {} ways",
abstutil::prettyprint_usize(ways_audited.len()),
abstutil::prettyprint_usize(ways_audited.len() + ways_missing.len())
))),
);
self.menu.set_info(Text::from(Line(format!(
"Parking data audited: {} / {} ways",
abstutil::prettyprint_usize(ways_audited.len()),
abstutil::prettyprint_usize(ways_audited.len() + ways_missing.len())
))));
}
}
@ -180,8 +177,8 @@ impl GUI for UI {
.input
.key_pressed(Key::P, "preview intersection geometry")
{
let (draw, labels) = preview_intersection(i, &self.model, ctx);
self.state = State::PreviewIntersection(draw, labels, false);
let draw = preview_intersection(i, &self.model, ctx);
self.state = State::PreviewIntersection(draw, false);
}
}
Some(ID::Building(b)) => {
@ -214,7 +211,7 @@ impl GUI for UI {
} else if ctx.input.key_pressed(Key::T, "toggle parking") {
self.model.toggle_r_parking(r, ctx.prerender);
self.model.world.handle_mouseover(ctx);
self.recount_parking_tags(ctx);
self.recount_parking_tags();
} else if ctx.input.key_pressed(Key::F, "toggle sidewalks") {
self.model.toggle_r_sidewalks(r, ctx.prerender);
self.model.world.handle_mouseover(ctx);
@ -308,28 +305,25 @@ impl GUI for UI {
} else if !self.model.intersection_geom
&& self.menu.action("preview all intersections")
{
let (draw, labels) = preview_all_intersections(&self.model, ctx);
self.state = State::PreviewIntersection(draw, labels, false);
let draw = preview_all_intersections(&self.model, ctx);
self.state = State::PreviewIntersection(draw, false);
} else if self.menu.action("find overlapping intersections") {
let (draw, labels) = find_overlapping_intersections(&self.model, ctx);
self.state = State::PreviewIntersection(draw, labels, false);
let draw = find_overlapping_intersections(&self.model, ctx);
self.state = State::PreviewIntersection(draw, false);
} else if short_roads.is_empty()
&& self
.menu
.swap_action("find short roads", "clear short roads", ctx)
.swap_action("find short roads", "clear short roads")
{
*short_roads = find_short_roads(&self.model);
if short_roads.is_empty() {
self.menu.change_action(
"clear short roads",
"find short roads",
ctx,
);
self.menu
.change_action("clear short roads", "find short roads");
}
} else if !short_roads.is_empty()
&& self
.menu
.swap_action("clear short roads", "find short roads", ctx)
.swap_action("clear short roads", "find short roads")
{
short_roads.clear();
}
@ -493,7 +487,7 @@ impl GUI for UI {
self.model.world.handle_mouseover(ctx);
}
}
State::PreviewIntersection(_, _, ref mut show_tooltip) => {
State::PreviewIntersection(_, ref mut show_tooltip) => {
if *show_tooltip && ctx.input.key_released(Key::RightAlt) {
*show_tooltip = false;
} else if !*show_tooltip && ctx.input.key_pressed(Key::RightAlt, "show map pt") {
@ -588,7 +582,7 @@ impl GUI for UI {
g.draw_polygon(Color::rgb(242, 239, 233), &self.model.map.boundary_polygon);
match self.state {
State::PreviewIntersection(_, _, _) => self.model.world.draw(g, |id| match id {
State::PreviewIntersection(_, _) => self.model.world.draw(g, |id| match id {
ID::Intersection(_) => false,
_ => true,
}),
@ -642,11 +636,8 @@ impl GUI for UI {
}
wizard.draw(g);
}
State::PreviewIntersection(ref draw, ref labels, show_tooltip) => {
State::PreviewIntersection(ref draw, show_tooltip) => {
g.redraw(draw);
for (txt, pt) in labels {
g.draw_text_at_mapspace(txt, *pt);
}
if show_tooltip {
// TODO Argh, covers up mouseover tooltip.
@ -676,28 +667,29 @@ impl GUI for UI {
}
}
fn preview_intersection(
i: OriginalIntersection,
model: &Model,
ctx: &EventCtx,
) -> (Drawable, Vec<(Text, Pt2D)>) {
fn preview_intersection(i: OriginalIntersection, model: &Model, ctx: &EventCtx) -> Drawable {
let (intersection, roads, debug) = model
.map
.preview_intersection(i, &mut Timer::new("calculate intersection_polygon"));
let mut batch = GeomBatch::new();
let mut labels = Vec::new();
batch.push(Color::ORANGE.alpha(0.5), intersection);
for r in roads {
batch.push(Color::GREEN.alpha(0.5), r);
}
for (label, poly) in debug {
labels.push((Text::from(Line(label)).with_bg(), poly.center()));
let center = poly.center();
batch.push(Color::RED.alpha(0.5), poly);
batch.add_transformed(
Text::from(Line(label)).with_bg().render_to_batch(),
center,
0.1,
Angle::ZERO,
);
}
(batch.upload(ctx), labels)
batch.upload(ctx)
}
fn preview_all_intersections(model: &Model, ctx: &EventCtx) -> (Drawable, Vec<(Text, Pt2D)>) {
fn preview_all_intersections(model: &Model, ctx: &EventCtx) -> Drawable {
let mut batch = GeomBatch::new();
let mut timer = Timer::new("preview all intersections");
timer.start_iter("preview", model.map.intersections.len());
@ -709,10 +701,10 @@ fn preview_all_intersections(model: &Model, ctx: &EventCtx) -> (Drawable, Vec<(T
let (intersection, _, _) = model.map.preview_intersection(*i, &mut timer);
batch.push(Color::ORANGE.alpha(0.5), intersection);
}
(batch.upload(ctx), Vec::new())
batch.upload(ctx)
}
fn find_overlapping_intersections(model: &Model, ctx: &EventCtx) -> (Drawable, Vec<(Text, Pt2D)>) {
fn find_overlapping_intersections(model: &Model, ctx: &EventCtx) -> Drawable {
let mut timer = Timer::new("find overlapping intersections");
let mut polygons = Vec::new();
for i in model.map.intersections.keys() {
@ -744,7 +736,7 @@ fn find_overlapping_intersections(model: &Model, ctx: &EventCtx) -> (Drawable, V
let mut batch = GeomBatch::new();
batch.extend(Color::RED.alpha(0.5), overlap);
(batch.upload(ctx), Vec::new())
batch.upload(ctx)
}
// TODO OriginalRoad is dangerous, as this map changes. :\