1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

various sprite animation improvements

- Added indexers and Count to SpriteAnimation and SpriteAnimationGroup
- Marked SpriteAnimation.ByName obsolete in favor of the new indexer
This commit is contained in:
Ell 2024-07-14 19:52:32 +02:00
parent fa1cafd751
commit 2df216c15d
3 changed files with 42 additions and 17 deletions

View file

@ -19,11 +19,15 @@ Jump to version:
Additions Additions
- **Added the ability for formatted (tokenized) strings to be drawn with custom rotation, origin and flipping** - **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 a RectangleF.FromCorners overload that accepts points
- Added indexers and Count to SpriteAnimation and SpriteAnimationGroup
Improvements Improvements
- Allow NumberExtensions.GetPoints to include bottom and right coordinates - Allow NumberExtensions.GetPoints to include bottom and right coordinates
- Allow AutoTiling overlayTextures to return null texture regions - Allow AutoTiling overlayTextures to return null texture regions
Removals
- Marked SpriteAnimation.ByName obsolete in favor of the new indexer
### MLEM.Ui ### MLEM.Ui
Additions Additions
- Added the ability to set the anchor that should be used when a tooltip attaches to an element or the mouse - Added the ability to set the anchor that should be used when a tooltip attaches to an element or the mouse

View file

@ -14,6 +14,10 @@ namespace MLEM.Animations {
private readonly AnimationFrame[] frames; private readonly AnimationFrame[] frames;
/// <summary> /// <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. /// Returns the <see cref="AnimationFrame"/> at the given index.
/// Index ordering is based on the order that animation frames were added in. /// Index ordering is based on the order that animation frames were added in.
/// </summary> /// </summary>
@ -26,15 +30,16 @@ namespace MLEM.Animations {
get { get {
// we might have overshot the end time by a little bit, so just return the last frame // we might have overshot the end time by a little bit, so just return the last frame
if (this.TimeIntoAnimation >= this.TotalTime) if (this.TimeIntoAnimation >= this.TotalTime)
return this.frames[this.frames.Length - 1]; return this[this.Count - 1];
var accum = 0D; var accum = 0D;
foreach (var frame in this.frames) { for (var i = 0; i < this.Count; i++) {
var frame = this[i];
accum += frame.Seconds; accum += frame.Seconds;
if (accum >= this.TimeIntoAnimation) if (accum >= this.TimeIntoAnimation)
return frame; return frame;
} }
// if we're here then the time is negative for some reason, so just return the first 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> /// <summary>

View file

@ -35,10 +35,24 @@ namespace MLEM.Animations {
/// <inheritdoc cref="SpriteAnimation.SpeedMultiplier"/> /// <inheritdoc cref="SpriteAnimation.SpeedMultiplier"/>
public float SpeedMultiplier { public float SpeedMultiplier {
set { set {
foreach (var anim in this.animations) for (var i = 0; i < this.Count; i++)
anim.Animation.SpeedMultiplier = value; 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> /// <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.
@ -79,8 +93,9 @@ namespace MLEM.Animations {
/// </summary> /// </summary>
/// <param name="name">The <see cref="SpriteAnimation.Name"/> of the animation</param> /// <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> /// <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) { public SpriteAnimation ByName(string name) {
return this.animations.Find(anim => anim.Animation.Name == name)?.Animation; return this[name];
} }
private void FindAnimationToPlay() { private void FindAnimationToPlay() {
@ -90,7 +105,8 @@ namespace MLEM.Animations {
if (this.currentAnimation != null && this.currentAnimation.ShouldPlay()) if (this.currentAnimation != null && this.currentAnimation.ShouldPlay())
animToPlay = this.currentAnimation; 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 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) if (animToPlay != null && anim.Priority <= animToPlay.Priority)
break; break;
@ -122,18 +138,18 @@ namespace MLEM.Animations {
/// <param name="newAnim">The new animation</param> /// <param name="newAnim">The new animation</param>
public delegate void AnimationChanged(SpriteAnimation oldAnim, SpriteAnimation newAnim); 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 ConditionedAnimation(SpriteAnimation animation, Func<bool> shouldPlay, int priority) {
public readonly Func<bool> ShouldPlay; this.Animation = animation;
public readonly int Priority; this.ShouldPlay = shouldPlay;
this.Priority = priority;
}
public ConditionedAnimation(SpriteAnimation animation, Func<bool> shouldPlay, int priority) {
this.Animation = animation;
this.ShouldPlay = shouldPlay;
this.Priority = priority;
} }
} }