2020-05-18 06:13:13 +00:00
|
|
|
use super::graphic::{Graphic, GraphicCache, Id as GraphicId};
|
2020-04-22 01:29:59 +00:00
|
|
|
use crate::{
|
|
|
|
render::{Renderer, Texture},
|
|
|
|
Error,
|
|
|
|
};
|
2020-05-18 06:13:13 +00:00
|
|
|
use glyph_brush::GlyphBrushBuilder;
|
|
|
|
use std::cell::{RefCell, RefMut};
|
2020-04-22 01:29:59 +00:00
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
// Multiplied by current window size
|
|
|
|
const GLYPH_CACHE_SIZE: u16 = 1;
|
|
|
|
// Glyph cache tolerances
|
2020-05-29 04:40:22 +00:00
|
|
|
const SCALE_TOLERANCE: f32 = 0.5; // Note: Changed from 0.1 change back if not decent
|
2020-04-22 01:29:59 +00:00
|
|
|
const POSITION_TOLERANCE: f32 = 0.1;
|
|
|
|
|
2020-05-29 04:40:22 +00:00
|
|
|
type GlyphBrush = glyph_brush::GlyphBrush<(Aabr<f32>, Aabr<f32>), ()>;
|
2020-04-22 01:29:59 +00:00
|
|
|
|
2020-05-29 04:40:22 +00:00
|
|
|
pub type Font = glyph_brush::ab_glyph::FontArc;
|
2020-04-22 01:29:59 +00:00
|
|
|
|
|
|
|
pub struct Cache {
|
2020-05-18 06:13:13 +00:00
|
|
|
glyph_brush: RefCell<GlyphBrush>,
|
2020-04-22 01:29:59 +00:00
|
|
|
glyph_cache_tex: Texture,
|
|
|
|
graphic_cache: GraphicCache,
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Should functions be returning UiError instead of Error?
|
|
|
|
impl Cache {
|
|
|
|
pub fn new(renderer: &mut Renderer, default_font: Font) -> Result<Self, Error> {
|
|
|
|
let (w, h) = renderer.get_resolution().into_tuple();
|
|
|
|
|
|
|
|
let max_texture_size = renderer.max_texture_size();
|
|
|
|
|
|
|
|
let glyph_cache_dims =
|
|
|
|
Vec2::new(w, h).map(|e| (e * GLYPH_CACHE_SIZE).min(max_texture_size as u16).max(512));
|
|
|
|
|
2020-05-18 06:13:13 +00:00
|
|
|
let glyph_brush = GlyphBrushBuilder::using_font(default_font)
|
|
|
|
.initial_cache_size((glyph_cache_dims.x as u32, glyph_cache_dims.y as u32))
|
2020-05-29 04:40:22 +00:00
|
|
|
.draw_cache_scale_tolerance(SCALE_TOLERANCE)
|
|
|
|
.draw_cache_position_tolerance(POSITION_TOLERANCE)
|
2020-05-18 06:13:13 +00:00
|
|
|
.build();
|
|
|
|
|
2020-04-22 01:29:59 +00:00
|
|
|
Ok(Self {
|
2020-05-18 06:13:13 +00:00
|
|
|
glyph_brush: RefCell::new(glyph_brush),
|
2020-04-22 01:29:59 +00:00
|
|
|
glyph_cache_tex: renderer.create_dynamic_texture(glyph_cache_dims.map(|e| e as u16))?,
|
|
|
|
graphic_cache: GraphicCache::new(renderer),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn glyph_cache_tex(&self) -> &Texture { &self.glyph_cache_tex }
|
|
|
|
|
|
|
|
pub fn glyph_cache_mut_and_tex(&mut self) -> (&mut GlyphBrush, &Texture) {
|
2020-05-18 06:13:13 +00:00
|
|
|
(self.glyph_brush.get_mut(), &self.glyph_cache_tex)
|
2020-04-22 01:29:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 06:13:13 +00:00
|
|
|
pub fn glyph_cache_mut(&mut self) -> &mut GlyphBrush { self.glyph_brush.get_mut() }
|
|
|
|
|
|
|
|
pub fn glyph_calculator(&self) -> RefMut<GlyphBrush> { self.glyph_brush.borrow_mut() }
|
2020-04-22 01:29:59 +00:00
|
|
|
|
|
|
|
// TODO: add font fn
|
|
|
|
|
|
|
|
pub fn graphic_cache(&self) -> &GraphicCache { &self.graphic_cache }
|
|
|
|
|
|
|
|
pub fn graphic_cache_mut(&mut self) -> &mut GraphicCache { &mut self.graphic_cache }
|
|
|
|
|
|
|
|
pub fn add_graphic(&mut self, graphic: Graphic) -> GraphicId {
|
|
|
|
self.graphic_cache.add_graphic(graphic)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn replace_graphic(&mut self, id: GraphicId, graphic: Graphic) {
|
|
|
|
self.graphic_cache.replace_graphic(id, graphic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resizes and clears the GraphicCache
|
|
|
|
pub fn resize_graphic_cache(&mut self, renderer: &mut Renderer) {
|
|
|
|
self.graphic_cache.clear_cache(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resizes and clears the GlyphCache
|
|
|
|
pub fn resize_glyph_cache(&mut self, renderer: &mut Renderer) -> Result<(), Error> {
|
|
|
|
let max_texture_size = renderer.max_texture_size();
|
|
|
|
let cache_dims = renderer
|
|
|
|
.get_resolution()
|
|
|
|
.map(|e| (e * GLYPH_CACHE_SIZE).min(max_texture_size as u16).max(512));
|
2020-05-18 06:13:13 +00:00
|
|
|
let glyph_brush = self.glyph_brush.get_mut();
|
|
|
|
*glyph_brush = glyph_brush
|
2020-04-22 01:29:59 +00:00
|
|
|
.to_builder()
|
|
|
|
.initial_cache_size((cache_dims.x as u32, cache_dims.y as u32))
|
|
|
|
.build();
|
2020-05-18 06:13:13 +00:00
|
|
|
|
2020-04-22 01:29:59 +00:00
|
|
|
self.glyph_cache_tex = renderer.create_dynamic_texture(cache_dims.map(|e| e as u16))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|