From e8401e0d6e5a0579c88df23f75bf35038e3bb61d Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 18 Jan 2023 12:24:56 +0100 Subject: [PATCH] Add a way to pass font as bytes --- src/guihandler/guihandler.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/guihandler/guihandler.rs b/src/guihandler/guihandler.rs index 4b41030..2737c9f 100644 --- a/src/guihandler/guihandler.rs +++ b/src/guihandler/guihandler.rs @@ -23,13 +23,20 @@ use std::{ops::Deref, sync::Weak}; use paste::paste; #[derive(Deserialize, Serialize, Clone, Debug)] -pub struct GuiHandlerCreateInfo { +pub enum Font<'a> { + Path(AssetPath), + Bytes(&'a [u8]), +} + +#[derive(Deserialize, Serialize, Clone, Debug)] +pub struct GuiHandlerCreateInfo<'a> { // default button textures pub menu_button: AssetPath, pub menu_button_selected: AssetPath, // path to the alphabet image - pub font_path: AssetPath, + #[serde(borrow)] + pub font_path: Font<'a>, // sound info #[cfg(feature = "audio")] @@ -41,7 +48,7 @@ pub struct GuiHandlerCreateInfo { pub resource_directory: AssetPath, } -impl GuiHandlerCreateInfo { +impl<'a> GuiHandlerCreateInfo<'a> { pub const fn default() -> Self { GuiHandlerCreateInfo { // default button textures @@ -49,7 +56,7 @@ impl GuiHandlerCreateInfo { menu_button_selected: AssetPath::default(), // path to the alphabet image - font_path: AssetPath::default(), + font_path: Font::Path(AssetPath::default()), // sound info #[cfg(feature = "audio")] @@ -156,7 +163,7 @@ pub struct GuiHandler { impl GuiHandler { pub fn new( - gui_handler_create_info: GuiHandlerCreateInfo, + gui_handler_create_info: GuiHandlerCreateInfo<'_>, context: &Arc, ) -> Result> { let device = context.device(); @@ -1264,13 +1271,16 @@ impl GuiHandler { device: &Arc, queue: &Arc>, descriptor_layout: Arc, - path: AssetPath, + font: Font<'_>, ) -> Result<(Arc, Arc, Arc)> { - let texture = Image::from_file(path)? - .format(VK_FORMAT_R8G8B8A8_UNORM) - .max_mip_map_levels() - .attach_sampler(Sampler::pretty_sampler().build(device)?) - .build(device, queue)?; + let texture = match font { + Font::Path(path) => Image::from_file(path)?, + Font::Bytes(bytes) => Image::from_slice(bytes)?, + } + .format(VK_FORMAT_R8G8B8A8_UNORM) + .max_mip_map_levels() + .attach_sampler(Sampler::pretty_sampler().build(device)?) + .build(device, queue)?; let descriptor_pool = DescriptorPool::builder() .set_layout(descriptor_layout)