Skip to content

Commit

Permalink
feat: create animation from iterator (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Jan 17, 2022
1 parent 9c02768 commit 6e670db
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,30 @@ impl SpriteSheetAnimation {
/// For more granular configuration, see [`from_frames`](SpriteSheetAnimation::from_frames)
#[must_use]
pub fn from_range(index_range: RangeInclusive<usize>, frame_duration: Duration) -> Self {
Self::from_frames(
index_range
.map(|index| Frame::new(index, frame_duration))
.collect(),
)
Self::from_iter(index_range, frame_duration)
}

/// Create a new animation from an index iterator, using the same frame duration for each frame.
///
/// # Example
///
/// ```
/// # use benimator::{AnimationMode, SpriteSheetAnimation};
/// # use std::time::Duration;
/// // Easily create a reversed animation
/// let animation = SpriteSheetAnimation::from_iter((0..5).rev(), Duration::from_millis(100));
///
/// assert_eq!(animation.frames.iter().map(|frame| frame.index).collect::<Vec<_>>(), vec![4, 3, 2, 1, 0]);
/// assert!(animation.frames.iter().all(|frame| frame.duration.as_millis() == 100));
/// assert_eq!(animation.mode, AnimationMode::Repeat);
/// ```
///
/// For more granular configuration, see [`from_frames`](SpriteSheetAnimation::from_frames)
pub fn from_iter(indices: impl IntoIterator<Item = usize>, frame_duration: Duration) -> Self {
indices
.into_iter()
.map(|index| Frame::new(index, frame_duration))
.collect()
}

/// Set the animation mode to [`AnimationMode::Once`]
Expand All @@ -75,6 +94,12 @@ impl SpriteSheetAnimation {
}
}

impl FromIterator<Frame> for SpriteSheetAnimation {
fn from_iter<T: IntoIterator<Item = Frame>>(iter: T) -> Self {
Self::from_frames(iter.into_iter().collect())
}
}

impl Default for AnimationMode {
#[inline]
fn default() -> Self {
Expand Down

0 comments on commit 6e670db

Please sign in to comment.