mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2025-12-20 09:16:01 +08:00
AnimationState for spine-csharp.
This commit is contained in:
parent
e1b7b713a2
commit
75e0f68832
@ -60,6 +60,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="src\Animation.cs" />
|
||||
<Compile Include="src\AnimationStateData.cs" />
|
||||
<Compile Include="src\Atlas.cs" />
|
||||
<Compile Include="src\Attachments\AttachmentLoader.cs" />
|
||||
<Compile Include="src\Attachments\AtlasAttachmentLoader.cs" />
|
||||
@ -68,6 +69,7 @@
|
||||
<Compile Include="src\Attachments\RegionAttachment.cs" />
|
||||
<Compile Include="src\Bone.cs" />
|
||||
<Compile Include="src\BoneData.cs" />
|
||||
<Compile Include="src\AnimationState.cs" />
|
||||
<Compile Include="src\Json.cs" />
|
||||
<Compile Include="src\Skeleton.cs" />
|
||||
<Compile Include="src\SkeletonData.cs" />
|
||||
|
||||
66
spine-csharp/src/AnimationState.cs
Normal file
66
spine-csharp/src/AnimationState.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
34
spine-csharp/src/AnimationStateData.cs
Normal file
34
spine-csharp/src/AnimationStateData.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spine {
|
||||
public class AnimationStateData {
|
||||
public SkeletonData SkeletonData { get; private set; }
|
||||
private Dictionary<KeyValuePair<Animation, Animation>, float> animationToMixTime = new Dictionary<KeyValuePair<Animation, Animation>, 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<Animation, Animation>(from, to), duration);
|
||||
}
|
||||
|
||||
public float GetMix (Animation from, Animation to) {
|
||||
KeyValuePair<Animation, Animation> key = new KeyValuePair<Animation, Animation>(from, to);
|
||||
float duration;
|
||||
animationToMixTime.TryGetValue(key, out duration);
|
||||
return duration;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -65,6 +65,7 @@ namespace Spine {
|
||||
SkeletonData skeletonData = new SkeletonData();
|
||||
|
||||
var root = Json.Deserialize(reader) as Dictionary<String, Object>;
|
||||
if (root == null) throw new Exception("Invalid JSON.");
|
||||
|
||||
// Bones.
|
||||
foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) {
|
||||
|
||||
@ -44,9 +44,9 @@ namespace Spine {
|
||||
|
||||
/** @return May be null. */
|
||||
public Attachment GetAttachment (int slotIndex, String name) {
|
||||
KeyValuePair<int, String> key = new KeyValuePair<int, String>(slotIndex, name);
|
||||
if (!attachments.ContainsKey(key)) return null;
|
||||
return attachments[key];
|
||||
Attachment attachment;
|
||||
attachments.TryGetValue(new KeyValuePair<int, String>(slotIndex, name), out attachment);
|
||||
return attachment;
|
||||
}
|
||||
|
||||
public void FindNamesForSlot (int slotIndex, List<String> names) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user