1use crate::*;
2
3impl AssetCache {
5 pub fn get_state(&self, url: &str) -> Option<AssetState> {
15 self.get_entries()
16 .get(url)
17 .map(|entry: &AssetEntry| entry.get_state())
18 }
19
20 pub fn get_image(&self, url: &str) -> Option<HtmlImageElement> {
30 let entry: &AssetEntry = self.get_entries().get(url)?;
31 if entry.get_state() != AssetState::Loaded {
32 return None;
33 }
34 entry.get_image()
35 }
36
37 pub fn is_all_loaded(&self) -> bool {
43 self.get_entries()
44 .values()
45 .all(|entry: &AssetEntry| entry.get_state() != AssetState::Loading)
46 }
47
48 pub fn loaded_count(&self) -> usize {
54 self.get_entries()
55 .values()
56 .filter(|entry: &&AssetEntry| entry.get_state() == AssetState::Loaded)
57 .count()
58 }
59
60 pub fn clear(&mut self) {
62 self.get_mut_entries().clear();
63 }
64}
65
66impl Default for AssetCache {
68 fn default() -> AssetCache {
69 AssetCache::new()
70 }
71}
72
73impl AssetLoader {
75 pub fn load_image(&mut self, url: String) {
84 let image: HtmlImageElement = HtmlImageElement::new().expect("should create image element");
85 let entry: AssetEntry = AssetEntry::new(
86 AssetType::Image,
87 AssetState::Loading,
88 Some(image.clone()),
89 url.clone(),
90 );
91 self.get_cache()
92 .borrow_mut()
93 .get_mut_entries()
94 .insert(url.clone(), entry);
95 *self.get_mut_pending_count() += 1;
96 let cache_clone: Rc<RefCell<AssetCache>> = self.get_cache().clone();
97 let url_for_onload: String = url.clone();
98 let url_for_onerror: String = url.clone();
99 let onload_closure: Closure<dyn FnMut()> = Closure::wrap(Box::new(move || {
100 let mut cache_ref = cache_clone.borrow_mut();
101 if let Some(mut entry) = cache_ref.get_entries().get(&url_for_onload).cloned() {
102 entry.set_state(AssetState::Loaded);
103 cache_ref
104 .get_mut_entries()
105 .insert(url_for_onload.clone(), entry);
106 }
107 }) as Box<dyn FnMut()>);
108 let cache_clone_err: Rc<RefCell<AssetCache>> = self.get_cache().clone();
109 let onerror_closure: Closure<dyn FnMut()> = Closure::wrap(Box::new(move || {
110 let mut cache_ref = cache_clone_err.borrow_mut();
111 if let Some(mut entry) = cache_ref.get_entries().get(&url_for_onerror).cloned() {
112 entry.set_state(AssetState::Error);
113 cache_ref
114 .get_mut_entries()
115 .insert(url_for_onerror.clone(), entry);
116 }
117 }) as Box<dyn FnMut()>);
118 image.set_onload(Some(onload_closure.as_ref().unchecked_ref()));
119 image.set_onerror(Some(onerror_closure.as_ref().unchecked_ref()));
120 image.set_src(&url);
121 self.get_closures().borrow_mut().push(onload_closure);
122 self.get_closures().borrow_mut().push(onerror_closure);
123 }
124
125 pub fn is_all_loaded(&self) -> bool {
131 self.get_cache().borrow().is_all_loaded()
132 }
133
134 pub fn get_image(&self, url: &str) -> Option<HtmlImageElement> {
144 self.get_cache().borrow().get_image(url)
145 }
146
147 pub fn progress(&self) -> f64 {
153 let cache_ref = self.get_cache().borrow();
154 let total: usize = cache_ref.get_entries().len();
155 if total == 0 {
156 return 1.0;
157 }
158 cache_ref.loaded_count() as f64 / total as f64
159 }
160}
161
162impl Default for AssetLoader {
164 fn default() -> AssetLoader {
165 AssetLoader::new()
166 }
167}
168
169impl AssetLoader {
171 pub fn create_image_element(url: &str) -> Option<HtmlImageElement> {
184 let image: HtmlImageElement = HtmlImageElement::new().ok()?;
185 image.set_src(url);
186 Some(image)
187 }
188}