-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathanimation.rs
More file actions
229 lines (200 loc) · 6.46 KB
/
Copy pathanimation.rs
File metadata and controls
229 lines (200 loc) · 6.46 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::fmt;
use bevy::reflect::prelude::*;
use crate::{clip::ClipId, easing::Easing};
/// An opaque identifier that references an [Animation].
///
/// Returned by [AnimationLibrary::register_animation](crate::prelude::AnimationLibrary::register_animation).
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Reflect)]
#[reflect(Debug, 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, Reflect)]
#[reflect(Debug)]
pub enum AnimationDuration {
/// Specifies the duration of each frame in milliseconds
PerFrame(u32),
/// Specifies the duration of one repetition of the animation in milliseconds
PerRepetition(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, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum AnimationRepeat {
/// Loops indefinitely
Loop,
/// Repeats a fixed number of times
Times(usize),
}
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, Reflect)]
#[reflect(Debug, PartialEq, Hash)]
pub enum AnimationDirection {
/// Frames play from left to right
Forwards,
/// Frames play from right to left
Backwards,
/// Alternates at each repetition of the animation, 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 [Clip](crate::prelude::Clip)s.
///
/// Parameters like duration, repetitions, direction and easing can be specified.
/// If specified, they will be combined with the parameters of the underlying [Clip](crate::prelude::Clip)s.
///
/// # Example
///
/// ```
/// # use bevy_spritesheet_animation::prelude::*;
/// # let mut library = AnimationLibrary::default();
/// let some_clip = Clip::from_frames([1, 2, 3])
/// .with_duration(AnimationDuration::PerRepetition(2000));
///
/// let some_clip_id = library.register_clip(some_clip);
///
/// let another_clip = Clip::from_frames([7, 8, 9, 7, 7])
/// .with_repetitions(10)
/// .with_direction(AnimationDirection::PingPong);
///
/// let another_clip_id = library.register_clip(another_clip);
///
/// let animation = Animation::from_clips([some_clip_id, another_clip_id])
/// .with_repetitions(AnimationRepeat::Loop)
/// .with_easing(Easing::In(EasingVariety::Quadratic));
///
/// let animation_id = library.register_animation(animation);
/// ```
#[derive(Debug, Clone, Reflect)]
#[reflect(Debug)]
pub struct Animation {
/// The IDs of the [Clip](crate::prelude::Clip)s that compose this animation
clip_ids: Vec<ClipId>,
/// The optional duration of this animation
duration: Option<AnimationDuration>,
/// The optional number of repetitions of this animation
repetitions: Option<AnimationRepeat>,
/// The optional direction of this animation
direction: Option<AnimationDirection>,
/// The optional easing of this animation
easing: Option<Easing>,
/// An optional key to group animations that are variants (e.g., different directions)
/// of the same conceptual animation. This won't reset the current frame duration when only
/// switching between animations with the same key.
pub animation_group_key: Option<String>,
}
impl Animation {
/// Creates a new animation from a single clip.
pub fn from_clip(clip_id: ClipId) -> Self {
Self {
clip_ids: vec![clip_id],
duration: None,
repetitions: None,
direction: None,
easing: None,
animation_group_key: None,
}
}
/// Creates a new animation from a sequence of clips.
pub fn from_clips(clip_ids: impl IntoIterator<Item = ClipId>) -> Self {
Self {
clip_ids: clip_ids.into_iter().collect(),
duration: None,
repetitions: None,
direction: None,
easing: None,
animation_group_key: None,
}
}
pub fn clip_ids(&self) -> &[ClipId] {
&self.clip_ids
}
pub fn duration(&self) -> &Option<AnimationDuration> {
&self.duration
}
pub fn with_duration(&self, duration: AnimationDuration) -> Self {
Self {
duration: Some(duration),
..self.clone()
}
}
pub fn set_duration(&mut self, duration: AnimationDuration) -> &mut Self {
self.duration = Some(duration);
self
}
pub fn repetitions(&self) -> &Option<AnimationRepeat> {
&self.repetitions
}
pub fn with_repetitions(&self, repetitions: AnimationRepeat) -> Self {
Self {
repetitions: Some(repetitions),
..self.clone()
}
}
pub fn set_repetitions(&mut self, repetitions: AnimationRepeat) -> &mut Self {
self.repetitions = Some(repetitions);
self
}
pub fn direction(&self) -> &Option<AnimationDirection> {
&self.direction
}
pub fn with_direction(&self, direction: AnimationDirection) -> Self {
Self {
direction: Some(direction),
..self.clone()
}
}
pub fn set_direction(&mut self, direction: AnimationDirection) -> &mut Self {
self.direction = Some(direction);
self
}
pub fn easing(&self) -> &Option<Easing> {
&self.easing
}
pub fn with_easing(&self, easing: Easing) -> Self {
Self {
easing: Some(easing),
..self.clone()
}
}
pub fn set_easing(&mut self, easing: Easing) -> &mut Self {
self.easing = Some(easing);
self
}
pub fn with_group_key(mut self, key: impl Into<String>) -> Self {
self.animation_group_key = Some(key.into());
self
}
pub fn set_group_key(&mut self, key: impl Into<String>) -> &mut Self {
self.animation_group_key = Some(key.into());
self
}
}