| 146 | NineSlice(UiNineSliceDraw), |
| 147 | Label(UiLabelDraw), |
| 148 | TextEdit(UiTextEditDraw), |
| 149 | } |
| 150 | |
| 151 | pub struct UiRenderer { |
| 152 | nodes: AHashMap<NodeID, UiDraw>, |
| 153 | revision: u64, |
| 154 | painter: EpaintUiPainter, |
| 155 | static_font_lookup: Option<crate::StaticFontLookup>, |
| 156 | default_font: perro_ui::UiFont, |
| 157 | // Path hashes already handed to the painter; labels re-submit every |
| 158 | // change, so gate here keeps registration off the per-frame path. |
| 159 | registered_resource_fonts: ahash::AHashSet<u64>, |
| 160 | } |
| 161 | |
| 162 | impl Default for UiRenderer { |
| 163 | fn default() -> Self { |
| 164 | Self::new() |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | impl UiRenderer { |
| 169 | pub fn new() -> Self { |
| 170 | Self { |
| 171 | nodes: AHashMap::new(), |
| 172 | revision: 0, |
| 173 | painter: EpaintUiPainter::new(), |
| 174 | static_font_lookup: None, |
| 175 | default_font: perro_ui::UiFont::Default, |
| 176 | registered_resource_fonts: ahash::AHashSet::new(), |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | pub fn submit(&mut self, mut command: UiCommand) { |
| 181 | match &mut command { |
| 182 | UiCommand::UpsertLabel { font, .. } | UiCommand::UpsertTextEdit { font, .. } |
| 183 | if matches!(font, perro_ui::UiFont::Default) => |
| 184 | { |
| 185 | *font = self.default_font.clone(); |
| 186 | } |
| 187 | _ => {} |
| 188 | } |
| 189 | match &command { |
| 190 | UiCommand::UpsertLabel { |
| 191 | font: perro_ui::UiFont::Resource(path), |
| 192 | .. |
| 193 | } |
| 194 | | UiCommand::UpsertTextEdit { |
| 195 | font: perro_ui::UiFont::Resource(path), |
| 196 | .. |
| 197 | } => { |
| 198 | let hash = perro_ids::string_to_u64(path); |
| 199 | if self.registered_resource_fonts.insert(hash) { |
| 200 | self.painter |
| 201 | .register_resource_font(path, self.static_font_lookup); |
| 202 | } |
| 203 | } |
| 204 | _ => {} |
| 205 | } |