From 8f4ad90a1790d9f89e82bf8de47d6ac8cc403356 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sat, 20 Aug 2016 18:52:13 +0200 Subject: [PATCH] AnimationState, allow delay to postpone the current track entry. #621 --- .../spine/AnimationStateTest.java | 33 ++++++++++++++----- .../spine/AnimationState.java | 16 +++++++-- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/spine-libgdx/spine-libgdx-tests/src/com/esotericsoftware/spine/AnimationStateTest.java b/spine-libgdx/spine-libgdx-tests/src/com/esotericsoftware/spine/AnimationStateTest.java index a9e395e9e..3e931078b 100644 --- a/spine-libgdx/spine-libgdx-tests/src/com/esotericsoftware/spine/AnimationStateTest.java +++ b/spine-libgdx/spine-libgdx-tests/src/com/esotericsoftware/spine/AnimationStateTest.java @@ -519,6 +519,17 @@ public class AnimationStateTest { state.setAnimation(0, "events2", false); run(0.1f, 1000, null); + setup("addAnimation with delay on empty track", // 22 + expect(0, "start", 0, 0), // + expect(0, "event 0", 0, 5), // + expect(0, "event 14", 0.5f, 5.5f), // + expect(0, "event 30", 1, 6), // + expect(0, "complete", 1, 6), // + expect(0, "end", 1, 6.1f) // + ); + state.addAnimation(0, "events1", false, 5); + run(0.1f, 10, null); + System.out.println("AnimationState tests passed."); } @@ -543,12 +554,12 @@ public class AnimationStateTest { skeleton.update(incr); state.update(incr); - // Reduce float error for tests. + // Reduce float discrepancies for tests. for (TrackEntry entry : state.getTracks()) { if (entry == null) continue; - entry.trackTime = Math.round(entry.trackTime * 1000000) / 1000000f; - if (entry.mixingFrom != null) - entry.mixingFrom.trackTime = Math.round(entry.mixingFrom.trackTime * 1000000) / 1000000f; + entry.trackTime = round(entry.trackTime, 6); + entry.delay = round(entry.delay, 3); + if (entry.mixingFrom != null) entry.mixingFrom.trackTime = round(entry.mixingFrom.trackTime, 6); } state.apply(skeleton); @@ -560,11 +571,11 @@ public class AnimationStateTest { } actual.clear(); expected.clear(); + log(""); if (fail) { - System.out.println("TEST " + test + " FAILED!"); + log("TEST " + test + " FAILED!"); System.exit(0); } - System.out.println(); } Result expect (int animationIndex, String name, float trackTime, float totalTime) { @@ -612,13 +623,17 @@ public class AnimationStateTest { } public String toString () { - return String.format("%-3s%-12s%-7s%-7s", "" + animationIndex, name, round(trackTime, 3), round(totalTime, 3)); + return String.format("%-3s%-12s%-7s%-7s", "" + animationIndex, name, roundTime(trackTime), roundTime(totalTime)); } } - static String round (float value, int decimals) { + static float round (float value, int decimals) { float shift = (float)Math.pow(10, decimals); - String text = Float.toString(Math.round(value * shift) / shift); + return Math.round(value * shift) / shift; + } + + static String roundTime (float value) { + String text = Float.toString(round(value, 3)); return text.endsWith(".0") ? text.substring(0, text.length() - 2) : text; } diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/AnimationState.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/AnimationState.java index 0b9de0de3..eff6fb8d1 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/AnimationState.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/AnimationState.java @@ -71,11 +71,19 @@ public class AnimationState { float currentDelta = delta * current.timeScale; + if (current.delay > 0) { + current.delay -= currentDelta; + if (current.delay > 0) continue; + currentDelta = -current.delay; + current.delay = 0; + } + TrackEntry next = current.next; if (next != null) { // When the next entry's delay is passed, change to the next entry. float nextTime = current.trackLast - next.delay; if (nextTime >= 0) { + next.delay = 0; next.trackTime = nextTime + delta * next.timeScale; current.trackTime += currentDelta; setCurrent(i, next); @@ -106,6 +114,7 @@ public class AnimationState { for (int i = 0; i < tracks.size; i++) { TrackEntry current = tracks.get(i); if (current == null) continue; + if (current.delay > 0) continue; float alpha = current.alpha; if (current.mixingFrom != null) { @@ -308,9 +317,10 @@ public class AnimationState { else delay = 0; } - entry.delay = delay; } else setCurrent(trackIndex, entry); + + entry.delay = delay; return entry; } @@ -444,7 +454,9 @@ public class AnimationState { this.loop = loop; } - /** Seconds from the start of the last animation (if any) to when this animation becomes the current animation. */ + /** Seconds to postpone playing the animation. When a track entry is the current track entry, delay postpones incrementing + * the track time. When a track entry is queued, delay is the time from the start of the previous animation to when the + * track entry will become the current track entry. */ public float getDelay () { return delay; }