mirror of
https://github.com/bevyengine/bevy
synced 2025-02-16 14:08:32 +00:00
bind the labeled asset type to the actual loaded asset (#1363)
bind the labeled asset type to the actual loaded asset
This commit is contained in:
parent
4904080382
commit
83e30a841a
1 changed files with 28 additions and 10 deletions
|
@ -29,15 +29,15 @@ impl<T> Asset for T where T: TypeUuid + AssetDynamic + TypeUuidDynamic {}
|
|||
|
||||
impl<T> AssetDynamic for T where T: Send + Sync + 'static + TypeUuidDynamic {}
|
||||
|
||||
pub struct LoadedAsset {
|
||||
pub(crate) value: Option<Box<dyn AssetDynamic>>,
|
||||
pub struct LoadedAsset<T: Asset> {
|
||||
pub(crate) value: Option<T>,
|
||||
pub(crate) dependencies: Vec<AssetPath<'static>>,
|
||||
}
|
||||
|
||||
impl LoadedAsset {
|
||||
pub fn new<T: Asset>(value: T) -> Self {
|
||||
impl<T: Asset> LoadedAsset<T> {
|
||||
pub fn new(value: T) -> Self {
|
||||
Self {
|
||||
value: Some(Box::new(value)),
|
||||
value: Some(value),
|
||||
dependencies: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
@ -53,10 +53,27 @@ impl LoadedAsset {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) struct BoxedLoadedAsset {
|
||||
pub(crate) value: Option<Box<dyn AssetDynamic>>,
|
||||
pub(crate) dependencies: Vec<AssetPath<'static>>,
|
||||
}
|
||||
|
||||
impl<T: Asset> From<LoadedAsset<T>> for BoxedLoadedAsset {
|
||||
fn from(asset: LoadedAsset<T>) -> Self {
|
||||
BoxedLoadedAsset {
|
||||
value: match asset.value {
|
||||
Some(value) => Some(Box::new(value)),
|
||||
None => None,
|
||||
},
|
||||
dependencies: asset.dependencies,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LoadContext<'a> {
|
||||
pub(crate) ref_change_channel: &'a RefChangeChannel,
|
||||
pub(crate) asset_io: &'a dyn AssetIo,
|
||||
pub(crate) labeled_assets: HashMap<Option<String>, LoadedAsset>,
|
||||
pub(crate) labeled_assets: HashMap<Option<String>, BoxedLoadedAsset>,
|
||||
pub(crate) path: &'a Path,
|
||||
pub(crate) version: usize,
|
||||
}
|
||||
|
@ -85,13 +102,14 @@ impl<'a> LoadContext<'a> {
|
|||
self.labeled_assets.contains_key(&Some(label.to_string()))
|
||||
}
|
||||
|
||||
pub fn set_default_asset(&mut self, asset: LoadedAsset) {
|
||||
self.labeled_assets.insert(None, asset);
|
||||
pub fn set_default_asset<T: Asset>(&mut self, asset: LoadedAsset<T>) {
|
||||
self.labeled_assets.insert(None, asset.into());
|
||||
}
|
||||
|
||||
pub fn set_labeled_asset<T: Asset>(&mut self, label: &str, asset: LoadedAsset) -> Handle<T> {
|
||||
pub fn set_labeled_asset<T: Asset>(&mut self, label: &str, asset: LoadedAsset<T>) -> Handle<T> {
|
||||
assert!(!label.is_empty());
|
||||
self.labeled_assets.insert(Some(label.to_string()), asset);
|
||||
self.labeled_assets
|
||||
.insert(Some(label.to_string()), asset.into());
|
||||
self.get_handle(AssetPath::new_ref(self.path(), Some(label)))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue