Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Channel and Sampler return their index with .index(). Fixes #398 #419

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/animation/iter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::slice;
use std::{iter, slice};

use crate::animation::{Animation, Channel, Sampler};

Expand All @@ -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<slice::Iter<'a, json::animation::Channel>>,
}

/// An `Iterator` that visits the samplers of an animation.
Expand All @@ -19,15 +19,15 @@ 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<slice::Iter<'a, json::animation::Sampler>>,
}

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))
.map(|(index, json)| Channel::new(self.anim.clone(), json, index))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
Expand All @@ -37,12 +37,14 @@ impl<'a> Iterator for Channels<'a> {
}
fn last(self) -> Option<Self::Item> {
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::Item> {
self.iter
.nth(n)
.map(|json| Channel::new(self.anim.clone(), json))
.map(|(index, json)| Channel::new(self.anim.clone(), json, index))
}
}

Expand All @@ -51,7 +53,7 @@ impl<'a> Iterator for Samplers<'a> {
fn next(&mut self) -> Option<Self::Item> {
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<usize>) {
self.iter.size_hint()
Expand All @@ -61,11 +63,13 @@ impl<'a> Iterator for Samplers<'a> {
}
fn last(self) -> Option<Self::Item> {
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::Item> {
self.iter
.nth(n)
.map(|json| Sampler::new(self.anim.clone(), json))
.map(|(index, json)| Sampler::new(self.anim.clone(), json, index))
}
}
36 changes: 30 additions & 6 deletions src/animation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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(),
}
}

Expand All @@ -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(),
}
}

Expand All @@ -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.
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
Loading