1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-28 07:18:35 +01:00

Compare commits

..

2 commits

Author SHA1 Message Date
Ell
fa60b5e8b1 fixed error in 2df216c 2024-07-14 20:04:18 +02:00
Ell
2df216c15d various sprite animation improvements
- Added indexers and Count to SpriteAnimation and SpriteAnimationGroup
- Marked SpriteAnimation.ByName obsolete in favor of the new indexer
2024-07-14 19:52:32 +02:00
3 changed files with 42 additions and 17 deletions

View file

@ -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

View file

@ -14,6 +14,10 @@ namespace MLEM.Animations {
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>
@ -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];
}
}
/// <summary>

View file

@ -35,13 +35,27 @@ namespace MLEM.Animations {
/// <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 => this.animations.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;
@ -79,8 +93,9 @@ namespace MLEM.Animations {
/// </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() {
@ -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 {
/// <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;
}
}