use std::slice;
use {accessor, json, scene, Gltf};
pub use json::animation::{InterpolationAlgorithm, TrsProperty};
#[derive(Clone, Debug)]
pub struct Animation<'a> {
gltf: &'a Gltf,
index: usize,
json: &'a json::animation::Animation,
}
#[derive(Clone, Debug)]
pub struct Channels<'a> {
anim: Animation<'a>,
iter: slice::Iter<'a, json::animation::Channel>,
}
#[derive(Clone, Debug)]
pub struct Samplers<'a> {
anim: Animation<'a>,
iter: slice::Iter<'a, json::animation::Sampler>,
}
impl<'a> Animation<'a> {
pub(crate) fn new(
gltf: &'a Gltf, index: usize,
json: &'a json::animation::Animation,
) -> Self {
Self {
gltf: gltf,
index: index,
json: json,
}
}
pub fn as_json(&self) -> &json::animation::Animation {
self.json
}
pub fn index(&self) -> usize {
self.index
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
pub fn channels(&self) -> Channels<'a> {
Channels {
anim: self.clone(),
iter: self.json.channels.iter(),
}
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_ref().map(String::as_str)
}
pub fn samplers(&self) -> Samplers<'a> {
Samplers {
anim: self.clone(),
iter: self.json.samplers.iter(),
}
}
}
#[derive(Clone, Debug)]
pub struct Channel<'a> {
anim: Animation<'a>,
json: &'a json::animation::Channel,
}
impl<'a> Channel<'a> {
pub(crate) fn new(
anim: Animation<'a>,
json: &'a json::animation::Channel,
) -> Self {
Self {
anim: anim,
json: json,
}
}
pub fn animation(&self) -> Animation<'a> {
self.anim.clone()
}
pub fn as_json(&self) -> &json::animation::Channel {
self.json
}
pub fn sampler(&self) -> Sampler<'a> {
self.anim.samplers().nth(self.json.sampler.value()).unwrap()
}
pub fn target(&self) -> Target<'a> {
Target::new(self.anim.clone(), &self.json.target)
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
#[derive(Clone, Debug)]
pub struct Target<'a> {
anim: Animation<'a>,
json: &'a json::animation::Target,
}
impl<'a> Target<'a> {
pub(crate) fn new(
anim: Animation<'a>,
json: &'a json::animation::Target,
) -> Self {
Self {
anim: anim,
json: json,
}
}
pub fn animation(&self) -> Animation<'a> {
self.anim.clone()
}
pub fn as_json(&self) -> &json::animation::Target {
self.json
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
pub fn node(&self) -> scene::Node {
self.anim.gltf.nodes().nth(self.json.node.value()).unwrap()
}
pub fn path(&self) -> TrsProperty {
self.json.path.unwrap()
}
}
#[derive(Clone, Debug)]
pub struct Sampler<'a> {
anim: Animation<'a>,
json: &'a json::animation::Sampler,
}
impl<'a> Sampler<'a> {
pub(crate) fn new(
anim: Animation<'a>,
json: &'a json::animation::Sampler,
) -> Self {
Self {
anim: anim,
json: json,
}
}
pub fn animation(&self) -> Animation<'a> {
self.anim.clone()
}
pub fn as_json(&self) -> &json::animation::Sampler {
self.json
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
pub fn input(&self) -> accessor::Accessor<'a> {
self.anim.gltf.accessors().nth(self.json.input.value()).unwrap()
}
pub fn interpolation(&self) -> InterpolationAlgorithm {
self.json.interpolation.unwrap()
}
pub fn output(&self) -> accessor::Accessor<'a> {
self.anim.gltf.accessors().nth(self.json.output.value()).unwrap()
}
}
impl<'a> Iterator for Channels<'a> {
type Item = Channel<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|json| Channel::new(self.anim.clone(), json))
}
}
impl<'a> Iterator for Samplers<'a> {
type Item = Sampler<'a>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|json| Sampler::new(self.anim.clone(), json))
}
}