mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-24 06:55:40 +03:00
Add method to GeomBatch to prefer cached flavor of loading svg. (#493)
It was my intention when introducing the cached vs uncached flavors that we'd prefer the cached flavor for anything referencing bytes from a file, like icons and UI glyphs, and that we'd only use the uncached flavor for dynamic things like "face" generation.
This commit is contained in:
parent
8a856d6608
commit
38dc60fbea
@ -191,9 +191,10 @@ pub fn trips(
|
||||
},
|
||||
{
|
||||
// TODO Maybe generalize ImageSource::Bytes beyond just buttons
|
||||
let mut icon = GeomBatch::from_uncached_svg_contents(include_bytes!(
|
||||
"../../../widgetry/icons/arrow_drop_down.svg"
|
||||
))
|
||||
let mut icon = GeomBatch::load_svg_bytes(
|
||||
&ctx.prerender,
|
||||
widgetry::include_labeled_bytes!("../../../widgetry/icons/arrow_drop_down.svg"),
|
||||
)
|
||||
.autocrop()
|
||||
.color(RewriteColor::ChangeAll(Color::WHITE))
|
||||
.scale(1.5);
|
||||
@ -276,7 +277,7 @@ pub fn bio(
|
||||
|
||||
let mut svg_data = Vec::new();
|
||||
svg_face::generate_face(&mut svg_data, &mut rng).unwrap();
|
||||
let batch = GeomBatch::from_uncached_svg_contents(&svg_data).autocrop();
|
||||
let batch = GeomBatch::load_svg_bytes_uncached(&svg_data).autocrop();
|
||||
let dims = batch.get_dims();
|
||||
let batch = batch.scale((200.0 / dims.width).min(200.0 / dims.height));
|
||||
rows.push(Widget::draw_batch(ctx, batch).centered_horiz());
|
||||
|
@ -77,9 +77,12 @@ impl Picker {
|
||||
Widget::row(vec![
|
||||
Widget::draw_batch(
|
||||
ctx,
|
||||
GeomBatch::from_uncached_svg_contents(include_bytes!(
|
||||
"../../widgetry/icons/arrow_keys.svg"
|
||||
)),
|
||||
GeomBatch::load_svg_bytes(
|
||||
&ctx.prerender,
|
||||
widgetry::include_labeled_bytes!(
|
||||
"../../widgetry/icons/arrow_keys.svg"
|
||||
),
|
||||
),
|
||||
),
|
||||
Text::from_all(vec![
|
||||
Line("arrow keys").fg(ctx.style().hotkey_color),
|
||||
|
@ -135,16 +135,30 @@ impl GeomBatch {
|
||||
ScreenDims::new(bounds.width(), bounds.height())
|
||||
}
|
||||
|
||||
/// Returns a batch containing a parsed SVG string.
|
||||
pub fn from_uncached_svg_contents(raw: &[u8]) -> GeomBatch {
|
||||
svg::load_svg_from_bytes_uncached(raw).unwrap().0
|
||||
}
|
||||
|
||||
/// Returns a batch containing an SVG from a file.
|
||||
pub fn load_svg<P: AsRef<Prerender>>(prerender: &P, filename: &str) -> GeomBatch {
|
||||
svg::load_svg(prerender.as_ref(), filename).0
|
||||
}
|
||||
|
||||
/// Returns a GeomBatch from the bytes of a utf8 encoded SVG string.
|
||||
pub fn load_svg_bytes<P: AsRef<Prerender>>(
|
||||
prerender: &P,
|
||||
labeled_bytes: (&str, &[u8]),
|
||||
) -> GeomBatch {
|
||||
svg::load_svg_bytes(prerender.as_ref(), labeled_bytes.0, labeled_bytes.1)
|
||||
.expect("invalid svg bytes")
|
||||
.0
|
||||
}
|
||||
|
||||
/// Returns a GeomBatch from the bytes of a utf8 encoded SVG string.
|
||||
///
|
||||
/// Prefer to use `load_svg_bytes`, which caches the parsed SVG, unless
|
||||
/// the SVG was dynamically generated, or is otherwise unlikely to be
|
||||
/// reused.
|
||||
pub fn load_svg_bytes_uncached(raw: &[u8]) -> GeomBatch {
|
||||
svg::load_svg_from_bytes_uncached(raw).unwrap().0
|
||||
}
|
||||
|
||||
/// Transforms all colors in a batch.
|
||||
pub fn color(mut self, transformation: RewriteColor) -> GeomBatch {
|
||||
for (fancy, _, _) in &mut self.list {
|
||||
|
@ -289,7 +289,7 @@ fn setup_scrollable_canvas(ctx: &mut EventCtx) -> Drawable {
|
||||
for i in 0..10 {
|
||||
let mut svg_data = Vec::new();
|
||||
svg_face::generate_face(&mut svg_data, &mut rng).unwrap();
|
||||
let face = GeomBatch::from_uncached_svg_contents(&svg_data).autocrop();
|
||||
let face = GeomBatch::load_svg_bytes_uncached(&svg_data).autocrop();
|
||||
let dims = face.get_dims();
|
||||
batch.append(
|
||||
face.scale((200.0 / dims.width).min(200.0 / dims.height))
|
||||
|
Loading…
Reference in New Issue
Block a user