gltf_v1/
scene.rs

1use std::slice;
2
3use json::StringIndex;
4
5use crate::{Document, node::Node};
6
7#[derive(Clone, Debug)]
8pub struct Scene<'a> {
9    /// The parent `Document` struct.
10    #[allow(dead_code)]
11    document: &'a Document,
12
13    /// The corresponding JSON index.
14    index: &'a String,
15
16    /// The corresponding JSON struct.
17    json: &'a json::Scene,
18}
19
20impl<'a> Scene<'a> {
21    /// Constructs a `Buffer`.
22    pub(crate) fn new(document: &'a Document, index: &'a String, json: &'a json::Scene) -> Self {
23        Self {
24            document,
25            index,
26            json,
27        }
28    }
29
30    /// Returns the internal JSON index.
31    pub fn index(&self) -> &str {
32        self.index
33    }
34    pub fn name(&self) -> Option<&'a str> {
35        self.json.name.as_deref()
36    }
37
38    pub fn nodes(&self) -> SceneNodes<'a> {
39        SceneNodes {
40            document: self.document,
41            iter: self.json.nodes.iter(),
42        }
43    }
44}
45
46/// An `Iterator` that visits every buffer in a glTF asset.
47#[derive(Clone, Debug)]
48pub struct Scenes<'a> {
49    /// Internal buffer iterator.
50    pub(crate) iter: indexmap::map::Iter<'a, String, gltf_v1_json::Scene>,
51
52    /// The internal root glTF object.
53    pub(crate) document: &'a Document,
54}
55
56#[derive(Clone, Debug)]
57pub struct SceneNodes<'a> {
58    /// Internal accessor iterator.
59    pub(crate) iter: slice::Iter<'a, StringIndex<json::node::Node>>,
60
61    /// The internal root glTF object.
62    pub(crate) document: &'a Document,
63}
64
65impl ExactSizeIterator for SceneNodes<'_> {}
66impl<'a> Iterator for SceneNodes<'a> {
67    type Item = Node<'a>;
68    fn next(&mut self) -> Option<Self::Item> {
69        self.iter
70            .next()
71            .and_then(|index| self.document.nodes().find(|x| x.index() == index.value()))
72    }
73    fn size_hint(&self) -> (usize, Option<usize>) {
74        self.iter.size_hint()
75    }
76    fn count(self) -> usize {
77        self.iter.count()
78    }
79    fn last(self) -> Option<Self::Item> {
80        self.iter
81            .last()
82            .and_then(|index| self.document.nodes().find(|x| x.index() == index.value()))
83    }
84    fn nth(&mut self, n: usize) -> Option<Self::Item> {
85        self.iter
86            .nth(n)
87            .and_then(|index| self.document.nodes().find(|x| x.index() == index.value()))
88    }
89}
90
91impl ExactSizeIterator for Scenes<'_> {}
92impl<'a> Iterator for Scenes<'a> {
93    type Item = Scene<'a>;
94    fn next(&mut self) -> Option<Self::Item> {
95        self.iter
96            .next()
97            .map(|(index, json)| Scene::new(self.document, index, json))
98    }
99    fn size_hint(&self) -> (usize, Option<usize>) {
100        self.iter.size_hint()
101    }
102    fn count(self) -> usize {
103        self.iter.count()
104    }
105    fn last(self) -> Option<Self::Item> {
106        let document = self.document;
107        self.iter
108            .last()
109            .map(|(index, json)| Scene::new(document, index, json))
110    }
111    fn nth(&mut self, n: usize) -> Option<Self::Item> {
112        self.iter
113            .nth(n)
114            .map(|(index, json)| Scene::new(self.document, index, json))
115    }
116}