@@ -165,45 +165,64 @@ pub struct AnimationPlayer {
165165
166166impl AnimationPlayer {
167167 /// Start playing an animation, resetting state of the player
168- /// If `transition_duration` is set, this will use a linear blending
169- /// between the previous and the new animation to make a smooth transition
170- pub fn start (
168+ /// This will use a linear blending between the previous and the new animation to make a smooth transition
169+ pub fn start ( & mut self , handle : Handle < AnimationClip > ) -> & mut Self {
170+ self . animation = PlayingAnimation {
171+ animation_clip : handle,
172+ ..Default :: default ( )
173+ } ;
174+
175+ // We want a hard transition.
176+ // In case any previous transitions are still playing, stop them
177+ self . transitions . clear ( ) ;
178+
179+ self
180+ }
181+
182+ /// Start playing an animation, resetting state of the player
183+ /// This will use a linear blending between the previous and the new animation to make a smooth transition
184+ pub fn start_with_transition (
171185 & mut self ,
172186 handle : Handle < AnimationClip > ,
173- transition_duration : Option < Duration > ,
187+ transition_duration : Duration ,
174188 ) -> & mut Self {
175189 let mut animation = PlayingAnimation {
176190 animation_clip : handle,
177191 ..Default :: default ( )
178192 } ;
179193 std:: mem:: swap ( & mut animation, & mut self . animation ) ;
180- if let Some ( transition_duration) = transition_duration {
181- // Add the current transition. If other transitions are still ongoing,
182- // this will keep those transitions running and cause a transition between
183- // the output of that previous transition to the new animation.
184- self . transitions . push ( AnimationTransition {
185- current_weight : 1.0 ,
186- weight_decline_per_sec : 1.0 / transition_duration. as_secs_f32 ( ) ,
187- animation,
188- } ) ;
189- } else {
190- // We want a hard transition.
191- // In case any previous transitions are still playing, stop them
192- self . transitions . clear ( ) ;
193- }
194+
195+ // Add the current transition. If other transitions are still ongoing,
196+ // this will keep those transitions running and cause a transition between
197+ // the output of that previous transition to the new animation.
198+ self . transitions . push ( AnimationTransition {
199+ current_weight : 1.0 ,
200+ weight_decline_per_sec : 1.0 / transition_duration. as_secs_f32 ( ) ,
201+ animation,
202+ } ) ;
203+
194204 self
195205 }
196206
197207 /// Start playing an animation, resetting state of the player, unless the requested animation is already playing.
198208 /// If `transition_duration` is set, this will use a linear blending
199209 /// between the previous and the new animation to make a smooth transition
200- pub fn play (
210+ pub fn play ( & mut self , handle : Handle < AnimationClip > ) -> & mut Self {
211+ if self . animation . animation_clip != handle || self . is_paused ( ) {
212+ self . start ( handle) ;
213+ }
214+ self
215+ }
216+
217+ /// Start playing an animation, resetting state of the player, unless the requested animation is already playing.
218+ /// This will use a linear blending between the previous and the new animation to make a smooth transition
219+ pub fn play_with_transition (
201220 & mut self ,
202221 handle : Handle < AnimationClip > ,
203- transition_duration : Option < Duration > ,
222+ transition_duration : Duration ,
204223 ) -> & mut Self {
205224 if self . animation . animation_clip != handle || self . is_paused ( ) {
206- self . start ( handle, transition_duration) ;
225+ self . start_with_transition ( handle, transition_duration) ;
207226 }
208227 self
209228 }
0 commit comments