mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2025-12-21 01:36:02 +08:00
Refactored AnimationState to better support setting a to/from time.
#113
This commit is contained in:
parent
ac302b31ee
commit
0049b4f7b4
@ -37,24 +37,24 @@ using System.Collections.Generic;
|
|||||||
namespace Spine {
|
namespace Spine {
|
||||||
public class AnimationState {
|
public class AnimationState {
|
||||||
private AnimationStateData data;
|
private AnimationStateData data;
|
||||||
private Animation current, previous;
|
private QueuedAnimation current, previous;
|
||||||
private float currentTime, currentLastTime, previousTime;
|
|
||||||
private bool currentLoop, previousLoop;
|
|
||||||
private QueueEntry currentQueueEntry;
|
|
||||||
private float mixTime, mixDuration;
|
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>();
|
||||||
private List<QueueEntry> queue = new List<QueueEntry>();
|
|
||||||
|
|
||||||
public AnimationStateData Data { get { return data; } }
|
public AnimationStateData Data { get { return data; } }
|
||||||
public Animation Animation { get { return current; } }
|
public List<QueuedAnimation> Queue { get { return queue; } }
|
||||||
public bool Loop { get { return currentLoop; } set { currentLoop = value; } }
|
public QueuedAnimation Current { get { return current; } }
|
||||||
|
public Animation Animation {
|
||||||
|
get { return current != null ? current.animation : null; }
|
||||||
|
}
|
||||||
public float Time {
|
public float Time {
|
||||||
get { return currentTime; }
|
get { return current != null ? current.time : 0; }
|
||||||
set {
|
set { if (current != null) current.Time = value; }
|
||||||
currentTime = value;
|
}
|
||||||
currentLastTime = value - 0.00001f;
|
public bool Loop {
|
||||||
}
|
get { return current != null ? current.loop : false; }
|
||||||
|
set { if (current != null) current.Loop = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public event EventHandler Start;
|
public event EventHandler Start;
|
||||||
@ -68,54 +68,63 @@ namespace Spine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Update (float delta) {
|
public void Update (float delta) {
|
||||||
currentTime += delta;
|
QueuedAnimation current = this.current;
|
||||||
previousTime += delta;
|
if (current == null) return;
|
||||||
mixTime += delta;
|
|
||||||
|
|
||||||
if (current != null) {
|
float time = current.time;
|
||||||
float duration = current.duration;
|
float duration = current.endTime;
|
||||||
if (currentLoop ? (currentLastTime % duration > currentTime % duration)
|
|
||||||
: (currentLastTime < duration && currentTime >= duration)) {
|
current.time = time + delta;
|
||||||
int count = (int)(currentTime / duration);
|
if (previous != null) {
|
||||||
if (currentQueueEntry != null) currentQueueEntry.OnComplete(this, count);
|
previous.time += delta;
|
||||||
if (Complete != null) Complete(this, new CompleteArgs(count));
|
mixTime += delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if completed the animation or a loop iteration.
|
||||||
|
if (current.loop ? (current.lastTime % duration > time % duration) : (current.lastTime < duration && time >= duration)) {
|
||||||
|
int count = (int)(time / duration);
|
||||||
|
current.OnComplete(this, count);
|
||||||
|
if (Complete != null) Complete(this, new CompleteArgs(count));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (queue.Count > 0) {
|
if (queue.Count > 0) {
|
||||||
QueueEntry entry = queue[0];
|
QueuedAnimation entry = queue[0];
|
||||||
if (currentTime >= entry.delay) {
|
if (time >= entry.delay) {
|
||||||
SetAnimationInternal(entry.animation, entry.loop, entry);
|
if (entry.animation == null)
|
||||||
queue.RemoveAt(0);
|
ClearAnimation();
|
||||||
|
else {
|
||||||
|
SetAnimationEntry(entry);
|
||||||
|
queue.RemoveAt(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Apply (Skeleton skeleton) {
|
public void Apply (Skeleton skeleton) {
|
||||||
|
QueuedAnimation current = this.current;
|
||||||
if (current == null) return;
|
if (current == null) return;
|
||||||
|
|
||||||
List<Event> events = this.events;
|
List<Event> events = this.events;
|
||||||
events.Clear();
|
events.Clear();
|
||||||
|
|
||||||
|
QueuedAnimation previous = this.previous;
|
||||||
if (previous != null) {
|
if (previous != null) {
|
||||||
previous.Apply(skeleton, int.MaxValue, previousTime, previousLoop, null);
|
previous.animation.Apply(skeleton, int.MaxValue, previous.time, previous.loop, null);
|
||||||
float alpha = mixTime / mixDuration;
|
float alpha = mixTime / mixDuration;
|
||||||
if (alpha >= 1) {
|
if (alpha >= 1) {
|
||||||
alpha = 1;
|
alpha = 1;
|
||||||
previous = null;
|
this.previous = null;
|
||||||
}
|
}
|
||||||
current.Mix(skeleton, currentLastTime, currentTime, currentLoop, events, alpha);
|
current.animation.Mix(skeleton, current.lastTime, current.time, current.loop, events, alpha);
|
||||||
} else
|
} else
|
||||||
current.Apply(skeleton, currentLastTime, currentTime, currentLoop, events);
|
current.animation.Apply(skeleton, current.lastTime, current.time, current.loop, events);
|
||||||
|
|
||||||
if (Event != null || currentQueueEntry != null) {
|
foreach (Event e in events) {
|
||||||
foreach (Event e in events) {
|
current.OnEvent(this, e);
|
||||||
if (currentQueueEntry != null) currentQueueEntry.OnEvent(this, e);
|
if (Event != null) Event(this, new EventTriggeredArgs(e));
|
||||||
if (Event != null) Event(this, new EventTriggeredArgs(e));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentLastTime = currentTime;
|
current.lastTime = current.time;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ClearAnimation () {
|
public void ClearAnimation () {
|
||||||
@ -124,72 +133,73 @@ namespace Spine {
|
|||||||
queue.Clear();
|
queue.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetAnimationInternal (Animation animation, bool loop, QueueEntry entry) {
|
private void SetAnimationEntry (QueuedAnimation entry) {
|
||||||
previous = null;
|
previous = null;
|
||||||
|
|
||||||
|
QueuedAnimation current = this.current;
|
||||||
if (current != null) {
|
if (current != null) {
|
||||||
if (currentQueueEntry != null) currentQueueEntry.OnEnd(this);
|
current.OnEnd(this);
|
||||||
if (End != null) End(this, EventArgs.Empty);
|
if (End != null) End(this, EventArgs.Empty);
|
||||||
|
|
||||||
if (animation != null) {
|
mixDuration = data.GetMix(current.animation, entry.animation);
|
||||||
mixDuration = data.GetMix(current, animation);
|
if (mixDuration > 0) {
|
||||||
if (mixDuration > 0) {
|
mixTime = 0;
|
||||||
mixTime = 0;
|
previous = current;
|
||||||
previous = current;
|
|
||||||
previousTime = currentTime;
|
|
||||||
previousLoop = currentLoop;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
current = animation;
|
this.current = entry;
|
||||||
currentLoop = loop;
|
|
||||||
currentTime = 0;
|
|
||||||
currentLastTime = 0;
|
|
||||||
currentQueueEntry = entry;
|
|
||||||
|
|
||||||
if (currentQueueEntry != null) currentQueueEntry.OnStart(this);
|
entry.OnStart(this);
|
||||||
if (Start != null) Start(this, EventArgs.Empty);
|
if (Start != null) Start(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetAnimation (String animationName, bool loop) {
|
public QueuedAnimation SetAnimation (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);
|
||||||
SetAnimation(animation, loop);
|
return SetAnimation(animation, loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set the current animation. Any queued animations are cleared and the current animation time is set to 0.
|
/** Set the current animation. Any queued animations are cleared. */
|
||||||
* @param animation May be null.
|
public QueuedAnimation SetAnimation (Animation animation, bool loop) {
|
||||||
* @param listener May be null. */
|
|
||||||
public void SetAnimation (Animation animation, bool loop) {
|
|
||||||
queue.Clear();
|
queue.Clear();
|
||||||
SetAnimationInternal(animation, loop, null);
|
QueuedAnimation entry = new QueuedAnimation();
|
||||||
|
entry.animation = animation;
|
||||||
|
entry.loop = loop;
|
||||||
|
entry.time = 0;
|
||||||
|
entry.endTime = animation.Duration;
|
||||||
|
SetAnimationEntry(entry);
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueueEntry AddAnimation (String animationName, bool loop) {
|
public QueuedAnimation AddAnimation (String animationName, bool loop) {
|
||||||
return AddAnimation(animationName, loop, 0);
|
return AddAnimation(animationName, loop, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueueEntry AddAnimation (String animationName, bool loop, float delay) {
|
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(animation, loop, delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueueEntry AddAnimation (Animation animation, bool loop) {
|
public QueuedAnimation AddAnimation (Animation animation, bool loop) {
|
||||||
return AddAnimation(animation, loop, 0);
|
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 QueueEntry AddAnimation (Animation animation, bool loop, float delay) {
|
public QueuedAnimation AddAnimation (Animation animation, bool loop, float delay) {
|
||||||
QueueEntry entry = new QueueEntry();
|
QueuedAnimation entry = new QueuedAnimation();
|
||||||
entry.animation = animation;
|
entry.animation = animation;
|
||||||
entry.loop = loop;
|
entry.loop = loop;
|
||||||
|
entry.time = 0;
|
||||||
|
entry.endTime = animation != null ? animation.Duration : 0;
|
||||||
|
|
||||||
if (delay <= 0) {
|
if (delay <= 0) {
|
||||||
Animation previousAnimation = queue.Count == 0 ? current : queue[queue.Count - 1].animation;
|
QueuedAnimation previousEntry = queue.Count > 0 ? queue[queue.Count - 1] : current;
|
||||||
if (previousAnimation != null)
|
if (previousEntry != null) {
|
||||||
delay = previousAnimation.duration - data.GetMix(previousAnimation, animation) + delay;
|
delay += previousEntry.endTime;
|
||||||
else
|
if (animation != null) delay += -data.GetMix(previousEntry.animation, animation);
|
||||||
|
} else
|
||||||
delay = 0;
|
delay = 0;
|
||||||
}
|
}
|
||||||
entry.delay = delay;
|
entry.delay = delay;
|
||||||
@ -200,11 +210,12 @@ namespace Spine {
|
|||||||
|
|
||||||
/** Returns true if no animation is set or if the current time is greater than the animation duration, regardless of looping. */
|
/** Returns true if no animation is set or if the current time is greater than the animation duration, regardless of looping. */
|
||||||
public bool IsComplete () {
|
public bool IsComplete () {
|
||||||
return current == null || currentTime >= current.duration;
|
return current == null || current.time >= current.endTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
override public String ToString () {
|
override public String ToString () {
|
||||||
return (current != null && current.name != null) ? current.name : base.ToString();
|
if (current == null || current.animation == null) return "<none>";
|
||||||
|
return current.animation.Name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,10 +235,23 @@ namespace Spine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class QueueEntry {
|
public class QueuedAnimation {
|
||||||
internal Spine.Animation animation;
|
internal Animation animation;
|
||||||
internal bool loop;
|
internal bool loop;
|
||||||
internal float delay;
|
internal float delay, time, lastTime, endTime;
|
||||||
|
|
||||||
|
public Animation Animation { get { return animation; } }
|
||||||
|
public bool Loop { get { return loop; } set { loop = value; } }
|
||||||
|
public float Delay { get { return delay; } set { delay = value; } }
|
||||||
|
public float EndTime { get { return EndTime; } set { EndTime = value; } }
|
||||||
|
|
||||||
|
public float Time {
|
||||||
|
get { return time; }
|
||||||
|
set {
|
||||||
|
time = value;
|
||||||
|
if (lastTime < value) lastTime = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public event EventHandler Start;
|
public event EventHandler Start;
|
||||||
public event EventHandler End;
|
public event EventHandler End;
|
||||||
|
|||||||
@ -34,19 +34,17 @@
|
|||||||
package com.esotericsoftware.spine;
|
package com.esotericsoftware.spine;
|
||||||
|
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
|
import com.badlogic.gdx.utils.Pool.Poolable;
|
||||||
import com.badlogic.gdx.utils.Pools;
|
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 Animation current, previous;
|
private QueuedAnimation current, previous;
|
||||||
private float currentTime, currentLastTime, previousTime;
|
|
||||||
private boolean currentLoop, previousLoop;
|
|
||||||
private AnimationStateListener currentListener;
|
|
||||||
private float mixTime, mixDuration;
|
private float mixTime, mixDuration;
|
||||||
private final Array<QueueEntry> queue = new Array();
|
private final Array<QueuedAnimation> queue = new Array();
|
||||||
private final Array<Event> events = new Array();
|
|
||||||
private final Array<AnimationStateListener> listeners = new Array();
|
private final Array<AnimationStateListener> listeners = new Array();
|
||||||
|
private final Array<Event> events = 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.");
|
||||||
@ -54,62 +52,79 @@ public class AnimationState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void update (float delta) {
|
public void update (float delta) {
|
||||||
currentTime += delta;
|
QueuedAnimation current = this.current;
|
||||||
previousTime += delta;
|
if (current == null) return;
|
||||||
mixTime += delta;
|
|
||||||
|
|
||||||
if (current != null) {
|
float time = current.time;
|
||||||
float duration = current.getDuration();
|
float duration = current.endTime;
|
||||||
if (currentLoop ? (currentLastTime % duration > currentTime % duration)
|
|
||||||
: (currentLastTime < duration && currentTime >= duration)) {
|
current.time = time + delta;
|
||||||
int count = (int)(currentTime / duration);
|
if (previous != null) {
|
||||||
if (currentListener != null) currentListener.complete(count);
|
previous.time += delta;
|
||||||
for (int i = 0, n = listeners.size; i < n; i++)
|
mixTime += delta;
|
||||||
listeners.get(i).complete(count);
|
}
|
||||||
}
|
|
||||||
|
// Check if completed the animation or a loop iteration.
|
||||||
|
if (current.loop ? (current.lastTime % duration > time % duration) : (current.lastTime < duration && time >= duration)) {
|
||||||
|
int count = (int)(time / duration);
|
||||||
|
if (current.listener != null) current.listener.complete(count);
|
||||||
|
for (int i = 0, n = listeners.size; i < n; i++)
|
||||||
|
listeners.get(i).complete(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (queue.size > 0) {
|
if (queue.size > 0) {
|
||||||
QueueEntry entry = queue.first();
|
QueuedAnimation entry = queue.first();
|
||||||
if (currentTime >= entry.delay) {
|
if (time >= entry.delay) {
|
||||||
setAnimationInternal(entry.animation, entry.loop, entry.listener);
|
if (entry.animation == null)
|
||||||
Pools.free(entry);
|
clearAnimation();
|
||||||
queue.removeIndex(0);
|
else {
|
||||||
|
setAnimationEntry(entry);
|
||||||
|
queue.removeIndex(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void apply (Skeleton skeleton) {
|
public void apply (Skeleton skeleton) {
|
||||||
|
QueuedAnimation current = this.current;
|
||||||
if (current == null) return;
|
if (current == null) return;
|
||||||
|
|
||||||
Array<Event> events = this.events;
|
Array<Event> events = this.events;
|
||||||
events.clear();
|
events.size = 0;
|
||||||
|
|
||||||
|
QueuedAnimation previous = this.previous;
|
||||||
if (previous != null) {
|
if (previous != null) {
|
||||||
previous.apply(skeleton, Integer.MAX_VALUE, previousTime, previousLoop, null);
|
previous.animation.apply(skeleton, Integer.MAX_VALUE, previous.time, previous.loop, null);
|
||||||
float alpha = mixTime / mixDuration;
|
float alpha = mixTime / mixDuration;
|
||||||
if (alpha >= 1) {
|
if (alpha >= 1) {
|
||||||
alpha = 1;
|
alpha = 1;
|
||||||
previous = null;
|
Pools.free(previous);
|
||||||
|
this.previous = null;
|
||||||
}
|
}
|
||||||
current.mix(skeleton, currentLastTime, currentTime, currentLoop, events, alpha);
|
current.animation.mix(skeleton, current.lastTime, current.time, current.loop, events, alpha);
|
||||||
} else
|
} else
|
||||||
current.apply(skeleton, currentLastTime, currentTime, currentLoop, events);
|
current.animation.apply(skeleton, current.lastTime, current.time, current.loop, events);
|
||||||
|
|
||||||
int listenerCount = listeners.size;
|
int listenerCount = listeners.size;
|
||||||
for (int i = 0, n = events.size; i < n; i++) {
|
for (int i = 0, n = events.size; i < n; i++) {
|
||||||
Event event = events.get(i);
|
Event event = events.get(i);
|
||||||
if (currentListener != null) currentListener.event(event);
|
if (current.listener != null) current.listener.event(event);
|
||||||
for (int ii = 0; ii < listenerCount; ii++)
|
for (int ii = 0; ii < listenerCount; ii++)
|
||||||
listeners.get(ii).event(event);
|
listeners.get(ii).event(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentLastTime = currentTime;
|
current.lastTime = current.time;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearAnimation () {
|
public void clearAnimation () {
|
||||||
previous = null;
|
if (previous != null) {
|
||||||
current = null;
|
Pools.free(previous);
|
||||||
|
previous = null;
|
||||||
|
}
|
||||||
|
if (current != null) {
|
||||||
|
Pools.free(current);
|
||||||
|
current = null;
|
||||||
|
}
|
||||||
clearQueue();
|
clearQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,117 +133,121 @@ public class AnimationState {
|
|||||||
queue.clear();
|
queue.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setAnimationInternal (Animation animation, boolean loop, AnimationStateListener listener) {
|
private void setAnimationEntry (QueuedAnimation entry) {
|
||||||
previous = null;
|
if (previous != null) {
|
||||||
|
Pools.free(previous);
|
||||||
|
previous = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
QueuedAnimation current = this.current;
|
||||||
if (current != null) {
|
if (current != null) {
|
||||||
if (currentListener != null) currentListener.end();
|
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();
|
||||||
|
|
||||||
if (animation != null) {
|
mixDuration = data.getMix(current.animation, entry.animation);
|
||||||
mixDuration = data.getMix(current, animation);
|
if (mixDuration > 0) {
|
||||||
if (mixDuration > 0) {
|
mixTime = 0;
|
||||||
mixTime = 0;
|
previous = current;
|
||||||
previous = current;
|
} else
|
||||||
previousTime = currentTime;
|
Pools.free(current);
|
||||||
previousLoop = currentLoop;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
current = animation;
|
this.current = entry;
|
||||||
currentLoop = loop;
|
|
||||||
currentTime = 0;
|
|
||||||
currentLastTime = 0;
|
|
||||||
currentListener = listener;
|
|
||||||
|
|
||||||
if (currentListener != null) currentListener.start();
|
if (entry != null && entry.listener != null) entry.listener.start();
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @see #setAnimation(Animation, boolean, AnimationStateListener) */
|
/** @see #setAnimation(Animation, boolean) */
|
||||||
public void setAnimation (String animationName, boolean loop) {
|
public QueuedAnimation setAnimation (String animationName, boolean loop) {
|
||||||
setAnimation(animationName, loop, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @see #setAnimation(Animation, boolean, AnimationStateListener) */
|
|
||||||
public void setAnimation (String animationName, boolean loop, AnimationStateListener listener) {
|
|
||||||
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);
|
||||||
setAnimation(animation, loop, listener);
|
return setAnimation(animation, loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @see #setAnimation(Animation, boolean, AnimationStateListener) */
|
/** Set the current animation. Any queued animations are cleared. */
|
||||||
public void setAnimation (Animation animation, boolean loop) {
|
public QueuedAnimation setAnimation (Animation animation, boolean loop) {
|
||||||
setAnimation(animation, loop, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Set the current animation. Any queued animations are cleared and the current animation time is set to 0. The specified
|
|
||||||
* listener receives events only for this animation.
|
|
||||||
* @param animation May be null.
|
|
||||||
* @param listener May be null. */
|
|
||||||
public void setAnimation (Animation animation, boolean loop, AnimationStateListener listener) {
|
|
||||||
clearQueue();
|
clearQueue();
|
||||||
setAnimationInternal(animation, loop, listener);
|
|
||||||
|
QueuedAnimation entry = Pools.obtain(QueuedAnimation.class);
|
||||||
|
entry.animation = animation;
|
||||||
|
entry.loop = loop;
|
||||||
|
entry.time = 0;
|
||||||
|
entry.endTime = animation.getDuration();
|
||||||
|
setAnimationEntry(entry);
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @see #addAnimation(Animation, boolean) */
|
/** @see #addAnimation(Animation, boolean) */
|
||||||
public void addAnimation (String animationName, boolean loop) {
|
public QueuedAnimation addAnimation (String animationName, boolean loop) {
|
||||||
addAnimation(animationName, loop, 0, null);
|
return addAnimation(animationName, loop, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @see #addAnimation(Animation, boolean, float, AnimationStateListener) */
|
/** @see #addAnimation(Animation, boolean, float) */
|
||||||
public void addAnimation (String animationName, boolean loop, float delay, AnimationStateListener listener) {
|
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);
|
||||||
addAnimation(animation, loop, delay, listener);
|
return addAnimation(animation, loop, delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Adds an animation to be played delay seconds after the current or last queued animation, taking into account any mix
|
/** Adds an animation to be played delay seconds after the current or last queued animation, taking into account any mix
|
||||||
* duration. */
|
* duration.
|
||||||
public void addAnimation (Animation animation, boolean loop) {
|
* @param animation May be null to queue clearing the AnimationState. */
|
||||||
addAnimation(animation, loop, 0, null);
|
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 delay May be <= 0 to use duration of previous animation minus any mix duration plus the negative delay.
|
* @param animation May be null to queue clearing the AnimationState.
|
||||||
* @param listener May be null. */
|
* @param delay May be <= 0 to use duration of previous animation minus any mix duration plus the negative delay. */
|
||||||
public void addAnimation (Animation animation, boolean loop, float delay, AnimationStateListener listener) {
|
public QueuedAnimation addAnimation (Animation animation, boolean loop, float delay) {
|
||||||
QueueEntry entry = Pools.obtain(QueueEntry.class);
|
QueuedAnimation entry = Pools.obtain(QueuedAnimation.class);
|
||||||
entry.animation = animation;
|
entry.animation = animation;
|
||||||
entry.loop = loop;
|
entry.loop = loop;
|
||||||
entry.listener = listener;
|
entry.time = 0;
|
||||||
|
entry.endTime = animation != null ? animation.getDuration() : 0;
|
||||||
|
|
||||||
if (delay <= 0) {
|
if (delay <= 0) {
|
||||||
Animation previousAnimation = queue.size == 0 ? current : queue.peek().animation;
|
QueuedAnimation previousEntry = queue.size > 0 ? queue.peek() : current;
|
||||||
if (previousAnimation != null)
|
if (previousEntry != null) {
|
||||||
delay = previousAnimation.getDuration() - data.getMix(previousAnimation, animation) + delay;
|
delay += previousEntry.endTime;
|
||||||
else
|
if (animation != null) delay += -data.getMix(previousEntry.animation, animation);
|
||||||
|
} else
|
||||||
delay = 0;
|
delay = 0;
|
||||||
}
|
}
|
||||||
entry.delay = delay;
|
entry.delay = delay;
|
||||||
|
|
||||||
queue.add(entry);
|
queue.add(entry);
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Array<QueuedAnimation> getQueue () {
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return May be null. */
|
||||||
|
public QueuedAnimation getCurrent () {
|
||||||
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return May be null. */
|
/** @return May be null. */
|
||||||
public Animation getAnimation () {
|
public Animation getAnimation () {
|
||||||
return current;
|
return current != null ? current.animation : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the time within the current animation. */
|
/** Returns the time within the current animation. */
|
||||||
public float getTime () {
|
public float getTime () {
|
||||||
return currentTime;
|
return current != null ? current.time : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTime (float time) {
|
public void setTime (float time) {
|
||||||
currentTime = time;
|
if (current != null) current.setTime(time);
|
||||||
currentLastTime = time - 0.00001f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns true if no animation is set or if the current time is greater than the animation duration, regardless of looping. */
|
/** Returns true if no animation is set or if the current time is greater than the animation duration, regardless of looping. */
|
||||||
public boolean isComplete () {
|
public boolean isComplete () {
|
||||||
return current == null || currentTime >= current.getDuration();
|
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. */
|
||||||
@ -237,15 +256,9 @@ public class AnimationState {
|
|||||||
listeners.add(listener);
|
listeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Removes the listener, which may be for all animations, the current animation, or a queued animation. */
|
/** Removes the listener added with {@link #addListener(AnimationStateListener)}. */
|
||||||
public void removeListener (AnimationStateListener listener) {
|
public void removeListener (AnimationStateListener listener) {
|
||||||
listeners.removeValue(listener, true);
|
listeners.removeValue(listener, true);
|
||||||
|
|
||||||
if (listener == currentListener) currentListener = null;
|
|
||||||
|
|
||||||
Array<QueueEntry> queue = this.queue;
|
|
||||||
for (int i = queue.size - 1; i >= 0; i--)
|
|
||||||
if (queue.get(i).listener == listener) queue.get(i).listener = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public AnimationStateData getData () {
|
public AnimationStateData getData () {
|
||||||
@ -253,14 +266,66 @@ public class AnimationState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toString () {
|
public String toString () {
|
||||||
return (current != null && current.getName() != null) ? current.getName() : super.toString();
|
if (current == null || current.animation == null) return "<none>";
|
||||||
|
return current.animation.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
static private class QueueEntry {
|
/** A queued animation. */
|
||||||
|
static public class QueuedAnimation implements Poolable {
|
||||||
Animation animation;
|
Animation animation;
|
||||||
boolean loop;
|
boolean loop;
|
||||||
float delay;
|
float delay, time, lastTime, endTime;
|
||||||
AnimationStateListener listener;
|
AnimationStateListener listener;
|
||||||
|
|
||||||
|
public void reset () {
|
||||||
|
animation = null;
|
||||||
|
listener = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Animation getAnimation () {
|
||||||
|
return animation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getLoop () {
|
||||||
|
return loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLoop (boolean loop) {
|
||||||
|
this.loop = loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getTime () {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTime (float time) {
|
||||||
|
this.time = time;
|
||||||
|
if (lastTime < time) lastTime = time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getEndTime () {
|
||||||
|
return endTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEndTime (float endTime) {
|
||||||
|
this.endTime = endTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnimationStateListener getListener () {
|
||||||
|
return listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListener (AnimationStateListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getDelay () {
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDelay (float delay) {
|
||||||
|
this.delay = delay;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static public interface AnimationStateListener {
|
static public interface AnimationStateListener {
|
||||||
|
|||||||
@ -112,12 +112,6 @@ public class AnimationStateTest extends ApplicationAdapter {
|
|||||||
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
|
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
state.apply(skeleton);
|
state.apply(skeleton);
|
||||||
if (state.getAnimation().getName().equals("walk")) {
|
|
||||||
// After one second, change the current animation. Mixing is done by AnimationState for you.
|
|
||||||
// if (state.getTime() > 2) state.setAnimation("jump", false);
|
|
||||||
// } else {
|
|
||||||
// if (state.getTime() > 1) state.setAnimation("walk", true);
|
|
||||||
}
|
|
||||||
skeleton.updateWorldTransform();
|
skeleton.updateWorldTransform();
|
||||||
|
|
||||||
batch.begin();
|
batch.begin();
|
||||||
|
|||||||
@ -995,6 +995,11 @@
|
|||||||
{ "time": 0.9333, "angle": 2.28 },
|
{ "time": 0.9333, "angle": 2.28 },
|
||||||
{ "time": 1.0666, "angle": 3.6 }
|
{ "time": 1.0666, "angle": 3.6 }
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"rotate": [
|
||||||
|
{ "time": 0, "angle": 0 }
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -88,7 +88,7 @@ namespace Spine {
|
|||||||
|
|
||||||
state = new AnimationState(stateData);
|
state = new AnimationState(stateData);
|
||||||
|
|
||||||
if (true) {
|
if (false) {
|
||||||
// Event handling for all animations.
|
// Event handling for all animations.
|
||||||
state.Start += new EventHandler(Start);
|
state.Start += new EventHandler(Start);
|
||||||
state.End += new EventHandler(End);
|
state.End += new EventHandler(End);
|
||||||
@ -98,7 +98,7 @@ namespace Spine {
|
|||||||
state.SetAnimation("drawOrder", true);
|
state.SetAnimation("drawOrder", true);
|
||||||
} else {
|
} else {
|
||||||
state.SetAnimation("walk", false);
|
state.SetAnimation("walk", false);
|
||||||
QueueEntry entry = state.AddAnimation("jump", false);
|
QueuedAnimation entry = state.AddAnimation("jump", false);
|
||||||
entry.End += new EventHandler(End); // Event handling for queued animations.
|
entry.End += new EventHandler(End); // Event handling for queued animations.
|
||||||
state.AddAnimation("walk", true);
|
state.AddAnimation("walk", true);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user