diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ec39b8..8380f02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,11 +19,15 @@ Jump to version: Additions - **Added the ability for formatted (tokenized) strings to be drawn with custom rotation, origin and flipping** - Added a RectangleF.FromCorners overload that accepts points +- Added indexers and Count to SpriteAnimation and SpriteAnimationGroup Improvements - Allow NumberExtensions.GetPoints to include bottom and right coordinates - Allow AutoTiling overlayTextures to return null texture regions +Removals +- Marked SpriteAnimation.ByName obsolete in favor of the new indexer + ### MLEM.Ui Additions - Added the ability to set the anchor that should be used when a tooltip attaches to an element or the mouse diff --git a/MLEM/Animations/SpriteAnimation.cs b/MLEM/Animations/SpriteAnimation.cs index 06b941d..2326b5a 100644 --- a/MLEM/Animations/SpriteAnimation.cs +++ b/MLEM/Animations/SpriteAnimation.cs @@ -14,6 +14,10 @@ namespace MLEM.Animations { private readonly AnimationFrame[] frames; /// + /// Returns the amount of entries that this sprite animation has. + /// + public int Count => this.frames.Length; + /// /// Returns the at the given index. /// Index ordering is based on the order that animation frames were added in. /// @@ -26,15 +30,16 @@ namespace MLEM.Animations { get { // we might have overshot the end time by a little bit, so just return the last frame if (this.TimeIntoAnimation >= this.TotalTime) - return this.frames[this.frames.Length - 1]; + return this[this.Count - 1]; var accum = 0D; - foreach (var frame in this.frames) { + for (var i = 0; i < this.Count; i++) { + var frame = this[i]; accum += frame.Seconds; if (accum >= this.TimeIntoAnimation) return frame; } // if we're here then the time is negative for some reason, so just return the first frame - return this.frames[0]; + return this[0]; } } /// diff --git a/MLEM/Animations/SpriteAnimationGroup.cs b/MLEM/Animations/SpriteAnimationGroup.cs index 0bacf27..b817077 100644 --- a/MLEM/Animations/SpriteAnimationGroup.cs +++ b/MLEM/Animations/SpriteAnimationGroup.cs @@ -35,13 +35,27 @@ namespace MLEM.Animations { /// public float SpeedMultiplier { set { - foreach (var anim in this.animations) - anim.Animation.SpeedMultiplier = value; + for (var i = 0; i < this.Count; i++) + this[i].SpeedMultiplier = value; } } + /// + /// Returns the amount of entries that this sprite animation group has. + /// + public int Count; + /// + /// Returns the at the given index. + /// + /// The index. + public SpriteAnimation this[int index] => this.animations[index].Animation; + /// + /// Returns the in this animation group with the given , if it exists, and otherwise. + /// + /// The name of the animation. + public SpriteAnimation this[string name] => this.animations.Find(anim => anim.Animation.Name == name)?.Animation; /// - /// A callback for when the currently displaying animation has changed due to a condition with a higher priority being met. + /// A callback for when the currently displaying animation has changed due to a condition with a higher priority being met. /// public event AnimationChanged OnAnimationChanged; @@ -79,8 +93,9 @@ namespace MLEM.Animations { /// /// The of the animation /// The animation by that name, or null if there is none + [Obsolete("Use the name-based indexer instead")] public SpriteAnimation ByName(string name) { - return this.animations.Find(anim => anim.Animation.Name == name)?.Animation; + return this[name]; } private void FindAnimationToPlay() { @@ -90,7 +105,8 @@ namespace MLEM.Animations { if (this.currentAnimation != null && this.currentAnimation.ShouldPlay()) animToPlay = this.currentAnimation; - foreach (var anim in this.animations) { + for (var i = 0; i < this.Count; i++) { + var anim = this.animations[i]; // if we find an animation with a lower priority then it means we can break since the list is sorted by priority if (animToPlay != null && anim.Priority <= animToPlay.Priority) break; @@ -122,18 +138,18 @@ namespace MLEM.Animations { /// The new animation public delegate void AnimationChanged(SpriteAnimation oldAnim, SpriteAnimation newAnim); - } + private class ConditionedAnimation { - internal class ConditionedAnimation { + public readonly SpriteAnimation Animation; + public readonly Func ShouldPlay; + public readonly int Priority; - public readonly SpriteAnimation Animation; - public readonly Func ShouldPlay; - public readonly int Priority; + public ConditionedAnimation(SpriteAnimation animation, Func shouldPlay, int priority) { + this.Animation = animation; + this.ShouldPlay = shouldPlay; + this.Priority = priority; + } - public ConditionedAnimation(SpriteAnimation animation, Func shouldPlay, int priority) { - this.Animation = animation; - this.ShouldPlay = shouldPlay; - this.Priority = priority; } }