12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214 |
- #include <ctype.h>
- #include <algorithm>
- #include "base/CCDirector.h"
- #include "platform/CCFileUtils.h"
- #include "2d/CCScene.h"
- #include "2d/CCSpriteFrameCache.h"
- #include "renderer/CCTextureCache.h"
- #include "editor-support/cocosbuilder/CCBReader.h"
- #include "editor-support/cocosbuilder/CCNodeLoader.h"
- #include "editor-support/cocosbuilder/CCNodeLoaderLibrary.h"
- #include "editor-support/cocosbuilder/CCNodeLoaderListener.h"
- #include "editor-support/cocosbuilder/CCBMemberVariableAssigner.h"
- #include "editor-support/cocosbuilder/CCBSelectorResolver.h"
- #include "editor-support/cocosbuilder/CCBAnimationManager.h"
- #include "editor-support/cocosbuilder/CCBSequenceProperty.h"
- #include "editor-support/cocosbuilder/CCBKeyframe.h"
- #include <sstream>
- #include "common/CocosConfig.h"
- using namespace cocos2d;
- using namespace cocos2d::extension;
- namespace cocosbuilder {
- /*************************************************************************
- Implementation of CCBFile
- *************************************************************************/
- //add by yutao
- std::unordered_map<std::string, std::shared_ptr<cocos2d::Data>> s_ccbFileCacheMap;
- std::shared_ptr<cocos2d::Data> CCBReader::getBytesFromCCBFileByName(std::string ccbFullFilePath)
- {
- std::string ccbfileStr(ccbFullFilePath);
- auto p = s_ccbFileCacheMap.find(ccbfileStr);
- if( p != s_ccbFileCacheMap.end() && strcmp(ccbFullFilePath.c_str(), "RedInterstitialAd.ccbi") != 0 && strcmp(ccbFullFilePath.c_str(), "BulldogTableViewLayer.ccbi") != 0 && strcmp(ccbFullFilePath.c_str(), "BulldogTableViewCell.ccbi") != 0)
- {
- //CCLOG("从缓存中读ccb文件%s",ccbfileStr.c_str());
- return p->second;
- }
- else
- {
- auto data = std::make_shared<Data>(FileUtils::getInstance()->getDataFromFile(ccbFullFilePath));
- s_ccbFileCacheMap.insert(make_pair(ccbfileStr, data));
- //CCLOG("从文件读ccb文件%s",ccbfileStr.c_str());
- return data;
- }
- }
- void CCBReader::releaseAllCCBFileCache()
- {
- s_ccbFileCacheMap.erase(s_ccbFileCacheMap.begin(),s_ccbFileCacheMap.end());
- }
-
- CCBFile::CCBFile():_CCBFileNode(nullptr) {}
- CCBFile* CCBFile::create()
- {
- CCBFile *ret = new (std::nothrow) CCBFile();
-
- if (ret)
- {
- ret->autorelease();
- }
-
- return ret;
- }
- Node* CCBFile::getCCBFileNode()
- {
- return _CCBFileNode;
- }
- void CCBFile::setCCBFileNode(Node *pNode)
- {
- CC_SAFE_RELEASE(_CCBFileNode);
- _CCBFileNode = pNode;
- CC_SAFE_RETAIN(_CCBFileNode);
- }
- /*************************************************************************
- Implementation of CCBReader
- *************************************************************************/
- CCBReader::CCBReader(NodeLoaderLibrary * pNodeLoaderLibrary, CCBMemberVariableAssigner * pCCBMemberVariableAssigner, CCBSelectorResolver * pCCBSelectorResolver, NodeLoaderListener * pNodeLoaderListener)
- : _data(nullptr)
- , _bytes(nullptr)
- , _currentByte(-1)
- , _currentBit(-1)
- , _owner(nullptr)
- , _animationManager(nullptr)
- , _animatedProps(nullptr)
- {
- this->_nodeLoaderLibrary = pNodeLoaderLibrary;
- this->_nodeLoaderLibrary->retain();
- this->_CCBMemberVariableAssigner = pCCBMemberVariableAssigner;
- this->_CCBSelectorResolver = pCCBSelectorResolver;
- this->_nodeLoaderListener = pNodeLoaderListener;
- init();
- }
- CCBReader::CCBReader(CCBReader * ccbReader)
- : _data(nullptr)
- , _bytes(nullptr)
- , _currentByte(-1)
- , _currentBit(-1)
- , _owner(nullptr)
- , _animationManager(nullptr)
- , _animatedProps(nullptr)
- {
- this->_loadedSpriteSheets = ccbReader->_loadedSpriteSheets;
- this->_nodeLoaderLibrary = ccbReader->_nodeLoaderLibrary;
- this->_nodeLoaderLibrary->retain();
- this->_CCBMemberVariableAssigner = ccbReader->_CCBMemberVariableAssigner;
- this->_CCBSelectorResolver = ccbReader->_CCBSelectorResolver;
- this->_nodeLoaderListener = ccbReader->_nodeLoaderListener;
-
- this->_CCBRootPath = ccbReader->getCCBRootPath();
-
- init();
- }
- CCBReader::CCBReader()
- : _data(nullptr)
- , _bytes(nullptr)
- , _currentByte(-1)
- , _currentBit(-1)
- , _owner(nullptr)
- , _animationManager(nullptr)
- , _nodeLoaderLibrary(nullptr)
- , _nodeLoaderListener(nullptr)
- , _CCBMemberVariableAssigner(nullptr)
- , _CCBSelectorResolver(nullptr)
- {
- init();
- }
- CCBReader::~CCBReader()
- {
- CC_SAFE_RELEASE_NULL(_owner);
- this->_nodeLoaderLibrary->release();
- _ownerOutletNames.clear();
- _ownerCallbackNames.clear();
-
- // Clear string cache.
- this->_stringCache.clear();
- setAnimationManager(nullptr);
- }
- void CCBReader::setCCBRootPath(const char* ccbRootPath)
- {
- CCASSERT(ccbRootPath != nullptr, "ccbRootPath can't be nullptr!");
- _CCBRootPath = ccbRootPath;
- }
- const std::string& CCBReader::getCCBRootPath() const
- {
- return _CCBRootPath;
- }
- bool CCBReader::init()
- {
- // Setup action manager
- CCBAnimationManager *pActionManager = new (std::nothrow) CCBAnimationManager();
- setAnimationManager(pActionManager);
- pActionManager->release();
-
- // Setup resolution scale and container size
- _animationManager->setRootContainerSize(Director::getInstance()->getWinSize());
-
- return true;
- }
- CCBAnimationManager* CCBReader::getAnimationManager()
- {
- return _animationManager;
- }
- void CCBReader::setAnimationManager(CCBAnimationManager *pAnimationManager)
- {
- CC_SAFE_RELEASE(_animationManager);
- _animationManager = pAnimationManager;
- CC_SAFE_RETAIN(_animationManager);
- }
- CCBReader::CCBAnimationManagerMapPtr CCBReader::getAnimationManagers()
- {
- return _animationManagers;
- }
- void CCBReader::setAnimationManagers(CCBAnimationManagerMapPtr x)
- {
- _animationManagers = x;
- }
- CCBMemberVariableAssigner * CCBReader::getCCBMemberVariableAssigner() {
- return this->_CCBMemberVariableAssigner;
- }
- CCBSelectorResolver * CCBReader::getCCBSelectorResolver() {
- return this->_CCBSelectorResolver;
- }
- std::set<std::string>* CCBReader::getAnimatedProperties()
- {
- return _animatedProps;
- }
- std::set<std::string>& CCBReader::getLoadedSpriteSheet()
- {
- return _loadedSpriteSheets;
- }
- Ref* CCBReader::getOwner()
- {
- return _owner;
- }
- Node* CCBReader::readNodeGraphFromFile(const char *pCCBFileName)
- {
- return this->readNodeGraphFromFile(pCCBFileName, nullptr);
- }
- Node* CCBReader::readNodeGraphFromFile(const char* pCCBFileName, Ref* pOwner)
- {
- return this->readNodeGraphFromFile(pCCBFileName, pOwner, Director::getInstance()->getWinSize());
- }
- Node* CCBReader::readNodeGraphFromFile(const char *pCCBFileName, Ref *pOwner, const Size &parentSize)
- {
- if (nullptr == pCCBFileName || strlen(pCCBFileName) == 0)
- {
- return nullptr;
- }
- std::string strCCBFileName(pCCBFileName);
- std::string strSuffix(".ccbi");
- // Add ccbi suffix
- if (!CCBReader::endsWith(strCCBFileName.c_str(), strSuffix.c_str()))
- {
- strCCBFileName += strSuffix;
- }
- this->getAnimationManager()->setCCBFileName(strCCBFileName);
- std::string strPath = FileUtils::getInstance()->fullPathForFilename(strCCBFileName);
- // auto dataPtr = std::make_shared<Data>(FileUtils::getInstance()->getDataFromFile(strPath));
- auto dataPtr = this->getBytesFromCCBFileByName(strPath);
- Node *ret = this->readNodeGraphFromData(dataPtr, pOwner, parentSize);
-
- return ret;
- }
- Node* CCBReader::readNodeGraphFromData(std::shared_ptr<cocos2d::Data> data, Ref *pOwner, const Size &parentSize)
- {
- _data = data;
- _bytes =_data->getBytes();
- _currentByte = 0;
- _currentBit = 0;
- _owner = pOwner;
- CC_SAFE_RETAIN(_owner);
- _animationManager->setRootContainerSize(parentSize);
- _animationManager->_owner = _owner;
-
- Node *pNodeGraph = readFileWithCleanUp(true, std::make_shared<CCBAnimationManagerMap>());
-
- int id = _animationManager->getAutoPlaySequenceId();
- // log("error_getAutoPlaySequenceId:%d", id);
- // log("error_getReferenceCount:%d", _animationManager->getReferenceCount());
- if(_animationManager->getReferenceCount()<=1){
- return nullptr;
- }
-
- if (pNodeGraph && id != -1)
- {
- // Auto play animations
- _animationManager->runAnimationsForSequenceIdTweenDuration(id, 0);
- }
-
- // Assign actionManagers to userObject
- for (auto iter = _animationManagers->begin(); iter != _animationManagers->end(); ++iter)
- {
- Node* pNode = iter->first;
- if (CocosConfig::getOpacityCCBEnable()) {
- pNode->setCascadeOpacityEnabled(true);//打开透明度
- }
-
- if(CocosConfig::getColorCCBEnable()){
- pNode->setCascadeColorEnabled(true);//打开颜色穿透
- }
-
- CCBAnimationManager* manager = iter->second;
-
- pNode->setUserObject(manager);
- if (_jsControlled)
- {
- _nodesWithAnimationManagers.pushBack(pNode);
- _animationManagersForNodes.pushBack(manager);
- }
- }
-
- return pNodeGraph;
- }
- Scene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName)
- {
- return createSceneWithNodeGraphFromFile(pCCBFileName, nullptr);
- }
- Scene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName, Ref *pOwner)
- {
- return createSceneWithNodeGraphFromFile(pCCBFileName, pOwner, Director::getInstance()->getWinSize());
- }
- Scene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName, Ref *pOwner, const Size &parentSize)
- {
- Node *pNode = readNodeGraphFromFile(pCCBFileName, pOwner, parentSize);
- Scene *pScene = Scene::create();
- pScene->addChild(pNode);
-
- return pScene;
- }
- void CCBReader::cleanUpNodeGraph(Node *node)
- {
- node->setUserObject(nullptr);
-
- auto& children = node->getChildren();
- for(const auto &obj : children) {
- cleanUpNodeGraph(obj);
- }
- }
- Node* CCBReader::readFileWithCleanUp(bool bCleanUp, CCBAnimationManagerMapPtr am)
- {
- if (! readHeader())
- {
- return nullptr;
- }
-
- if (! readStringCache())
- {
- return nullptr;
- }
-
- if (! readSequences())
- {
- return nullptr;
- }
-
- setAnimationManagers(am);
- Node *pNode = readNodeGraph(nullptr);
- _animationManagers->insert(pNode, _animationManager);
- if (bCleanUp)
- {
- cleanUpNodeGraph(pNode);
- }
-
- return pNode;
- }
- bool CCBReader::readStringCache() {
- int numStrings = this->readInt(false);
- for(int i = 0; i < numStrings; i++) {
- this->_stringCache.push_back(this->readUTF8());
- }
- return true;
- }
- bool CCBReader::readHeader()
- {
- /* If no bytes loaded, don't crash about it. */
- if(this->_bytes == nullptr) {
- return false;
- }
- /* Read magic bytes */
- int magicBytes = *((int*)(this->_bytes + this->_currentByte));
- this->_currentByte += 4;
- if(CC_SWAP_INT32_BIG_TO_HOST(magicBytes) != (*reinterpret_cast<const int*>("ccbi"))) {
- return false;
- }
- /* Read version. */
- int version = this->readInt(false);
- if(version != CCB_VERSION) {
- log("WARNING! Incompatible ccbi file version (file: %d reader: %d)", version, CCB_VERSION);
- return false;
- }
- // Read JS check
- _jsControlled = this->readBool();
- _animationManager->_jsControlled = _jsControlled;
- return true;
- }
- unsigned char CCBReader::readByte()
- {
- unsigned char byte = this->_bytes[this->_currentByte];
- this->_currentByte++;
- return byte;
- }
- bool CCBReader::readBool()
- {
- return 0 == this->readByte() ? false : true;
- }
- std::string CCBReader::readUTF8()
- {
- std::string ret;
- int b0 = this->readByte();
- int b1 = this->readByte();
- int numBytes = b0 << 8 | b1;
- char* pStr = (char*)malloc(numBytes+1);
- memcpy(pStr, _bytes+_currentByte, numBytes);
- pStr[numBytes] = '\0';
- ret = pStr;
- free(pStr);
- _currentByte += numBytes;
- return ret;
- }
- bool CCBReader::getBit() {
- bool bit;
- unsigned char byte = *(this->_bytes + this->_currentByte);
- if(byte & (1 << this->_currentBit)) {
- bit = true;
- } else {
- bit = false;
- }
- this->_currentBit++;
- if(this->_currentBit >= 8) {
- this->_currentBit = 0;
- this->_currentByte++;
- }
- return bit;
- }
- void CCBReader::alignBits() {
- if(this->_currentBit) {
- this->_currentBit = 0;
- this->_currentByte++;
- }
- }
- int CCBReader::readInt(bool pSigned) {
- // Read encoded int
- int numBits = 0;
- while(!this->getBit()) {
- numBits++;
- }
-
- long long current = 0;
- for(int a = numBits - 1; a >= 0; a--) {
- if(this->getBit()) {
- current |= 1LL << a;
- }
- }
- current |= 1LL << numBits;
-
- int num;
- if(pSigned) {
- int s = current % 2;
- if(s) {
- num = static_cast<int>(current / 2);
- } else {
- num = static_cast<int>(-current / 2);
- }
- } else {
- num = static_cast<int>(current - 1);
- }
-
- this->alignBits();
-
- return num;
- }
- float CCBReader::readFloat()
- {
- FloatType type = static_cast<FloatType>(this->readByte());
-
- switch (type)
- {
- case FloatType::_0:
- return 0;
- case FloatType::_1:
- return 1;
- case FloatType::MINUS1:
- return -1;
- case FloatType::_05:
- return 0.5f;
- case FloatType::INTEGER:
- return (float)this->readInt(true);
- default:
- {
- /* using a memcpy since the compiler isn't
- * doing the float ptr math correctly on device.
- * TODO: still applies in C++ ? */
- unsigned char* pF = (this->_bytes + this->_currentByte);
- float f = 0;
-
- // N.B - in order to avoid an unaligned memory access crash on 'memcpy()' the the (void*) casts of the source and
- // destination pointers are EXTREMELY important for the ARM compiler.
- //
- // Without a (void*) cast, the ARM compiler makes the assumption that the float* pointer is naturally aligned
- // according to it's type size (aligned along 4 byte boundaries) and thus tries to call a more optimized
- // version of memcpy() which makes this alignment assumption also. When reading back from a file of course our pointers
- // may not be aligned, hence we need to avoid the compiler making this assumption. The (void*) cast serves this purpose,
- // and causes the ARM compiler to choose the slower, more generalized (unaligned) version of memcpy()
- //
- // For more about this compiler behavior, see:
- // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3934.html
- memcpy((void*) &f, (const void*) pF, sizeof(float));
-
- this->_currentByte += sizeof(float);
- return f;
- }
- }
- }
- std::string CCBReader::readCachedString()
- {
- int n = this->readInt(false);
- return this->_stringCache[n];
- }
- Node * CCBReader::readNodeGraph(Node * pParent)
- {
- /* Read class name. */
- std::string className = this->readCachedString();
- std::string _jsControlledName;
-
- if(_jsControlled) {
- _jsControlledName = this->readCachedString();
- }
-
- // Read assignment type and name
- TargetType memberVarAssignmentType = static_cast<TargetType>(this->readInt(false));
- std::string memberVarAssignmentName;
- if(memberVarAssignmentType != TargetType::NONE)
- {
- memberVarAssignmentName = this->readCachedString();
- }
-
- NodeLoader *ccNodeLoader = this->_nodeLoaderLibrary->getNodeLoader(className.c_str());
-
- if (! ccNodeLoader)
- {
- log("no corresponding node loader for %s", className.c_str());
- return nullptr;
- }
- Node *node = ccNodeLoader->loadNode(pParent, this);
- // Set root node
- if (! _animationManager->getRootNode())
- {
- _animationManager->setRootNode(node);
- }
-
- // Assign controller
- if(_jsControlled && node == _animationManager->getRootNode())
- {
- _animationManager->setDocumentControllerName(_jsControlledName);
- }
- // Read animated properties
- std::unordered_map<int, Map<std::string, CCBSequenceProperty*>> seqs;
- _animatedProps = new std::set<std::string>();
-
- int numSequence = readInt(false);// 时间线数量
- for (int i = 0; i < numSequence; ++i)
- {
- int seqId = readInt(false);
- Map<std::string, CCBSequenceProperty*> seqNodeProps;
-
- int numProps = readInt(false);// 变化属性的数量
-
- for (int j = 0; j < numProps; ++j)
- {
- CCBSequenceProperty *seqProp = new (std::nothrow) CCBSequenceProperty();
- seqProp->autorelease();
-
- seqProp->setName(readCachedString().c_str());
- seqProp->setType(readInt(false));
- _animatedProps->insert(seqProp->getName());
-
- int numKeyframes = readInt(false); // 关键帧的数量
-
- for (int k = 0; k < numKeyframes; ++k)
- {
- CCBKeyframe *keyframe = readKeyframe(static_cast<PropertyType>(seqProp->getType()));
-
- seqProp->getKeyframes().pushBack(keyframe);
- }
-
- seqNodeProps.insert(seqProp->getName(), seqProp);
- }
-
- seqs[seqId] = seqNodeProps;
- }
-
- if (!seqs.empty())
- {
- _animationManager->addNode(node, seqs);
- }
-
- // Read properties
- ccNodeLoader->parseProperties(node, pParent, this);
-
- bool isCCBFileNode = (nullptr == dynamic_cast<CCBFile*>(node)) ? false : true;
- // Handle sub ccb files (remove middle node)
- if (isCCBFileNode)
- {
- CCBFile *ccbFileNode = (CCBFile*)node;
-
- Node *embeddedNode = ccbFileNode->getCCBFileNode();
- embeddedNode->setPosition(ccbFileNode->getPosition());
- embeddedNode->setRotation(ccbFileNode->getRotation());
- embeddedNode->setScaleX(ccbFileNode->getScaleX());
- embeddedNode->setScaleY(ccbFileNode->getScaleY());
- embeddedNode->setTag(ccbFileNode->getTag());
- embeddedNode->setVisible(ccbFileNode->isVisible());
- //embeddedNode->setIgnoreAnchorPointForPosition(ccbFileNode->isIgnoreAnchorPointForPosition());
-
- _animationManager->moveAnimationsFromNode(ccbFileNode, embeddedNode);
- ccbFileNode->setCCBFileNode(nullptr);
-
- node = embeddedNode;
- }
- //如果父类打开透明度默认子类也打开透明度
- if(pParent){
- auto parentNode = pParent->getParent();
- if(parentNode&&parentNode->isCascadeOpacityEnabled()){
- parentNode->setCascadeOpacityEnabled(true);
- }
- if(pParent->isCascadeOpacityEnabled()){
- node->setCascadeOpacityEnabled(true);
- }
-
- }
-
- if (memberVarAssignmentType != TargetType::NONE)
- {
- if(!_jsControlled)
- {
- Ref* target = nullptr;
- if(memberVarAssignmentType == TargetType::DOCUMENT_ROOT)
- {
- target = _animationManager->getRootNode();
- }
- else if(memberVarAssignmentType == TargetType::OWNER)
- {
- target = this->_owner;
- }
-
- if(target != nullptr)
- {
- CCBMemberVariableAssigner * targetAsCCBMemberVariableAssigner = dynamic_cast<CCBMemberVariableAssigner *>(target);
-
- bool assigned = false;
- if (memberVarAssignmentType != TargetType::NONE)
- {
- if(targetAsCCBMemberVariableAssigner != nullptr)
- {
- assigned = targetAsCCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node);
- }
-
- if(!assigned && this->_CCBMemberVariableAssigner != nullptr)
- {
- assigned = this->_CCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node);
- }
- }
- }
- }
- else
- {
- if(memberVarAssignmentType == TargetType::DOCUMENT_ROOT)
- {
- _animationManager->addDocumentOutletName(memberVarAssignmentName);
- _animationManager->addDocumentOutletNode(node);
- }
- else
- {
- _ownerOutletNames.push_back(memberVarAssignmentName);
- _ownerOutletNodes.pushBack(node);
- }
- }
- }
-
- // Assign custom properties.
- if (!ccNodeLoader->getCustomProperties().empty())
- {
- bool customAssigned = false;
-
- if(!_jsControlled)
- {
- Ref* target = node;
- if(target != nullptr)
- {
- CCBMemberVariableAssigner * targetAsCCBMemberVariableAssigner = dynamic_cast<CCBMemberVariableAssigner *>(target);
- if(targetAsCCBMemberVariableAssigner != nullptr)
- {
- auto& customPropeties = ccNodeLoader->getCustomProperties();
-
- for (auto iter = customPropeties.begin(); iter != customPropeties.end(); ++iter)
- {
- customAssigned = targetAsCCBMemberVariableAssigner->onAssignCCBCustomProperty(target, iter->first.c_str(), iter->second);
- //ccb添加字段可打开透明度
- if(strcmp(iter->first.c_str(), "CascadeOpacityEnabled") == 0){
- Value value = iter->second;
- int isCascadeOpacityEnabled = value.asInt();
- if(isCascadeOpacityEnabled == 1){
- node->setCascadeOpacityEnabled(true);
- }
- }
- if(!customAssigned && this->_CCBMemberVariableAssigner != nullptr)
- {
- customAssigned = this->_CCBMemberVariableAssigner->onAssignCCBCustomProperty(target, iter->first.c_str(), iter->second);
- }
- }
- }
- }
- }
- }
-
- delete _animatedProps;
- _animatedProps = nullptr;
- /* Read and add children. */
- int numChildren = this->readInt(false);
- for(int i = 0; i < numChildren; i++)
- {
- Node * child = this->readNodeGraph(node);
- //如果配置为激活ccb透明度,则打开所有ccb透明度
- if (CocosConfig::getOpacityCCBEnable()) {
- child->setCascadeOpacityEnabled(true); //打开透明度
- }
- if (CocosConfig::getColorCCBEnable()) {
- child->setCascadeColorEnabled(true); //打开颜色
- }
- //如果父类打开透明度默认子类也打开透明度
- if(node->isCascadeOpacityEnabled()){
- child->setCascadeOpacityEnabled(true);
- }
- //如果父类打开颜色默认子类也打开透明度
- if(node->isCascadeColorEnabled()){
- child->setCascadeColorEnabled(true);
- }
- node->addChild(child);
- }
- // 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)
- {
- // Call onNodeLoaded
- NodeLoaderListener * nodeAsNodeLoaderListener = dynamic_cast<NodeLoaderListener *>(node);
- if(nodeAsNodeLoaderListener != nullptr)
- {
- nodeAsNodeLoaderListener->onNodeLoaded(node, ccNodeLoader);
- }
- else if(this->_nodeLoaderListener != nullptr)
- {
- this->_nodeLoaderListener->onNodeLoaded(node, ccNodeLoader);
- }
- }
- return node;
- }
- CCBKeyframe* CCBReader::readKeyframe(PropertyType type)
- {
- CCBKeyframe *keyframe = new (std::nothrow) CCBKeyframe();
- keyframe->autorelease();
-
- keyframe->setTime(readFloat());
-
- CCBKeyframe::EasingType easingType = static_cast<CCBKeyframe::EasingType>(readInt(false));
- float easingOpt = 0;
- Value value;
-
- if (easingType == CCBKeyframe::EasingType::CUBIC_IN
- || easingType == CCBKeyframe::EasingType::CUBIC_OUT
- || easingType == CCBKeyframe::EasingType::CUBIC_INOUT
- || easingType == CCBKeyframe::EasingType::ELASTIC_IN
- || easingType == CCBKeyframe::EasingType::ELASTIC_OUT
- || easingType == CCBKeyframe::EasingType::ELASTIC_INOUT)
- {
- easingOpt = readFloat();
- }
- keyframe->setEasingType(easingType);
- keyframe->setEasingOpt(easingOpt);
-
- if (type == PropertyType::CHECK)
- {
- value = readBool();
- }
- else if (type == PropertyType::BYTE)
- {
- value = readByte();
- }
- else if (type == PropertyType::COLOR3)
- {
- unsigned char r = readByte();
- unsigned char g = readByte();
- unsigned char b = readByte();
-
- ValueMap colorMap;
- colorMap["r"] = r;
- colorMap["g"] = g;
- colorMap["b"] = b;
-
- value = colorMap;
- }
- else if (type == PropertyType::COLOR4F_VAR)
- {
- float red = readFloat();
- float green = readFloat();
- float blue = readFloat();
- float alpha = readFloat();
- float redVar = readFloat();
- float greenVar = readFloat();
- float blueVar = readFloat();
- float alphaVar = readFloat();
-
- // readUTF8();
- // int test = readInt(false);
- // readByte();
- // CCLOG("test %d",test);
- // unsigned char r = readFloat();
- // unsigned char g = readFloat();
- // unsigned char b = readFloat();
- // unsigned char a = readFloat();
- //
- // unsigned char rvar = readFloat();
- // unsigned char gvar = readFloat();
- // unsigned char bvar = readFloat();
- // unsigned char avar = readFloat();
- CCLOG("%f, %f, %f, %f, || %f, %f, %f, %f",red,green,blue,alpha,redVar,greenVar,blueVar,alphaVar);
- // ValueMap colorMap;
- // colorMap["r"] = r;
- // colorMap["g"] = g;
- // colorMap["b"] = b;
-
- // value = colorMap;
- }
- else if (type == PropertyType::DEGREES)
- {
- value = readFloat();
- }
- else if (type == PropertyType::SCALE_LOCK || type == PropertyType::POSITION
- || type == PropertyType::FLOAT_XY || type == PropertyType::POINT
- || type == PropertyType::FLOAT_VAR)
- {
- float a = readFloat();
- float b = readFloat();
-
- ValueVector ab;
- ab.push_back(Value(a));
- ab.push_back(Value(b));
-
- value = ab;
- }
- else if (type == PropertyType::SPRITEFRAME)
- {
- std::string spriteSheet = readCachedString();
- std::string spriteFile = readCachedString();
-
- SpriteFrame* spriteFrame;
- if (CocosConfig::getIgnoreCCBPath()) {
- SpriteFrameCache * frameCache = SpriteFrameCache::getInstance();
- spriteFrame = frameCache->getSpriteFrameByName(spriteFile.c_str());
-
- if (!spriteFrame) {
- spriteFile = _CCBRootPath + spriteFile;
- Texture2D * texture = Director::getInstance()->getTextureCache()->addImage(spriteFile.c_str());
- if(texture != NULL) {
- Rect bounds = Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height);
- spriteFrame = SpriteFrame::createWithTexture(texture, bounds);
- }
- }
- }else{
- if (spriteSheet.empty())
- {
- SpriteFrameCache * frameCache = SpriteFrameCache::getInstance();
- spriteFrame = frameCache->getSpriteFrameByName(spriteFile.c_str());
-
- if (!spriteFrame) {
- spriteFile = _CCBRootPath + spriteFile;
- Texture2D * texture = Director::getInstance()->getTextureCache()->addImage(spriteFile.c_str());
- if(texture != NULL) {
- Rect bounds = Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height);
- spriteFrame = SpriteFrame::createWithTexture(texture, bounds);
- }
- }
-
- }
- else
- {
- spriteSheet = _CCBRootPath + spriteSheet;
- SpriteFrameCache* frameCache = SpriteFrameCache::getInstance();
-
- // Load the sprite sheet only if it is not loaded
- if (_loadedSpriteSheets.find(spriteSheet) == _loadedSpriteSheets.end())
- {
- frameCache->addSpriteFramesWithFile(spriteSheet);
- _loadedSpriteSheets.insert(spriteSheet);
- }
-
- spriteFrame = frameCache->getSpriteFrameByName(spriteFile);
- }
- }
- //add by djd 防止崩溃,用默认空白图
- if (!spriteFrame) {
- spriteFile = CocosConfig::getDefaultEmptyPic();
- Texture2D * texture = Director::getInstance()->getTextureCache()->addImage(spriteFile.c_str());
- if(texture != NULL) {
- Rect bounds = Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height);
- spriteFrame = SpriteFrame::createWithTexture(texture, bounds);
- }
- }
- keyframe->setObject(spriteFrame);
- }
-
- if (!value.isNull())
- keyframe->setValue(value);
-
- return keyframe;
- }
- bool CCBReader::readCallbackKeyframesForSeq(CCBSequence* seq)
- {
- int numKeyframes = readInt(false);
- if(!numKeyframes) return true;
-
- CCBSequenceProperty* channel = new (std::nothrow) CCBSequenceProperty();
- channel->autorelease();
- for(int i = 0; i < numKeyframes; ++i) {
-
- float time = readFloat();
- std::string callbackName = readCachedString();
-
- int callbackType = readInt(false);
-
- ValueVector valueVector;
- valueVector.push_back(Value(callbackName));
- valueVector.push_back(Value(callbackType));
-
- CCBKeyframe* keyframe = new (std::nothrow) CCBKeyframe();
- keyframe->autorelease();
-
- keyframe->setTime(time);
- keyframe->setValue(Value(valueVector));
-
- if(_jsControlled) {
- std::stringstream callbackIdentifier;
- callbackIdentifier << callbackType;
- callbackIdentifier << ":" + callbackName;
- _animationManager->getKeyframeCallbacks().push_back(Value(callbackIdentifier.str()));
- }
-
- channel->getKeyframes().pushBack(keyframe);
- }
-
- seq->setCallbackChannel(channel);
-
- return true;
- }
- bool CCBReader::readSoundKeyframesForSeq(CCBSequence* seq) {
- int numKeyframes = readInt(false);
- if(!numKeyframes) return true;
-
- CCBSequenceProperty* channel = new (std::nothrow) CCBSequenceProperty();
- channel->autorelease();
- for(int i = 0; i < numKeyframes; ++i) {
-
- float time = readFloat();
- std::string soundFile = readCachedString();
- float pitch = readFloat();
- float pan = readFloat();
- float gain = readFloat();
-
- ValueVector vec;
- vec.push_back(Value(soundFile));
- vec.push_back(Value(pitch));
- vec.push_back(Value(pan));
- vec.push_back(Value(gain));
-
- CCBKeyframe* keyframe = new (std::nothrow) CCBKeyframe();
- keyframe->setTime(time);
- keyframe->setValue(Value(vec));
- channel->getKeyframes().pushBack(keyframe);
- keyframe->release();
- }
-
- seq->setSoundChannel(channel);
-
- return true;
- }
- Node * CCBReader::readNodeGraph() {
- return this->readNodeGraph(nullptr);
- }
- bool CCBReader::readSequences()
- {
- auto& sequences = _animationManager->getSequences();
-
- int numSeqs = readInt(false);
-
- for (int i = 0; i < numSeqs; i++)
- {
- CCBSequence *seq = new (std::nothrow) CCBSequence();
- seq->autorelease();
-
- seq->setDuration(readFloat());
- seq->setName(readCachedString().c_str());
- seq->setSequenceId(readInt(false));
- seq->setChainedSequenceId(readInt(true));
-
- if(!readCallbackKeyframesForSeq(seq)) return false;
- if(!readSoundKeyframesForSeq(seq)) return false;
-
- sequences.pushBack(seq);
- }
-
- _animationManager->setAutoPlaySequenceId(readInt(true));
- return true;
- }
- std::string CCBReader::lastPathComponent(const char* pPath) {
- std::string path(pPath);
- size_t slashPos = path.find_last_of("/");
- if(slashPos != std::string::npos) {
- return path.substr(slashPos + 1, path.length() - slashPos);
- }
- return path;
- }
- std::string CCBReader::deletePathExtension(const char* pPath) {
- std::string path(pPath);
- size_t dotPos = path.find_last_of(".");
- if(dotPos != std::string::npos) {
- return path.substr(0, dotPos);
- }
- return path;
- }
- std::string CCBReader::toLowerCase(const char* pString) {
- std::string copy(pString);
- std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
- return copy;
- }
- bool CCBReader::endsWith(const char* pString, const char* pEnding) {
- std::string string(pString);
- std::string ending(pEnding);
- if(string.length() >= ending.length()) {
- return (string.compare(string.length() - ending.length(), ending.length(), ending) == 0);
- } else {
- return false;
- }
- }
- bool CCBReader::isJSControlled()
- {
- return _jsControlled;
- }
- void CCBReader::addOwnerCallbackName(const std::string& name)
- {
- _ownerCallbackNames.push_back(name);
- }
- void CCBReader::addOwnerCallbackNode(Node *node)
- {
- _ownerCallbackNodes.pushBack(node);
- }
- void CCBReader::addOwnerCallbackControlEvents(Control::EventType type)
- {
- _ownerOwnerCallbackControlEvents.push_back(Value((int)type));
- }
- void CCBReader::addDocumentCallbackName(const std::string& name)
- {
- _animationManager->addDocumentCallbackName(name);
- }
- void CCBReader::addDocumentCallbackNode(Node *node)
- {
- _animationManager->addDocumentCallbackNode(node);
- }
- void CCBReader::addDocumentCallbackControlEvents(Control::EventType eventType)
- {
- _animationManager->addDocumentCallbackControlEvents(eventType);
- }
- ValueVector CCBReader::getOwnerCallbackNames()
- {
- ValueVector ret;
- ret.reserve(_ownerCallbackNames.size());
-
- std::vector<std::string>::iterator it = _ownerCallbackNames.begin();
- for (; it != _ownerCallbackNames.end(); ++it)
- {
- ret.push_back(Value(*it));
- }
-
- return ret;
- }
- Vector<Node*>& CCBReader::getOwnerCallbackNodes()
- {
- return _ownerCallbackNodes;
- }
- ValueVector& CCBReader::getOwnerCallbackControlEvents()
- {
- return _ownerOwnerCallbackControlEvents;
- }
- ValueVector CCBReader::getOwnerOutletNames()
- {
- ValueVector ret;
- ret.reserve(_ownerOutletNames.size());
- std::vector<std::string>::iterator it = _ownerOutletNames.begin();
- for (; it != _ownerOutletNames.end(); ++it)
- {
- ret.push_back(Value(*it));
- }
- return ret;
- }
- Vector<Node*>& CCBReader::getOwnerOutletNodes()
- {
- return _ownerOutletNodes;
- }
- Vector<Node*>& CCBReader::getNodesWithAnimationManagers()
- {
- return _nodesWithAnimationManagers;
- }
- Vector<CCBAnimationManager*>& CCBReader::getAnimationManagersForNodes()
- {
- return _animationManagersForNodes;
- }
- void CCBReader::addOwnerOutletName(std::string name)
- {
- _ownerOutletNames.push_back(name);
- }
- void CCBReader::addOwnerOutletNode(Node *node)
- {
- if (nullptr == node)
- return;
-
- _ownerOutletNodes.pushBack(node);
- }
- /************************************************************************
- Static functions
- ************************************************************************/
- static float __ccbResolutionScale = 1.0f;
- float CCBReader::getResolutionScale()
- {
- return __ccbResolutionScale;
- }
- void CCBReader::setResolutionScale(float scale)
- {
- __ccbResolutionScale = scale;
- }
- };
|