be able to handle generic Text

This commit is contained in:
Dustin Carlino 2020-02-06 15:31:16 -08:00
parent b6b1be7d18
commit fa6cbea28c
4 changed files with 79 additions and 44 deletions

View File

@ -409,14 +409,6 @@ impl GeomBatch {
self.push(color, poly.scale(scale).translate(dx, dy).rotate(rotate));
}
}
pub fn add_text(&mut self, text: String) {
let mut batch = GeomBatch::new();
svg::add_text(&mut batch, text);
for (color, poly) in batch.consume() {
self.push(color, poly);
}
}
}
pub enum RewriteColor {

View File

@ -1,4 +1,4 @@
use crate::{Color, GeomBatch, ScreenDims};
use crate::{Color, GeomBatch};
use abstutil::VecMap;
use geom::{Bounds, Polygon, Pt2D};
use lyon::geom::{CubicBezierSegment, LineSegment};
@ -24,7 +24,7 @@ pub fn add_svg(batch: &mut GeomBatch, filename: &str) -> Bounds {
// No offset. I'm not exactly sure how the simplification in usvg works, but this doesn't support
// transforms or strokes or text, just fills. Luckily, all of the files exported from Figma so far
// work just fine.
fn add_svg_inner(batch: &mut GeomBatch, svg_tree: usvg::Tree) -> Result<Bounds, String> {
pub fn add_svg_inner(batch: &mut GeomBatch, svg_tree: usvg::Tree) -> Result<Bounds, String> {
let mut fill_tess = tessellation::FillTessellator::new();
let mut stroke_tess = tessellation::StrokeTessellator::new();
let mut fill_mesh_per_color: VecMap<Color, VertexBuffers<FillVertex, u16>> = VecMap::new();
@ -189,35 +189,3 @@ fn convert_color(paint: &usvg::Paint, opacity: f64) -> Color {
panic!("Unsupported paint {:?}", paint);
}
}
// Returns the dims of the text
pub fn add_text(batch: &mut GeomBatch, text: String) -> ScreenDims {
// If these are large enough, does this work?
let max_w = 9999.0;
let max_h = 9999.0;
let pt = crate::ScreenPt::new(30.0, 30.0);
let color = Color::RED;
let size = 30;
let family = "DejaVu Sans";
let svg = format!(
r##"<svg width="{}" height="{}" viewBox="0 0 {} {}" fill="none" xmlns="http://www.w3.org/2000/svg"><text x="{}" y="{}" fill="{}" font-size="{}" font-family="{}">{}</text></svg>"##,
max_w,
max_h,
max_w,
max_h,
pt.x,
pt.y,
color.to_hex(),
size,
family,
text
);
let svg_tree = usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap();
match add_svg_inner(batch, svg_tree) {
// TODO If the text doesn't start at 0,0 should we just take max_x and max_y here?
Ok(b) => ScreenDims::new(b.width(), b.height()),
Err(err) => panic!("add_text({}): {}", text, err),
}
}

View File

@ -1,5 +1,5 @@
use crate::assets::Assets;
use crate::{Color, GfxCtx, ScreenDims, ScreenPt, ScreenRectangle};
use crate::{Color, GeomBatch, GfxCtx, ScreenDims, ScreenPt, ScreenRectangle};
use geom::{Polygon, Pt2D};
use glium_glyph::glyph_brush::rusttype::Scale;
use glium_glyph::glyph_brush::{FontId, GlyphCruncher};
@ -352,3 +352,75 @@ pub fn draw_text_bubble_mapspace(
g.assets.mapspace_glyphs.borrow_mut().queue(section);
}
}
// TODO Rearrange
impl Text {
pub fn render(self, master_batch: &mut GeomBatch, top_left: ScreenPt) -> ScreenRectangle {
/*if let Some(c) = txt.bg_color {
g.draw_polygon(
c,
&Polygon::rectangle(total_dims.width, total_dims.height)
.translate(top_left.x, top_left.y),
);
}*/
let mut y = top_left.y;
let mut max_width = 0.0_f64;
for (line_color, line) in self.lines {
let mut x = top_left.x;
let mut line_batch = GeomBatch::new();
for piece in line {
let mini_batch = render_text(piece);
let dims = mini_batch.get_dims();
for (color, poly) in mini_batch.consume() {
line_batch.push(color, poly.translate(x, y));
}
x += dims.width;
}
let line_dims = line_batch.get_dims();
if let Some(c) = line_color {
master_batch.push(
c,
Polygon::rectangle(x - top_left.x, line_dims.height).translate(top_left.x, y),
);
}
for (color, poly) in line_batch.consume() {
master_batch.push(color, poly.translate(0.0, line_dims.height));
}
y += line_dims.height;
max_width = max_width.max(x - top_left.x);
}
ScreenRectangle::top_left(top_left, ScreenDims::new(max_width, y - top_left.y))
}
}
fn render_text(txt: TextSpan) -> GeomBatch {
// If these are large enough, does this work?
let max_w = 9999.0;
let max_h = 9999.0;
let svg = format!(
r##"<svg width="{}" height="{}" viewBox="0 0 {} {}" fill="none" xmlns="http://www.w3.org/2000/svg"><text x="0" y="0" fill="{}" font-size="{}" font-family="{}">{}</text></svg>"##,
max_w,
max_h,
max_w,
max_h,
txt.fg_color.to_hex(),
// TODO Plumb through default font size?
txt.size.unwrap_or(30),
// TODO Make these work
"DejaVu Sans",
txt.text
);
let svg_tree = usvg::Tree::from_str(&svg, &usvg::Options::default()).unwrap();
let mut batch = GeomBatch::new();
match crate::svg::add_svg_inner(&mut batch, svg_tree) {
Ok(_) => batch,
Err(err) => panic!("render_text({}): {}", txt.text, err),
}
}

View File

@ -235,7 +235,10 @@ impl Button {
);
let mut hovered_batch = GeomBatch::from(vec![(selected_bg_color, geom.clone())]);
// TODO Testing things out
hovered_batch.add_text(tooltip.to_string());
let mut test = Text::from(Line(tooltip).fg(Color::PURPLE));
test.add(Line("123").fg(Color::RED));
test.append(Line(" space").fg(Color::BLUE));
test.render(&mut hovered_batch, ScreenPt::new(5.0, 5.0));
let hovered = DrawBoth::new(ctx, hovered_batch, draw_text);
Button::new(normal, hovered, hotkey, tooltip, geom)