1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::{borrow::Borrow, hash::Hash};

use amethyst_core::ecs::{
    shred::{ResourceId, SystemData},
    Read, ReadExpect, World,
};

use crate::{Asset, AssetStorage, Format, Handle, Loader, Progress};

/// Helper type for loading assets
#[derive(SystemData)]
pub struct AssetLoaderSystemData<'a, A>
where
    A: Asset,
{
    loader: ReadExpect<'a, Loader>,
    storage: Read<'a, AssetStorage<A>>,
}

impl<'a, A> AssetLoaderSystemData<'a, A>
where
    A: Asset,
{
    /// Loads an asset with a given format from the default (directory) source.
    /// If you want to load from a custom source instead, use `load_from`.
    ///
    /// See `load_from` for more information.
    pub fn load<F, N, P>(&self, name: N, format: F, progress: P) -> Handle<A>
    where
        F: Format<A::Data>,
        N: Into<String>,
        P: Progress,
    {
        self.loader.load(name, format, progress, &*self.storage)
    }

    /// Loads an asset with a given id and format from a custom source.
    /// The actual work is done in a worker thread, thus this method immediately returns a handle.
    ///
    /// ## Parameters
    ///
    /// * `name`: this is just an identifier for the asset, most likely a file name e.g.
    ///   `"meshes/a.obj"`
    /// * `format`: A format struct which loads bytes from a `source` and produces `Asset::Data`
    ///   with them
    /// * `source`: An identifier for a source which has previously been added using `with_source`
    /// * `progress`: A tracker which will be notified of assets which have been imported
    pub fn load_from<F, N, P, S>(&self, name: N, format: F, source: &S, progress: P) -> Handle<A>
    where
        F: Format<A::Data>,
        N: Into<String>,
        P: Progress,
        S: AsRef<str> + Eq + Hash + ?Sized,
        String: Borrow<S>,
    {
        self.loader
            .load_from(name, format, source, progress, &*self.storage)
    }

    /// Load an asset from data and return a handle.
    pub fn load_from_data<P>(&self, data: A::Data, progress: P) -> Handle<A>
    where
        A: Asset,
        P: Progress,
    {
        self.loader.load_from_data(data, progress, &*self.storage)
    }

    /// Asynchronously load an asset from data and return a handle.
    pub fn load_from_data_async<P, F>(&self, data: F, progress: P) -> Handle<A>
    where
        A: Asset,
        P: Progress,
        F: FnOnce() -> A::Data + Send + Sync + 'static,
    {
        self.loader
            .load_from_data_async(data, progress, &*self.storage)
    }
}