use crate::{accessor, scene, Document};
#[cfg(feature = "utils")]
use crate::Buffer;
pub use json::animation::{Interpolation, Property};
#[cfg(feature = "extensions")]
use serde_json::{Map, Value};
pub mod iter;
#[cfg(feature = "utils")]
#[cfg_attr(docsrs, doc(cfg(feature = "utils")))]
pub mod util;
#[cfg(feature = "utils")]
#[doc(inline)]
pub use self::util::Reader;
#[derive(Clone, Debug)]
pub struct Animation<'a> {
document: &'a Document,
index: usize,
json: &'a json::animation::Animation,
}
#[derive(Clone, Debug)]
pub struct Channel<'a> {
anim: Animation<'a>,
index: usize,
json: &'a json::animation::Channel,
}
#[derive(Clone, Debug)]
pub struct Sampler<'a> {
anim: Animation<'a>,
index: usize,
json: &'a json::animation::Sampler,
}
#[derive(Clone, Debug)]
pub struct Target<'a> {
anim: Animation<'a>,
json: &'a json::animation::Target,
}
impl<'a> Animation<'a> {
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::animation::Animation,
) -> Self {
Self {
document,
index,
json,
}
}
pub fn index(&self) -> usize {
self.index
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
pub fn channels(&self) -> iter::Channels<'a> {
iter::Channels {
anim: self.clone(),
iter: self.json.channels.iter().enumerate(),
}
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&'a str> {
self.json.name.as_deref()
}
pub fn samplers(&self) -> iter::Samplers<'a> {
iter::Samplers {
anim: self.clone(),
iter: self.json.samplers.iter().enumerate(),
}
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
}
impl<'a> Channel<'a> {
pub(crate) fn new(
anim: Animation<'a>,
json: &'a json::animation::Channel,
index: usize,
) -> Self {
Self { anim, json, index }
}
pub fn animation(&self) -> Animation<'a> {
self.anim.clone()
}
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)
}
#[cfg(feature = "utils")]
#[cfg_attr(docsrs, doc(cfg(feature = "utils")))]
pub fn reader<'s, F>(&self, get_buffer_data: F) -> Reader<'a, 's, F>
where
F: Clone + Fn(Buffer<'a>) -> Option<&'s [u8]>,
{
Reader {
channel: self.clone(),
get_buffer_data,
}
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
pub fn index(&self) -> usize {
self.index
}
}
impl<'a> Target<'a> {
pub(crate) fn new(anim: Animation<'a>, json: &'a json::animation::Target) -> Self {
Self { anim, json }
}
pub fn animation(&self) -> Animation<'a> {
self.anim.clone()
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
pub fn node(&self) -> scene::Node<'a> {
self.anim
.document
.nodes()
.nth(self.json.node.value())
.unwrap()
}
pub fn property(&self) -> Property {
self.json.path.unwrap()
}
}
impl<'a> Sampler<'a> {
pub(crate) fn new(
anim: Animation<'a>,
json: &'a json::animation::Sampler,
index: usize,
) -> Self {
Self { anim, json, index }
}
pub fn animation(&self) -> Animation<'a> {
self.anim.clone()
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
pub fn index(&self) -> usize {
self.index
}
pub fn input(&self) -> accessor::Accessor<'a> {
self.anim
.document
.accessors()
.nth(self.json.input.value())
.unwrap()
}
pub fn interpolation(&self) -> Interpolation {
self.json.interpolation.unwrap()
}
pub fn output(&self) -> accessor::Accessor<'a> {
self.anim
.document
.accessors()
.nth(self.json.output.value())
.unwrap()
}
}