C++11 AnimationState listener.

Because 2014.
This commit is contained in:
NathanSweet 2014-05-08 22:29:50 +02:00
parent 87a5f683c3
commit 90ec4db279
3 changed files with 8 additions and 15 deletions

View File

@ -49,7 +49,7 @@ bool ExampleLayer::init () {
skeletonNode = SkeletonAnimation::createWithFile("spineboy.json", "spineboy.atlas", 0.6f); skeletonNode = SkeletonAnimation::createWithFile("spineboy.json", "spineboy.atlas", 0.6f);
skeletonNode->setMix("walk", "jump", 0.2f); skeletonNode->setMix("walk", "jump", 0.2f);
skeletonNode->setMix("jump", "run", 0.2f); skeletonNode->setMix("jump", "run", 0.2f);
skeletonNode->setAnimationListener(this, animationStateEvent_selector(ExampleLayer::animationStateEvent)); skeletonNode->setAnimationListener(&ExampleLayer::animationStateEvent, this);
// skeletonNode->timeScale = 0.3f; // skeletonNode->timeScale = 0.3f;
skeletonNode->debugBones = true; skeletonNode->debugBones = true;

View File

@ -63,9 +63,6 @@ SkeletonAnimation* SkeletonAnimation::createWithFile (const char* skeletonDataFi
} }
void SkeletonAnimation::initialize () { void SkeletonAnimation::initialize () {
listenerInstance = 0;
listenerMethod = 0;
ownsAnimationStateData = true; ownsAnimationStateData = true;
state = spAnimationState_create(spAnimationStateData_create(skeleton->data)); state = spAnimationState_create(spAnimationStateData_create(skeleton->data));
state->context = this; state->context = this;
@ -117,11 +114,6 @@ void SkeletonAnimation::setMix (const char* fromAnimation, const char* toAnimati
spAnimationStateData_setMixByName(state->data, fromAnimation, toAnimation, duration); spAnimationStateData_setMixByName(state->data, fromAnimation, toAnimation, duration);
} }
void SkeletonAnimation::setAnimationListener (Ref* instance, SEL_AnimationStateEvent method) {
listenerInstance = instance;
listenerMethod = method;
}
spTrackEntry* SkeletonAnimation::setAnimation (int trackIndex, const char* name, bool loop) { spTrackEntry* SkeletonAnimation::setAnimation (int trackIndex, const char* name, bool loop) {
spAnimation* animation = spSkeletonData_findAnimation(skeleton->data, name); spAnimation* animation = spSkeletonData_findAnimation(skeleton->data, name);
if (!animation) { if (!animation) {
@ -153,7 +145,7 @@ void SkeletonAnimation::clearTrack (int trackIndex) {
} }
void SkeletonAnimation::onAnimationStateEvent (int trackIndex, spEventType type, spEvent* event, int loopCount) { void SkeletonAnimation::onAnimationStateEvent (int trackIndex, spEventType type, spEvent* event, int loopCount) {
if (listenerInstance) (listenerInstance->*listenerMethod)(this, trackIndex, type, event, loopCount); if (listener) listener(this, trackIndex, type, event, loopCount);
} }
} }

View File

@ -38,8 +38,7 @@
namespace spine { namespace spine {
class SkeletonAnimation; class SkeletonAnimation;
typedef void (cocos2d::Ref::*SEL_AnimationStateEvent)(spine::SkeletonAnimation* node, int trackIndex, spEventType type, spEvent* event, int loopCount); typedef std::function<void(spine::SkeletonAnimation* node, int trackIndex, spEventType type, spEvent* event, int loopCount)> AnimationStateListener;
#define animationStateEvent_selector(_SELECTOR) (SEL_AnimationStateEvent)(&_SELECTOR)
/** Draws an animated skeleton, providing an AnimationState for applying one or more animations and queuing animations to be /** Draws an animated skeleton, providing an AnimationState for applying one or more animations and queuing animations to be
* played later. */ * played later. */
@ -62,7 +61,10 @@ public:
void setAnimationStateData (spAnimationStateData* stateData); void setAnimationStateData (spAnimationStateData* stateData);
void setMix (const char* fromAnimation, const char* toAnimation, float duration); void setMix (const char* fromAnimation, const char* toAnimation, float duration);
void setAnimationListener (cocos2d::Ref* instance, SEL_AnimationStateEvent method); template<class _Rx, class _Farg0, class _Arg0> void setAnimationListener (_Rx _Farg0::* const type, _Arg0&& target) {
this->listener = std::bind(type, target, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5);
}
spTrackEntry* setAnimation (int trackIndex, const char* name, bool loop); spTrackEntry* setAnimation (int trackIndex, const char* name, bool loop);
spTrackEntry* addAnimation (int trackIndex, const char* name, bool loop, float delay = 0); spTrackEntry* addAnimation (int trackIndex, const char* name, bool loop, float delay = 0);
spTrackEntry* getCurrent (int trackIndex = 0); spTrackEntry* getCurrent (int trackIndex = 0);
@ -76,8 +78,7 @@ protected:
private: private:
typedef SkeletonRenderer super; typedef SkeletonRenderer super;
cocos2d::Ref* listenerInstance; AnimationStateListener listener;
SEL_AnimationStateEvent listenerMethod;
bool ownsAnimationStateData; bool ownsAnimationStateData;
void initialize (); void initialize ();