Newer
Older
use micro_musicbox::prelude::AudioSource;
use micro_musicbox::utilities::{SuppliesAudio, TrackType};
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
#[derive(Copy, Clone, Debug)]
pub struct SpriteSheetConfig {
pub tile_width: usize,
pub tile_height: usize,
pub columns: usize,
pub rows: usize,
}
impl SpriteSheetConfig {
pub fn squares(tile_wh: usize, columns: usize, rows: usize) -> Self {
Self {
tile_width: tile_wh,
tile_height: tile_wh,
columns,
rows,
}
}
pub fn rectangles(tile_width: usize, tile_height: usize, columns: usize, rows: usize) -> Self {
Self {
tile_width,
tile_height,
columns,
rows,
}
}
}
#[derive(Default)]
pub struct AssetHandles {
pub images: HashMap<String, Handle<Image>>,
pub atlas: HashMap<String, Handle<TextureAtlas>>,
pub sounds: HashMap<String, Handle<AudioSource>>,
pub fonts: HashMap<String, Handle<Font>>,
}
macro_rules! fetch_wrapper {
($name: tt, $type: ty => $key: ident) => {
pub fn $name<T: ToString>(&self, name: T) -> Handle<$type> {
let key = name.to_string();
match self.$key.get(&key) {
Some(handle) => handle.clone_weak(),
None => {
let keys = self.$key.keys();
panic!(
"\n\nTried to fetch {} asset with a missing key: {}.\nPossible keys: {}\n\n",
stringify!($name),
name.to_string(),
keys.map(|k| format!("'{}'", k))
.collect::<Vec<String>>()
.join(", ")
)
}
}
}
};
}
impl AssetHandles {
fetch_wrapper!(image, Image => images);
fetch_wrapper!(atlas, TextureAtlas => atlas);
fetch_wrapper!(sound, AudioSource => sounds);
fetch_wrapper!(font, Font => fonts);
}
impl SuppliesAudio for AssetHandles {
fn resolve_track_name<T: ToString>(&self, name: T) -> TrackType<String> {
if self.sounds.contains_key(&name.to_string()) {
TrackType::Single(name.to_string())
} else {
TrackType::Missing
}
}
fn get_audio_track<T: ToString>(&self, name: T) -> Option<Handle<AudioSource>> {
self.sounds.get(&name.to_string()).map(Handle::clone_weak)
}
}
pub type AssetNameMapping = (String, String);
pub type FixedAssetNameMapping = (&'static str, &'static str);