@@ -99,7 +99,7 @@ void EditorResourcePreview::_thread_func(void *ud) {
9999 erp->_thread ();
100100}
101101
102- void EditorResourcePreview::_preview_ready (const String &p_str, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud) {
102+ void EditorResourcePreview::_preview_ready (const String &p_str, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, const Ref<Texture2D> &p_custom_type_icon, ObjectID id, const StringName &p_func, const Variant &p_ud) {
103103 String path = p_str;
104104 {
105105 MutexLock lock (preview_mutex);
@@ -118,16 +118,17 @@ void EditorResourcePreview::_preview_ready(const String &p_str, const Ref<Textur
118118 item.order = order++;
119119 item.preview = p_texture;
120120 item.small_preview = p_small_texture;
121+ item.custom_type_icon = p_custom_type_icon;
121122 item.last_hash = hash;
122123 item.modified_time = modified_time;
123124
124125 cache[path] = item;
125126 }
126127
127- MessageQueue::get_singleton ()->push_call (id, p_func, path, p_texture, p_small_texture, p_ud);
128+ MessageQueue::get_singleton ()->push_call (id, p_func, path, p_texture, p_small_texture, p_custom_type_icon, p_ud);
128129}
129130
130- void EditorResourcePreview::_generate_preview (Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base) {
131+ void EditorResourcePreview::_generate_preview (Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, Ref<ImageTexture> &r_custom_type_icon, const QueueItem &p_item, const String &cache_base) {
131132 String type;
132133
133134 if (p_item.resource .is_valid ()) {
@@ -184,19 +185,52 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<
184185 break ;
185186 }
186187
188+ // Fetch custom type icon.
189+ r_custom_type_icon = Ref<ImageTexture>();
190+ Ref<Resource> res;
191+ if (p_item.resource .is_valid ()) {
192+ res = p_item.resource ;
193+ } else {
194+ res = ResourceLoader::load (p_item.path , " " );
195+ }
196+ if (res.is_valid ()) {
197+ r_custom_type_icon = EditorNode::get_singleton ()->get_object_icon (res.ptr (), " " );
198+ }
199+
187200 if (!p_item.resource .is_valid ()) {
201+ // Cache the preview in case it's a resource on disk
202+ bool has_custom_type_icon = r_custom_type_icon.is_valid ();
203+ bool has_texture = r_texture.is_valid ();
204+ bool has_small_texture = r_small_texture.is_valid ();
205+ if (has_texture || has_custom_type_icon) {
206+ // Preview is valid, saving the cache now...
207+ if (has_texture) {
208+ ResourceSaver::save (r_texture, cache_base + " .png" );
209+
210+ // Small texture could only exist if the main texture exists.
211+ if (has_small_texture) {
212+ ResourceSaver::save (r_small_texture, cache_base + " _small.png" );
213+ }
214+ }
215+ if (has_custom_type_icon) {
216+ ResourceSaver::save (r_custom_type_icon, cache_base + " _custom_type_icon.png" );
217+ }
218+ }
219+
188220 // cache the preview in case it's a resource on disk
189221 if (r_texture.is_valid ()) {
190222 // wow it generated a preview... save cache
191- bool has_small_texture = r_small_texture.is_valid ();
223+ has_small_texture = r_small_texture.is_valid ();
192224 ResourceSaver::save (r_texture, cache_base + " .png" );
193225 if (has_small_texture) {
194226 ResourceSaver::save (r_small_texture, cache_base + " _small.png" );
195227 }
196228 Ref<FileAccess> f = FileAccess::open (cache_base + " .txt" , FileAccess::WRITE );
197229 ERR_FAIL_COND_MSG (f.is_null (), " Cannot create file '" + cache_base + " .txt'. Check user write permissions." );
198230 f->store_line (itos (thumbnail_size));
231+ f->store_line (itos (has_texture));
199232 f->store_line (itos (has_small_texture));
233+ f->store_line (itos (has_custom_type_icon));
200234 f->store_line (itos (FileAccess::get_modified_time (p_item.path )));
201235 f->store_line (FileAccess::get_md5 (p_item.path ));
202236 }
@@ -217,23 +251,24 @@ void EditorResourcePreview::_iterate() {
217251 path += " :" + itos (cache[item.path ].last_hash ); // keep last hash (see description of what this is in condition below)
218252 }
219253
220- _preview_ready (path, cache[item.path ].preview , cache[item.path ].small_preview , item.id , item.function , item.userdata );
254+ _preview_ready (path, cache[item.path ].preview , cache[item.path ].small_preview , cache[item. path ]. custom_type_icon , item.id , item.function , item.userdata );
221255
222256 preview_mutex.unlock ();
223257 } else {
224258 preview_mutex.unlock ();
225259
226260 Ref<ImageTexture> texture;
227261 Ref<ImageTexture> small_texture;
262+ Ref<ImageTexture> custom_type_icon_texture;
228263
229264 int thumbnail_size = EDITOR_GET (" filesystem/file_dialog/thumbnail_size" );
230265 thumbnail_size *= EDSCALE ;
231266
232267 if (item.resource .is_valid ()) {
233- _generate_preview (texture, small_texture, item, String ());
268+ _generate_preview (texture, small_texture, custom_type_icon_texture, item, String ());
234269
235270 // adding hash to the end of path (should be ID:<objid>:<hash>) because of 5 argument limit to call_deferred
236- _preview_ready (item.path + " :" + itos (item.resource ->hash_edited_version ()), texture, small_texture, item.id , item.function , item.userdata );
271+ _preview_ready (item.path + " :" + itos (item.resource ->hash_edited_version ()), texture, small_texture, custom_type_icon_texture, item.id , item.function , item.userdata );
237272
238273 } else {
239274 String temp_path = EditorPaths::get_singleton ()->get_cache_dir ();
@@ -246,11 +281,13 @@ void EditorResourcePreview::_iterate() {
246281 Ref<FileAccess> f = FileAccess::open (file, FileAccess::READ );
247282 if (f.is_null ()) {
248283 // No cache found, generate
249- _generate_preview (texture, small_texture, item, cache_base);
284+ _generate_preview (texture, small_texture, custom_type_icon_texture, item, cache_base);
250285 } else {
251286 uint64_t modtime = FileAccess::get_modified_time (item.path );
252287 int tsize = f->get_line ().to_int ();
288+ bool has_texture = f->get_line ().to_int ();
253289 bool has_small_texture = f->get_line ().to_int ();
290+ bool has_custom_type_icon = f->get_line ().to_int ();
254291 uint64_t last_modtime = f->get_line ().to_int ();
255292
256293 bool cache_valid = true ;
@@ -275,7 +312,9 @@ void EditorResourcePreview::_iterate() {
275312 ERR_PRINT (" Cannot create file '" + file + " '. Check user write permissions." );
276313 } else {
277314 f2->store_line (itos (thumbnail_size));
315+ f2->store_line (itos (has_texture));
278316 f2->store_line (itos (has_small_texture));
317+ f2->store_line (itos (has_custom_type_icon));
279318 f2->store_line (itos (modtime));
280319 f2->store_line (md5);
281320 }
@@ -289,32 +328,44 @@ void EditorResourcePreview::_iterate() {
289328 img.instantiate ();
290329 Ref<Image> small_img;
291330 small_img.instantiate ();
331+ Ref<Image> custom_type_icon_img;
332+ custom_type_icon_img.instantiate ();
292333
293- if (img->load (cache_base + " .png" ) != OK ) {
294- cache_valid = false ;
295- } else {
296- texture.instantiate ();
297- texture->set_image (img);
298-
299- if (has_small_texture) {
300- if (small_img->load (cache_base + " _small.png" ) != OK ) {
301- cache_valid = false ;
302- } else {
303- small_texture.instantiate ();
304- small_texture->set_image (small_img);
334+ if (has_texture) {
335+ if (img->load (cache_base + " .png" ) != OK ) {
336+ cache_valid = false ;
337+ } else {
338+ texture.instantiate ();
339+ texture->create_from_image (img);
340+
341+ if (has_small_texture) {
342+ if (small_img->load (cache_base + " _small.png" ) != OK ) {
343+ cache_valid = false ;
344+ } else {
345+ small_texture.instantiate ();
346+ small_texture->create_from_image (small_img);
347+ }
305348 }
306349 }
307350 }
351+
352+ if (has_custom_type_icon) {
353+ if (custom_type_icon_img->load (cache_base + " _custom_type_icon.png" ) != OK ) {
354+ cache_valid = false ;
355+ } else {
356+ custom_type_icon_texture.instantiate ();
357+ custom_type_icon_texture->create_from_image (custom_type_icon_img);
358+ }
359+ }
308360 }
309361
310362 if (!cache_valid) {
311- _generate_preview (texture, small_texture, item, cache_base);
363+ _generate_preview (texture, small_texture, custom_type_icon_texture, item, cache_base);
312364 }
313365 }
314- _preview_ready (item.path , texture, small_texture, item.id , item.function , item.userdata );
366+ _preview_ready (item.path , texture, small_texture, custom_type_icon_texture, item.id , item.function , item.userdata );
315367 }
316368 }
317-
318369 } else {
319370 preview_mutex.unlock ();
320371 }
@@ -340,7 +391,7 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p
340391
341392 if (cache.has (path_id) && cache[path_id].last_hash == p_res->hash_edited_version ()) {
342393 cache[path_id].order = order++;
343- p_receiver->call (p_receiver_func, path_id, cache[path_id].preview , cache[path_id].small_preview , p_userdata);
394+ p_receiver->call (p_receiver_func, path_id, cache[path_id].preview , cache[path_id].small_preview , cache[path_id]. custom_type_icon , p_userdata);
344395 return ;
345396 }
346397
@@ -365,7 +416,7 @@ void EditorResourcePreview::queue_resource_preview(const String &p_path, Object
365416
366417 if (cache.has (p_path)) {
367418 cache[p_path].order = order++;
368- p_receiver->call (p_receiver_func, p_path, cache[p_path].preview , cache[p_path].small_preview , p_userdata);
419+ p_receiver->call (p_receiver_func, p_path, cache[p_path].preview , cache[p_path].small_preview , cache[p_path]. custom_type_icon , p_userdata);
369420 return ;
370421 }
371422
0 commit comments