use crate::*;
use wasset::*;
pub type AssetId = WassetId;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Asset {
Image {
data: Vec<u8>,
format: ImageFormat,
},
Text {
value: String,
},
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ImageFormat {
Jpeg,
Png,
}
#[system_trait(host)]
pub trait AssetManager: 'static {
fn try_get_raw(&self, id: AssetId) -> Result<Asset, EngineError>;
fn try_get_ui_texture(&self, id: AssetId) -> Result<UiTextureIndex, EngineError>;
}
impl dyn AssetManager {
#[cfg(feature = "toml")]
pub fn get_from_toml<T: 'static + serde::de::DeserializeOwned>(&self, id: AssetId) -> T {
match self.try_get_raw(id).expect("Failed to get asset.") {
Asset::Text { value } => {
toml::from_str(&value).expect("Failed to deserialize TOML map.")
}
x => panic!("Expected text asset; got {x:?}"),
}
}
pub fn get_ui_texture(&self, id: AssetId) -> UiTextureIndex {
self.try_get_ui_texture(id)
.expect("Failed to load image asset.")
}
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct UiTextureIndex {
pub id: u64,
pub width: f32,
pub height: f32,
}
#[cfg(feature = "egui")]
impl<'a> From<UiTextureIndex> for egui_wings::egui::ImageSource<'a> {
fn from(value: UiTextureIndex) -> Self {
Self::Texture(egui_wings::egui::load::SizedTexture {
id: egui_wings::egui::TextureId::User(value.id),
size: egui_wings::egui::Vec2 {
x: value.width,
y: value.height,
},
})
}
}