veloren_voxygen/ui/ice/
cache.rs

1use super::graphic::{Graphic, GraphicCache, Id as GraphicId};
2use crate::{
3    error::Error,
4    render::{Renderer, Texture, UiTextureBindGroup},
5};
6use common::assets::{self, AssetExt};
7use glyph_brush::GlyphBrushBuilder;
8use std::cell::{RefCell, RefMut};
9use vek::*;
10
11// TODO: probably make cache fields where we have mut getters into just public
12// fields
13
14// Multiplied by current window size
15const GLYPH_CACHE_SIZE: u32 = 1;
16// Glyph cache tolerances
17// TODO: consider scaling based on dpi as well as providing as an option to the
18// user
19const SCALE_TOLERANCE: f32 = 0.5;
20const POSITION_TOLERANCE: f32 = 0.5;
21
22type GlyphBrush = glyph_brush::GlyphBrush<(Aabr<f32>, Aabr<f32>), ()>;
23
24// TODO: might not need pub
25pub type Font = glyph_brush::ab_glyph::FontArc;
26
27pub fn load_font(specifier: &str) -> Font { Font::load_expect(specifier).cloned() }
28
29#[derive(Clone, Copy, Default)]
30pub struct FontId(pub(super) glyph_brush::FontId);
31
32pub struct Cache {
33    glyph_brush: RefCell<GlyphBrush>,
34    glyph_cache_tex: (Texture, UiTextureBindGroup),
35    graphic_cache: GraphicCache,
36}
37
38// TODO: Should functions be returning UiError instead of Error?
39impl Cache {
40    pub fn new(renderer: &mut Renderer, default_font: Font) -> Result<Self, Error> {
41        let (w, h) = renderer.resolution().into_tuple();
42
43        let max_texture_size = renderer.max_texture_size();
44
45        let glyph_cache_dims =
46            Vec2::new(w, h).map(|e| (e * GLYPH_CACHE_SIZE).clamp(512, max_texture_size));
47
48        let glyph_brush = GlyphBrushBuilder::using_font(default_font)
49            .initial_cache_size((glyph_cache_dims.x, glyph_cache_dims.y))
50            .draw_cache_scale_tolerance(SCALE_TOLERANCE)
51            .draw_cache_position_tolerance(POSITION_TOLERANCE)
52            .build();
53
54        let glyph_cache_tex = {
55            let tex = renderer.create_dynamic_texture(glyph_cache_dims);
56            let bind = renderer.ui_bind_texture(&tex);
57            (tex, bind)
58        };
59
60        Ok(Self {
61            glyph_brush: RefCell::new(glyph_brush),
62            glyph_cache_tex,
63            graphic_cache: GraphicCache::new(renderer),
64        })
65    }
66
67    pub fn glyph_cache_tex(&self) -> (&Texture, &UiTextureBindGroup) {
68        (&self.glyph_cache_tex.0, &self.glyph_cache_tex.1)
69    }
70
71    pub fn glyph_cache_mut_and_tex(&mut self) -> (&mut GlyphBrush, &(Texture, UiTextureBindGroup)) {
72        (self.glyph_brush.get_mut(), &self.glyph_cache_tex)
73    }
74
75    pub fn glyph_cache_mut(&mut self) -> &mut GlyphBrush { self.glyph_brush.get_mut() }
76
77    pub fn glyph_calculator(&self) -> RefMut<GlyphBrush> { self.glyph_brush.borrow_mut() }
78
79    // TODO: consider not re-adding default font
80    pub fn add_font(&mut self, font: RawFont) -> FontId {
81        let font = Font::try_from_vec(font.0).unwrap();
82        let id = self.glyph_brush.get_mut().add_font(font);
83        FontId(id)
84    }
85
86    /// Allows clearing out the fonts when switching languages
87    pub fn clear_fonts(&mut self, default_font: Font) {
88        self.glyph_brush = RefCell::new(
89            self.glyph_brush
90                .get_mut()
91                .to_builder()
92                .replace_fonts(|mut fonts| {
93                    fonts.clear();
94                    fonts.push(default_font);
95                    fonts
96                })
97                .build(),
98        );
99    }
100
101    pub fn graphic_cache(&self) -> &GraphicCache { &self.graphic_cache }
102
103    pub fn graphic_cache_mut(&mut self) -> &mut GraphicCache { &mut self.graphic_cache }
104
105    pub fn add_graphic(&mut self, graphic: Graphic) -> GraphicId {
106        self.graphic_cache.add_graphic(graphic)
107    }
108
109    pub fn replace_graphic(&mut self, id: GraphicId, graphic: Graphic) {
110        self.graphic_cache.replace_graphic(id, graphic)
111    }
112
113    // TODO: combine resize functions
114    // Resizes and clears the GraphicCache
115    pub fn resize_graphic_cache(&mut self, renderer: &mut Renderer) {
116        self.graphic_cache.clear_cache(renderer);
117    }
118
119    // Resizes and clears the GlyphCache
120    pub fn resize_glyph_cache(&mut self, renderer: &mut Renderer) -> Result<(), Error> {
121        let max_texture_size = renderer.max_texture_size();
122        let cache_dims = renderer
123            .resolution()
124            .map(|e| (e * GLYPH_CACHE_SIZE).clamp(512, max_texture_size));
125        let glyph_brush = self.glyph_brush.get_mut();
126        *glyph_brush = glyph_brush
127            .to_builder()
128            .initial_cache_size((cache_dims.x, cache_dims.y))
129            .build();
130
131        self.glyph_cache_tex = {
132            let tex = renderer.create_dynamic_texture(cache_dims);
133            let bind = renderer.ui_bind_texture(&tex);
134            (tex, bind)
135        };
136
137        Ok(())
138    }
139}
140
141// TODO: use font type instead of raw vec once we convert to full iced
142#[derive(Clone)]
143pub struct RawFont(pub Vec<u8>);
144
145impl From<Vec<u8>> for RawFont {
146    fn from(raw: Vec<u8>) -> RawFont { RawFont(raw) }
147}
148
149impl assets::Asset for RawFont {
150    type Loader = assets::LoadFrom<Vec<u8>, assets::BytesLoader>;
151
152    const EXTENSION: &'static str = "ttf";
153}