use crate::prelude::*;
use crate::UnderlyingYarnLine;
#[cfg(feature = "audio_assets")]
pub use audio_asset_provider_plugin::AudioAssetProvider;
use bevy::asset::{Asset, LoadedUntypedAsset};
use bevy::prelude::*;
use bevy::utils::HashMap;
pub use file_extension_asset_provider_plugin::{file_extensions, FileExtensionAssetProvider};
use std::any::Any;
use std::fmt::Debug;
#[cfg(feature = "audio_assets")]
mod audio_asset_provider_plugin;
mod file_extension_asset_provider_plugin;
pub(crate) fn asset_provider_plugin(app: &mut App) {
app.fn_plugin(file_extension_asset_provider_plugin::file_extension_asset_provider_plugin);
#[cfg(feature = "audio_assets")]
app.fn_plugin(audio_asset_provider_plugin::audio_asset_provider_plugin);
}
pub trait AssetProvider: Debug + Send + Sync {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn get_language(&self) -> Option<Language>;
fn set_language(&mut self, language: Option<Language>);
fn set_localizations(&mut self, localizations: Localizations);
fn set_asset_server(&mut self, asset_server: AssetServer);
fn update_asset_availability(
&mut self,
loaded_untyped_assets: &Assets<LoadedUntypedAsset>,
) -> bool;
fn accept_line_hints(&mut self, line_ids: &[LineId]);
fn get_assets(&self, line: &UnderlyingYarnLine) -> LineAssets;
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct LineAssets(HashMap<&'static str, UntypedHandle>);
impl LineAssets {
pub fn new() -> Self {
Self(HashMap::new())
}
pub fn with_assets(handles: impl IntoIterator<Item = (&'static str, UntypedHandle)>) -> Self {
Self(handles.into_iter().collect())
}
pub fn get_handle<T>(&self) -> Option<Handle<T>>
where
T: Asset,
{
self.0.iter().find_map(|(type_id, handle)| {
(T::type_path() == *type_id).then(|| handle.clone().typed())
})
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl From<HashMap<&'static str, UntypedHandle>> for LineAssets {
fn from(h: HashMap<&'static str, UntypedHandle>) -> Self {
Self(h)
}
}
impl IntoIterator for LineAssets {
type Item = <HashMap<&'static str, UntypedHandle> as IntoIterator>::Item;
type IntoIter = <HashMap<&'static str, UntypedHandle> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl Extend<LineAssets> for LineAssets {
fn extend<T: IntoIterator<Item = LineAssets>>(&mut self, iter: T) {
self.0.extend(
iter.into_iter()
.flat_map(|line_assets| line_assets.0.into_iter()),
)
}
}
impl Extend<(&'static str, UntypedHandle)> for LineAssets {
fn extend<T: IntoIterator<Item = (&'static str, UntypedHandle)>>(&mut self, iter: T) {
self.0.extend(iter)
}
}
impl FromIterator<(&'static str, UntypedHandle)> for LineAssets {
fn from_iter<T: IntoIterator<Item = (&'static str, UntypedHandle)>>(iter: T) -> Self {
Self(HashMap::from_iter(iter))
}
}
impl FromIterator<LineAssets> for LineAssets {
fn from_iter<T: IntoIterator<Item = LineAssets>>(iter: T) -> Self {
Self(
iter.into_iter()
.flat_map(|line_assets| line_assets.0.into_iter())
.collect(),
)
}
}