use crate::{Asset, AssetId, AssetLoadError, AssetPath, UntypedAssetId};
use bevy_ecs::event::Event;
use bevy_reflect::Reflect;
use core::fmt::Debug;
#[derive(Event, Clone, Debug)]
pub struct AssetLoadFailedEvent<A: Asset> {
pub id: AssetId<A>,
pub path: AssetPath<'static>,
pub error: AssetLoadError,
}
impl<A: Asset> AssetLoadFailedEvent<A> {
pub fn untyped(&self) -> UntypedAssetLoadFailedEvent {
self.into()
}
}
#[derive(Event, Clone, Debug)]
pub struct UntypedAssetLoadFailedEvent {
pub id: UntypedAssetId,
pub path: AssetPath<'static>,
pub error: AssetLoadError,
}
impl<A: Asset> From<&AssetLoadFailedEvent<A>> for UntypedAssetLoadFailedEvent {
fn from(value: &AssetLoadFailedEvent<A>) -> Self {
UntypedAssetLoadFailedEvent {
id: value.id.untyped(),
path: value.path.clone(),
error: value.error.clone(),
}
}
}
#[derive(Event, Reflect)]
pub enum AssetEvent<A: Asset> {
Added { id: AssetId<A> },
Modified { id: AssetId<A> },
Removed { id: AssetId<A> },
Unused { id: AssetId<A> },
LoadedWithDependencies { id: AssetId<A> },
}
impl<A: Asset> AssetEvent<A> {
pub fn is_loaded_with_dependencies(&self, asset_id: impl Into<AssetId<A>>) -> bool {
matches!(self, AssetEvent::LoadedWithDependencies { id } if *id == asset_id.into())
}
pub fn is_added(&self, asset_id: impl Into<AssetId<A>>) -> bool {
matches!(self, AssetEvent::Added { id } if *id == asset_id.into())
}
pub fn is_modified(&self, asset_id: impl Into<AssetId<A>>) -> bool {
matches!(self, AssetEvent::Modified { id } if *id == asset_id.into())
}
pub fn is_removed(&self, asset_id: impl Into<AssetId<A>>) -> bool {
matches!(self, AssetEvent::Removed { id } if *id == asset_id.into())
}
pub fn is_unused(&self, asset_id: impl Into<AssetId<A>>) -> bool {
matches!(self, AssetEvent::Unused { id } if *id == asset_id.into())
}
}
impl<A: Asset> Clone for AssetEvent<A> {
fn clone(&self) -> Self {
*self
}
}
impl<A: Asset> Copy for AssetEvent<A> {}
impl<A: Asset> Debug for AssetEvent<A> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Added { id } => f.debug_struct("Added").field("id", id).finish(),
Self::Modified { id } => f.debug_struct("Modified").field("id", id).finish(),
Self::Removed { id } => f.debug_struct("Removed").field("id", id).finish(),
Self::Unused { id } => f.debug_struct("Unused").field("id", id).finish(),
Self::LoadedWithDependencies { id } => f
.debug_struct("LoadedWithDependencies")
.field("id", id)
.finish(),
}
}
}
impl<A: Asset> PartialEq for AssetEvent<A> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Added { id: l_id }, Self::Added { id: r_id })
| (Self::Modified { id: l_id }, Self::Modified { id: r_id })
| (Self::Removed { id: l_id }, Self::Removed { id: r_id })
| (Self::Unused { id: l_id }, Self::Unused { id: r_id })
| (
Self::LoadedWithDependencies { id: l_id },
Self::LoadedWithDependencies { id: r_id },
) => l_id == r_id,
_ => false,
}
}
}
impl<A: Asset> Eq for AssetEvent<A> {}