AnimationState#apply has no side effects.

This commit is contained in:
NathanSweet 2016-08-23 09:55:06 +02:00
parent 285aead074
commit 0a90565ba1
2 changed files with 24 additions and 9 deletions

View File

@ -621,6 +621,12 @@ public class AnimationStateTest {
} }
state.apply(skeleton); state.apply(skeleton);
// Apply multiple times to ensure no side effects.
if (expected.size > 0) state.removeListener(stateListener);
state.apply(skeleton);
state.apply(skeleton);
if (expected.size > 0) state.addListener(stateListener);
} }
// Expecting more than actual is a failure. // Expecting more than actual is a failure.
for (int i = actual.size, n = expected.size; i < n; i++) { for (int i = actual.size, n = expected.size; i < n; i++) {

View File

@ -73,6 +73,9 @@ public class AnimationState {
TrackEntry current = tracks.get(i); TrackEntry current = tracks.get(i);
if (current == null) continue; if (current == null) continue;
current.animationLast = current.nextAnimationLast;
current.trackLast = current.nextTrackLast;
float currentDelta = delta * current.timeScale; float currentDelta = delta * current.timeScale;
if (current.delay > 0) { if (current.delay > 0) {
@ -114,6 +117,8 @@ public class AnimationState {
current.mixingFrom = null; current.mixingFrom = null;
animationsChanged = true; animationsChanged = true;
} else { } else {
mixingFrom.animationLast = mixingFrom.nextAnimationLast;
mixingFrom.trackLast = mixingFrom.nextTrackLast;
float mixingFromDelta = delta * mixingFrom.timeScale; float mixingFromDelta = delta * mixingFrom.timeScale;
mixingFrom.trackTime += mixingFromDelta; mixingFrom.trackTime += mixingFromDelta;
current.mixTime += mixingFromDelta; current.mixTime += mixingFromDelta;
@ -124,7 +129,8 @@ public class AnimationState {
queue.drain(); queue.drain();
} }
/** Poses the skeleton using the track entry animations. */ /** Poses the skeleton using the track entry animations. There are no side effects other than invoking listeners, so multiple
* skeletons can be posed identically. */
public void apply (Skeleton skeleton) { public void apply (Skeleton skeleton) {
if (skeleton == null) throw new IllegalArgumentException("skeleton cannot be null."); if (skeleton == null) throw new IllegalArgumentException("skeleton cannot be null.");
@ -159,8 +165,8 @@ public class AnimationState {
timelines.get(ii).apply(skeleton, animationLast, animationTime, events, mix, timelinesFirst[ii], false); timelines.get(ii).apply(skeleton, animationLast, animationTime, events, mix, timelinesFirst[ii], false);
} }
queueEvents(current, animationTime); queueEvents(current, animationTime);
current.animationLast = animationTime; current.nextAnimationLast = animationTime;
current.trackLast = current.trackTime; current.nextTrackLast = current.trackTime;
} }
queue.drain(); queue.drain();
@ -196,8 +202,8 @@ public class AnimationState {
} }
queueEvents(entry, animationTime); queueEvents(entry, animationTime);
entry.animationLast = animationTime; entry.nextAnimationLast = animationTime;
entry.trackLast = entry.trackTime; entry.nextTrackLast = entry.trackTime;
} }
private void queueEvents (TrackEntry entry, float animationTime) { private void queueEvents (TrackEntry entry, float animationTime) {
@ -355,12 +361,12 @@ public class AnimationState {
queue.drain(); queue.drain();
} else { } else {
freeAll(current.next); freeAll(current.next);
if (current.trackLast == -1) { // If current was never applied, replace it. if (current.nextTrackLast == -1) { // If current was never applied, replace it.
setCurrent(trackIndex, entry); setCurrent(trackIndex, entry);
queue.drain(); queue.drain();
} else { } else {
current.next = entry; current.next = entry;
entry.delay = current.trackLast; entry.delay = current.nextTrackLast;
} }
} }
return entry; return entry;
@ -422,9 +428,11 @@ public class AnimationState {
entry.animationStart = 0; entry.animationStart = 0;
entry.animationEnd = animation.getDuration(); entry.animationEnd = animation.getDuration();
entry.animationLast = -1; entry.animationLast = -1;
entry.nextAnimationLast = -1;
entry.trackTime = 0; entry.trackTime = 0;
entry.trackEnd = loop ? Integer.MAX_VALUE : entry.animationEnd; entry.trackEnd = loop ? Integer.MAX_VALUE : entry.animationEnd;
entry.trackLast = -1; entry.trackLast = -1;
entry.nextTrackLast = -1;
entry.timeScale = 1; entry.timeScale = 1;
entry.alpha = 1; entry.alpha = 1;
@ -507,8 +515,8 @@ public class AnimationState {
boolean loop; boolean loop;
float eventThreshold, attachmentThreshold, drawOrderThreshold; float eventThreshold, attachmentThreshold, drawOrderThreshold;
float delay, trackTime, trackLast, trackEnd, animationStart, animationEnd, animationLast, timeScale; float delay, trackTime, trackLast, trackEnd, animationStart, animationEnd, animationLast, timeScale;
float alpha; float nextTrackLast, nextAnimationLast;
float mixTime, mixDuration; float alpha, mixTime, mixDuration;
final BooleanArray timelinesFirst = new BooleanArray(); final BooleanArray timelinesFirst = new BooleanArray();
public void reset () { public void reset () {
@ -603,6 +611,7 @@ public class AnimationState {
public void setAnimationLast (float animationLast) { public void setAnimationLast (float animationLast) {
this.animationLast = animationLast; this.animationLast = animationLast;
nextAnimationLast = animationLast;
} }
/** Uses the {@link #getTrackTime() track time} to compute the animation time between the {@link #getAnimationStart() /** Uses the {@link #getTrackTime() track time} to compute the animation time between the {@link #getAnimationStart()