draw traffic signal text label properly (and way more efficiently now)

This commit is contained in:
Dustin Carlino 2020-02-06 19:18:45 -08:00
parent 37bcfab7af
commit d806d9509b
3 changed files with 24 additions and 27 deletions

View File

@ -374,27 +374,17 @@ impl GeomBatch {
ctx.prerender.upload(self) ctx.prerender.upload(self)
} }
// TODO Funky API. Make sure to call this in the right places. // Sets the top-left to 0, 0. Not sure exactly when this should be used.
pub(crate) fn fix(mut self) -> GeomBatch { pub fn realign(mut self) -> GeomBatch {
let mut bounds = Bounds::new(); let mut bounds = Bounds::new();
for (_, poly) in &self.list { for (_, poly) in &self.list {
bounds.union(poly.get_bounds()); bounds.union(poly.get_bounds());
} }
let dx = if bounds.min_x < 0.0 { if bounds.min_x == 0.0 && bounds.min_y == 0.0 {
-bounds.min_x
} else {
0.0
};
let dy = if bounds.min_y < 0.0 {
-bounds.min_y
} else {
0.0
};
if dx == 0.0 && dy == 0.0 {
return self; return self;
} }
for (_, poly) in &mut self.list { for (_, poly) in &mut self.list {
*poly = poly.translate(dx, dy); *poly = poly.translate(-bounds.min_x, -bounds.min_y);
} }
self self
} }
@ -435,10 +425,14 @@ impl GeomBatch {
pub fn add_svg(&mut self, filename: &str, center: Pt2D, scale: f64, rotate: Angle) { pub fn add_svg(&mut self, filename: &str, center: Pt2D, scale: f64, rotate: Angle) {
let mut batch = GeomBatch::new(); let mut batch = GeomBatch::new();
svg::add_svg(&mut batch, filename); svg::add_svg(&mut batch, filename);
let dims = batch.get_dims(); self.add_transformed(batch, center, scale, rotate);
}
pub fn add_transformed(&mut self, other: GeomBatch, center: Pt2D, scale: f64, rotate: Angle) {
let dims = other.get_dims();
let dx = center.x() - dims.width * scale / 2.0; let dx = center.x() - dims.width * scale / 2.0;
let dy = center.y() - dims.height * scale / 2.0; let dy = center.y() - dims.height * scale / 2.0;
for (color, poly) in batch.consume() { for (color, poly) in other.consume() {
self.push(color, poly.scale(scale).translate(dx, dy).rotate(rotate)); self.push(color, poly.scale(scale).translate(dx, dy).rotate(rotate));
} }
} }

View File

@ -215,7 +215,7 @@ impl Text {
); );
} }
// TODO Do this first or not? Should we call fix() around here? // TODO Do this first or not? Should we call realign() around here?
y += line_dims.height; y += line_dims.height;
for (color, poly) in line_batch.consume() { for (color, poly) in line_batch.consume() {

View File

@ -4,7 +4,7 @@ use crate::render::{
OUTLINE_THICKNESS, OUTLINE_THICKNESS,
}; };
use abstutil::Timer; use abstutil::Timer;
use ezgui::{Color, Drawable, GeomBatch, GfxCtx, Line, Prerender, Text}; use ezgui::{Color, Drawable, GeomBatch, GfxCtx, Line, Prerender, ScreenPt, Text};
use geom::{Angle, Distance, Line, PolyLine, Polygon, Pt2D, Time, EPSILON_DIST}; use geom::{Angle, Distance, Line, PolyLine, Polygon, Pt2D, Time, EPSILON_DIST};
use map_model::{ use map_model::{
Intersection, IntersectionID, IntersectionType, Map, Road, RoadWithStopSign, Turn, TurnType, Intersection, IntersectionID, IntersectionType, Map, Road, RoadWithStopSign, Turn, TurnType,
@ -17,7 +17,7 @@ pub struct DrawIntersection {
zorder: isize, zorder: isize,
draw_default: Drawable, draw_default: Drawable,
pub draw_traffic_signal: RefCell<Option<(Time, Drawable, Text, Pt2D)>>, pub draw_traffic_signal: RefCell<Option<(Time, Drawable)>>,
} }
impl DrawIntersection { impl DrawIntersection {
@ -130,7 +130,7 @@ impl Renderable for DrawIntersection {
let mut maybe_redraw = self.draw_traffic_signal.borrow_mut(); let mut maybe_redraw = self.draw_traffic_signal.borrow_mut();
let recalc = maybe_redraw let recalc = maybe_redraw
.as_ref() .as_ref()
.map(|(t, _, _, _)| *t != ctx.sim.time()) .map(|(t, _)| *t != ctx.sim.time())
.unwrap_or(true); .unwrap_or(true);
if recalc { if recalc {
let (idx, phase, t) = signal.current_phase_and_remaining_time(ctx.sim.time()); let (idx, phase, t) = signal.current_phase_and_remaining_time(ctx.sim.time());
@ -143,16 +143,19 @@ impl Renderable for DrawIntersection {
ctx, ctx,
ctx.opts.traffic_signal_style.clone(), ctx.opts.traffic_signal_style.clone(),
); );
*maybe_redraw = Some(( let mut txt = GeomBatch::new();
ctx.sim.time(), Text::from(Line(format!("{}", idx + 1)).roboto())
g.prerender.upload(batch), .render(&mut txt, ScreenPt::new(0.0, 0.0));
Text::from(Line(format!("{}", idx + 1)).roboto().size(90)), batch.add_transformed(
txt.realign(),
ctx.map.get_i(self.id).polygon.center(), ctx.map.get_i(self.id).polygon.center(),
)); 0.1,
Angle::ZERO,
);
*maybe_redraw = Some((ctx.sim.time(), g.prerender.upload(batch)));
} }
let (_, batch, txt, pt) = maybe_redraw.as_ref().unwrap(); let (_, batch) = maybe_redraw.as_ref().unwrap();
g.redraw(batch); g.redraw(batch);
g.draw_text_at_mapspace(txt, *pt);
} }
} }