1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
use crate::{easing::Easing, stage::AnimationStage};
use std::fmt;
/// An opaque identifier that references an [Animation].
///
/// Returned by [SpritesheetLibrary::new_animation](crate::prelude::SpritesheetLibrary::new_animation).
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct AnimationId {
pub(crate) value: usize,
}
impl fmt::Display for AnimationId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "animation:{}", self.value)
}
}
/// Specifies the duration of an animation.
///
/// Defaults to `PerFrame(100)`.
#[derive(Debug, Clone, Copy)]
pub enum AnimationDuration {
/// Specifies the duration of each frame in milliseconds
PerFrame(u32),
/// Specifies the duration of one animation cycle in milliseconds
PerCycle(u32),
}
impl Default for AnimationDuration {
fn default() -> Self {
Self::PerFrame(100)
}
}
/// Specifies how many times an [Animation] repeats.
///
/// Defaults to `AnimationRepeat::Loop`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AnimationRepeat {
/// Loops indefinitely
Loop,
/// Repeats a fixed number of times
Cycles(u32),
}
impl Default for AnimationRepeat {
fn default() -> Self {
Self::Loop
}
}
/// Specifies the direction of an animation.
///
/// Defaults to `AnimationDirection::Forwards`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AnimationDirection {
/// Frames play from left to right
Forwards,
/// Frames play from right to left
Backwards,
/// Alternates at each animation cycle, starting from left to right
PingPong,
}
impl Default for AnimationDirection {
fn default() -> Self {
Self::Forwards
}
}
/// A playable animation to assign to a [SpritesheetAnimation](crate::prelude::SpritesheetAnimation) component.
///
/// An animation is composed of one or several [AnimationStage]s.
///
/// Parameters like duration, repeat, direction and easing can be specified.
///
/// If specified, they will be combined with the parameters of the underlying [AnimationStage]s and [AnimationClip](crate::prelude::AnimationClip)s.
///
/// # Example
///
/// ```
/// # use bevy_spritesheet_animation::prelude::*;
/// # let mut library = SpritesheetLibrary::new();
/// # let some_clip_id = library.new_clip(|clip| {});
/// # let another_clip_id = library.new_clip(|clip| {});
/// let animation_id = library.new_animation(|animation| {
/// let mut stage1 = AnimationStage::from_clip(some_clip_id);
/// stage1
/// .set_duration(AnimationDuration::PerCycle(2000))
/// .set_easing(Easing::In(EasingVariety::Quadratic));
///
/// let mut stage2 = AnimationStage::from_clip(another_clip_id);
/// stage2
/// .set_repeat(10)
/// .set_direction(AnimationDirection::PingPong);
///
/// animation
/// .add_stage(stage1)
/// .add_stage(stage2)
/// .set_repeat(AnimationRepeat::Cycles(5));
/// });
/// ```
#[derive(Debug, Clone)]
pub struct Animation {
/// The [AnimationStage]s that compose this animation
stages: Vec<AnimationStage>,
/// The optional duration of this animation
duration: Option<AnimationDuration>,
/// The optional number of repetitions of this animation
repeat: Option<AnimationRepeat>,
/// The optional direction of this animation
direction: Option<AnimationDirection>,
/// The optional easing of this animation
easing: Option<Easing>,
}
impl Animation {
pub(crate) fn new() -> Self {
Self {
stages: Vec::new(),
duration: None,
repeat: None,
direction: None,
easing: None,
}
}
/// Adds a stage to the animation.
///
/// Stages are played in the same order that they are added.
///
/// # Arguments
///
/// `stage` - the stage to add to the animation
///
/// # Example
///
/// ```
/// # use bevy_spritesheet_animation::prelude::*;
/// # let mut library = SpritesheetLibrary::new();
/// # let some_clip_id = library.new_clip(|clip| {});
/// let animation_id = library.new_animation(|animation| {
/// // Directly add a first clip to the animation as a stage
///
/// animation.add_stage(some_clip_id.into());
///
/// // Add a second clip
/// //
/// // This time, we create an explicit stage to tweak the clip's parameters
///
/// let mut stage = AnimationStage::from_clip(some_clip_id);
/// stage.set_direction(AnimationDirection::Backwards);
///
/// animation.add_stage(stage);
/// });
/// ```
pub fn add_stage(&mut self, stage: AnimationStage) -> &mut Self {
self.stages.push(stage);
self
}
pub fn stages(&self) -> &[AnimationStage] {
&self.stages
}
pub fn set_duration(&mut self, duration: AnimationDuration) -> &mut Self {
self.duration = Some(duration);
self
}
pub fn duration(&self) -> &Option<AnimationDuration> {
&self.duration
}
pub fn set_repeat(&mut self, repeat: AnimationRepeat) -> &mut Self {
self.repeat = Some(repeat);
self
}
pub fn repeat(&self) -> &Option<AnimationRepeat> {
&self.repeat
}
pub fn set_direction(&mut self, direction: AnimationDirection) -> &mut Self {
self.direction = Some(direction);
self
}
pub fn direction(&self) -> &Option<AnimationDirection> {
&self.direction
}
pub fn set_easing(&mut self, easing: Easing) -> &mut Self {
self.easing = Some(easing);
self
}
pub fn easing(&self) -> &Option<Easing> {
&self.easing
}
}