/// <summary> /// Updates the controller. /// </summary> /// <param name="time">Time elapsed since the last call to <see cref="UltravioletContext.Update(UltravioletTime)"/>.</param> public void Update(UltravioletTime time) { if (IsPlaying) { // If the current animation has no frames, there's no need to update. if (animation.Frames.Count < 1) { if (defaultAnimation != null) PlayAnimation(defaultAnimation); return; } // Calculate the duration of the current frame. If we have a specified playback time // for this animation, then we normalize the frame duration and multiply by the playback time. var duration = playbackTime ?? 0.0; if (animation.Frames.Count > 1) { duration = (playbackTime.HasValue) ? (frame.Duration / (float)animation.Frames.Duration) * playbackTime.GetValueOrDefault() : (frame.Duration); } // Update the animation timer and advance to the next frame if necessary. timer += time.ElapsedTime.TotalMilliseconds; if (timer >= duration) { timer -= duration; // Have we reached the end of our frame list? if (frameIndex + 1 == animation.Frames.Count) { // If this is a fire-and-forget animation, play the default. if (defaultAnimation != null) { PlayAnimation(defaultAnimation); return; } // If this is a non-repeating animation, stop it. if (animation.Repeat == SpriteAnimationRepeat.None) { StopAnimation(); return; } // Othewise, return to the first frame of the animation. frameIndex = 0; frame = animation.Frames[frameIndex]; } else { // Advance to the next frame of the animation. frameIndex++; frame = animation.Frames[frameIndex]; } } } }
/// <summary> /// Resets the controller's current animation to its default state. /// </summary> public void ResetAnimation() { this.timer = 0.0; this.frame = (animation == null || animation.Frames.Count == 0) ? null : animation.Frames[0]; this.frameIndex = 0; }
/// <summary> /// Stops the controller's current animation. /// </summary> public void StopAnimation() { this.animation = null; this.defaultAnimation = null; this.playbackTime = null; this.timer = 0.0; this.frame = null; }
/// <summary> /// Draws a single animation frame. /// </summary> /// <param name="frame">The <see cref="SpriteFrame"/> to draw.</param> /// <param name="destinationRectangle">A rectangle which indicates where on the screen the sprite will be drawn.</param> /// <param name="color">The sprite's tint color.</param> /// <param name="rotation">The sprite's rotation in radians.</param> /// <param name="effects">The sprite's rendering effects.</param> /// <param name="layerDepth">The sprite's layer depth.</param> public void DrawFrame(SpriteFrame frame, Rectangle destinationRectangle, Color color, Single rotation, SpriteEffects effects, Single layerDepth) { if (SpriteBatch == null) throw new InvalidOperationException(PresentationStrings.DrawingContextDoesNotHaveSpriteBatch); SpriteBatch.DrawFrame(frame, destinationRectangle, color * Opacity, rotation, effects, layerDepth); }