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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use {
    super::{MaterialHandle, ModelHandle},
    glam::{Quat, Vec3},
    serde::{Deserialize, Serialize},
    std::collections::HashMap,
};

// TODO: Scene refs should be easier to get without using an iterator

type Idx = u16;

#[derive(Default)]
pub struct Instance {
    pub id: Option<String>,
    pub material: Option<MaterialHandle>, // TODO: MAKE VEC!
    pub model: Option<ModelHandle>,       // TODO: MAKE VEC!
    pub position: Vec3,
    pub rotation: Quat,
    pub tags: Vec<String>,
}

/// An individual `Scene` reference.
#[derive(Debug)]
pub struct SceneBufRef<'a> {
    idx: usize,
    scene: &'a SceneBuf,
}

impl SceneBufRef<'_> {
    /// Returns `true` if the scene contains the given tag.
    pub fn has_tag<T: AsRef<str>>(&self, tag: T) -> bool {
        let tag = tag.as_ref();
        self.scene_ref()
            .tags
            .binary_search_by(|probe| self.scene_str(*probe).cmp(tag))
            .is_ok()
    }

    /// Returns `id`, if set.
    pub fn id(&self) -> Option<&str> {
        self.scene.refs[self.idx]
            .id
            .map(|idx| self.scene.strs[idx as usize].as_str())
    }

    /// Returns `material`, if set.
    pub fn material(&self) -> Option<MaterialHandle> {
        self.scene_ref().material
    }

    /// Returns `model`, if set.
    pub fn model(&self) -> Option<ModelHandle> {
        self.scene_ref().model
    }

    /// Returns `position` or the zero vector.
    pub fn position(&self) -> Vec3 {
        self.scene_ref().position
    }

    /// Returns `rotation` or the identity quaternion.
    pub fn rotation(&self) -> Quat {
        self.scene_ref().rotation
    }

    fn scene_ref(&self) -> &SceneRef {
        &self.scene.refs[self.idx]
    }

    fn scene_str<I: Into<usize>>(&self, idx: I) -> &str {
        self.scene.strs[idx.into()].as_str()
    }

    /// Returns an `Iterator` of tags.
    pub fn tags(&self) -> impl Iterator<Item = &str> {
        self.scene_ref()
            .tags
            .iter()
            .map(move |idx| self.scene_str(*idx))
    }
}

/// An `Iterator` of [`Ref`] items.
#[derive(Debug)]
pub struct SceneBufRefIter<'a> {
    idx: usize,
    scene: &'a SceneBuf,
}

impl<'a> Iterator for SceneBufRefIter<'a> {
    type Item = SceneBufRef<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx < self.scene.refs.len() {
            let res = SceneBufRef {
                scene: self.scene,
                idx: self.idx,
            };
            self.idx += 1;
            Some(res)
        } else {
            None
        }
    }
}

/// A container for [`Ref`] references.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct SceneBuf {
    refs: Vec<SceneRef>,
    strs: Vec<String>,
}

impl SceneBuf {
    #[allow(unused)]
    pub(crate) fn new<I: Iterator<Item = Instance>>(instances: I) -> Self {
        let mut refs = vec![];
        let mut strs = vec![];

        // Use a string table
        let mut cache = HashMap::new();
        let mut idx = |s: String| -> Idx {
            *cache.entry(s.clone()).or_insert_with(|| {
                let res = strs.len() as Idx;
                strs.push(s);
                res
            })
        };

        for mut instance in instances {
            refs.push(SceneRef {
                id: instance.id.map(&mut idx),
                model: instance.model,
                material: instance.material,
                position: instance.position,
                rotation: instance.rotation,
                tags: instance.tags.drain(..).map(&mut idx).collect(),
            });
        }

        Self { refs, strs }
    }

    /// Gets an iterator of the `Ref` instances stored in this `Scene`.
    pub fn refs(&self) -> SceneBufRefIter {
        SceneBufRefIter {
            idx: 0,
            scene: self,
        }
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
struct SceneRef {
    id: Option<Idx>,
    model: Option<ModelHandle>,
    material: Option<MaterialHandle>,
    position: Vec3,
    rotation: Quat,
    tags: Vec<Idx>,
}