1use crate::{Asset, AssetId, AssetLoadError, AssetPath, UntypedAssetId};
2use bevy_ecs::event::Event;
3use bevy_reflect::Reflect;
4use core::fmt::Debug;
5
6#[derive(Event, Clone, Debug)]
10pub struct AssetLoadFailedEvent<A: Asset> {
11 pub id: AssetId<A>,
12 pub path: AssetPath<'static>,
14 pub error: AssetLoadError,
16}
17
18impl<A: Asset> AssetLoadFailedEvent<A> {
19 pub fn untyped(&self) -> UntypedAssetLoadFailedEvent {
21 self.into()
22 }
23}
24
25#[derive(Event, Clone, Debug)]
27pub struct UntypedAssetLoadFailedEvent {
28 pub id: UntypedAssetId,
29 pub path: AssetPath<'static>,
31 pub error: AssetLoadError,
33}
34
35impl<A: Asset> From<&AssetLoadFailedEvent<A>> for UntypedAssetLoadFailedEvent {
36 fn from(value: &AssetLoadFailedEvent<A>) -> Self {
37 UntypedAssetLoadFailedEvent {
38 id: value.id.untyped(),
39 path: value.path.clone(),
40 error: value.error.clone(),
41 }
42 }
43}
44
45#[derive(Event, Reflect)]
47pub enum AssetEvent<A: Asset> {
48 Added { id: AssetId<A> },
50 Modified { id: AssetId<A> },
52 Removed { id: AssetId<A> },
54 Unused { id: AssetId<A> },
56 LoadedWithDependencies { id: AssetId<A> },
58}
59
60impl<A: Asset> AssetEvent<A> {
61 pub fn is_loaded_with_dependencies(&self, asset_id: impl Into<AssetId<A>>) -> bool {
63 matches!(self, AssetEvent::LoadedWithDependencies { id } if *id == asset_id.into())
64 }
65
66 pub fn is_added(&self, asset_id: impl Into<AssetId<A>>) -> bool {
68 matches!(self, AssetEvent::Added { id } if *id == asset_id.into())
69 }
70
71 pub fn is_modified(&self, asset_id: impl Into<AssetId<A>>) -> bool {
73 matches!(self, AssetEvent::Modified { id } if *id == asset_id.into())
74 }
75
76 pub fn is_removed(&self, asset_id: impl Into<AssetId<A>>) -> bool {
78 matches!(self, AssetEvent::Removed { id } if *id == asset_id.into())
79 }
80
81 pub fn is_unused(&self, asset_id: impl Into<AssetId<A>>) -> bool {
83 matches!(self, AssetEvent::Unused { id } if *id == asset_id.into())
84 }
85}
86
87impl<A: Asset> Clone for AssetEvent<A> {
88 fn clone(&self) -> Self {
89 *self
90 }
91}
92
93impl<A: Asset> Copy for AssetEvent<A> {}
94
95impl<A: Asset> Debug for AssetEvent<A> {
96 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
97 match self {
98 Self::Added { id } => f.debug_struct("Added").field("id", id).finish(),
99 Self::Modified { id } => f.debug_struct("Modified").field("id", id).finish(),
100 Self::Removed { id } => f.debug_struct("Removed").field("id", id).finish(),
101 Self::Unused { id } => f.debug_struct("Unused").field("id", id).finish(),
102 Self::LoadedWithDependencies { id } => f
103 .debug_struct("LoadedWithDependencies")
104 .field("id", id)
105 .finish(),
106 }
107 }
108}
109
110impl<A: Asset> PartialEq for AssetEvent<A> {
111 fn eq(&self, other: &Self) -> bool {
112 match (self, other) {
113 (Self::Added { id: l_id }, Self::Added { id: r_id })
114 | (Self::Modified { id: l_id }, Self::Modified { id: r_id })
115 | (Self::Removed { id: l_id }, Self::Removed { id: r_id })
116 | (Self::Unused { id: l_id }, Self::Unused { id: r_id })
117 | (
118 Self::LoadedWithDependencies { id: l_id },
119 Self::LoadedWithDependencies { id: r_id },
120 ) => l_id == r_id,
121 _ => false,
122 }
123 }
124}
125
126impl<A: Asset> Eq for AssetEvent<A> {}