use std::slice;
use {camera, extensions, json, mesh, skin, Gltf};
#[derive(Clone, Debug)]
pub struct Node<'a> {
gltf: &'a Gltf,
index: usize,
json: &'a json::scene::Node,
}
#[derive(Clone, Debug)]
pub struct Scene<'a> {
#[allow(dead_code)]
gltf: &'a Gltf,
index: usize,
json: &'a json::scene::Scene,
}
#[derive(Clone, Debug)]
pub struct Nodes<'a> {
gltf: &'a Gltf,
iter: slice::Iter<'a, json::Index<json::scene::Node>>,
}
#[derive(Clone, Debug)]
pub struct Children<'a> {
parent: &'a Node<'a>,
iter: slice::Iter<'a, json::Index<json::scene::Node>>,
}
impl<'a> Node<'a> {
pub fn new(gltf: &'a Gltf, index: usize, json: &'a json::scene::Node) -> Self {
Self {
gltf: gltf,
index: index,
json: json,
}
}
pub fn index(&self) -> usize {
self.index
}
pub fn as_json(&self) -> &json::scene::Node {
self.json
}
pub fn camera(&self) -> Option<camera::Camera> {
self.json.camera.as_ref().map(|index| {
self.gltf.cameras().nth(index.value()).unwrap()
})
}
pub fn children(&'a self) -> Children<'a> {
Children {
parent: self,
iter: self.json.children.as_ref().map_or([].iter(), |x| x.iter()),
}
}
pub fn extensions(&self) -> extensions::scene::Node<'a> {
extensions::scene::Node::new(
self.gltf,
&self.json.extensions,
)
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
pub fn matrix(&self) -> [f32; 16] {
self.json.matrix
}
pub fn mesh(&self) -> Option<mesh::Mesh<'a>> {
self.json.mesh.as_ref().map(|index| {
self.gltf.meshes().nth(index.value()).unwrap()
})
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_ref().map(String::as_str)
}
pub fn rotation(&self) -> [f32; 4] {
self.json.rotation.0
}
pub fn scale(&self) -> [f32; 3] {
self.json.scale
}
pub fn translation(&self) -> [f32; 3] {
self.json.translation
}
pub fn skin(&self) -> Option<skin::Skin<'a>> {
self.json.skin.as_ref().map(|index| {
self.gltf.skins().nth(index.value()).unwrap()
})
}
pub fn weights(&self) -> Option<&[f32]> {
self.json.weights.as_ref().map(Vec::as_slice)
}
}
impl<'a> Scene<'a> {
pub fn new(gltf: &'a Gltf, index: usize, json: &'a json::scene::Scene) -> Self {
Self {
gltf: gltf,
index: index,
json: json,
}
}
pub fn index(&self) -> usize {
self.index
}
pub fn as_json(&self) -> &json::scene::Scene {
self.json
}
pub fn extensions(&self) -> extensions::scene::Scene<'a> {
extensions::scene::Scene::new(
self.gltf,
&self.json.extensions,
)
}
pub fn extras(&self) -> &json::Extras{
&self.json.extras
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_ref().map(String::as_str)
}
pub fn nodes(&self) -> Nodes<'a> {
Nodes {
gltf: self.gltf,
iter: self.json.nodes.iter(),
}
}
}
impl<'a> Iterator for Nodes<'a> {
type Item = Node<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|index| self.gltf.nodes().nth(index.value()).unwrap())
}
}
impl<'a> Iterator for Children<'a> {
type Item = Node<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|index| self.parent.gltf.nodes().nth(index.value()).unwrap())
}
}