From 331f0ef7b04d9fb4da2877c6e14d4f4ab1b43555 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 12 Aug 2013 11:18:07 +0800 Subject: [PATCH] issue #2521: Capture event of ControlButton in CCB for JSB. --- extensions/CCBReader/CCBAnimationManager.cpp | 12 +++ extensions/CCBReader/CCBAnimationManager.h | 6 ++ extensions/CCBReader/CCBReader.cpp | 92 ++++++++++++-------- extensions/CCBReader/CCBReader.h | 21 ++--- extensions/CCBReader/CCNodeLoader.cpp | 37 +++++--- 5 files changed, 112 insertions(+), 56 deletions(-) diff --git a/extensions/CCBReader/CCBAnimationManager.cpp b/extensions/CCBReader/CCBAnimationManager.cpp index 7526e4d0a3..7b6172a3dd 100644 --- a/extensions/CCBReader/CCBAnimationManager.cpp +++ b/extensions/CCBReader/CCBAnimationManager.cpp @@ -42,6 +42,7 @@ bool CCBAnimationManager::init() _documentOutletNodes = new Array(); _documentCallbackNames = new Array(); _documentCallbackNodes = new Array(); + _documentCallbackControlEvents = new Array(); _keyframeCallbacks = new Array(); _keyframeCallFuncs = new Dictionary(); @@ -76,6 +77,7 @@ CCBAnimationManager::~CCBAnimationManager() CC_SAFE_RELEASE(_documentOutletNodes); CC_SAFE_RELEASE(_documentCallbackNames); CC_SAFE_RELEASE(_documentCallbackNodes); + CC_SAFE_RELEASE(_documentCallbackControlEvents); CC_SAFE_RELEASE(_keyframeCallFuncs); CC_SAFE_RELEASE(_keyframeCallbacks); @@ -130,6 +132,11 @@ void CCBAnimationManager::addDocumentCallbackName(std::string name) { _documentCallbackNames->addObject(tmpName); } +void CCBAnimationManager::addDocumentCallbackControlEvents(Control::EventType eventType) +{ + _documentCallbackControlEvents->addObject(Integer::create((int)eventType)); +} + Array* CCBAnimationManager::getDocumentCallbackNames() { return _documentCallbackNames; } @@ -138,6 +145,11 @@ Array* CCBAnimationManager::getDocumentCallbackNodes() { return _documentCallbackNodes; } +Array* CCBAnimationManager::getDocumentCallbackControlEvents() +{ + return _documentCallbackControlEvents; +} + void CCBAnimationManager::addDocumentOutletNode(Node *node) { _documentOutletNodes->addObject(node); } diff --git a/extensions/CCBReader/CCBAnimationManager.h b/extensions/CCBReader/CCBAnimationManager.h index b43276ff40..9ae3f24bbc 100644 --- a/extensions/CCBReader/CCBAnimationManager.h +++ b/extensions/CCBReader/CCBAnimationManager.h @@ -6,6 +6,7 @@ #include "CCBSequence.h" #include "CCBValue.h" #include "CCBSequenceProperty.h" +#include "GUI/CCControlExtension/CCControl.h" NS_CC_EXT_BEGIN @@ -40,6 +41,8 @@ public: void addDocumentCallbackNode(Node *node); void addDocumentCallbackName(std::string name); + void addDocumentCallbackControlEvents(Control::EventType eventType); + void addDocumentOutletNode(Node *node); void addDocumentOutletName(std::string name); @@ -48,6 +51,8 @@ public: std::string getDocumentControllerName(); Array* getDocumentCallbackNames(); Array* getDocumentCallbackNodes(); + Array* getDocumentCallbackControlEvents(); + Array* getDocumentOutletNames(); Array* getDocumentOutletNodes(); std::string getLastCompletedSequenceName(); @@ -121,6 +126,7 @@ private: Array *_documentOutletNodes; Array *_documentCallbackNames; Array *_documentCallbackNodes; + Array *_documentCallbackControlEvents; Array *_keyframeCallbacks; Dictionary *_keyframeCallFuncs; diff --git a/extensions/CCBReader/CCBReader.cpp b/extensions/CCBReader/CCBReader.cpp index 9d6df2baba..a4a6bf7811 100644 --- a/extensions/CCBReader/CCBReader.cpp +++ b/extensions/CCBReader/CCBReader.cpp @@ -65,7 +65,6 @@ CCBReader::CCBReader(NodeLoaderLibrary * pNodeLoaderLibrary, CCBMemberVariableAs , _nodesWithAnimationManagers(NULL) , _animationManagersForNodes(NULL) , _ownerCallbackNodes(NULL) -, _hasScriptingOwner(false) { this->_nodeLoaderLibrary = pNodeLoaderLibrary; this->_nodeLoaderLibrary->retain(); @@ -88,7 +87,6 @@ CCBReader::CCBReader(CCBReader * ccbReader) , _nodesWithAnimationManagers(NULL) , _animationManagersForNodes(NULL) , _ownerCallbackNodes(NULL) -, _hasScriptingOwner(false) { this->_loadedSpriteSheets = ccbReader->_loadedSpriteSheets; this->_nodeLoaderLibrary = ccbReader->_nodeLoaderLibrary; @@ -97,13 +95,6 @@ CCBReader::CCBReader(CCBReader * ccbReader) this->_CCBMemberVariableAssigner = ccbReader->_CCBMemberVariableAssigner; this->_CCBSelectorResolver = ccbReader->_CCBSelectorResolver; this->_nodeLoaderListener = ccbReader->_nodeLoaderListener; - - this->_ownerCallbackNames = ccbReader->_ownerCallbackNames; - this->_ownerCallbackNodes = ccbReader->_ownerCallbackNodes; - this->_ownerCallbackNodes->retain(); - this->_ownerOutletNames = ccbReader->_ownerOutletNames; - this->_ownerOutletNodes = ccbReader->_ownerOutletNodes; - this->_ownerOutletNodes->retain(); this->_CCBRootPath = ccbReader->getCCBRootPath(); @@ -124,12 +115,12 @@ CCBReader::CCBReader() , _CCBSelectorResolver(NULL) , _nodesWithAnimationManagers(NULL) , _animationManagersForNodes(NULL) -, _hasScriptingOwner(false) { init(); } -CCBReader::~CCBReader() { +CCBReader::~CCBReader() +{ CC_SAFE_RELEASE_NULL(_owner); CC_SAFE_RELEASE_NULL(_data); @@ -139,9 +130,9 @@ CCBReader::~CCBReader() { _ownerOutletNames.clear(); CC_SAFE_RELEASE(_ownerCallbackNodes); _ownerCallbackNames.clear(); - + CC_SAFE_RELEASE(_ownerOwnerCallbackControlEvents); + // Clear string cache. - this->_stringCache.clear(); CC_SAFE_RELEASE(_nodesWithAnimationManagers); CC_SAFE_RELEASE(_animationManagersForNodes); @@ -162,6 +153,10 @@ const std::string& CCBReader::getCCBRootPath() const bool CCBReader::init() { + _ownerOutletNodes = new Array(); + _ownerCallbackNodes = new Array(); + _ownerOwnerCallbackControlEvents = new Array(); + // Setup action manager CCBAnimationManager *pActionManager = new CCBAnimationManager(); setAnimationManager(pActionManager); @@ -269,8 +264,6 @@ Node* CCBReader::readNodeGraphFromData(Data *pData, Object *pOwner, const Size & _actionManager->setRootContainerSize(parentSize); _actionManager->_owner = _owner; - _ownerOutletNodes = new Array(); - _ownerCallbackNodes = new Array(); Dictionary* animationManagers = Dictionary::create(); Node *pNodeGraph = readFileWithCleanUp(true, animationManagers); @@ -280,8 +273,10 @@ Node* CCBReader::readNodeGraphFromData(Data *pData, Object *pOwner, const Size & // Auto play animations _actionManager->runAnimationsForSequenceIdTweenDuration(_actionManager->getAutoPlaySequenceId(), 0); } + // Assign actionManagers to userObject - if(_jsControlled) { + if(_jsControlled) + { _nodesWithAnimationManagers = new Array(); _animationManagersForNodes = new Array(); } @@ -716,8 +711,8 @@ Node * CCBReader::readNodeGraph(Node * pParent) if(target != NULL) { CCBMemberVariableAssigner * targetAsCCBMemberVariableAssigner = dynamic_cast(target); - if(targetAsCCBMemberVariableAssigner != NULL) { - + if(targetAsCCBMemberVariableAssigner != NULL) + { Dictionary* pCustomPropeties = ccNodeLoader->getCustomProperties(); DictElement* pElement; CCDICT_FOREACH(pCustomPropeties, pElement) @@ -741,7 +736,8 @@ Node * CCBReader::readNodeGraph(Node * pParent) /* Read and add children. */ int numChildren = this->readInt(false); - for(int i = 0; i < numChildren; i++) { + for(int i = 0; i < numChildren; i++) + { Node * child = this->readNodeGraph(node); node->addChild(child); } @@ -749,12 +745,16 @@ Node * CCBReader::readNodeGraph(Node * pParent) // FIX ISSUE #1860: "onNodeLoaded will be called twice if ccb was added as a CCBFile". // If it's a sub-ccb node, skip notification to NodeLoaderListener since it will be // notified at LINE #734: Node * child = this->readNodeGraph(node); - if (!isCCBFileNode) { + if (!isCCBFileNode) + { // Call onNodeLoaded NodeLoaderListener * nodeAsNodeLoaderListener = dynamic_cast(node); - if(nodeAsNodeLoaderListener != NULL) { + if(nodeAsNodeLoaderListener != NULL) + { nodeAsNodeLoaderListener->onNodeLoaded(node, ccNodeLoader); - } else if(this->_nodeLoaderListener != NULL) { + } + else if(this->_nodeLoaderListener != NULL) + { this->_nodeLoaderListener->onNodeLoaded(node, ccNodeLoader); } } @@ -991,29 +991,43 @@ bool CCBReader::endsWith(const char* pString, const char* pEnding) { } } -bool CCBReader::isJSControlled() { +bool CCBReader::isJSControlled() +{ return _jsControlled; } -void CCBReader::addOwnerCallbackName(std::string name) { +void CCBReader::addOwnerCallbackName(const std::string& name) +{ _ownerCallbackNames.push_back(name); } -void CCBReader::addOwnerCallbackNode(Node *node) { +void CCBReader::addOwnerCallbackNode(Node *node) +{ _ownerCallbackNodes->addObject(node); } +void CCBReader::addOwnerCallbackControlEvents(Control::EventType type) +{ + _ownerOwnerCallbackControlEvents->addObject(Integer::create((int)type)); +} -void CCBReader::addDocumentCallbackName(std::string name) { +void CCBReader::addDocumentCallbackName(const std::string& name) +{ _actionManager->addDocumentCallbackName(name); } -void CCBReader::addDocumentCallbackNode(Node *node) { +void CCBReader::addDocumentCallbackNode(Node *node) +{ _actionManager->addDocumentCallbackNode(node); } +void CCBReader::addDocumentCallbackControlEvents(Control::EventType eventType) +{ + _actionManager->addDocumentCallbackControlEvents(eventType); +} -Array* CCBReader::getOwnerCallbackNames() { +Array* CCBReader::getOwnerCallbackNames() +{ Array* pRet = Array::createWithCapacity(_ownerCallbackNames.size()); std::vector::iterator it = _ownerCallbackNames.begin(); for (; it != _ownerCallbackNames.end(); ++it) @@ -1024,11 +1038,18 @@ Array* CCBReader::getOwnerCallbackNames() { return pRet; } -Array* CCBReader::getOwnerCallbackNodes() { +Array* CCBReader::getOwnerCallbackNodes() +{ return _ownerCallbackNodes; } -Array* CCBReader::getOwnerOutletNames() { +Array* CCBReader::getOwnerCallbackControlEvents() +{ + return _ownerOwnerCallbackControlEvents; +} + +Array* CCBReader::getOwnerOutletNames() +{ Array* pRet = Array::createWithCapacity(_ownerOutletNames.size()); std::vector::iterator it = _ownerOutletNames.begin(); for (; it != _ownerOutletNames.end(); ++it) @@ -1038,23 +1059,26 @@ Array* CCBReader::getOwnerOutletNames() { return pRet; } -Array* CCBReader::getOwnerOutletNodes() { +Array* CCBReader::getOwnerOutletNodes() +{ return _ownerOutletNodes; } -Array* CCBReader::getNodesWithAnimationManagers() { +Array* CCBReader::getNodesWithAnimationManagers() +{ return _nodesWithAnimationManagers; } -Array* CCBReader::getAnimationManagersForNodes() { +Array* CCBReader::getAnimationManagersForNodes() +{ return _animationManagersForNodes; } void CCBReader::addOwnerOutletName(std::string name) { _ownerOutletNames.push_back(name); - } + void CCBReader::addOwnerOutletNode(Node *node) { if (NULL != node) diff --git a/extensions/CCBReader/CCBReader.h b/extensions/CCBReader/CCBReader.h index d30c7ecbea..ecbd7ff728 100644 --- a/extensions/CCBReader/CCBReader.h +++ b/extensions/CCBReader/CCBReader.h @@ -6,7 +6,7 @@ #include #include #include "CCBSequence.h" - +#include "GUI/CCControlExtension/CCControl.h" #define CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(T, METHOD) static T * METHOD() { \ T * ptr = new T(); \ @@ -187,28 +187,29 @@ public: float readFloat(); std::string readCachedString(); bool isJSControlled(); - bool readCallbackKeyframesForSeq(CCBSequence* seq); bool readSoundKeyframesForSeq(CCBSequence* seq); - - Array* getOwnerCallbackNames(); Array* getOwnerCallbackNodes(); + Array* getOwnerCallbackControlEvents(); + Array* getOwnerOutletNames(); Array* getOwnerOutletNodes(); Array* getNodesWithAnimationManagers(); Array* getAnimationManagersForNodes(); - + Dictionary* getAnimationManagers(); void setAnimationManagers(Dictionary* x); // weak reference - void addOwnerCallbackName(std::string name); + void addOwnerCallbackName(const std::string& name); void addOwnerCallbackNode(Node *node); + void addOwnerCallbackControlEvents(Control::EventType type); - void addDocumentCallbackName(std::string name); + void addDocumentCallbackName(const std::string& name); void addDocumentCallbackNode(Node *node); + void addDocumentCallbackControlEvents(Control::EventType eventType); static float getResolutionScale(); static void setResolutionScale(float scale); @@ -232,6 +233,8 @@ private: bool getBit(); void alignBits(); + bool init(); + friend class NodeLoader; private: @@ -262,12 +265,10 @@ private: std::vector _ownerCallbackNames; Array* _ownerCallbackNodes; + Array* _ownerOwnerCallbackControlEvents; std::string _CCBRootPath; bool _jsControlled; - - bool _hasScriptingOwner; - bool init(); }; // end of effects group diff --git a/extensions/CCBReader/CCNodeLoader.cpp b/extensions/CCBReader/CCNodeLoader.cpp index de877acfe2..2c6ec045f8 100644 --- a/extensions/CCBReader/CCNodeLoader.cpp +++ b/extensions/CCBReader/CCNodeLoader.cpp @@ -800,14 +800,22 @@ BlockData * NodeLoader::parsePropTypeBlock(Node * pNode, Node * pParent, CCBRead } else { CCLOG("Unexpected NULL target for selector."); } - } else { - if(selectorTarget == CCBReader::TargetType::DOCUMENT_ROOT) { + } + else + { + if (selectorTarget == CCBReader::TargetType::DOCUMENT_ROOT) + { ccbReader->addDocumentCallbackNode(pNode); ccbReader->addDocumentCallbackName(selectorName); - - } else { + // Since there isn't a Control::EventType::NONE, add a TOUCH_DOWN type as a placeholder. + ccbReader->addDocumentCallbackControlEvents(Control::EventType::TOUCH_DOWN); + } + else if (selectorTarget == CCBReader::TargetType::OWNER) + { ccbReader->addOwnerCallbackNode(pNode); ccbReader->addOwnerCallbackName(selectorName); + // Since there isn't a Control::EventType::NONE, add a TOUCH_DOWN type as a placeholder. + ccbReader->addOwnerCallbackControlEvents(Control::EventType::TOUCH_DOWN); } } } @@ -884,12 +892,13 @@ BlockControlData * NodeLoader::parsePropTypeBlockControl(Node * pNode, Node * pP { ccbReader->addDocumentCallbackNode(pNode); ccbReader->addDocumentCallbackName(selectorName); - + ccbReader->addDocumentCallbackControlEvents((Control::EventType)controlEvents); } else { ccbReader->addOwnerCallbackNode(pNode); ccbReader->addOwnerCallbackName(selectorName); + ccbReader->addOwnerCallbackControlEvents((Control::EventType)controlEvents); } } } @@ -953,11 +962,13 @@ Node * NodeLoader::parsePropTypeCCBFile(Node * pNode, Node * pParent, CCBReader if (NULL != ownerCallbackNames && ownerCallbackNames->count() > 0 && NULL != ownerCallbackNodes && ownerCallbackNodes->count() > 0) { - assert(ownerCallbackNames->count() == ownerCallbackNodes->count()); + CCASSERT(ownerCallbackNames->count() == ownerCallbackNodes->count(), ""); int nCount = ownerCallbackNames->count(); - for (int i = 0 ; i < nCount; i++) { + + for (int i = 0 ; i < nCount; i++) + { pCCBReader->addOwnerCallbackName((dynamic_cast(ownerCallbackNames->objectAtIndex(i)))->getCString()); - pCCBReader->addOwnerCallbackNode(dynamic_cast(ownerCallbackNames->objectAtIndex(i)) ); + pCCBReader->addOwnerCallbackNode(dynamic_cast(ownerCallbackNodes->objectAtIndex(i)) ); } } //set variables @@ -966,11 +977,13 @@ Node * NodeLoader::parsePropTypeCCBFile(Node * pNode, Node * pParent, CCBReader if (NULL != ownerOutletNames && ownerOutletNames->count() > 0 && NULL != ownerOutletNodes && ownerOutletNodes->count() > 0) { - assert(ownerOutletNames->count() == ownerOutletNodes->count()); + CCASSERT(ownerOutletNames->count() == ownerOutletNodes->count(), ""); int nCount = ownerOutletNames->count(); - for (int i = 0 ; i < nCount; i++) { - pCCBReader->addOwnerOutletName((dynamic_cast(ownerOutletNames->objectAtIndex(i)))->getCString()); - pCCBReader->addOwnerOutletNode(dynamic_cast(ownerOutletNodes->objectAtIndex(i)) ); + + for (int i = 0 ; i < nCount; i++) + { + pCCBReader->addOwnerOutletName((static_cast(ownerOutletNames->objectAtIndex(i)))->getCString()); + pCCBReader->addOwnerOutletNode(static_cast(ownerOutletNodes->objectAtIndex(i))); } } }