Skip to main content

euv_engine/asset/
struct.rs

1use crate::*;
2
3/// An entry in the asset cache containing the loaded data and its state.
4#[derive(Clone, Data, Debug, New, PartialEq)]
5pub struct AssetEntry {
6    /// The type of this asset.
7    #[get(type(copy))]
8    pub(crate) asset_type: AssetType,
9    /// The current loading state.
10    #[get(type(copy))]
11    pub(crate) state: AssetState,
12    /// The loaded image element, if this is an image asset.
13    #[get(type(clone))]
14    pub(crate) image: Option<HtmlImageElement>,
15    /// The URL this asset was loaded from.
16    pub(crate) url: String,
17}
18
19/// A cache for storing loaded game assets, keyed by URL.
20#[derive(Clone, Data, Debug, New, PartialEq)]
21pub struct AssetCache {
22    /// All cached assets keyed by their source URL.
23    #[get(pub(crate))]
24    #[get_mut(pub(crate))]
25    #[set(pub(crate))]
26    #[new(skip)]
27    pub(crate) entries: HashMap<String, AssetEntry>,
28}
29
30/// An asynchronous asset loader that fetches resources over HTTP
31/// and populates a shared `AssetCache`.
32#[derive(Clone, Data, New)]
33pub struct AssetLoader {
34    /// The shared cache that loaded assets are stored into.
35    #[new(skip)]
36    pub(crate) cache: Rc<RefCell<AssetCache>>,
37    /// The number of assets currently being loaded.
38    #[get(pub(crate), type(copy))]
39    #[get_mut(pub(crate))]
40    #[set(pub(crate))]
41    #[new(skip)]
42    pub(crate) pending_count: u32,
43    /// Stored closures keeping `onload`/`onerror` callbacks alive, preventing memory leaks.
44    #[new(skip)]
45    pub(crate) closures: AssetClosures,
46}