Skip to content

Commit

Permalink
New Map key type
Browse files Browse the repository at this point in the history
currently broken, because the handles have no label
  • Loading branch information
NiklasEi committed Feb 25, 2024
1 parent b886c96 commit 789c581
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

- new map key type `AssetLabel` that creates an asset map using the label as the key

## v0.20.0 - 18.02.2024
- update to Bevy 0.13
- support any type implementing the new trait `MapKey` as keys for mapped assets (resolves [#153](https://github.com/NiklasEi/bevy_asset_loader/issues/153))
Expand Down
Binary file added bevy_asset_loader/assets/animated/Fox.glb
Binary file not shown.
9 changes: 9 additions & 0 deletions bevy_asset_loader/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ by [Kenny](https://kenney.nl/assets/toon-characters-1)

Pixelart tree [CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/)
by [Kenny](https://www.kenney.nl/assets/tiny-town)

glTF animated fox from [glTF Sample Models][fox]
* Low poly fox [by PixelMannen] (CC0 1.0 Universal)
* Rigging and animation [by @tomkranis on Sketchfab] ([CC-BY 4.0])

[fox]: https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/Fox
[by PixelMannen]: https://opengameart.org/content/fox-and-shiba
[by @tomkranis on Sketchfab]: https://sketchfab.com/models/371dea88d7e04a76af5763f2a36866bc
[CC-BY 4.0]: https://creativecommons.org/licenses/by/4.0/
13 changes: 11 additions & 2 deletions bevy_asset_loader/examples/asset_maps.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::asset::AssetPath;
use bevy::asset::{AssetPath, LoadedUntypedAsset};
use bevy::prelude::*;
use bevy::utils::HashMap;
use bevy_asset_loader::prelude::*;
Expand Down Expand Up @@ -27,13 +27,18 @@ struct AudioAssets {
// `FileStem` as the key will use the file name without the extension
#[asset(path = "audio", collection(mapped, typed))]
file_stem: HashMap<FileStem, Handle<AudioSource>>,
// `FileStem` as the key will use the file name without the extension
// #[asset(paths("animated/Fox.glb#Animation0"), collection(mapped, typed))]
// labels: HashMap<AssetLabel, Handle<AnimationClip>>,
#[asset(path = "animated/Fox.glb#Animation0")]
anim: Handle<AnimationClip>,

// You can implement your own map key types
#[asset(path = "audio", collection(mapped, typed))]
custom: HashMap<MyAudio, Handle<AudioSource>>,
}

fn use_audio_assets(audio_assets: Res<AudioAssets>) {
fn use_audio_assets(audio_assets: Res<AudioAssets>, asset_server: Res<AssetServer>) {
audio_assets
.full_path
.get("audio/plop.ogg")
Expand All @@ -46,6 +51,10 @@ fn use_audio_assets(audio_assets: Res<AudioAssets>) {
.file_stem
.get("plop")
.expect("Can access audio asset with file stem");
// audio_assets.labels.get("Animation0").expect("");

// let anim: Handle<LoadedUntypedAsset> = asset_server.load_untyped("animated/Fox.glb#Animation0");
let path = audio_assets.anim.path().unwrap().label().unwrap();

// custom key
audio_assets
Expand Down
2 changes: 1 addition & 1 deletion bevy_asset_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub mod prelude {
DynamicAssets,
},
loading_state::{LoadingState, LoadingStateAppExt, LoadingStateSet},
mapped::{FileName, FileStem, MapKey},
mapped::{AssetLabel, FileName, FileStem, MapKey},
};
}

Expand Down
21 changes: 21 additions & 0 deletions bevy_asset_loader/src/mapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ impl MapKey for FileStem {
}
}

/// A [`MapKey`] that uses the [`label`] of the asset's path as key.
///
/// # Panics
///
/// This type requires every asset in the collection to be loaded with a label.
/// If an asset path does not have a label, it will panic.
///
/// [`label`]: AssetPath::label
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AssetLabel(Box<str>);

impl_map_key_extras!(AssetLabel);

impl MapKey for AssetLabel {
#[inline]
fn from_asset_path(path: &AssetPath) -> Self {
println!("{:?}", path);
Self(path.label().expect("Asset does not have a label").into())
}
}

impl MapKey for String {
#[inline]
fn from_asset_path(path: &AssetPath) -> Self {
Expand Down

0 comments on commit 789c581

Please sign in to comment.