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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::{image::ImageFiltering, math::*};
use core::prefab::{Prefab, PrefabComponent};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct HaVirtualImageUniform {
    pub virtual_asset_name: String,
    #[serde(default)]
    pub filtering: ImageFiltering,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct HaVirtualImageUniforms {
    /// { uniform name: uniform data }
    #[serde(flatten)]
    data: HashMap<String, HaVirtualImageUniform>,
    #[serde(skip)]
    pub(crate) dirty: bool,
}

impl HaVirtualImageUniforms {
    pub fn get(&self, uniform: &str) -> Option<&HaVirtualImageUniform> {
        self.data.get(uniform)
    }

    pub fn set(&mut self, uniform_name: impl ToString, uniform_data: HaVirtualImageUniform) {
        self.data.insert(uniform_name.to_string(), uniform_data);
        self.dirty = true;
    }

    pub fn remove(&mut self, uniform: &str) {
        self.data.remove(uniform);
        self.dirty = true;
    }

    pub fn clear(&mut self) {
        self.data.clear();
        self.dirty = true;
    }

    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    pub fn len(&self) -> usize {
        self.data.len()
    }

    pub fn iter(&self) -> impl Iterator<Item = (&str, &HaVirtualImageUniform)> {
        self.data.iter().map(|(k, v)| (k.as_str(), v))
    }

    pub fn mark_dirty(&mut self) {
        self.dirty = true;
    }
}

impl Prefab for HaVirtualImageUniforms {
    fn post_from_prefab(&mut self) {
        self.dirty = true;
    }
}
impl PrefabComponent for HaVirtualImageUniforms {}