diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 04d1f3bda07d4..1612b406b4f73 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -781,8 +781,11 @@ mod test { asset_server.get_handle_untyped(id) } - fn get_asset(id: impl Into, world: &World) -> Option<&PngAsset> { - world.resource::>().get(id.into()) + fn get_asset<'world>( + id: &Handle, + world: &'world World, + ) -> Option<&'world PngAsset> { + world.resource::>().get(id) } fn get_load_state(id: impl Into, world: &World) -> LoadState { @@ -800,7 +803,7 @@ mod test { ); // load the asset - let handle = load_asset(path.clone(), &app.world); + let handle = load_asset(path.clone(), &app.world).typed(); let weak_handle = handle.clone_weak(); // asset is loading @@ -826,7 +829,7 @@ mod test { assert!(get_asset(&weak_handle, &app.world).is_none()); // finally, reload the asset - let handle = load_asset(path.clone(), &app.world); + let handle = load_asset(path.clone(), &app.world).typed(); assert_eq!(LoadState::Loading, get_load_state(&handle, &app.world)); app.update(); assert_eq!(LoadState::Loaded, get_load_state(&handle, &app.world)); diff --git a/crates/bevy_asset/src/assets.rs b/crates/bevy_asset/src/assets.rs index 0a111ffbd8867..23ec2cf3dee66 100644 --- a/crates/bevy_asset/src/assets.rs +++ b/crates/bevy_asset/src/assets.rs @@ -129,12 +129,12 @@ impl Assets { /// /// This is the main method for accessing asset data from an [Assets] collection. If you need /// mutable access to the asset, use [`get_mut`](Assets::get_mut). - pub fn get>(&self, handle: H) -> Option<&T> { + pub fn get(&self, handle: &Handle) -> Option<&T> { self.assets.get(&handle.into()) } /// Checks if an asset exists for the given handle - pub fn contains>(&self, handle: H) -> bool { + pub fn contains(&self, handle: &Handle) -> bool { self.assets.contains_key(&handle.into()) } @@ -142,7 +142,7 @@ impl Assets { /// /// This is the main method for mutably accessing asset data from an [Assets] collection. If you /// do not need mutable access to the asset, you may also use [get](Assets::get). - pub fn get_mut>(&mut self, handle: H) -> Option<&mut T> { + pub fn get_mut(&mut self, handle: &Handle) -> Option<&mut T> { let id: HandleId = handle.into(); self.events.send(AssetEvent::Modified { handle: Handle::weak(id), @@ -398,6 +398,6 @@ mod tests { let handle = assets_before.add(MyAsset); app.add_asset::(); // Ensure this doesn't overwrite the Asset let assets_after = app.world.resource_mut::>(); - assert!(assets_after.get(handle).is_some()); + assert!(assets_after.get(&handle).is_some()); } } diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index 06fdee6907a37..3cc5df54b069e 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -169,7 +169,7 @@ impl Handle { /// Makes this handle Strong if it wasn't already. /// /// This method requires the corresponding [Assets](crate::Assets) collection - pub fn make_strong(&mut self, assets: &mut Assets) { + pub fn make_strong(&mut self, assets: &Assets) { if self.is_strong() { return; } @@ -341,6 +341,14 @@ impl HandleUntyped { matches!(self.handle_type, HandleType::Strong(_)) } + /// Create a weak typed [`Handle`] from this handle. + /// + /// If this handle is strong and dropped, there is no guarantee that the asset + /// will still be available (if only the returned handle is kept) + pub fn typed_weak(&self) -> Handle { + self.clone_weak().typed() + } + /// Convert this handle into a typed [Handle]. /// /// The new handle will maintain the Strong or Weak status of the current handle. diff --git a/crates/bevy_text/src/pipeline.rs b/crates/bevy_text/src/pipeline.rs index 5b728552513ab..60196fe2787ce 100644 --- a/crates/bevy_text/src/pipeline.rs +++ b/crates/bevy_text/src/pipeline.rs @@ -66,7 +66,7 @@ impl TextPipeline { .iter() .map(|section| { let font = fonts - .get(section.style.font.id) + .get(§ion.style.font) .ok_or(TextError::NoSuchFont)?; let font_id = self.get_or_insert_font_id(§ion.style.font, font); let font_size = scale_value(section.style.font_size, scale_factor); diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index a9eefb7528d36..804351836100b 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -98,7 +98,7 @@ pub fn extract_text2d_sprite( .color .as_rgba_linear(); let atlas = texture_atlases - .get(text_glyph.atlas_info.texture_atlas.clone_weak()) + .get(&text_glyph.atlas_info.texture_atlas) .unwrap(); let handle = atlas.texture.clone_weak(); let index = text_glyph.atlas_info.glyph_index as usize; diff --git a/crates/bevy_ui/Cargo.toml b/crates/bevy_ui/Cargo.toml index 2997283d94d7a..968a14a53da9f 100644 --- a/crates/bevy_ui/Cargo.toml +++ b/crates/bevy_ui/Cargo.toml @@ -13,6 +13,7 @@ keywords = ["bevy"] bevy_app = { path = "../bevy_app", version = "0.8.0-dev" } bevy_asset = { path = "../bevy_asset", version = "0.8.0-dev" } bevy_core_pipeline = { path = "../bevy_core_pipeline", version = "0.8.0-dev" } +bevy_derive = { path = "../bevy_derive", version = "0.8.0-dev" } bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev" } bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.8.0-dev" } bevy_input = { path = "../bevy_input", version = "0.8.0-dev" } diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 3548d5c297b6f..e9fa461263149 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -146,7 +146,7 @@ pub fn extract_uinodes( } let image = image.0.clone_weak(); // Skip loading images - if !images.contains(image.clone_weak()) { + if !images.contains(&image) { continue; } extracted_uinodes.uinodes.push(ExtractedUiNode { @@ -196,7 +196,7 @@ pub fn extract_text_uinodes( for text_glyph in text_glyphs { let color = text.sections[text_glyph.section_index].style.color; let atlas = texture_atlases - .get(text_glyph.atlas_info.texture_atlas.clone_weak()) + .get(&text_glyph.atlas_info.texture_atlas) .unwrap(); let texture = atlas.texture.clone_weak(); let index = text_glyph.atlas_info.glyph_index as usize; diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 05d9010b9c6b8..3d7996139cc8d 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -1,5 +1,6 @@ use crate::{Size, UiRect}; use bevy_asset::Handle; +use bevy_derive::{Deref, DerefMut}; use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_math::Vec2; use bevy_reflect::prelude::*; @@ -369,7 +370,7 @@ impl From for UiColor { } /// The image of the node -#[derive(Component, Clone, Debug, Reflect)] +#[derive(Component, Clone, Debug, Reflect, Deref, DerefMut)] #[reflect(Component, Default)] pub struct UiImage(pub Handle); diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index f8866e7260414..21f7d6288e803 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -31,7 +31,7 @@ pub fn image_node_system( mut query: Query<(&mut CalculatedSize, &UiImage), With>, ) { for (mut calculated_size, image) in query.iter_mut() { - if let Some(texture) = textures.get(image.0.clone_weak()) { + if let Some(texture) = textures.get(image) { let size = Size { width: texture.texture_descriptor.size.width as f32, height: texture.texture_descriptor.size.height as f32, diff --git a/examples/2d/texture_atlas.rs b/examples/2d/texture_atlas.rs index 45f7a24606b97..dbc90e8aa48a9 100644 --- a/examples/2d/texture_atlas.rs +++ b/examples/2d/texture_atlas.rs @@ -50,8 +50,9 @@ fn setup( ) { let mut texture_atlas_builder = TextureAtlasBuilder::default(); for handle in &rpg_sprite_handles.handles { - let texture = textures.get(handle).unwrap(); - texture_atlas_builder.add_texture(handle.clone_weak().typed::(), texture); + let handle = handle.typed_weak(); + let texture = textures.get(&handle).expect("Textures folder contained a file which way matched by a loader which did not create an `Image` asset"); + texture_atlas_builder.add_texture(handle, texture); } let texture_atlas = texture_atlas_builder.finish(&mut textures).unwrap(); diff --git a/examples/tools/scene_viewer.rs b/examples/tools/scene_viewer.rs index 7032cb0637d08..a0c2e0dc59653 100644 --- a/examples/tools/scene_viewer.rs +++ b/examples/tools/scene_viewer.rs @@ -67,7 +67,7 @@ Controls: } struct SceneHandle { - handle: Handle, + handle: Handle, animations: Vec>, instance_id: Option, is_loaded: bool,