vizia_core/systems/
image.rs

1use crate::context::{Context, ResourceContext};
2use crate::prelude::*;
3// use crate::resource::{ImageId, ImageRetentionPolicy, StoredImage};
4use crate::style::ImageOrGradient;
5// use hashbrown::HashSet;
6
7// Iterate the tree and load any images used by entities which aren't already loaded. Remove any images no longer being used.
8pub(crate) fn image_system(cx: &mut Context) {
9    let cx = &mut ResourceContext::new(cx);
10
11    cx.resource_manager.mark_images_unused();
12
13    // Iterate the tree and load any defined images that aren't already loaded
14    for entity in cx.tree.into_iter() {
15        // Load a background-image if the entity has one
16        if let Some(background_images) = cx.style.background_image.get(entity).cloned() {
17            for image in background_images.iter() {
18                match image {
19                    ImageOrGradient::Image(name) => {
20                        load_image(cx, entity, name);
21                    }
22                    _ => {}
23                }
24            }
25        }
26    }
27
28    cx.resource_manager.evict_unused_images();
29}
30
31fn load_image(cx: &mut ResourceContext, entity: Entity, image_name: &str) {
32    // if let Some(image_id) = cx.resource_manager.image_ids.get(image_name) {}
33
34    if !try_load_image(cx, entity, image_name) {
35        // Image doesn't exists yet so call the image loader
36        if let Some(callback) = cx.resource_manager.image_loader.take() {
37            (callback)(cx, image_name);
38
39            cx.resource_manager.image_loader = Some(callback);
40
41            // Then try to load the image again
42            try_load_image(cx, entity, image_name);
43        }
44    }
45}
46
47fn try_load_image(cx: &mut ResourceContext, entity: Entity, image_name: &str) -> bool {
48    if let Some(image_id) = cx.resource_manager.image_ids.get(&image_name.to_owned()) {
49        // Check if the image is already loaded
50        if let Some(image_store) = cx.resource_manager.images.get_mut(image_id) {
51            // match &image_store.image {
52            //     // Image exists and is already loaded so just add this entity as an observer and mark image as used
53            //     ::Id(_, _) => {
54            //         // TODO: check if the image is actually the same?
55            //         image_store.observers.insert(entity);
56            //         image_store.used = true;
57            //     }
58
59            //     // Image exists but isn't loaded yet
60            //     ImageOrId::Image(_, _) => {
61            //         if let Some(canvas) = cx.canvases.get_mut(&Entity::root()) {
62            //             // This loads the image and sets the image id
63            //             image_store.image.id(canvas);
64            //             image_store.used = true;
65            //             cx.style.needs_relayout();
66            //             cx.style.needs_redraw();
67            //         }
68            //     }
69            // }
70
71            image_store.observers.insert(entity);
72            image_store.used = true;
73
74            return true;
75        } else {
76            // Safe to unwrap because we know it exsits.
77            let broken_image = cx.resource_manager.images.get_mut(&ImageId::root()).unwrap();
78            // Image doesn't exist yet so load and show placeholder image
79            broken_image.observers.insert(entity);
80        }
81    }
82
83    false
84}