#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{
fmt,
sync::atomic::{AtomicUsize, Ordering},
};
use crate::{
animation::{AnimationDirection, AnimationDuration},
easing::Easing,
events::Marker,
};
use bevy::{platform::collections::HashMap, prelude::*};
#[derive(Clone, Copy, Eq, PartialEq, Hash, Reflect)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
#[reflect(Debug, PartialEq, Hash)]
pub struct ClipId {
pub(crate) value: usize,
}
static NEXT_CLIP_ID: AtomicUsize = AtomicUsize::new(0);
impl fmt::Debug for ClipId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "clip{}", self.value)
}
}
impl ClipId {
pub(crate) fn new() -> Self {
Self {
value: NEXT_CLIP_ID.fetch_add(1, Ordering::Relaxed),
}
}
pub fn dummy() -> Self {
Self { value: usize::MAX }
}
}
#[derive(Debug, Clone, Reflect)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[reflect(Debug)]
pub struct Clip {
id: ClipId,
pub(crate) atlas_indices: Vec<usize>,
pub(crate) duration: Option<AnimationDuration>,
pub(crate) repetitions: Option<usize>,
pub(crate) direction: Option<AnimationDirection>,
pub(crate) easing: Option<Easing>,
pub(crate) markers: HashMap<usize, Vec<Marker>>,
}
impl Clip {
pub(crate) fn empty() -> Self {
Clip {
id: ClipId::new(),
atlas_indices: Vec::new(),
duration: None,
repetitions: None,
direction: None,
easing: None,
markers: HashMap::new(),
}
}
pub fn id(&self) -> ClipId {
self.id
}
pub fn atlas_indices(&self) -> &[usize] {
&self.atlas_indices
}
pub fn duration(&self) -> &Option<AnimationDuration> {
&self.duration
}
pub fn repetitions(&self) -> &Option<usize> {
&self.repetitions
}
pub fn direction(&self) -> &Option<AnimationDirection> {
&self.direction
}
pub fn easing(&self) -> &Option<Easing> {
&self.easing
}
pub fn markers(&self) -> &HashMap<usize, Vec<Marker>> {
&self.markers
}
}