Skip to content

Commit

Permalink
various sprite animation improvements
Browse files Browse the repository at this point in the history
- Added indexers and Count to SpriteAnimation and SpriteAnimationGroup
- Marked SpriteAnimation.ByName obsolete in favor of the new indexer
  • Loading branch information
Ellpeck committed Jul 14, 2024
1 parent fa1cafd commit 2df216c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions MLEM/Animations/SpriteAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class SpriteAnimation : GenericDataHolder {

private readonly AnimationFrame[] frames;
/// <summary>
/// Returns the amount of <see cref="AnimationFrame"/> entries that this sprite animation has.
/// </summary>
public int Count => this.frames.Length;
/// <summary>
/// Returns the <see cref="AnimationFrame"/> at the given index.
/// Index ordering is based on the order that animation frames were added in.
/// </summary>
Expand All @@ -26,15 +30,16 @@ public AnimationFrame CurrentFrame {
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];
}
}
/// <summary>
Expand Down
44 changes: 30 additions & 14 deletions MLEM/Animations/SpriteAnimationGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,27 @@ public SpriteAnimation CurrentAnimation {
/// <inheritdoc cref="SpriteAnimation.SpeedMultiplier"/>
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;
}
}
/// <summary>
/// Returns the amount of <see cref="SpriteAnimation"/> entries that this sprite animation group has.
/// </summary>
public int Count;
/// <summary>
/// Returns the <see cref="SpriteAnimation"/> at the given index.
/// </summary>
/// <param name="index">The index.</param>
public SpriteAnimation this[int index] => this.animations[index].Animation;
/// <summary>
/// Returns the <see cref="SpriteAnimation"/> in this animation group with the given <see cref="SpriteAnimation.Name"/>, if it exists, and <see langword="null"/> otherwise.
/// </summary>
/// <param name="name">The name of the animation.</param>
public SpriteAnimation this[string name] => this.animations.Find(anim => anim.Animation.Name == name)?.Animation;

/// <summary>
/// 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.
/// </summary>
public event AnimationChanged OnAnimationChanged;

Expand Down Expand Up @@ -79,8 +93,9 @@ public void Update(TimeSpan elapsed) {
/// </summary>
/// <param name="name">The <see cref="SpriteAnimation.Name"/> of the animation</param>
/// <returns>The animation by that name, or <c>null</c> if there is none</returns>
[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() {
Expand All @@ -90,7 +105,8 @@ private void FindAnimationToPlay() {
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;
Expand Down Expand Up @@ -122,18 +138,18 @@ private void SortAnimationsIfDirty(bool findAnimationToPlay) {
/// <param name="newAnim">The new animation</param>
public delegate void AnimationChanged(SpriteAnimation oldAnim, SpriteAnimation newAnim);

}
private class ConditionedAnimation {

internal class ConditionedAnimation {
public readonly SpriteAnimation Animation;
public readonly Func<bool> ShouldPlay;
public readonly int Priority;

public readonly SpriteAnimation Animation;
public readonly Func<bool> ShouldPlay;
public readonly int Priority;
public ConditionedAnimation(SpriteAnimation animation, Func<bool> shouldPlay, int priority) {
this.Animation = animation;
this.ShouldPlay = shouldPlay;
this.Priority = priority;
}

public ConditionedAnimation(SpriteAnimation animation, Func<bool> shouldPlay, int priority) {
this.Animation = animation;
this.ShouldPlay = shouldPlay;
this.Priority = priority;
}

}
Expand Down

0 comments on commit 2df216c

Please sign in to comment.