Better AnimationState event delegates.

This commit is contained in:
NathanSweet 2014-01-26 21:08:09 +01:00
parent 8506ed5154
commit 4216077c0e
4 changed files with 41 additions and 54 deletions

View File

@ -40,13 +40,15 @@ namespace Spine {
public AnimationStateData Data { get { return data; } }
public float TimeScale { get { return timeScale; } set { timeScale = value; } }
/// <summary>The list of tracks that have animations, which may contain nulls.</summary>
public List<TrackEntry> Tracks { get { return tracks; } }
public delegate void StartEndDelegate(AnimationState state, int trackIndex);
public StartEndDelegate Start;
public StartEndDelegate End;
public event EventHandler<StartEndArgs> Start;
public event EventHandler<StartEndArgs> End;
public event EventHandler<EventTriggeredArgs> Event;
public event EventHandler<CompleteArgs> Complete;
public delegate void EventDelegate(AnimationState state, int trackIndex, Event e);
public EventDelegate Event;
public delegate void CompleteDelete(AnimationState state, int trackIndex, int loopCount);
public CompleteDelete Complete;
public AnimationState (AnimationStateData data) {
if (data == null) throw new ArgumentNullException("data cannot be null.");
@ -73,7 +75,7 @@ namespace Spine {
if (current.loop ? (current.lastTime % endTime > time % endTime) : (current.lastTime < endTime && time >= endTime)) {
int count = (int)(time / endTime);
current.OnComplete(this, i, count);
if (Complete != null) Complete(this, new CompleteArgs(i, count));
if (Complete != null) Complete(this, i, count);
}
TrackEntry next = current.next;
@ -118,7 +120,7 @@ namespace Spine {
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));
if (Event != null) Event(this, i, e);
}
current.lastTime = current.time;
@ -137,7 +139,7 @@ namespace Spine {
if (current == null) return;
current.OnEnd(this, trackIndex);
if (End != null) End(this, new StartEndArgs(trackIndex));
if (End != null) End(this, trackIndex);
tracks[trackIndex] = null;
}
@ -156,7 +158,7 @@ namespace Spine {
current.previous = null;
current.OnEnd(this, index);
if (End != null) End(this, new StartEndArgs(index));
if (End != null) End(this, index);
entry.mixDuration = data.GetMix(current.animation, entry.animation);
if (entry.mixDuration > 0) {
@ -172,7 +174,7 @@ namespace Spine {
tracks[index] = entry;
entry.OnStart(this, index);
if (Start != null) Start(this, new StartEndArgs(index));
if (Start != null) Start(this, index);
}
public TrackEntry SetAnimation (int trackIndex, String animationName, bool loop) {
@ -245,34 +247,6 @@ namespace Spine {
}
}
public class EventTriggeredArgs : EventArgs {
public int TrackIndex { get; private set; }
public Event Event { get; private set; }
public EventTriggeredArgs (int trackIndex, Event e) {
TrackIndex = trackIndex;
Event = e;
}
}
public class CompleteArgs : EventArgs {
public int TrackIndex { get; private set; }
public int LoopCount { get; private set; }
public CompleteArgs (int trackIndex, int loopCount) {
TrackIndex = trackIndex;
LoopCount = loopCount;
}
}
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;
@ -287,26 +261,26 @@ namespace Spine {
public float EndTime { get { return endTime; } set { endTime = value; } }
public float TimeScale { get { return timeScale; } set { timeScale = value; } }
public bool Loop { get { return loop; } set { loop = value; } }
public event EventHandler<StartEndArgs> Start;
public event EventHandler<StartEndArgs> End;
public event EventHandler<EventTriggeredArgs> Event;
public event EventHandler<CompleteArgs> Complete;
public AnimationState.StartEndDelegate Start;
public AnimationState.StartEndDelegate End;
public AnimationState.EventDelegate Event;
public AnimationState.CompleteDelete Complete;
internal void OnStart (AnimationState state, int index) {
if (Start != null) Start(state, new StartEndArgs(index));
if (Start != null) Start(state, index);
}
internal void OnEnd (AnimationState state, int index) {
if (End != null) End(state, new StartEndArgs(index));
if (End != null) End(state, index);
}
internal void OnEvent (AnimationState state, int index, Event e) {
if (Event != null) Event(state, new EventTriggeredArgs(index, e));
if (Event != null) Event(state, index, e);
}
internal void OnComplete (AnimationState state, int index, int loopCount) {
if (Complete != null) Complete(state, new CompleteArgs(index, loopCount));
if (Complete != null) Complete(state, index, loopCount);
}
override public String ToString () {

View File

@ -38,9 +38,9 @@ public class Spineboy : MonoBehaviour {
skeleton = GetComponent<SkeletonAnimation>();
skeleton.state.Event += Event;
}
public void Event (object sender, EventTriggeredArgs e) {
Debug.Log(e.TrackIndex + " " + skeleton.state.GetCurrent(e.TrackIndex) + ": event " + e.Event + ", " + e.Event.Int);
public void Event (Spine.AnimationState state, int trackIndex, Spine.Event e) {
Debug.Log(trackIndex + " " + state.GetCurrent(trackIndex) + ": event " + e + ", " + e.Int);
}
void OnMouseDown() {

View File

@ -28,12 +28,25 @@
using UnityEngine;
using System.Collections;
using Spine;
public class Goblins : MonoBehaviour {
private bool girlSkin;
private SkeletonAnimation skeletonAnimation;
private Bone headBone;
public void Start () {
skeletonAnimation = GetComponent<SkeletonAnimation>();
headBone = skeletonAnimation.skeleton.FindBone("head");
skeletonAnimation.UpdateBones += UpdateBones;
}
// This is called after the animation is applied to the skeleton and can be used to adjust the bones dynamically.
public void UpdateBones (SkeletonComponent skeletonAnimation) {
headBone.Rotation -= 15;
}
public void OnMouseDown () {
SkeletonAnimation skeletonAnimation = GetComponent<SkeletonAnimation>();
skeletonAnimation.skeleton.SetSkin(girlSkin ? "goblin" : "goblingirl");
skeletonAnimation.skeleton.SetSlotsToSetupPose();

View File

@ -39,8 +39,8 @@ public class Spineboy : MonoBehaviour {
skeletonAnimation.state.Event += Event;
}
public void Event (object sender, EventTriggeredArgs e) {
Debug.Log(e.TrackIndex + " " + skeletonAnimation.state.GetCurrent(e.TrackIndex) + ": event " + e.Event + ", " + e.Event.Int);
public void Event (Spine.AnimationState state, int trackIndex, Spine.Event e) {
Debug.Log(trackIndex + " " + state.GetCurrent(trackIndex) + ": event " + e + ", " + e.Int);
}
public void OnMouseDown () {