1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use std::sync::{Arc, Mutex};

use anyhow::*;

use crate::cache::Cache;
use crate::draw::{ImageData, Patch};

/// Cloneable image loader
pub struct Graphics {
    pub(crate) cache: Arc<Mutex<Cache>>,
}

impl Graphics {
    /// Loads an image
    pub fn load_image<B: AsRef<[u8]>>(&self, bytes: B) -> Result<ImageData> {
        let image = image::load_from_memory(bytes.as_ref())?;
        let image = self.cache.lock().unwrap().load_image(image.into_rgba8());
        Ok(image)
    }

    /// Loads a 9 patch.
    pub fn load_patch<B: AsRef<[u8]>>(&self, bytes: B) -> Result<Patch> {
        let image = image::load_from_memory(bytes.as_ref())?;
        let image = self.cache.lock().unwrap().load_patch(image.into_rgba8());
        Ok(image)
    }
}

impl Clone for Graphics {
    fn clone(&self) -> Self {
        Self {
            cache: self.cache.clone(),
        }
    }
}