using System; using Microsoft.Xna.Framework; using MLEM.Misc; using MLEM.Ui.Elements; namespace MLEM.Ui { /// /// A ui animation is a simple timed event that an in a can use to play a visual or other type of animation. /// To use ui animations, you can use , or one of the built-in style properties like or . /// public class UiAnimation : GenericDataHolder { /// /// The total time that this ui animation plays for. /// public readonly TimeSpan TotalTime; /// /// The that is invoked every . /// public readonly AnimationFunction Function; /// /// An event that is raised when this ui animation is (re)started in . /// public Action Started; /// /// An event that is raised when this ui animation is stopped or finished through . /// public Action Finished; /// /// The current time that this ui animation has been playing for, out of the . /// public TimeSpan CurrentTime { get; private set; } /// /// Creates a new ui animation with the given settings. /// /// The amount of seconds that this ui animation should play for. /// The that is invoked every . public UiAnimation(double seconds, AnimationFunction function) : this(TimeSpan.FromSeconds(seconds), function) {} /// /// Creates a new ui animation with the given settings. /// /// The that this ui animation should play for. /// The that is invoked every . public UiAnimation(TimeSpan totalTime, AnimationFunction function) { this.TotalTime = totalTime; this.Function = function; } /// /// Updates this ui animation, invoking its event if necessary, increasing its and invoking its . /// This method is called by an in . /// /// The element that this ui animation is attached to. /// The game's current time. /// Whether this animation is ready to finish, that is, if its is greater than or equal to its . public virtual bool Update(Element element, GameTime time) { if (this.CurrentTime <= TimeSpan.Zero) this.Started?.Invoke(this, element); this.CurrentTime += time.ElapsedGameTime; this.Function?.Invoke(this, element, this.CurrentTime.Ticks / (float) this.TotalTime.Ticks); return this.CurrentTime >= this.TotalTime; } /// /// Causes this ui animation's event to be raised, and sets the to . /// This allows the animation to play from the start again. /// This method is invoked automatically when returns in , as well as in . /// /// public virtual void OnFinished(Element element) { this.Finished?.Invoke(this, element); this.CurrentTime = TimeSpan.Zero; } /// /// A delegate method used by . /// public delegate void AnimationFunction(UiAnimation animation, Element element, float timePercentage); } }