use std::collections::BTreeMap;
use std::fmt;
use std::time::Duration;
use nadir_assets::Handle;
use nadir_core::{GenArena, Rect, Vec2};
use nadir_ecs::{Component, Entity};
use nadir_render::{Atlas, AtlasSprite, Texture};
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct ClipFrame {
pub texture: Handle<Texture>,
pub uv: Rect,
pub duration: Duration,
pub placement: Option<Placement>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Placement {
pub size: Vec2,
pub pivot: Vec2,
}
impl ClipFrame {
pub const fn new(texture: Handle<Texture>, uv: Rect, duration: Duration) -> Self {
Self {
texture,
uv,
duration,
placement: None,
}
}
pub fn from_atlas(
texture: Handle<Texture>,
sprite: &AtlasSprite,
frame: usize,
duration: Duration,
) -> Option<Self> {
let pivot = sprite.pivot()?;
let frame = sprite.frame(frame)?;
Some(Self {
texture,
uv: frame.uv(),
duration,
placement: Some(Placement {
size: frame.size().as_vec2(),
pivot: frame.pivot_in(pivot),
}),
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ClipError {
NoFrames,
FrameLastsZero {
index: usize,
},
TooLong,
NameTaken(String),
WrongPages {
needed: usize,
given: usize,
},
}
impl fmt::Display for ClipError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoFrames => write!(f, "a clip needs a frame"),
Self::FrameLastsZero { index } => write!(f, "frame {index} of a clip lasts zero"),
Self::TooLong => write!(f, "a clip's frames last longer than a Duration holds"),
Self::NameTaken(name) => write!(f, "there is a clip named `{name}` already"),
Self::WrongPages { needed, given } => write!(
f,
"the atlas has {needed} pages, and {given} textures were given for them"
),
}
}
}
impl std::error::Error for ClipError {}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Playback {
#[default]
Loop,
Once,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Clip {
frames: Vec<ClipFrame>,
ends: Vec<Duration>,
playback: Playback,
}
impl Clip {
pub fn new(frames: Vec<ClipFrame>, playback: Playback) -> Self {
match Self::try_new(frames, playback) {
Ok(clip) => clip,
Err(error) => panic!("{error}"),
}
}
pub fn try_new(frames: Vec<ClipFrame>, playback: Playback) -> Result<Self, ClipError> {
if frames.is_empty() {
return Err(ClipError::NoFrames);
}
let mut ends = Vec::with_capacity(frames.len());
let mut end = Duration::ZERO;
for (index, frame) in frames.iter().enumerate() {
if frame.duration.is_zero() {
return Err(ClipError::FrameLastsZero { index });
}
end = end.checked_add(frame.duration).ok_or(ClipError::TooLong)?;
ends.push(end);
}
Ok(Self {
frames,
ends,
playback,
})
}
pub fn frames(&self) -> &[ClipFrame] {
&self.frames
}
pub fn playback(&self) -> Playback {
self.playback
}
pub fn duration(&self) -> Duration {
*self.ends.last().expect("a clip has a frame")
}
pub fn frame_at(&self, elapsed: Duration) -> usize {
let at = match self.playback {
Playback::Loop => from_nanos(elapsed.as_nanos() % self.duration().as_nanos()),
Playback::Once if elapsed >= self.duration() => return self.frames.len() - 1,
Playback::Once => elapsed,
};
self.ends.partition_point(|&end| end <= at)
}
}
fn from_nanos(nanos: u128) -> Duration {
let seconds = u64::try_from(nanos / 1_000_000_000).expect("less than a Duration holds");
Duration::new(seconds, (nanos % 1_000_000_000) as u32)
}
pub struct Clips {
clips: GenArena<Named>,
names: BTreeMap<String, Handle<Clip>>,
}
struct Named {
clip: Clip,
name: Option<String>,
}
impl Clips {
pub fn new() -> Self {
Self {
clips: GenArena::new(),
names: BTreeMap::new(),
}
}
pub fn add(&mut self, clip: Clip) -> Handle<Clip> {
Handle::new(self.clips.insert(Named { clip, name: None }))
}
pub fn add_named(
&mut self,
name: impl Into<String>,
clip: Clip,
) -> Result<Handle<Clip>, ClipError> {
let name = name.into();
if self.names.contains_key(&name) {
return Err(ClipError::NameTaken(name));
}
let handle = Handle::new(self.clips.insert(Named {
clip,
name: Some(name.clone()),
}));
self.names.insert(name, handle);
Ok(handle)
}
pub fn add_atlas(
&mut self,
atlas: &Atlas,
pages: &[Handle<Texture>],
) -> Result<Vec<Handle<Clip>>, ClipError> {
if pages.len() != atlas.pages().len() {
return Err(ClipError::WrongPages {
needed: atlas.pages().len(),
given: pages.len(),
});
}
let mut made = Vec::new();
for sprite in atlas.sprites() {
for clip in sprite.clips() {
let name = format!("{}/{}", sprite.name(), clip.name());
if self.names.contains_key(&name) {
return Err(ClipError::NameTaken(name));
}
let frames = clip
.frames()
.iter()
.filter_map(|step| {
let page = sprite.frame(step.frame())?.page();
ClipFrame::from_atlas(
*pages.get(page)?,
sprite,
step.frame(),
step.duration(),
)
})
.collect();
let playback = if clip.is_looping() {
Playback::Loop
} else {
Playback::Once
};
made.push((name, Clip::try_new(frames, playback)?));
}
}
Ok(made
.into_iter()
.map(|(name, clip)| {
self.add_named(name, clip)
.expect("each name was checked as free, and they differ")
})
.collect())
}
pub fn get(&self, handle: Handle<Clip>) -> Option<&Clip> {
self.clips.get(handle.id()).map(|named| &named.clip)
}
pub fn find(&self, name: &str) -> Option<Handle<Clip>> {
self.names.get(name).copied()
}
pub fn name(&self, handle: Handle<Clip>) -> Option<&str> {
self.clips.get(handle.id())?.name.as_deref()
}
pub fn remove(&mut self, handle: Handle<Clip>) -> Option<Clip> {
let named = self.clips.remove(handle.id())?;
if let Some(name) = &named.name {
self.names.remove(name);
}
Some(named.clip)
}
pub fn len(&self) -> usize {
self.clips.len()
}
pub fn is_empty(&self) -> bool {
self.clips.is_empty()
}
}
impl Default for Clips {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for Clips {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Clips").field("len", &self.len()).finish()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Animator {
clip: Handle<Clip>,
elapsed: Duration,
finished: bool,
}
impl Animator {
pub const fn new(clip: Handle<Clip>) -> Self {
Self::starting_at(clip, Duration::ZERO)
}
pub const fn starting_at(clip: Handle<Clip>, elapsed: Duration) -> Self {
Self {
clip,
elapsed,
finished: false,
}
}
pub fn clip(&self) -> Handle<Clip> {
self.clip
}
pub fn elapsed(&self) -> Duration {
self.elapsed
}
pub fn is_finished(&self) -> bool {
self.finished
}
pub fn play(&mut self, clip: Handle<Clip>) {
if clip != self.clip {
*self = Self::new(clip);
}
}
pub fn restart(&mut self) {
*self = Self::new(self.clip);
}
pub(crate) fn advance(&mut self, clip: &Clip, by: Duration) -> (usize, bool) {
let duration = clip.duration();
let ended = match clip.playback() {
Playback::Loop => {
let nanos = self.elapsed.as_nanos() + by.as_nanos();
self.elapsed = from_nanos(nanos % duration.as_nanos());
false
}
Playback::Once => {
self.elapsed = self.elapsed.saturating_add(by).min(duration);
let ended = !self.finished && self.elapsed == duration;
self.finished |= ended;
ended
}
};
(clip.frame_at(self.elapsed), ended)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AnimationFinished {
pub entity: Entity,
pub clip: Handle<Clip>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct AnimationClock(pub(crate) Duration);
impl Component for Animator {}
#[cfg(test)]
mod tests {
use std::num::NonZeroU32;
use nadir_core::{GenIndex, Vec2};
use super::*;
const MS: Duration = Duration::from_millis(1);
const NS: Duration = Duration::from_nanos(1);
fn texture(index: u32) -> Handle<Texture> {
Handle::new(GenIndex::new(index, NonZeroU32::MIN))
}
fn clip(ms: &[u64], playback: Playback) -> Clip {
let frames = (0..)
.zip(ms)
.map(|(i, &ms)| {
let uv = Rect::new(Vec2::new(i as f32, 0.0), Vec2::new(i as f32 + 1.0, 1.0));
ClipFrame::new(texture(0), uv, Duration::from_millis(ms))
})
.collect();
Clip::new(frames, playback)
}
fn handle() -> Handle<Clip> {
Handle::new(GenIndex::new(0, NonZeroU32::MIN))
}
#[test]
fn a_looping_clip_shows_each_frame_from_its_start_up_to_its_end() {
let clip = clip(&[30, 50, 20], Playback::Loop);
assert_eq!(clip.duration(), 100 * MS);
let cases = [
(Duration::ZERO, 0),
(30 * MS - NS, 0),
(30 * MS, 1),
(80 * MS - NS, 1),
(80 * MS, 2),
(100 * MS - NS, 2),
(100 * MS, 0),
(130 * MS, 1),
(1_000_080 * MS, 2),
(Duration::MAX, 2),
];
for (elapsed, frame) in cases {
assert_eq!(clip.frame_at(elapsed), frame, "at {elapsed:?}");
}
}
#[test]
fn a_clip_that_plays_once_holds_its_last_frame_from_its_end() {
let clip = clip(&[30, 50, 20], Playback::Once);
let cases = [
(Duration::ZERO, 0),
(30 * MS, 1),
(100 * MS - NS, 2),
(100 * MS, 2),
(100 * MS + NS, 2),
(130 * MS, 2),
(Duration::MAX, 2),
];
for (elapsed, frame) in cases {
assert_eq!(clip.frame_at(elapsed), frame, "at {elapsed:?}");
}
}
#[test]
fn a_clip_needs_frames_that_last() {
let empty = std::panic::catch_unwind(|| Clip::new(Vec::new(), Playback::Loop));
assert!(empty.is_err(), "no frames");
let zero = std::panic::catch_unwind(|| clip(&[30, 0], Playback::Loop));
assert!(zero.is_err(), "a frame of zero");
let frame =
|duration| ClipFrame::new(texture(0), Rect::new(Vec2::ZERO, Vec2::ONE), duration);
let long = std::panic::catch_unwind(|| {
Clip::new(vec![frame(Duration::MAX), frame(NS)], Playback::Loop)
});
assert!(long.is_err(), "longer than a Duration");
assert_eq!(
Clip::try_new(Vec::new(), Playback::Loop),
Err(ClipError::NoFrames)
);
assert_eq!(
Clip::try_new(vec![frame(MS), frame(Duration::ZERO)], Playback::Once),
Err(ClipError::FrameLastsZero { index: 1 })
);
assert_eq!(
Clip::try_new(vec![frame(Duration::MAX), frame(NS)], Playback::Loop),
Err(ClipError::TooLong)
);
assert_eq!(
ClipError::FrameLastsZero { index: 1 }.to_string(),
"frame 1 of a clip lasts zero"
);
let longest = Clip::new(vec![frame(Duration::MAX - NS), frame(NS)], Playback::Loop);
assert_eq!(longest.duration(), Duration::MAX);
assert_eq!(longest.frame_at(Duration::MAX - NS), 1);
}
#[test]
fn a_looping_clip_wraps_whole_cycles_and_never_ends() {
let clip = clip(&[30, 50, 20], Playback::Loop);
let mut animator = Animator::new(handle());
assert_eq!(animator.advance(&clip, Duration::ZERO), (0, false));
assert_eq!(animator.elapsed(), Duration::ZERO);
assert_eq!(animator.advance(&clip, 30 * MS), (1, false));
assert_eq!(animator.elapsed(), 30 * MS);
assert_eq!(animator.advance(&clip, 250 * MS), (2, false));
assert_eq!(animator.elapsed(), 80 * MS);
assert_eq!(animator.advance(&clip, 200 * MS), (2, false));
assert_eq!(animator.elapsed(), 80 * MS);
assert_eq!(animator.advance(&clip, 20 * MS), (0, false));
assert_eq!(animator.elapsed(), Duration::ZERO);
assert_eq!(animator.advance(&clip, Duration::MAX), (2, false));
assert_eq!(animator.elapsed(), 100 * MS - NS);
assert!(!animator.is_finished());
}
#[test]
fn a_clip_that_plays_once_ends_once_on_the_step_that_reaches_its_end() {
let clip = clip(&[30, 50, 20], Playback::Once);
let mut animator = Animator::new(handle());
assert_eq!(animator.advance(&clip, 100 * MS - NS), (2, false));
assert!(!animator.is_finished());
assert_eq!(animator.advance(&clip, NS), (2, true));
assert_eq!(animator.elapsed(), 100 * MS);
assert!(animator.is_finished());
assert_eq!(animator.advance(&clip, Duration::ZERO), (2, false));
assert_eq!(animator.advance(&clip, 250 * MS), (2, false));
assert_eq!(animator.elapsed(), 100 * MS);
assert_eq!(animator.advance(&clip, Duration::MAX), (2, false));
}
#[test]
fn a_step_past_the_end_of_a_clip_that_plays_once_ends_it_there() {
let clip = clip(&[80, 80, 80], Playback::Once);
let mut animator = Animator::new(handle());
assert_eq!(animator.advance(&clip, 250 * MS), (2, true));
assert_eq!(animator.elapsed(), 240 * MS);
let flash = clip_of_one(16);
let mut animator = Animator::new(handle());
assert_eq!(animator.advance(&flash, 16 * MS), (0, true));
}
fn clip_of_one(ms: u64) -> Clip {
clip(&[ms], Playback::Once)
}
#[test]
fn restart_plays_a_finished_clip_again_and_play_switches_only_to_another() {
let once = clip(&[30, 50, 20], Playback::Once);
let mut animator = Animator::new(handle());
assert_eq!(animator.advance(&once, 100 * MS), (2, true));
animator.play(handle());
assert!(animator.is_finished());
assert_eq!(animator.advance(&once, 16 * MS), (2, false));
animator.restart();
assert_eq!(
(animator.elapsed(), animator.is_finished()),
(Duration::ZERO, false)
);
assert_eq!(animator.advance(&once, 50 * MS), (1, false));
assert_eq!(animator.advance(&once, 50 * MS), (2, true));
let other = Handle::new(GenIndex::new(1, NonZeroU32::MIN));
animator.play(other);
assert_eq!(animator.clip(), other);
assert_eq!(
(animator.elapsed(), animator.is_finished()),
(Duration::ZERO, false)
);
let looping = clip(&[30, 50, 20], Playback::Loop);
let mut animator = Animator::new(handle());
animator.advance(&looping, 50 * MS);
animator.play(handle());
assert_eq!(animator.elapsed(), 50 * MS);
assert_eq!(animator.advance(&looping, 30 * MS), (2, false));
}
#[test]
fn an_animator_can_start_partway_through_its_clip() {
let looping = clip(&[100, 100, 100], Playback::Loop);
let mut animator = Animator::starting_at(handle(), 250 * MS);
assert_eq!(animator.advance(&looping, Duration::ZERO), (2, false));
let mut animator = Animator::starting_at(handle(), 37_310 * MS);
assert_eq!(animator.advance(&looping, Duration::ZERO), (1, false));
assert_eq!(animator.elapsed(), 110 * MS);
let once = clip(&[30, 50, 20], Playback::Once);
let mut animator = Animator::starting_at(handle(), 130 * MS);
assert_eq!(animator.advance(&once, Duration::ZERO), (2, true));
assert_eq!(animator.elapsed(), 100 * MS);
}
#[test]
fn clips_are_found_by_handle_until_removed() {
let mut clips = Clips::new();
assert!(clips.is_empty());
let walk = clips.add(clip(&[100, 100], Playback::Loop));
let jump = clips.add(clip(&[50], Playback::Once));
assert_eq!(clips.len(), 2);
assert_eq!(clips.get(walk).map(Clip::playback), Some(Playback::Loop));
assert_eq!(clips.get(jump).map(Clip::duration), Some(50 * MS));
assert_eq!(clips.remove(walk).map(|clip| clip.frames().len()), Some(2));
assert_eq!(clips.get(walk), None);
assert_eq!(clips.remove(walk), None);
let fall = clips.add(clip(&[10], Playback::Once));
assert_ne!(fall, walk);
assert_eq!(clips.get(walk), None);
assert_eq!(format!("{clips:?}"), "Clips { len: 2 }");
}
#[test]
fn a_named_clip_is_found_by_its_name_until_removed() {
let mut clips = Clips::new();
let walk = clips
.add_named("sprites/person/walk", clip(&[100], Playback::Loop))
.expect("a free name");
let unnamed = clips.add(clip(&[10], Playback::Once));
assert_eq!(clips.find("sprites/person/walk"), Some(walk));
assert_eq!(clips.name(walk), Some("sprites/person/walk"));
assert_eq!(clips.name(unnamed), None);
assert_eq!(clips.find("sprites/person"), None);
assert_eq!(
clips.add_named("sprites/person/walk", clip(&[5], Playback::Loop)),
Err(ClipError::NameTaken("sprites/person/walk".to_string()))
);
assert_eq!(clips.len(), 2);
clips.remove(walk);
assert_eq!(clips.find("sprites/person/walk"), None);
assert_eq!(clips.name(walk), None);
let again = clips
.add_named("sprites/person/walk", clip(&[5], Playback::Loop))
.expect("a free name");
assert_ne!(again, walk);
}
fn atlas() -> Atlas {
use nadir_assets::{Asset, DecodeCtx};
use nadir_codec::{Limits, json, kbj};
let text = r#"{ "version": 2,
"pages": [ { "path": "atlas/sprites_0.qoi", "w": 64, "h": 64 },
{ "path": "atlas/sprites_1.qoi", "w": 64, "h": 64 } ],
"sprites": [
{ "name": "sprites/person",
"frames": [ { "page": 0, "x": 1, "y": 1, "w": 10, "h": 20, "ox": 1, "oy": 0, "sw": 12, "sh": 20 },
{ "page": 1, "x": 1, "y": 1, "w": 9, "h": 18, "ox": 2, "oy": 2, "sw": 12, "sh": 20 } ],
"pivot": [6.0, 20.0],
"clips": [ { "name": "stand", "loop": true, "frames": [[0, 100]] },
{ "name": "wave", "loop": false, "frames": [[1, 40], [0, 60]] } ] },
{ "name": "sprites/tree",
"frames": [ { "page": 0, "x": 20, "y": 1, "w": 24, "h": 44, "ox": 0, "oy": 4, "sw": 24, "sh": 48 } ],
"pivot": [12.0, 48.0] } ] }"#;
let value = json::parse(text, Limits::default()).expect("the test's JSON");
let bytes = kbj::encode(&value, Limits::default()).expect("encodable");
Atlas::decode(
&bytes,
&DecodeCtx::new("atlas/sprites.kbj", Limits::default()),
)
.expect("an atlas")
}
#[test]
fn an_atlas_s_clips_are_added_by_name_each_frame_placed_on_its_page() {
let atlas = atlas();
let pages = [texture(7), texture(8)];
let mut clips = Clips::new();
let added = clips.add_atlas(&atlas, &pages).expect("the clips");
let stand = clips.find("sprites/person/stand").expect("stand");
let wave = clips.find("sprites/person/wave").expect("wave");
assert_eq!(added, [stand, wave]);
assert_eq!(clips.len(), 2);
let stand = clips.get(stand).expect("a clip");
assert_eq!(stand.playback(), Playback::Loop);
assert_eq!(stand.duration(), 100 * MS);
let wave = clips.get(wave).expect("a clip");
assert_eq!(wave.playback(), Playback::Once);
let person = atlas.sprite("sprites/person").expect("the person");
let shown: Vec<(Handle<Texture>, Rect, Duration)> = wave
.frames()
.iter()
.map(|frame| (frame.texture, frame.uv, frame.duration))
.collect();
assert_eq!(
shown,
[
(pages[1], person.frames()[1].uv(), 40 * MS),
(pages[0], person.frames()[0].uv(), 60 * MS),
]
);
assert_eq!(
wave.frames()[0].placement,
Some(Placement {
size: Vec2::new(9.0, 18.0),
pivot: Vec2::new(4.0 / 9.0, 18.0 / 18.0),
})
);
assert_eq!(
wave.frames()[1].placement,
Some(Placement {
size: Vec2::new(10.0, 20.0),
pivot: Vec2::new(0.5, 1.0),
})
);
}
#[test]
fn an_atlas_s_clips_are_added_all_or_none() {
let atlas = atlas();
let mut clips = Clips::new();
assert_eq!(
clips.add_atlas(&atlas, &[texture(7)]),
Err(ClipError::WrongPages {
needed: 2,
given: 1
})
);
assert!(clips.is_empty());
clips
.add_named("sprites/person/wave", clip(&[5], Playback::Loop))
.expect("a free name");
assert_eq!(
clips.add_atlas(&atlas, &[texture(7), texture(8)]),
Err(ClipError::NameTaken("sprites/person/wave".to_string()))
);
assert_eq!(clips.len(), 1);
assert_eq!(clips.find("sprites/person/stand"), None);
}
#[test]
fn a_frame_of_an_atlas_is_placed_as_a_sprite_of_it_would_be() {
let atlas = atlas();
let person = atlas.sprite("sprites/person").expect("the person");
let frame = ClipFrame::from_atlas(texture(8), person, 1, 40 * MS).expect("a frame");
let sprite = crate::Sprite::from_atlas(texture(8), person, 1).expect("a sprite");
let placement = frame.placement.expect("placed");
assert_eq!(
(frame.uv, placement.size, placement.pivot),
(sprite.uv, sprite.size, sprite.pivot)
);
assert!(ClipFrame::from_atlas(texture(8), person, 2, MS).is_none());
}
}