|
| 1 | +use bevy::{ |
| 2 | + asset::{AssetId, AssetPath}, |
| 3 | + image::Image, |
| 4 | + platform::collections::HashMap, |
| 5 | + prelude::{Handle, Resource}, |
| 6 | +}; |
| 7 | + |
| 8 | +/// Cache entry for a preview image. |
| 9 | +#[derive(Clone, Debug)] |
| 10 | +pub struct PreviewCacheEntry { |
| 11 | + /// The preview image handle. |
| 12 | + pub image_handle: Handle<Image>, |
| 13 | + /// The asset ID that this preview is for. |
| 14 | + pub asset_id: AssetId<Image>, |
| 15 | + /// Timestamp when the preview was generated (for cache invalidation). |
| 16 | + pub timestamp: u64, |
| 17 | +} |
| 18 | + |
| 19 | +/// Cache for preview images to avoid re-rendering unchanged assets. |
| 20 | +#[derive(Resource, Default)] |
| 21 | +pub struct PreviewCache { |
| 22 | + /// Maps asset path to cache entry. |
| 23 | + path_cache: HashMap<AssetPath<'static>, PreviewCacheEntry>, |
| 24 | + /// Maps asset ID to cache entry. |
| 25 | + id_cache: HashMap<AssetId<Image>, PreviewCacheEntry>, |
| 26 | +} |
| 27 | + |
| 28 | +impl PreviewCache { |
| 29 | + /// Creates a new empty cache. |
| 30 | + pub fn new() -> Self { |
| 31 | + Self { |
| 32 | + path_cache: HashMap::new(), |
| 33 | + id_cache: HashMap::new(), |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Gets a cached preview by asset path. |
| 38 | + pub fn get_by_path<'a>(&self, path: &AssetPath<'a>) -> Option<&PreviewCacheEntry> { |
| 39 | + // Convert to owned path for lookup |
| 40 | + let owned_path: AssetPath<'static> = path.clone().into_owned(); |
| 41 | + self.path_cache.get(&owned_path) |
| 42 | + } |
| 43 | + |
| 44 | + /// Gets a cached preview by asset ID. |
| 45 | + pub fn get_by_id(&self, asset_id: AssetId<Image>) -> Option<&PreviewCacheEntry> { |
| 46 | + self.id_cache.get(&asset_id) |
| 47 | + } |
| 48 | + |
| 49 | + /// Inserts a preview into the cache. |
| 50 | + pub fn insert<'a>( |
| 51 | + &mut self, |
| 52 | + path: impl Into<AssetPath<'a>>, |
| 53 | + asset_id: AssetId<Image>, |
| 54 | + image_handle: Handle<Image>, |
| 55 | + timestamp: u64, |
| 56 | + ) { |
| 57 | + let path: AssetPath<'static> = path.into().into_owned(); |
| 58 | + let entry = PreviewCacheEntry { |
| 59 | + image_handle, |
| 60 | + asset_id, |
| 61 | + timestamp, |
| 62 | + }; |
| 63 | + self.path_cache.insert(path.clone(), entry.clone()); |
| 64 | + self.id_cache.insert(asset_id, entry); |
| 65 | + } |
| 66 | + |
| 67 | + /// Removes a preview from the cache by path. |
| 68 | + pub fn remove_by_path<'a>(&mut self, path: &AssetPath<'a>) -> Option<PreviewCacheEntry> { |
| 69 | + // Convert to owned path for lookup |
| 70 | + let owned_path: AssetPath<'static> = path.clone().into_owned(); |
| 71 | + if let Some(entry) = self.path_cache.remove(&owned_path) { |
| 72 | + self.id_cache.remove(&entry.asset_id); |
| 73 | + Some(entry) |
| 74 | + } else { |
| 75 | + None |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Removes a preview from the cache by asset ID. |
| 80 | + pub fn remove_by_id(&mut self, asset_id: AssetId<Image>) -> Option<PreviewCacheEntry> { |
| 81 | + if let Some(entry) = self.id_cache.remove(&asset_id) { |
| 82 | + // Find and remove from path cache |
| 83 | + self.path_cache.retain(|_, e| e.asset_id != asset_id); |
| 84 | + Some(entry) |
| 85 | + } else { |
| 86 | + None |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// Clears all cached previews. |
| 91 | + pub fn clear(&mut self) { |
| 92 | + self.path_cache.clear(); |
| 93 | + self.id_cache.clear(); |
| 94 | + } |
| 95 | + |
| 96 | + /// Returns the number of cached previews. |
| 97 | + pub fn len(&self) -> usize { |
| 98 | + self.path_cache.len() |
| 99 | + } |
| 100 | + |
| 101 | + /// Checks if the cache is empty. |
| 102 | + pub fn is_empty(&self) -> bool { |
| 103 | + self.path_cache.is_empty() |
| 104 | + } |
| 105 | +} |
0 commit comments