Add a way to pass font as bytes

This commit is contained in:
hodasemi 2023-01-18 12:24:56 +01:00
parent 0501a4edde
commit e8401e0d6e

View file

@ -23,13 +23,20 @@ use std::{ops::Deref, sync::Weak};
use paste::paste; use paste::paste;
#[derive(Deserialize, Serialize, Clone, Debug)] #[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 // default button textures
pub menu_button: AssetPath, pub menu_button: AssetPath,
pub menu_button_selected: AssetPath, pub menu_button_selected: AssetPath,
// path to the alphabet image // path to the alphabet image
pub font_path: AssetPath, #[serde(borrow)]
pub font_path: Font<'a>,
// sound info // sound info
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
@ -41,7 +48,7 @@ pub struct GuiHandlerCreateInfo {
pub resource_directory: AssetPath, pub resource_directory: AssetPath,
} }
impl GuiHandlerCreateInfo { impl<'a> GuiHandlerCreateInfo<'a> {
pub const fn default() -> Self { pub const fn default() -> Self {
GuiHandlerCreateInfo { GuiHandlerCreateInfo {
// default button textures // default button textures
@ -49,7 +56,7 @@ impl GuiHandlerCreateInfo {
menu_button_selected: AssetPath::default(), menu_button_selected: AssetPath::default(),
// path to the alphabet image // path to the alphabet image
font_path: AssetPath::default(), font_path: Font::Path(AssetPath::default()),
// sound info // sound info
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
@ -156,7 +163,7 @@ pub struct GuiHandler {
impl GuiHandler { impl GuiHandler {
pub fn new( pub fn new(
gui_handler_create_info: GuiHandlerCreateInfo, gui_handler_create_info: GuiHandlerCreateInfo<'_>,
context: &Arc<dyn ContextInterface>, context: &Arc<dyn ContextInterface>,
) -> Result<Arc<GuiHandler>> { ) -> Result<Arc<GuiHandler>> {
let device = context.device(); let device = context.device();
@ -1264,13 +1271,16 @@ impl GuiHandler {
device: &Arc<Device>, device: &Arc<Device>,
queue: &Arc<Mutex<Queue>>, queue: &Arc<Mutex<Queue>>,
descriptor_layout: Arc<DescriptorSetLayout>, descriptor_layout: Arc<DescriptorSetLayout>,
path: AssetPath, font: Font<'_>,
) -> Result<(Arc<Image>, Arc<DescriptorPool>, Arc<DescriptorSet>)> { ) -> Result<(Arc<Image>, Arc<DescriptorPool>, Arc<DescriptorSet>)> {
let texture = Image::from_file(path)? let texture = match font {
.format(VK_FORMAT_R8G8B8A8_UNORM) Font::Path(path) => Image::from_file(path)?,
.max_mip_map_levels() Font::Bytes(bytes) => Image::from_slice(bytes)?,
.attach_sampler(Sampler::pretty_sampler().build(device)?) }
.build(device, queue)?; .format(VK_FORMAT_R8G8B8A8_UNORM)
.max_mip_map_levels()
.attach_sampler(Sampler::pretty_sampler().build(device)?)
.build(device, queue)?;
let descriptor_pool = DescriptorPool::builder() let descriptor_pool = DescriptorPool::builder()
.set_layout(descriptor_layout) .set_layout(descriptor_layout)