From 4d557957788da1d8ac2aaf99f8ae1d6d592f8738 Mon Sep 17 00:00:00 2001 From: Schell Carl Scivally Date: Tue, 7 May 2024 09:51:59 +1200 Subject: [PATCH] feat: Channel and Sampler return their index with .index(). Fixes #398 --- src/animation/iter.rs | 22 +++++++++++++--------- src/animation/mod.rs | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/animation/iter.rs b/src/animation/iter.rs index dd135186..6775a557 100644 --- a/src/animation/iter.rs +++ b/src/animation/iter.rs @@ -1,4 +1,4 @@ -use std::slice; +use std::{iter, slice}; use crate::animation::{Animation, Channel, Sampler}; @@ -9,7 +9,7 @@ pub struct Channels<'a> { pub(crate) anim: Animation<'a>, /// The internal channel iterator. - pub(crate) iter: slice::Iter<'a, json::animation::Channel>, + pub(crate) iter: iter::Enumerate>, } /// An `Iterator` that visits the samplers of an animation. @@ -19,7 +19,7 @@ pub struct Samplers<'a> { pub(crate) anim: Animation<'a>, /// The internal channel iterator. - pub(crate) iter: slice::Iter<'a, json::animation::Sampler>, + pub(crate) iter: iter::Enumerate>, } impl<'a> Iterator for Channels<'a> { @@ -27,7 +27,7 @@ impl<'a> Iterator for Channels<'a> { fn next(&mut self) -> Option { self.iter .next() - .map(|json| Channel::new(self.anim.clone(), json)) + .map(|(index, json)| Channel::new(self.anim.clone(), json, index)) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() @@ -37,12 +37,14 @@ impl<'a> Iterator for Channels<'a> { } fn last(self) -> Option { let anim = self.anim; - self.iter.last().map(|json| Channel::new(anim, json)) + self.iter + .last() + .map(|(index, json)| Channel::new(anim, json, index)) } fn nth(&mut self, n: usize) -> Option { self.iter .nth(n) - .map(|json| Channel::new(self.anim.clone(), json)) + .map(|(index, json)| Channel::new(self.anim.clone(), json, index)) } } @@ -51,7 +53,7 @@ impl<'a> Iterator for Samplers<'a> { fn next(&mut self) -> Option { self.iter .next() - .map(|json| Sampler::new(self.anim.clone(), json)) + .map(|(index, json)| Sampler::new(self.anim.clone(), json, index)) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() @@ -61,11 +63,13 @@ impl<'a> Iterator for Samplers<'a> { } fn last(self) -> Option { let anim = self.anim; - self.iter.last().map(|json| Sampler::new(anim, json)) + self.iter + .last() + .map(|(index, json)| Sampler::new(anim, json, index)) } fn nth(&mut self, n: usize) -> Option { self.iter .nth(n) - .map(|json| Sampler::new(self.anim.clone(), json)) + .map(|(index, json)| Sampler::new(self.anim.clone(), json, index)) } } diff --git a/src/animation/mod.rs b/src/animation/mod.rs index eecad625..6bcb2342 100644 --- a/src/animation/mod.rs +++ b/src/animation/mod.rs @@ -38,6 +38,9 @@ pub struct Channel<'a> { /// The parent `Animation` struct. anim: Animation<'a>, + /// The corresponding JSON index. + index: usize, + /// The corresponding JSON struct. json: &'a json::animation::Channel, } @@ -48,6 +51,9 @@ pub struct Sampler<'a> { /// The parent `Animation` struct. anim: Animation<'a>, + /// The corresponding JSON index. + index: usize, + /// The corresponding JSON struct. json: &'a json::animation::Sampler, } @@ -92,7 +98,7 @@ impl<'a> Animation<'a> { pub fn channels(&self) -> iter::Channels<'a> { iter::Channels { anim: self.clone(), - iter: self.json.channels.iter(), + iter: self.json.channels.iter().enumerate(), } } @@ -109,7 +115,7 @@ impl<'a> Animation<'a> { pub fn samplers(&self) -> iter::Samplers<'a> { iter::Samplers { anim: self.clone(), - iter: self.json.samplers.iter(), + iter: self.json.samplers.iter().enumerate(), } } @@ -132,8 +138,12 @@ impl<'a> Animation<'a> { impl<'a> Channel<'a> { /// Constructs a `Channel`. - pub(crate) fn new(anim: Animation<'a>, json: &'a json::animation::Channel) -> Self { - Self { anim, json } + pub(crate) fn new( + anim: Animation<'a>, + json: &'a json::animation::Channel, + index: usize, + ) -> Self { + Self { anim, json, index } } /// Returns the parent `Animation` struct. @@ -169,6 +179,11 @@ impl<'a> Channel<'a> { pub fn extras(&self) -> &'a json::Extras { &self.json.extras } + + /// Returns the internal JSON index. + pub fn index(&self) -> usize { + self.index + } } impl<'a> Target<'a> { @@ -205,8 +220,12 @@ impl<'a> Target<'a> { impl<'a> Sampler<'a> { /// Constructs a `Sampler`. - pub(crate) fn new(anim: Animation<'a>, json: &'a json::animation::Sampler) -> Self { - Self { anim, json } + pub(crate) fn new( + anim: Animation<'a>, + json: &'a json::animation::Sampler, + index: usize, + ) -> Self { + Self { anim, json, index } } /// Returns the parent `Animation` struct. @@ -219,6 +238,11 @@ impl<'a> Sampler<'a> { &self.json.extras } + /// Returns the internal JSON index. + pub fn index(&self) -> usize { + self.index + } + /// Returns the accessor containing the keyframe input values (e.g. time). pub fn input(&self) -> accessor::Accessor<'a> { self.anim