diff --git a/spine-csharp/spine-csharp.csproj b/spine-csharp/spine-csharp.csproj index beff1de63..65297ac44 100644 --- a/spine-csharp/spine-csharp.csproj +++ b/spine-csharp/spine-csharp.csproj @@ -60,6 +60,7 @@ + @@ -68,6 +69,7 @@ + diff --git a/spine-csharp/src/AnimationState.cs b/spine-csharp/src/AnimationState.cs new file mode 100644 index 000000000..c3ea60729 --- /dev/null +++ b/spine-csharp/src/AnimationState.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; + +namespace Spine { + public class AnimationState { + public AnimationStateData Data { get; private set; } + public Animation Animation { get; private set; } + public float Time { get; set; } + public bool Loop { get; set; } + private Animation previous; + float previousTime; + bool previousLoop; + float mixTime, mixDuration; + + public AnimationState (AnimationStateData data) { + if (data == null) throw new ArgumentNullException("data cannot be null."); + Data = data; + } + + public void Update (float delta) { + Time += delta; + previousTime += delta; + mixTime += delta; + } + + public void Apply (Skeleton skeleton) { + if (Animation == null) return; + if (previous != null) { + previous.Apply(skeleton, previousTime, previousLoop); + float alpha = mixTime / mixDuration; + if (alpha >= 1) { + alpha = 1; + previous = null; + } + Animation.Mix(skeleton, Time, Loop, alpha); + } else + Animation.Apply(skeleton, Time, Loop); + } + + public void SetAnimation (String animationName, bool loop) { + Animation animation = Data.SkeletonData.FindAnimation(animationName); + if (animation == null) throw new ArgumentException("Animation not found: " + animationName); + SetAnimation(animation, loop); + } + + public void SetAnimation (Animation animation, bool loop) { + previous = null; + if (animation != null && Animation != null) { + mixDuration = Data.GetMix(Animation, animation); + if (mixDuration > 0) { + mixTime = 0; + previous = Animation; + previousTime = Time; + previousLoop = Loop; + } + } + Animation = animation; + Loop = loop; + Time = 0; + } + + override public String ToString () { + return (Animation != null && Animation.Name != null) ? Animation.Name : base.ToString(); + } + } +} diff --git a/spine-csharp/src/AnimationStateData.cs b/spine-csharp/src/AnimationStateData.cs new file mode 100644 index 000000000..9bb171dbe --- /dev/null +++ b/spine-csharp/src/AnimationStateData.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; + +namespace Spine { + public class AnimationStateData { + public SkeletonData SkeletonData { get; private set; } + private Dictionary, float> animationToMixTime = new Dictionary, float>(); + + public AnimationStateData (SkeletonData skeletonData) { + SkeletonData = skeletonData; + } + + public void SetMix (String fromName, String toName, float duration) { + Animation from = SkeletonData.FindAnimation(fromName); + if (from == null) throw new ArgumentException("Animation not found: " + fromName); + Animation to = SkeletonData.FindAnimation(toName); + if (to == null) throw new ArgumentException("Animation not found: " + toName); + SetMix(from, to, duration); + } + + public void SetMix (Animation from, Animation to, float duration) { + if (from == null) throw new ArgumentNullException("from cannot be null."); + if (to == null) throw new ArgumentNullException("to cannot be null."); + animationToMixTime.Add(new KeyValuePair(from, to), duration); + } + + public float GetMix (Animation from, Animation to) { + KeyValuePair key = new KeyValuePair(from, to); + float duration; + animationToMixTime.TryGetValue(key, out duration); + return duration; + } + } +} diff --git a/spine-csharp/src/SkeletonJson.cs b/spine-csharp/src/SkeletonJson.cs index 7e7bb4de5..725c1f10a 100644 --- a/spine-csharp/src/SkeletonJson.cs +++ b/spine-csharp/src/SkeletonJson.cs @@ -65,6 +65,7 @@ namespace Spine { SkeletonData skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary; + if (root == null) throw new Exception("Invalid JSON."); // Bones. foreach (Dictionary boneMap in (List)root["bones"]) { diff --git a/spine-csharp/src/Skin.cs b/spine-csharp/src/Skin.cs index 78df8ef03..5a65855e3 100644 --- a/spine-csharp/src/Skin.cs +++ b/spine-csharp/src/Skin.cs @@ -44,9 +44,9 @@ namespace Spine { /** @return May be null. */ public Attachment GetAttachment (int slotIndex, String name) { - KeyValuePair key = new KeyValuePair(slotIndex, name); - if (!attachments.ContainsKey(key)) return null; - return attachments[key]; + Attachment attachment; + attachments.TryGetValue(new KeyValuePair(slotIndex, name), out attachment); + return attachment; } public void FindNamesForSlot (int slotIndex, List names) {