More AnimationState refactoring, getting good!

#113
This commit is contained in:
NathanSweet 2013-09-25 19:39:48 +02:00
parent 6441bd93b6
commit 2df28f8564
7 changed files with 389 additions and 329 deletions

View File

@ -70,7 +70,7 @@ namespace Spine {
List<Timeline> timelines = this.timelines; List<Timeline> timelines = this.timelines;
for (int i = 0, n = timelines.Count; i < n; i++) for (int i = 0, n = timelines.Count; i < n; i++)
timelines[i].Apply(skeleton, lastTime, time, 1, events); timelines[i].Apply(skeleton, lastTime, time, events, 1);
} }
/** @deprecated */ /** @deprecated */
@ -92,7 +92,7 @@ namespace Spine {
List<Timeline> timelines = this.timelines; List<Timeline> timelines = this.timelines;
for (int i = 0, n = timelines.Count; i < n; i++) for (int i = 0, n = timelines.Count; i < n; i++)
timelines[i].Apply(skeleton, lastTime, time, alpha, events); timelines[i].Apply(skeleton, lastTime, time, events, alpha);
} }
/** @param target After the first and before the last entry. */ /** @param target After the first and before the last entry. */
@ -120,7 +120,7 @@ namespace Spine {
public interface Timeline { public interface Timeline {
/** Sets the value(s) for the specified time. */ /** Sets the value(s) for the specified time. */
void Apply (Skeleton skeleton, float lastTime, float time, float alpha, List<Event> firedEvents); void Apply (Skeleton skeleton, float lastTime, float time, List<Event> firedEvents, float alpha);
} }
/** Base class for frames that use an interpolation bezier curve. */ /** Base class for frames that use an interpolation bezier curve. */
@ -136,7 +136,7 @@ namespace Spine {
curves = new float[(frameCount - 1) * 6]; curves = new float[(frameCount - 1) * 6];
} }
abstract public void Apply (Skeleton skeleton, float lastTime, float time, float alpha, List<Event> firedEvents); abstract public void Apply (Skeleton skeleton, float lastTime, float time, List<Event> firedEvents, float alpha);
public void SetLinear (int frameIndex) { public void SetLinear (int frameIndex) {
curves[frameIndex * 6] = LINEAR; curves[frameIndex * 6] = LINEAR;
@ -225,7 +225,7 @@ namespace Spine {
frames[frameIndex + 1] = angle; frames[frameIndex + 1] = angle;
} }
override public void Apply (Skeleton skeleton, float lastTime, float time, float alpha, List<Event> firedEvents) { override public void Apply (Skeleton skeleton, float lastTime, float time, List<Event> firedEvents, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -288,7 +288,7 @@ namespace Spine {
frames[frameIndex + 2] = y; frames[frameIndex + 2] = y;
} }
override public void Apply (Skeleton skeleton, float lastTime, float time, float alpha, List<Event> firedEvents) { override public void Apply (Skeleton skeleton, float lastTime, float time, List<Event> firedEvents, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -318,7 +318,7 @@ namespace Spine {
: base(frameCount) { : base(frameCount) {
} }
override public void Apply (Skeleton skeleton, float lastTime, float time, float alpha, List<Event> firedEvents) { override public void Apply (Skeleton skeleton, float lastTime, float time, List<Event> firedEvents, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -370,7 +370,7 @@ namespace Spine {
frames[frameIndex + 4] = a; frames[frameIndex + 4] = a;
} }
override public void Apply (Skeleton skeleton, float lastTime, float time, float alpha, List<Event> firedEvents) { override public void Apply (Skeleton skeleton, float lastTime, float time, List<Event> firedEvents, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -434,7 +434,7 @@ namespace Spine {
attachmentNames[frameIndex] = attachmentName; attachmentNames[frameIndex] = attachmentName;
} }
public void Apply (Skeleton skeleton, float lastTime, float time, float alpha, List<Event> firedEvents) { public void Apply (Skeleton skeleton, float lastTime, float time, List<Event> firedEvents, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -469,7 +469,7 @@ namespace Spine {
events[frameIndex] = e; events[frameIndex] = e;
} }
public void Apply (Skeleton skeleton, float lastTime, float time, float alpha, List<Event> firedEvents) { public void Apply (Skeleton skeleton, float lastTime, float time, List<Event> firedEvents, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -478,7 +478,7 @@ namespace Spine {
if (lastTime > time) { if (lastTime > time) {
// Fire events after last time for looped animations. // Fire events after last time for looped animations.
Apply(skeleton, lastTime, int.MaxValue, alpha, firedEvents); Apply(skeleton, lastTime, int.MaxValue, firedEvents, alpha);
lastTime = 0; lastTime = 0;
} }
@ -520,7 +520,7 @@ namespace Spine {
drawOrders[frameIndex] = drawOrder; drawOrders[frameIndex] = drawOrder;
} }
public void Apply (Skeleton skeleton, float lastTime, float time, float alpha, List<Event> firedEvents) { public void Apply (Skeleton skeleton, float lastTime, float time, List<Event> firedEvents, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.

View File

@ -33,32 +33,18 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
namespace Spine { namespace Spine {
public class AnimationState { public class AnimationState {
private AnimationStateData data; private AnimationStateData data;
private QueuedAnimation current, previous; private List<TrackEntry> tracks = new List<TrackEntry>();
private float mixTime, mixDuration;
private List<QueuedAnimation> queue = new List<QueuedAnimation>();
private List<Event> events = new List<Event>(); private List<Event> events = new List<Event>();
public AnimationStateData Data { get { return data; } } public AnimationStateData Data { get { return data; } }
public List<QueuedAnimation> Queue { get { return queue; } }
public QueuedAnimation Current { get { return current; } }
public Animation Animation {
get { return current != null ? current.animation : null; }
}
public float Time {
get { return current != null ? current.time : 0; }
set { if (current != null) current.Time = value; }
}
public bool Loop {
get { return current != null ? current.loop : false; }
set { if (current != null) current.Loop = value; }
}
public event EventHandler Start; public event EventHandler<StartEndArgs> Start;
public event EventHandler End; public event EventHandler<StartEndArgs> End;
public event EventHandler<EventTriggeredArgs> Event; public event EventHandler<EventTriggeredArgs> Event;
public event EventHandler<CompleteArgs> Complete; public event EventHandler<CompleteArgs> Complete;
@ -68,177 +54,218 @@ namespace Spine {
} }
public void Update (float delta) { public void Update (float delta) {
QueuedAnimation current = this.current; for (int i = 0, n = tracks.Count; i < n; i++) {
if (current == null) return; TrackEntry current = tracks[i];
if (current == null) continue;
float time = current.time; float time = current.time + delta;
float duration = current.endTime; float endTime = current.endTime;
current.time = time + delta; current.time = time;
if (previous != null) { if (current.previous != null) {
previous.time += delta; current.previous.time += delta;
mixTime += delta; current.mixTime += delta;
} }
// Check if completed the animation or a loop iteration. // Check if completed the animation or a loop iteration.
if (current.loop ? (current.lastTime % duration > time % duration) : (current.lastTime < duration && time >= duration)) { if (current.loop ? (current.lastTime % endTime > time % endTime) : (current.lastTime < endTime && time >= endTime)) {
int count = (int)(time / duration); int count = (int)(time / endTime);
current.OnComplete(this, count); current.OnComplete(this, i, count);
if (Complete != null) Complete(this, new CompleteArgs(count)); if (Complete != null) Complete(this, new CompleteArgs(i, count));
} }
if (queue.Count > 0) { TrackEntry next = current.next;
QueuedAnimation entry = queue[0]; if (next != null && time >= next.delay) {
if (time >= entry.delay) { if (next.animation == null)
if (entry.animation == null) Clear(i);
ClearAnimation(); else
else { SetCurrent(i, next);
SetAnimationEntry(entry);
queue.RemoveAt(0);
}
} }
} }
} }
public void Apply (Skeleton skeleton) { public void Apply (Skeleton skeleton) {
QueuedAnimation current = this.current; List<Event> events = this.events;
for (int i = 0, n = tracks.Count; i < n; i++) {
TrackEntry current = tracks[i];
if (current == null) continue;
events.Clear();
TrackEntry previous = current.previous;
if (previous == null)
current.animation.Apply(skeleton, current.lastTime, current.time, current.loop, events);
else {
previous.animation.Apply(skeleton, int.MaxValue, previous.time, previous.loop, null);
float alpha = current.mixTime / current.mixDuration;
if (alpha >= 1) {
alpha = 1;
current.previous = null;
}
current.animation.Mix(skeleton, current.lastTime, current.time, current.loop, events, alpha);
}
for (int ii = 0, nn = events.Count; ii < nn; ii++) {
Event e = events[ii];
current.OnEvent(this, i, e);
if (Event != null) Event(this, new EventTriggeredArgs(i, e));
}
current.lastTime = current.time;
}
}
public void Clear () {
for (int i = 0, n = tracks.Count; i < n; i++)
Clear(i);
tracks.Clear();
}
public void Clear (int trackIndex) {
if (trackIndex >= tracks.Count) return;
TrackEntry current = tracks[trackIndex];
if (current == null) return; if (current == null) return;
List<Event> events = this.events; current.OnEnd(this, trackIndex);
events.Clear(); if (End != null) End(this, new StartEndArgs(trackIndex));
QueuedAnimation previous = this.previous; tracks[trackIndex] = null;
if (previous != null) {
previous.animation.Apply(skeleton, int.MaxValue, previous.time, previous.loop, null);
float alpha = mixTime / mixDuration;
if (alpha >= 1) {
alpha = 1;
this.previous = null;
}
current.animation.Mix(skeleton, current.lastTime, current.time, current.loop, events, alpha);
} else
current.animation.Apply(skeleton, current.lastTime, current.time, current.loop, events);
foreach (Event e in events) {
current.OnEvent(this, e);
if (Event != null) Event(this, new EventTriggeredArgs(e));
}
current.lastTime = current.time;
} }
public void ClearAnimation () { private TrackEntry ExpandToIndex (int index) {
previous = null; if (index < tracks.Count) return tracks[index];
current = null; while (index >= tracks.Count)
queue.Clear(); tracks.Add(null);
return null;
} }
private void SetAnimationEntry (QueuedAnimation entry) { private void SetCurrent (int index, TrackEntry entry) {
previous = null; TrackEntry current = ExpandToIndex(index);
QueuedAnimation current = this.current;
if (current != null) { if (current != null) {
current.OnEnd(this); current.previous = null;
if (End != null) End(this, EventArgs.Empty);
mixDuration = data.GetMix(current.animation, entry.animation); current.OnEnd(this, index);
if (mixDuration > 0) { if (End != null) End(this, new StartEndArgs(index));
mixTime = 0;
previous = current; entry.mixDuration = data.GetMix(current.animation, entry.animation);
if (entry.mixDuration > 0) {
entry.mixTime = 0;
entry.previous = current;
} }
} }
this.current = entry;
entry.OnStart(this); tracks[index] = entry;
if (Start != null) Start(this, EventArgs.Empty);
entry.OnStart(this, index);
if (Start != null) Start(this, new StartEndArgs(index));
} }
public QueuedAnimation SetAnimation (String animationName, bool loop) { public TrackEntry SetAnimation (int trackIndex, String animationName, bool loop) {
Animation animation = data.skeletonData.FindAnimation(animationName); Animation animation = data.skeletonData.FindAnimation(animationName);
if (animation == null) throw new ArgumentException("Animation not found: " + animationName); if (animation == null) throw new ArgumentException("Animation not found: " + animationName);
return SetAnimation(animation, loop); return SetAnimation(trackIndex, animation, loop);
} }
/** Set the current animation. Any queued animations are cleared. */ /** Set the current animation. Any queued animations are cleared. */
public QueuedAnimation SetAnimation (Animation animation, bool loop) { public TrackEntry SetAnimation (int trackIndex, Animation animation, bool loop) {
queue.Clear(); TrackEntry entry = new TrackEntry();
QueuedAnimation entry = new QueuedAnimation();
entry.animation = animation; entry.animation = animation;
entry.loop = loop; entry.loop = loop;
entry.time = 0; entry.time = 0;
entry.endTime = animation.Duration; entry.endTime = animation.Duration;
SetAnimationEntry(entry); SetCurrent(trackIndex, entry);
return entry; return entry;
} }
public QueuedAnimation AddAnimation (String animationName, bool loop) { public TrackEntry AddAnimation (int trackIndex, String animationName, bool loop, float delay) {
return AddAnimation(animationName, loop, 0);
}
public QueuedAnimation AddAnimation (String animationName, bool loop, float delay) {
Animation animation = data.skeletonData.FindAnimation(animationName); Animation animation = data.skeletonData.FindAnimation(animationName);
if (animation == null) throw new ArgumentException("Animation not found: " + animationName); if (animation == null) throw new ArgumentException("Animation not found: " + animationName);
return AddAnimation(animation, loop, delay); return AddAnimation(trackIndex, animation, loop, delay);
}
public QueuedAnimation AddAnimation (Animation animation, bool loop) {
return AddAnimation(animation, loop, 0);
} }
/** Adds an animation to be played delay seconds after the current or last queued animation. /** Adds an animation to be played delay seconds after the current or last queued animation.
* @param delay May be <= 0 to use duration of previous animation minus any mix duration plus the negative delay. */ * @param delay May be <= 0 to use duration of previous animation minus any mix duration plus the negative delay. */
public QueuedAnimation AddAnimation (Animation animation, bool loop, float delay) { public TrackEntry AddAnimation (int trackIndex, Animation animation, bool loop, float delay) {
QueuedAnimation entry = new QueuedAnimation(); TrackEntry entry = new TrackEntry();
entry.animation = animation; entry.animation = animation;
entry.loop = loop; entry.loop = loop;
entry.time = 0; entry.time = 0;
entry.endTime = animation != null ? animation.Duration : 0; entry.endTime = animation != null ? animation.Duration : 0;
TrackEntry last = ExpandToIndex(trackIndex);
if (last != null) {
while (last.next != null)
last = last.next;
last.next = entry;
} else
tracks[trackIndex] = entry;
if (delay <= 0) { if (delay <= 0) {
QueuedAnimation previousEntry = queue.Count > 0 ? queue[queue.Count - 1] : current; if (last != null) {
if (previousEntry != null) { delay += last.endTime;
delay += previousEntry.endTime; if (animation != null) delay += -data.GetMix(last.animation, animation);
if (animation != null) delay += -data.GetMix(previousEntry.animation, animation);
} else } else
delay = 0; delay = 0;
} }
entry.delay = delay; entry.delay = delay;
queue.Add(entry);
return entry; return entry;
} }
/** Returns true if no animation is set or if the current time is greater than the animation duration, regardless of looping. */ /** @return May be null. */
public bool IsComplete () { public TrackEntry getTrackEntry (int trackIndex) {
return current == null || current.time >= current.endTime; if (trackIndex >= tracks.Count) return null;
return tracks[trackIndex];
} }
override public String ToString () { override public String ToString () {
if (current == null || current.animation == null) return "<none>"; StringBuilder buffer = new StringBuilder();
return current.animation.Name; for (int i = 0, n = tracks.Count; i < n; i++) {
TrackEntry entry = tracks[i];
if (entry == null) continue;
if (buffer.Length > 0) buffer.Append(", ");
buffer.Append(entry.ToString());
}
if (buffer.Length == 0) return "<none>";
return buffer.ToString();
} }
} }
public class EventTriggeredArgs : EventArgs { public class EventTriggeredArgs : EventArgs {
public int TrackIndex { get; private set; }
public Event Event { get; private set; } public Event Event { get; private set; }
public EventTriggeredArgs (Event e) { public EventTriggeredArgs (int trackIndex, Event e) {
TrackIndex = trackIndex;
Event = e; Event = e;
} }
} }
public class CompleteArgs : EventArgs { public class CompleteArgs : EventArgs {
public int TrackIndex { get; private set; }
public int LoopCount { get; private set; } public int LoopCount { get; private set; }
public CompleteArgs (int loopCount) { public CompleteArgs (int trackIndex, int loopCount) {
TrackIndex = trackIndex;
LoopCount = loopCount; LoopCount = loopCount;
} }
} }
public class QueuedAnimation { public class StartEndArgs : EventArgs {
public int TrackIndex { get; private set; }
public StartEndArgs (int trackIndex) {
TrackIndex = trackIndex;
}
}
public class TrackEntry {
internal TrackEntry next, previous;
internal Animation animation; internal Animation animation;
internal bool loop; internal bool loop;
internal float delay, time, lastTime, endTime; internal float delay, time, lastTime, endTime;
internal float mixTime, mixDuration;
public Animation Animation { get { return animation; } } public Animation Animation { get { return animation; } }
public bool Loop { get { return loop; } set { loop = value; } } public bool Loop { get { return loop; } set { loop = value; } }
@ -253,25 +280,25 @@ namespace Spine {
} }
} }
public event EventHandler Start; public event EventHandler<StartEndArgs> Start;
public event EventHandler End; public event EventHandler<StartEndArgs> End;
public event EventHandler<EventTriggeredArgs> Event; public event EventHandler<EventTriggeredArgs> Event;
public event EventHandler<CompleteArgs> Complete; public event EventHandler<CompleteArgs> Complete;
internal void OnStart (AnimationState state) { internal void OnStart (AnimationState state, int index) {
if (Start != null) Start(state, EventArgs.Empty); if (Start != null) Start(state, new StartEndArgs(index));
} }
internal void OnEnd (AnimationState state) { internal void OnEnd (AnimationState state, int index) {
if (End != null) End(state, EventArgs.Empty); if (End != null) End(state, new StartEndArgs(index));
} }
internal void OnEvent (AnimationState state, Event e) { internal void OnEvent (AnimationState state, int index, Event e) {
if (Event != null) Event(state, new EventTriggeredArgs(e)); if (Event != null) Event(state, new EventTriggeredArgs(index, e));
} }
internal void OnComplete (AnimationState state, int loopCount) { internal void OnComplete (AnimationState state, int index, int loopCount) {
if (Complete != null) Complete(state, new CompleteArgs(loopCount)); if (Complete != null) Complete(state, new CompleteArgs(index, loopCount));
} }
} }
} }

View File

@ -81,7 +81,7 @@ public class Animation {
Array<Timeline> timelines = this.timelines; Array<Timeline> timelines = this.timelines;
for (int i = 0, n = timelines.size; i < n; i++) for (int i = 0, n = timelines.size; i < n; i++)
timelines.get(i).apply(skeleton, lastTime, time, 1, events); timelines.get(i).apply(skeleton, lastTime, time, events, 1);
} }
/** @deprecated */ /** @deprecated */
@ -103,7 +103,7 @@ public class Animation {
Array<Timeline> timelines = this.timelines; Array<Timeline> timelines = this.timelines;
for (int i = 0, n = timelines.size; i < n; i++) for (int i = 0, n = timelines.size; i < n; i++)
timelines.get(i).apply(skeleton, lastTime, time, alpha, events); timelines.get(i).apply(skeleton, lastTime, time, events, alpha);
} }
public String getName () { public String getName () {
@ -138,7 +138,7 @@ public class Animation {
static public interface Timeline { static public interface Timeline {
/** Sets the value(s) for the specified time. */ /** Sets the value(s) for the specified time. */
public void apply (Skeleton skeleton, float lastTime, float time, float alpha, Array<Event> events); public void apply (Skeleton skeleton, float lastTime, float time, Array<Event> events, float alpha);
} }
/** Base class for frames that use an interpolation bezier curve. */ /** Base class for frames that use an interpolation bezier curve. */
@ -263,7 +263,7 @@ public class Animation {
frames[frameIndex + 1] = angle; frames[frameIndex + 1] = angle;
} }
public void apply (Skeleton skeleton, float lastTime, float time, float alpha, Array<Event> events) { public void apply (Skeleton skeleton, float lastTime, float time, Array<Event> events, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -333,7 +333,7 @@ public class Animation {
frames[frameIndex + 2] = y; frames[frameIndex + 2] = y;
} }
public void apply (Skeleton skeleton, float lastTime, float time, float alpha, Array<Event> events) { public void apply (Skeleton skeleton, float lastTime, float time, Array<Event> events, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -363,7 +363,7 @@ public class Animation {
super(frameCount); super(frameCount);
} }
public void apply (Skeleton skeleton, float lastTime, float time, float alpha, Array<Event> events) { public void apply (Skeleton skeleton, float lastTime, float time, Array<Event> events, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -426,7 +426,7 @@ public class Animation {
frames[frameIndex + 4] = a; frames[frameIndex + 4] = a;
} }
public void apply (Skeleton skeleton, float lastTime, float time, float alpha, Array<Event> events) { public void apply (Skeleton skeleton, float lastTime, float time, Array<Event> events, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -499,7 +499,7 @@ public class Animation {
attachmentNames[frameIndex] = attachmentName; attachmentNames[frameIndex] = attachmentName;
} }
public void apply (Skeleton skeleton, float lastTime, float time, float alpha, Array<Event> events) { public void apply (Skeleton skeleton, float lastTime, float time, Array<Event> events, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -542,7 +542,7 @@ public class Animation {
events[frameIndex] = event; events[frameIndex] = event;
} }
public void apply (Skeleton skeleton, float lastTime, float time, float alpha, Array<Event> firedEvents) { public void apply (Skeleton skeleton, float lastTime, float time, Array<Event> firedEvents, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.
@ -551,7 +551,7 @@ public class Animation {
if (lastTime > time) { if (lastTime > time) {
// Fire events after last time for looped animations. // Fire events after last time for looped animations.
apply(skeleton, lastTime, Integer.MAX_VALUE, alpha, firedEvents); apply(skeleton, lastTime, Integer.MAX_VALUE, firedEvents, alpha);
lastTime = 0; lastTime = 0;
} }
@ -601,7 +601,7 @@ public class Animation {
drawOrders[frameIndex] = drawOrder; drawOrders[frameIndex] = drawOrder;
} }
public void apply (Skeleton skeleton, float lastTime, float time, float alpha, Array<Event> firedEvents) { public void apply (Skeleton skeleton, float lastTime, float time, Array<Event> firedEvents, float alpha) {
float[] frames = this.frames; float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame. if (time < frames[0]) return; // Time is before first frame.

View File

@ -40,11 +40,9 @@ import com.badlogic.gdx.utils.Pools;
/** Stores state for an animation and automatically mixes between animations. */ /** Stores state for an animation and automatically mixes between animations. */
public class AnimationState { public class AnimationState {
private final AnimationStateData data; private final AnimationStateData data;
private QueuedAnimation current, previous; private Array<TrackEntry> tracks = new Array();
private float mixTime, mixDuration;
private final Array<QueuedAnimation> queue = new Array();
private final Array<AnimationStateListener> listeners = new Array();
private final Array<Event> events = new Array(); private final Array<Event> events = new Array();
private final Array<AnimationStateListener> listeners = new Array();
public AnimationState (AnimationStateData data) { public AnimationState (AnimationStateData data) {
if (data == null) throw new IllegalArgumentException("data cannot be null."); if (data == null) throw new IllegalArgumentException("data cannot be null.");
@ -52,202 +50,196 @@ public class AnimationState {
} }
public void update (float delta) { public void update (float delta) {
QueuedAnimation current = this.current; for (int i = 0, n = tracks.size; i < n; i++) {
if (current == null) return; TrackEntry current = tracks.get(i);
if (current == null) continue;
float time = current.time; float time = current.time + delta;
float duration = current.endTime; float endTime = current.endTime;
current.time = time + delta; current.time = time;
if (previous != null) { if (current.previous != null) {
previous.time += delta; current.previous.time += delta;
mixTime += delta; current.mixTime += delta;
} }
// Check if completed the animation or a loop iteration. // Check if completed the animation or a loop iteration.
if (current.loop ? (current.lastTime % duration > time % duration) : (current.lastTime < duration && time >= duration)) { if (current.loop ? (current.lastTime % endTime > time % endTime) : (current.lastTime < endTime && time >= endTime)) {
int count = (int)(time / duration); int count = (int)(time / endTime);
if (current.listener != null) current.listener.complete(count); if (current.listener != null) current.listener.complete(i, count);
for (int i = 0, n = listeners.size; i < n; i++) for (int ii = 0, nn = listeners.size; ii < nn; ii++)
listeners.get(i).complete(count); listeners.get(ii).complete(i, count);
} }
if (queue.size > 0) { TrackEntry next = current.next;
QueuedAnimation entry = queue.first(); if (next != null && time >= next.delay) {
if (time >= entry.delay) { if (next.animation == null)
if (entry.animation == null) clear(i);
clearAnimation(); else
else { setCurrent(i, next);
setAnimationEntry(entry);
queue.removeIndex(0);
}
} }
} }
} }
public void apply (Skeleton skeleton) { public void apply (Skeleton skeleton) {
QueuedAnimation current = this.current; Array<Event> events = this.events;
int listenerCount = listeners.size;
for (int i = 0, n = tracks.size; i < n; i++) {
TrackEntry current = tracks.get(i);
if (current == null) continue;
events.size = 0;
TrackEntry previous = current.previous;
if (previous == null)
current.animation.apply(skeleton, current.lastTime, current.time, current.loop, events);
else {
previous.animation.apply(skeleton, Integer.MAX_VALUE, previous.time, previous.loop, null);
float alpha = current.mixTime / current.mixDuration;
if (alpha >= 1) {
alpha = 1;
Pools.free(previous);
current.previous = null;
}
current.animation.mix(skeleton, current.lastTime, current.time, current.loop, events, alpha);
}
for (int ii = 0, nn = events.size; ii < nn; ii++) {
Event event = events.get(ii);
if (current.listener != null) current.listener.event(i, event);
for (int iii = 0; iii < listenerCount; iii++)
listeners.get(iii).event(i, event);
}
current.lastTime = current.time;
}
}
public void clear () {
for (int i = 0, n = tracks.size; i < n; i++)
clear(i);
tracks.clear();
}
public void clear (int trackIndex) {
if (trackIndex >= tracks.size) return;
TrackEntry current = tracks.get(trackIndex);
if (current == null) return; if (current == null) return;
Array<Event> events = this.events; if (current.listener != null) current.listener.end(trackIndex);
events.size = 0; for (int i = 0, n = listeners.size; i < n; i++)
listeners.get(i).end(trackIndex);
QueuedAnimation previous = this.previous; tracks.set(trackIndex, null);
if (previous != null) { freeAll(current);
previous.animation.apply(skeleton, Integer.MAX_VALUE, previous.time, previous.loop, null); if (current.previous != null) Pools.free(current.previous);
float alpha = mixTime / mixDuration; }
if (alpha >= 1) {
alpha = 1; private void freeAll (TrackEntry entry) {
Pools.free(previous); while (entry != null) {
this.previous = null; TrackEntry next = entry.next;
Pools.free(entry);
entry = next;
}
}
private TrackEntry expandToIndex (int index) {
if (index < tracks.size) return tracks.get(index);
tracks.ensureCapacity(index - tracks.size + 1);
tracks.size = index + 1;
return null;
}
private void setCurrent (int index, TrackEntry entry) {
TrackEntry current = expandToIndex(index);
if (current != null) {
if (current.previous != null) {
Pools.free(current.previous);
current.previous = null;
} }
current.animation.mix(skeleton, current.lastTime, current.time, current.loop, events, alpha);
} else
current.animation.apply(skeleton, current.lastTime, current.time, current.loop, events);
int listenerCount = listeners.size; if (current.listener != null) current.listener.end(index);
for (int i = 0, n = events.size; i < n; i++) {
Event event = events.get(i);
if (current.listener != null) current.listener.event(event);
for (int ii = 0; ii < listenerCount; ii++)
listeners.get(ii).event(event);
}
current.lastTime = current.time;
}
public void clearAnimation () {
if (previous != null) {
Pools.free(previous);
previous = null;
}
if (current != null) {
Pools.free(current);
current = null;
}
clearQueue();
}
private void clearQueue () {
Pools.freeAll(queue);
queue.clear();
}
private void setAnimationEntry (QueuedAnimation entry) {
if (previous != null) {
Pools.free(previous);
previous = null;
}
QueuedAnimation current = this.current;
if (current != null) {
if (current.listener != null) current.listener.end();
for (int i = 0, n = listeners.size; i < n; i++) for (int i = 0, n = listeners.size; i < n; i++)
listeners.get(i).end(); listeners.get(i).end(index);
mixDuration = data.getMix(current.animation, entry.animation); entry.mixDuration = data.getMix(current.animation, entry.animation);
if (mixDuration > 0) { if (entry.mixDuration > 0) {
mixTime = 0; entry.mixTime = 0;
previous = current; entry.previous = current;
} else } else
Pools.free(current); Pools.free(current);
} }
this.current = entry;
if (entry != null && entry.listener != null) entry.listener.start(); tracks.set(index, entry);
if (entry.listener != null) entry.listener.start(index);
for (int i = 0, n = listeners.size; i < n; i++) for (int i = 0, n = listeners.size; i < n; i++)
listeners.get(i).start(); listeners.get(i).start(index);
} }
/** @see #setAnimation(Animation, boolean) */ /** @see #setAnimation(int, Animation, boolean) */
public QueuedAnimation setAnimation (String animationName, boolean loop) { public TrackEntry setAnimation (int trackIndex, String animationName, boolean loop) {
Animation animation = data.getSkeletonData().findAnimation(animationName); Animation animation = data.getSkeletonData().findAnimation(animationName);
if (animation == null) throw new IllegalArgumentException("Animation not found: " + animationName); if (animation == null) throw new IllegalArgumentException("Animation not found: " + animationName);
return setAnimation(animation, loop); return setAnimation(trackIndex, animation, loop);
} }
/** Set the current animation. Any queued animations are cleared. */ /** Set the current animation. Any queued animations are cleared. */
public QueuedAnimation setAnimation (Animation animation, boolean loop) { public TrackEntry setAnimation (int trackIndex, Animation animation, boolean loop) {
clearQueue(); TrackEntry current = expandToIndex(trackIndex);
if (current != null) freeAll(current.next);
QueuedAnimation entry = Pools.obtain(QueuedAnimation.class); TrackEntry entry = Pools.obtain(TrackEntry.class);
entry.animation = animation; entry.animation = animation;
entry.loop = loop; entry.loop = loop;
entry.time = 0; entry.time = 0;
entry.endTime = animation.getDuration(); entry.endTime = animation.getDuration();
setAnimationEntry(entry); setCurrent(trackIndex, entry);
return entry; return entry;
} }
/** @see #addAnimation(Animation, boolean) */ /** {@link #addAnimation(int, Animation, boolean, float)} */
public QueuedAnimation addAnimation (String animationName, boolean loop) { public TrackEntry addAnimation (int trackIndex, String animationName, boolean loop, float delay) {
return addAnimation(animationName, loop, 0);
}
/** @see #addAnimation(Animation, boolean, float) */
public QueuedAnimation addAnimation (String animationName, boolean loop, float delay) {
Animation animation = data.getSkeletonData().findAnimation(animationName); Animation animation = data.getSkeletonData().findAnimation(animationName);
if (animation == null) throw new IllegalArgumentException("Animation not found: " + animationName); if (animation == null) throw new IllegalArgumentException("Animation not found: " + animationName);
return addAnimation(animation, loop, delay); return addAnimation(trackIndex, animation, loop, delay);
}
/** Adds an animation to be played delay seconds after the current or last queued animation, taking into account any mix
* duration.
* @param animation May be null to queue clearing the AnimationState. */
public QueuedAnimation addAnimation (Animation animation, boolean loop) {
return addAnimation(animation, loop, 0);
} }
/** Adds an animation to be played delay seconds after the current or last queued animation. /** Adds an animation to be played delay seconds after the current or last queued animation.
* @param animation May be null to queue clearing the AnimationState. * @param animation May be null to queue clearing the AnimationState.
* @param delay May be <= 0 to use duration of previous animation minus any mix duration plus the negative delay. */ * @param delay May be <= 0 to use duration of previous animation minus any mix duration plus the negative delay. */
public QueuedAnimation addAnimation (Animation animation, boolean loop, float delay) { public TrackEntry addAnimation (int trackIndex, Animation animation, boolean loop, float delay) {
QueuedAnimation entry = Pools.obtain(QueuedAnimation.class); TrackEntry entry = Pools.obtain(TrackEntry.class);
entry.animation = animation; entry.animation = animation;
entry.loop = loop; entry.loop = loop;
entry.time = 0; entry.time = 0;
entry.endTime = animation != null ? animation.getDuration() : 0; entry.endTime = animation != null ? animation.getDuration() : 0;
TrackEntry last = expandToIndex(trackIndex);
if (last != null) {
while (last.next != null)
last = last.next;
last.next = entry;
} else
tracks.set(trackIndex, entry);
if (delay <= 0) { if (delay <= 0) {
QueuedAnimation previousEntry = queue.size > 0 ? queue.peek() : current; if (last != null) {
if (previousEntry != null) { delay += last.endTime;
delay += previousEntry.endTime; if (animation != null) delay += -data.getMix(last.animation, animation);
if (animation != null) delay += -data.getMix(previousEntry.animation, animation);
} else } else
delay = 0; delay = 0;
} }
entry.delay = delay; entry.delay = delay;
queue.add(entry);
return entry; return entry;
} }
public Array<QueuedAnimation> getQueue () {
return queue;
}
/** @return May be null. */ /** @return May be null. */
public QueuedAnimation getCurrent () { public TrackEntry getTrackEntry (int trackIndex) {
return current; if (trackIndex >= tracks.size) return null;
} return tracks.get(trackIndex);
/** @return May be null. */
public Animation getAnimation () {
return current != null ? current.animation : null;
}
/** Returns the time within the current animation. */
public float getTime () {
return current != null ? current.time : 0;
}
public void setTime (float time) {
if (current != null) current.setTime(time);
}
/** Returns true if no animation is set or if the current time is greater than the animation duration, regardless of looping. */
public boolean isComplete () {
return current == null || current.time >= current.endTime;
} }
/** Adds a listener to receive events for all animations. */ /** Adds a listener to receive events for all animations. */
@ -266,26 +258,39 @@ public class AnimationState {
} }
public String toString () { public String toString () {
if (current == null || current.animation == null) return "<none>"; StringBuilder buffer = new StringBuilder(64);
return current.animation.getName(); for (int i = 0, n = tracks.size; i < n; i++) {
TrackEntry entry = tracks.get(i);
if (entry == null) continue;
if (buffer.length() > 0) buffer.append(", ");
buffer.append(entry.toString());
}
if (buffer.length() == 0) return "<none>";
return buffer.toString();
} }
/** A queued animation. */ static public class TrackEntry implements Poolable {
static public class QueuedAnimation implements Poolable { TrackEntry next, previous;
Animation animation; Animation animation;
boolean loop; boolean loop;
float delay, time, lastTime, endTime; float delay, time, lastTime, endTime;
AnimationStateListener listener; AnimationStateListener listener;
float mixTime, mixDuration;
public void reset () { public void reset () {
animation = null; animation = null;
listener = null; listener = null;
next = null;
} }
public Animation getAnimation () { public Animation getAnimation () {
return animation; return animation;
} }
public void setAnimation (Animation animation) {
this.animation = animation;
}
public boolean getLoop () { public boolean getLoop () {
return loop; return loop;
} }
@ -294,13 +299,20 @@ public class AnimationState {
this.loop = loop; this.loop = loop;
} }
public float getDelay () {
return delay;
}
public void setDelay (float delay) {
this.delay = delay;
}
public float getTime () { public float getTime () {
return time; return time;
} }
public void setTime (float time) { public void setTime (float time) {
this.time = time; this.time = time;
if (lastTime < time) lastTime = time;
} }
public float getEndTime () { public float getEndTime () {
@ -319,41 +331,58 @@ public class AnimationState {
this.listener = listener; this.listener = listener;
} }
public float getDelay () { public float getLastTime () {
return delay; return lastTime;
} }
public void setDelay (float delay) { public void setLastTime (float lastTime) {
this.delay = delay; this.lastTime = lastTime;
}
public TrackEntry getNext () {
return next;
}
public void setNext (TrackEntry next) {
this.next = next;
}
/** Returns true if the current time is greater than the end time, regardless of looping. */
public boolean isComplete () {
return time >= endTime;
}
public String toString () {
return animation == null ? "<none>" : animation.name;
} }
} }
static public interface AnimationStateListener { static public interface AnimationStateListener {
/** Invoked when the current animation triggers an event. */ /** Invoked when the current animation triggers an event. */
public void event (Event event); public void event (int trackIndex, Event event);
/** Invoked when the current animation has completed. /** Invoked when the current animation has completed.
* @param loopCount The number of times the animation reached the end. */ * @param loopCount The number of times the animation reached the end. */
public void complete (int loopCount); public void complete (int trackIndex, int loopCount);
/** Invoked just after the current animation is set. */ /** Invoked just after the current animation is set. */
public void start (); public void start (int trackIndex);
/** Invoked just before the current animation is replaced. */ /** Invoked just before the current animation is replaced. */
public void end (); public void end (int trackIndex);
} }
static public abstract class AnimationStateAdapter implements AnimationStateListener { static public abstract class AnimationStateAdapter implements AnimationStateListener {
public void event (Event event) { public void event (int trackIndex, Event event) {
} }
public void complete (int loopCount) { public void complete (int trackIndex, int loopCount) {
} }
public void start () { public void start (int trackIndex) {
} }
public void end () { public void end (int trackIndex) {
} }
} }
} }

View File

@ -48,8 +48,9 @@ public class SkeletonData {
bones.clear(); bones.clear();
slots.clear(); slots.clear();
skins.clear(); skins.clear();
animations.clear();
defaultSkin = null; defaultSkin = null;
eventDatas.clear();
animations.clear();
} }
// --- Bones. // --- Bones.

View File

@ -69,23 +69,23 @@ public class AnimationStateTest extends ApplicationAdapter {
state = new AnimationState(stateData); state = new AnimationState(stateData);
state.addListener(new AnimationStateListener() { state.addListener(new AnimationStateListener() {
public void event (Event event) { public void event (int trackIndex, Event event) {
System.out.println("Event: " + event.getData().getName()); System.out.println(trackIndex + " event: " + state.getTrackEntry(trackIndex) + ", " + event.getData().getName());
} }
public void complete (int loopCount) { public void complete (int trackIndex, int loopCount) {
System.out.println("Complete: " + state.getAnimation() + ", " + loopCount); System.out.println(trackIndex + " complete: " + state.getTrackEntry(trackIndex) + ", " + loopCount);
} }
public void start () { public void start (int trackIndex) {
System.out.println("Start: " + state.getAnimation()); System.out.println(trackIndex + " start: " + state.getTrackEntry(trackIndex));
} }
public void end () { public void end (int trackIndex) {
System.out.println("End: " + state.getAnimation()); System.out.println(trackIndex + " end: " + state.getTrackEntry(trackIndex));
} }
}); });
state.setAnimation("walk", true); state.setAnimation(0, "walk", true);
skeleton = new Skeleton(skeletonData); skeleton = new Skeleton(skeletonData);
skeleton.setX(250); skeleton.setX(250);
@ -99,8 +99,11 @@ public class AnimationStateTest extends ApplicationAdapter {
} }
public boolean keyDown (int keycode) { public boolean keyDown (int keycode) {
state.setAnimation("jump", false); // state.setAnimation(1, "jump", false);
state.addAnimation("walk", true); // state.addAnimation(1, (Animation)null, true, 0);
state.setAnimation(0, "jump", false);
state.addAnimation(0, "walk", true, 0);
return true; return true;
} }
}); });

View File

@ -88,19 +88,19 @@ namespace Spine {
state = new AnimationState(stateData); state = new AnimationState(stateData);
if (false) { if (true) {
// Event handling for all animations. // Event handling for all animations.
state.Start += new EventHandler(Start); state.Start += new EventHandler<StartEndArgs>(Start);
state.End += new EventHandler(End); state.End += new EventHandler<StartEndArgs>(End);
state.Complete += new EventHandler<CompleteArgs>(Complete); state.Complete += new EventHandler<CompleteArgs>(Complete);
state.Event += new EventHandler<EventTriggeredArgs>(Event); state.Event += new EventHandler<EventTriggeredArgs>(Event);
state.SetAnimation("drawOrder", true); state.SetAnimation(0, "drawOrder", true);
} else { } else {
state.SetAnimation("walk", false); state.SetAnimation(0, "walk", false);
QueuedAnimation entry = state.AddAnimation("jump", false); TrackEntry entry = state.AddAnimation(0, "jump", false, 0);
entry.End += new EventHandler(End); // Event handling for queued animations. entry.End += new EventHandler<StartEndArgs>(End); // Event handling for queued animations.
state.AddAnimation("walk", true); state.AddAnimation(0, "walk", true, 0);
} }
skeleton.X = 320; skeleton.X = 320;
@ -149,20 +149,20 @@ namespace Spine {
base.Draw(gameTime); base.Draw(gameTime);
} }
public void Start (object sender, EventArgs e) { public void Start (object sender, StartEndArgs e) {
Console.WriteLine(state + ": start"); Console.WriteLine(e.TrackIndex + " " + state.getTrackEntry(e.TrackIndex) + ": start");
} }
public void End (object sender, EventArgs e) { public void End (object sender, StartEndArgs e) {
Console.WriteLine(state + ": end"); Console.WriteLine(e.TrackIndex + " " + state.getTrackEntry(e.TrackIndex) + ": end");
} }
public void Complete (object sender, CompleteArgs e) { public void Complete (object sender, CompleteArgs e) {
Console.WriteLine(state + ": complete " + e.LoopCount); Console.WriteLine(e.TrackIndex + " " + state.getTrackEntry(e.TrackIndex) + ": complete " + e.LoopCount);
} }
public void Event (object sender, EventTriggeredArgs e) { public void Event (object sender, EventTriggeredArgs e) {
Console.WriteLine(state + ": event " + e.Event); Console.WriteLine(e.TrackIndex + " " + state.getTrackEntry(e.TrackIndex) + ": event " + e.Event);
} }
} }
} }