2019-04-26 04:29:35 +00:00
|
|
|
use super::graphic::{Graphic, GraphicCache, Id as GraphicId};
|
|
|
|
use crate::{
|
2019-11-19 19:22:56 +00:00
|
|
|
render::{Renderer, Texture},
|
2019-04-26 04:29:35 +00:00
|
|
|
Error,
|
|
|
|
};
|
|
|
|
use conrod_core::text::GlyphCache;
|
|
|
|
use vek::*;
|
|
|
|
|
2019-07-03 02:31:20 +00:00
|
|
|
// Multiplied by current window size
|
|
|
|
const GLYPH_CACHE_SIZE: u16 = 1;
|
|
|
|
// Glyph cache tolerances
|
|
|
|
const SCALE_TOLERANCE: f32 = 0.1;
|
|
|
|
const POSITION_TOLERANCE: f32 = 0.1;
|
|
|
|
|
2019-04-26 04:29:35 +00:00
|
|
|
pub struct Cache {
|
|
|
|
glyph_cache: GlyphCache<'static>,
|
2019-11-17 22:41:00 +00:00
|
|
|
glyph_cache_tex: Texture,
|
2019-04-26 04:29:35 +00:00
|
|
|
graphic_cache: GraphicCache,
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Should functions be returning UiError instead of Error?
|
|
|
|
impl Cache {
|
|
|
|
pub fn new(renderer: &mut Renderer) -> Result<Self, Error> {
|
|
|
|
let (w, h) = renderer.get_resolution().into_tuple();
|
|
|
|
|
2019-07-03 01:21:08 +00:00
|
|
|
let max_texture_size = renderer.max_texture_size();
|
|
|
|
|
2019-07-03 02:31:20 +00:00
|
|
|
let glyph_cache_dims =
|
2019-09-18 16:46:12 +00:00
|
|
|
Vec2::new(w, h).map(|e| (e * GLYPH_CACHE_SIZE).min(max_texture_size as u16).max(512));
|
2019-07-03 01:21:08 +00:00
|
|
|
|
2019-04-26 04:29:35 +00:00
|
|
|
Ok(Self {
|
|
|
|
glyph_cache: GlyphCache::builder()
|
2019-07-03 01:21:08 +00:00
|
|
|
.dimensions(glyph_cache_dims.x as u32, glyph_cache_dims.y as u32)
|
2019-04-26 04:29:35 +00:00
|
|
|
.scale_tolerance(SCALE_TOLERANCE)
|
|
|
|
.position_tolerance(POSITION_TOLERANCE)
|
|
|
|
.build(),
|
2019-07-03 01:21:08 +00:00
|
|
|
glyph_cache_tex: renderer.create_dynamic_texture(glyph_cache_dims.map(|e| e as u16))?,
|
2019-10-20 01:28:30 +00:00
|
|
|
graphic_cache: GraphicCache::new(renderer),
|
2019-04-26 04:29:35 +00:00
|
|
|
})
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
|
|
|
pub fn glyph_cache_tex(&self) -> &Texture { &self.glyph_cache_tex }
|
|
|
|
|
2019-11-17 22:41:00 +00:00
|
|
|
pub fn glyph_cache_mut_and_tex(&mut self) -> (&mut GlyphCache<'static>, &Texture) {
|
2019-04-26 04:29:35 +00:00
|
|
|
(&mut self.glyph_cache, &self.glyph_cache_tex)
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
|
|
|
pub fn graphic_cache(&self) -> &GraphicCache { &self.graphic_cache }
|
|
|
|
|
|
|
|
pub fn graphic_cache_mut(&mut self) -> &mut GraphicCache { &mut self.graphic_cache }
|
|
|
|
|
2019-04-26 04:29:35 +00:00
|
|
|
pub fn add_graphic(&mut self, graphic: Graphic) -> GraphicId {
|
|
|
|
self.graphic_cache.add_graphic(graphic)
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-09 19:28:05 +00:00
|
|
|
pub fn replace_graphic(&mut self, id: GraphicId, graphic: Graphic) {
|
|
|
|
self.graphic_cache.replace_graphic(id, graphic)
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-07-03 02:31:20 +00:00
|
|
|
// Resizes and clears the GraphicCache
|
2019-10-20 01:28:30 +00:00
|
|
|
pub fn resize_graphic_cache(&mut self, renderer: &mut Renderer) {
|
|
|
|
self.graphic_cache.clear_cache(renderer);
|
2019-07-03 02:31:20 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-07-03 02:31:20 +00:00
|
|
|
// Resizes and clears the GlyphCache
|
|
|
|
pub fn resize_glyph_cache(&mut self, renderer: &mut Renderer) -> Result<(), Error> {
|
2019-07-03 01:21:08 +00:00
|
|
|
let max_texture_size = renderer.max_texture_size();
|
2019-07-03 02:31:20 +00:00
|
|
|
let cache_dims = renderer
|
|
|
|
.get_resolution()
|
2019-09-18 16:46:12 +00:00
|
|
|
.map(|e| (e * GLYPH_CACHE_SIZE).min(max_texture_size as u16).max(512));
|
2019-07-03 02:31:20 +00:00
|
|
|
self.glyph_cache = GlyphCache::builder()
|
|
|
|
.dimensions(cache_dims.x as u32, cache_dims.y as u32)
|
|
|
|
.scale_tolerance(SCALE_TOLERANCE)
|
|
|
|
.position_tolerance(POSITION_TOLERANCE)
|
|
|
|
.build();
|
|
|
|
self.glyph_cache_tex = renderer.create_dynamic_texture(cache_dims.map(|e| e as u16))?;
|
|
|
|
Ok(())
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
|
|
|
}
|