axmol/cocos/editor-support/cocosbuilder/CCBReader.cpp

1102 lines
31 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include <ctype.h>
#include <algorithm>
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCScene.h"
#include "2d/CCSpriteFrameCache.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#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>
using namespace cocos2d;
using namespace cocos2d::extension;
namespace cocosbuilder {
/*************************************************************************
Implementation of CCBFile
*************************************************************************/
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)
2012-12-03 18:27:50 +08:00
{
CCASSERT(ccbRootPath != nullptr, "ccbRootPath can't be nullptr!");
_CCBRootPath = ccbRootPath;
2012-12-03 18:27:50 +08:00
}
const std::string& CCBReader::getCCBRootPath() const
{
return _CCBRootPath;
2012-12-03 18:27:50 +08:00
}
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;
2012-06-05 07:16:42 +08:00
}
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)
2012-11-30 14:16:38 +08:00
{
return nullptr;
2012-11-30 14:16:38 +08:00
}
std::string strCCBFileName(pCCBFileName);
std::string strSuffix(".ccbi");
// Add ccbi suffix
if (!CCBReader::endsWith(strCCBFileName.c_str(), strSuffix.c_str()))
{
strCCBFileName += strSuffix;
}
std::string strPath = FileUtils::getInstance()->fullPathForFilename(strCCBFileName);
auto dataPtr = std::make_shared<Data>(FileUtils::getInstance()->getDataFromFile(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>());
if (pNodeGraph && _animationManager->getAutoPlaySequenceId() != -1)
{
// Auto play animations
_animationManager->runAnimationsForSequenceIdTweenDuration(_animationManager->getAutoPlaySequenceId(), 0);
}
// Assign actionManagers to userObject
for (auto iter = _animationManagers->begin(); iter != _animationManagers->end(); ++iter)
{
Node* pNode = iter->first;
CCBAnimationManager* manager = iter->second;
pNode->setUserObject(manager);
2012-12-04 18:44:32 +08:00
if (_jsControlled)
2012-12-04 18:44:32 +08:00
{
_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;
}
2012-12-04 18:44:32 +08:00
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);
2012-12-04 18:44:32 +08:00
pStr[numBytes] = '\0';
ret = pStr;
free(pStr);
_currentByte += numBytes;
2012-12-04 18:44:32 +08:00
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) {
2013-11-11 12:49:38 +08:00
num = static_cast<int>(current / 2);
} else {
2013-11-11 12:49:38 +08:00
num = static_cast<int>(-current / 2);
}
} else {
2013-11-11 12:49:38 +08:00
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(true);
2016-04-22 20:36:02 +08:00
//embeddedNode->setIgnoreAnchorPointForPosition(ccbFileNode->isIgnoreAnchorPointForPosition());
_animationManager->moveAnimationsFromNode(ccbFileNode, embeddedNode);
ccbFileNode->setCCBFileNode(nullptr);
node = embeddedNode;
}
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);
if(!customAssigned && this->_CCBMemberVariableAssigner != nullptr)
{
customAssigned = this->_CCBMemberVariableAssigner->onAssignCCBCustomProperty(target, iter->first.c_str(), iter->second);
}
}
}
}
}
}
2012-09-18 17:04:10 +08:00
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);
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::DEGREES)
{
value = readFloat();
}
else if (type == PropertyType::SCALE_LOCK || type == PropertyType::POSITION
|| type == PropertyType::FLOAT_XY)
{
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 (spriteSheet.empty())
{
spriteFile = _CCBRootPath + spriteFile;
2013-07-12 18:04:32 +08:00
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(spriteFile);
Rect bounds = Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height);
2013-07-12 18:04:32 +08:00
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);
}
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();
2013-03-21 09:00:55 +08:00
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);
2013-03-21 09:00:55 +08:00
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;
}
};