cutover mapspace text quickly (without doing the scaling down yet.

delete lots of stuff.
This commit is contained in:
Dustin Carlino 2020-02-06 17:29:26 -08:00
parent 234689533a
commit 9355bcf5fd
4 changed files with 14 additions and 134 deletions

View File

@ -1,32 +1,15 @@
use crate::{ScreenDims, Text};
use glium_glyph::glyph_brush::FontId;
use glium_glyph::{GlyphBrush, GlyphBrushBuilder};
use std::cell::RefCell;
// TODO We don't need refcell maybe
pub struct Assets {
pub mapspace_glyphs: RefCell<GlyphBrush<'static, 'static>>,
pub default_line_height: f64,
pub font_size: usize,
}
impl Assets {
pub fn new(display: &glium::Display, font_size: usize) -> Assets {
let dejavu: &[u8] = include_bytes!("assets/DejaVuSans.ttf");
let mapspace_glyphs = GlyphBrushBuilder::using_font_bytes(dejavu)
.params(glium::DrawParameters {
blend: glium::Blend::alpha_blending(),
depth: glium::Depth {
test: glium::DepthTest::IfLessOrEqual,
write: true,
..Default::default()
},
..Default::default()
})
.build(display);
pub fn new(font_size: usize) -> Assets {
let mut a = Assets {
mapspace_glyphs: RefCell::new(mapspace_glyphs),
default_line_height: 0.0,
font_size,
};

View File

@ -1,13 +1,12 @@
use crate::assets::Assets;
use crate::svg;
use crate::{
text, Canvas, Color, EventCtx, HorizontalAlignment, ScreenDims, ScreenPt, ScreenRectangle,
Text, VerticalAlignment,
Canvas, Color, EventCtx, HorizontalAlignment, ScreenDims, ScreenPt, ScreenRectangle, Text,
VerticalAlignment,
};
use geom::{Angle, Bounds, Circle, Distance, Line, Polygon, Pt2D};
use glium::uniforms::{SamplerBehavior, SamplerWrapFunction, UniformValue};
use glium::Surface;
use glium_glyph::glyph_brush::FontId;
use std::cell::Cell;
const MAPSPACE: f32 = 0.0;
@ -272,15 +271,15 @@ impl<'a> GfxCtx<'a> {
pub fn draw_text_at_mapspace(&mut self, txt: &Text, map_pt: Pt2D) {
let dims = self.text_dims(&txt);
text::draw_text_bubble_mapspace(
self,
Pt2D::new(
map_pt.x() - (dims.width / (2.0 * text::SCALE_DOWN)),
map_pt.y() - (dims.height / (2.0 * text::SCALE_DOWN)),
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),
),
txt,
dims,
);
batch.draw(self);
}
pub fn draw_mouse_tooltip(&mut self, txt: &Text) {
@ -331,9 +330,6 @@ impl<'a> GfxCtx<'a> {
pub fn default_line_height(&self) -> f64 {
self.assets.default_line_height
}
pub(crate) fn line_height(&self, font: FontId, font_size: usize) -> f64 {
self.assets.line_height(font, font_size)
}
pub fn text_dims(&self, txt: &Text) -> ScreenDims {
self.assets.text_dims(txt)
}

View File

@ -1,5 +1,5 @@
use crate::assets::Assets;
use crate::{text, widgets, Canvas, Event, EventCtx, GfxCtx, Key, Prerender, UserInput};
use crate::{widgets, Canvas, Event, EventCtx, GfxCtx, Key, Prerender, UserInput};
use geom::Duration;
use glium::glutin;
use std::cell::Cell;
@ -139,27 +139,6 @@ impl<G: GUI> State<G> {
}
let naming_hint = g.naming_hint.take();
// Flush text just once, so that GlyphBrush's internal caching works. We have to assume
// nothing will ever cover up text.
{
let top_left = self
.canvas
.screen_to_map(crate::screen_geom::ScreenPt::new(0.0, 0.0));
let bottom_right = self.canvas.screen_to_map(crate::screen_geom::ScreenPt::new(
self.canvas.window_width,
self.canvas.window_height,
));
let transform = ortho(
(top_left.x() as f32, bottom_right.x() as f32),
(top_left.y() as f32, bottom_right.y() as f32),
text::SCALE_DOWN,
);
self.assets
.mapspace_glyphs
.borrow_mut()
.draw_queued_with_transform(transform, display, g.target);
}
target.finish().unwrap();
naming_hint
}
@ -276,7 +255,7 @@ pub fn run<G: GUI, F: FnOnce(&mut EventCtx) -> G>(settings: Settings, make_gui:
}
let window_size = events_loop.get_primary_monitor().get_dimensions();
let mut canvas = Canvas::new(window_size.width, window_size.height, hidpi_factor);
let assets = Assets::new(&display, settings.default_font_size);
let assets = Assets::new(settings.default_font_size);
let prerender = Prerender {
display: &display,
num_uploads: Cell::new(0),
@ -422,16 +401,3 @@ fn loop_forever<G: GUI>(
}
}
}
fn ortho((left, right): (f32, f32), (bottom, top): (f32, f32), scale: f64) -> [[f32; 4]; 4] {
let s_x = 2.0 / (right - left) / (scale as f32);
let s_y = 2.0 / (top - bottom) / (scale as f32);
let t_x = -(right + left) / (right - left);
let t_y = -(top + bottom) / (top - bottom);
[
[s_x, 0.0, 0.0, 0.0],
[0.0, s_y, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[t_x, t_y, 0.0, 1.0],
]
}

View File

@ -1,9 +1,7 @@
use crate::assets::Assets;
use crate::{Color, GeomBatch, GfxCtx, ScreenDims, ScreenPt, ScreenRectangle};
use geom::{Polygon, Pt2D};
use glium_glyph::glyph_brush::rusttype::Scale;
use crate::{Color, GeomBatch, ScreenDims, ScreenPt, ScreenRectangle};
use geom::Polygon;
use glium_glyph::glyph_brush::FontId;
use glium_glyph::glyph_brush::{SectionText, VariedSection};
use textwrap;
const FG_COLOR: Color = Color::WHITE;
@ -15,7 +13,6 @@ pub const INACTIVE_CHOICE_COLOR: Color = Color::grey(0.4);
// TODO Don't do this!
const MAX_CHAR_WIDTH: f64 = 25.0;
pub const SCALE_DOWN: f64 = 60.0;
// These're hardcoded for simplicity; this list doesn't change much.
const DEJA_VU: FontId = FontId(0);
@ -194,69 +191,7 @@ impl Text {
let rect = self.clone().render(&mut batch, ScreenPt::new(0.0, 0.0));
rect.dims()
}
}
pub fn draw_text_bubble_mapspace(
g: &mut GfxCtx,
top_left: Pt2D,
txt: &Text,
// Callers almost always calculate this anyway
total_dims: ScreenDims,
) {
if let Some(c) = txt.bg_color {
g.draw_polygon(
c,
&Polygon::rectangle(
total_dims.width / SCALE_DOWN,
total_dims.height / SCALE_DOWN,
)
.translate(top_left.x(), top_left.y()),
);
}
let mut y = top_left.y();
for (line_color, line) in &txt.lines {
let mut max_size = 0;
let section = VariedSection {
// This in map-space, but the transform matrix for mapspace_glyphs will take care of
// it.
screen_position: ((top_left.x() * SCALE_DOWN) as f32, (y * SCALE_DOWN) as f32),
z: 0.1,
text: line
.iter()
.map(|span| {
max_size = max_size.max(span.size.unwrap_or(g.assets.font_size));
SectionText {
text: &span.text,
color: match span.fg_color {
Color::RGBA(r, g, b, a) => [r, g, b, a],
_ => unreachable!(),
},
scale: Scale::uniform(span.size.unwrap_or(g.assets.font_size) as f32),
..SectionText::default()
}
})
.collect(),
..VariedSection::default()
};
// TODO Assume the same font for all spans
let height = g.line_height(line[0].font, max_size) / SCALE_DOWN;
if let Some(c) = line_color {
g.draw_polygon(
*c,
&Polygon::rectangle(total_dims.width / SCALE_DOWN, height)
.translate(top_left.x(), y),
);
}
y += height;
g.assets.mapspace_glyphs.borrow_mut().queue(section);
}
}
// TODO Rearrange
impl Text {
pub fn render(self, output_batch: &mut GeomBatch, top_left: ScreenPt) -> ScreenRectangle {
// TODO Bad guess
let empty_line_height = 30.0;