Merge pull request #6715 from dumganhar/iss5160-animation-notification

closed #5160: Dispatch a custom event after an animation frame is displayed
This commit is contained in:
minggo 2014-05-13 18:38:44 +08:00
commit 05c16de87a
8 changed files with 40 additions and 9 deletions

View File

@ -30,6 +30,9 @@ THE SOFTWARE.
#include "2d/CCNode.h"
#include "CCStdC.h"
#include "2d/CCActionInstant.h"
#include "base/CCDirector.h"
#include "base/CCEventCustom.h"
#include <stdarg.h>
NS_CC_BEGIN
@ -2137,6 +2140,7 @@ Animate::Animate()
, _origFrame(nullptr)
, _executedLoops(0)
, _animation(nullptr)
, _frameDisplayedEvent(nullptr)
{
}
@ -2146,6 +2150,7 @@ Animate::~Animate()
CC_SAFE_RELEASE(_animation);
CC_SAFE_RELEASE(_origFrame);
CC_SAFE_DELETE(_splitTimes);
CC_SAFE_RELEASE(_frameDisplayedEvent);
}
bool Animate::initWithAnimation(Animation* animation)
@ -2256,7 +2261,13 @@ void Animate::update(float t)
const ValueMap& dict = frame->getUserInfo();
if ( !dict.empty() )
{
//TODO: [[NSNotificationCenter defaultCenter] postNotificationName:AnimationFrameDisplayedNotification object:target_ userInfo:dict];
if (_frameDisplayedEvent == nullptr)
_frameDisplayedEvent = new EventCustom(AnimationFrameDisplayedNotification);
_frameDisplayedEventInfo.target = _target;
_frameDisplayedEventInfo.userInfo = &dict;
_frameDisplayedEvent->setUserData(&_frameDisplayedEventInfo);
Director::getInstance()->getEventDispatcher()->dispatchEvent(_frameDisplayedEvent);
}
_nextFrame = i+1;
}

View File

@ -1037,6 +1037,8 @@ protected:
unsigned int _executedLoops;
Animation* _animation;
EventCustom* _frameDisplayedEvent;
AnimationFrame::DisplayedEventInfo _frameDisplayedEventInfo;
private:
CC_DISALLOW_COPY_AND_ASSIGN(Animate);
};

View File

@ -57,6 +57,13 @@ class SpriteFrame;
class CC_DLL AnimationFrame : public Ref, public Clonable
{
public:
struct DisplayedEventInfo
{
Node* target;
const ValueMap* userInfo;
};
/**
* Creates the animation frame with a spriteframe, number of delay units and a notification user info
* @since 3.0

View File

@ -174,7 +174,7 @@ void AnimationCache::parseVersion2(const ValueMap& animations)
float delayUnits = entry["delayUnits"].asFloat();
Value& userInfo = entry["notification"];
AnimationFrame *animFrame = AnimationFrame::create(spriteFrame, delayUnits, userInfo.asValueMap());
AnimationFrame *animFrame = AnimationFrame::create(spriteFrame, delayUnits, userInfo.getType() == Value::Type::MAP ? userInfo.asValueMap() : ValueMapNull);
array.pushBack(animFrame);
}

View File

@ -28,6 +28,10 @@
NS_CC_BEGIN
const ValueVector ValueVectorNull;
const ValueMap ValueMapNull;
const ValueMapIntKey ValueMapIntKeyNull;
const Value Value::Null;
Value::Value()

View File

@ -39,6 +39,10 @@ typedef std::vector<Value> ValueVector;
typedef std::unordered_map<std::string, Value> ValueMap;
typedef std::unordered_map<int, Value> ValueMapIntKey;
extern const ValueVector ValueVectorNull;
extern const ValueMap ValueMapNull;
extern const ValueMapIntKey ValueMapIntKeyNull;
class Value
{
public:

View File

@ -723,13 +723,13 @@ void ActionAnimate::onEnter()
auto action2 = Animate::create(animation2);
_tamara->runAction(Sequence::create(action2, action2->reverse(), NULL));
// TODO:
// observer_ = [[NSNotificationCenter defaultCenter] addObserverForName:AnimationFrameDisplayedNotification object:nil queue:nil usingBlock:^(NSNotification* notification) {
//
// auto userInfo = [notification userInfo);
// NSLog(@"object %@ with data %@", [notification object), userInfo );
// });
_frameDisplayedListener = EventListenerCustom::create(AnimationFrameDisplayedNotification, [](EventCustom * event){
auto userData = static_cast<AnimationFrame::DisplayedEventInfo*>(event->getUserData());
log("target %p with data %s", userData->target, Value(userData->userInfo).getDescription().c_str());
});
_eventDispatcher->addEventListenerWithFixedPriority(_frameDisplayedListener, -1);
//
// File animation
@ -746,7 +746,7 @@ void ActionAnimate::onEnter()
void ActionAnimate::onExit()
{
ActionsDemo::onExit();
//TODO:[[NSNotificationCenter defaultCenter] removeObserver:observer_);
_eventDispatcher->removeEventListener(_frameDisplayedListener);
}
std::string ActionAnimate::title() const

View File

@ -197,6 +197,9 @@ public:
virtual void onExit() override;
virtual std::string title() const override;
virtual std::string subtitle() const override;
private:
EventListenerCustom* _frameDisplayedListener;
};
class ActionSequence : public ActionsDemo