2012-05-31 07:51:33 +08:00
|
|
|
#include "CCBReader.h"
|
|
|
|
|
2012-06-14 06:25:59 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2012-05-31 02:28:50 +08:00
|
|
|
#include "CCNodeLoader.h"
|
2012-06-06 08:15:28 +08:00
|
|
|
#include "CCNodeLoaderLibrary.h"
|
2012-06-13 02:59:49 +08:00
|
|
|
#include "CCNodeLoaderListener.h"
|
|
|
|
#include "CCBMemberVariableAssigner.h"
|
|
|
|
#include "CCBSelectorResolver.h"
|
2012-09-17 14:27:17 +08:00
|
|
|
#include "CCData.h"
|
|
|
|
#include "CCBAnimationManager.h"
|
|
|
|
#include "CCBSequenceProperty.h"
|
|
|
|
#include "CCBKeyframe.h"
|
|
|
|
#include "CCBValue.h"
|
2012-05-31 02:28:50 +08:00
|
|
|
|
2012-08-18 05:45:21 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2012-06-05 07:16:42 +08:00
|
|
|
#ifdef __CC_PLATFORM_IOS
|
2012-06-13 02:59:49 +08:00
|
|
|
#include <UIKit/UIDevice.h>
|
2012-06-05 07:16:42 +08:00
|
|
|
#endif
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
using namespace std;
|
2012-06-14 05:19:13 +08:00
|
|
|
USING_NS_CC;
|
2012-07-19 17:22:36 +08:00
|
|
|
NS_CC_EXT_BEGIN;
|
2012-05-31 02:28:50 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
/*************************************************************************
|
|
|
|
Implementation of CCBFile
|
|
|
|
*************************************************************************/
|
2012-06-06 08:15:28 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCBFile::CCBFile():mCCBFileNode(NULL) {}
|
|
|
|
|
|
|
|
CCBFile* CCBFile::create()
|
|
|
|
{
|
|
|
|
CCBFile *ret = new CCBFile();
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCNode* CCBFile::getCCBFileNode()
|
|
|
|
{
|
|
|
|
return mCCBFileNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCBFile::setCCBFileNode(CCNode *pNode)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(mCCBFileNode);
|
|
|
|
mCCBFileNode = pNode;
|
|
|
|
CC_SAFE_RETAIN(mCCBFileNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
Implementation of CCBReader
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
CCBReader::CCBReader(CCNodeLoaderLibrary * pCCNodeLoaderLibrary, CCBMemberVariableAssigner * pCCBMemberVariableAssigner, CCBSelectorResolver * pCCBSelectorResolver, CCNodeLoaderListener * pCCNodeLoaderListener)
|
|
|
|
: mData(NULL)
|
|
|
|
, mBytes(NULL)
|
|
|
|
, mCurrentByte(-1)
|
|
|
|
, mCurrentBit(-1)
|
|
|
|
, mOwner(NULL)
|
|
|
|
, mActionManager(NULL)
|
2012-09-18 17:04:10 +08:00
|
|
|
, mAnimatedProps(NULL)
|
2012-09-17 14:27:17 +08:00
|
|
|
{
|
2012-06-06 08:15:28 +08:00
|
|
|
this->mCCNodeLoaderLibrary = pCCNodeLoaderLibrary;
|
2012-06-13 02:59:49 +08:00
|
|
|
this->mCCNodeLoaderLibrary->retain();
|
2012-06-05 06:52:49 +08:00
|
|
|
this->mCCBMemberVariableAssigner = pCCBMemberVariableAssigner;
|
|
|
|
this->mCCBSelectorResolver = pCCBSelectorResolver;
|
2012-06-13 02:59:49 +08:00
|
|
|
this->mCCNodeLoaderListener = pCCNodeLoaderListener;
|
2012-06-01 05:57:13 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCBReader::CCBReader(CCBReader * pCCBReader)
|
|
|
|
: mData(NULL)
|
|
|
|
, mBytes(NULL)
|
|
|
|
, mCurrentByte(-1)
|
|
|
|
, mCurrentBit(-1)
|
|
|
|
, mOwner(NULL)
|
|
|
|
, mActionManager(NULL)
|
2012-09-18 17:04:10 +08:00
|
|
|
, mAnimatedProps(NULL)
|
2012-09-17 14:27:17 +08:00
|
|
|
{
|
2012-06-01 05:57:13 +08:00
|
|
|
this->mLoadedSpriteSheets = pCCBReader->mLoadedSpriteSheets;
|
2012-06-06 08:15:28 +08:00
|
|
|
this->mCCNodeLoaderLibrary = pCCBReader->mCCNodeLoaderLibrary;
|
2012-06-14 05:19:13 +08:00
|
|
|
this->mCCNodeLoaderLibrary->retain();
|
|
|
|
|
2012-06-05 06:52:49 +08:00
|
|
|
this->mCCBMemberVariableAssigner = pCCBReader->mCCBMemberVariableAssigner;
|
|
|
|
this->mCCBSelectorResolver = pCCBReader->mCCBSelectorResolver;
|
2012-06-13 02:59:49 +08:00
|
|
|
this->mCCNodeLoaderListener = pCCBReader->mCCNodeLoaderListener;
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCBReader::CCBReader()
|
|
|
|
: mData(NULL)
|
|
|
|
, mBytes(NULL)
|
|
|
|
, mCurrentByte(-1)
|
|
|
|
, mCurrentBit(-1)
|
|
|
|
, mOwner(NULL)
|
|
|
|
, mActionManager(NULL)
|
|
|
|
, mCCNodeLoaderLibrary(NULL)
|
|
|
|
, mCCNodeLoaderListener(NULL)
|
|
|
|
, mCCBMemberVariableAssigner(NULL)
|
|
|
|
, mCCBSelectorResolver(NULL)
|
2012-09-18 17:04:10 +08:00
|
|
|
, mAnimatedProps(NULL)
|
2012-09-17 14:27:17 +08:00
|
|
|
{}
|
|
|
|
|
2012-06-14 15:01:01 +08:00
|
|
|
CCBReader::~CCBReader() {
|
2012-09-17 14:27:17 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(mOwner);
|
|
|
|
CC_SAFE_RELEASE_NULL(mData);
|
2012-06-14 15:01:01 +08:00
|
|
|
|
|
|
|
this->mCCNodeLoaderLibrary->release();
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
// Clear string cache.
|
2012-06-14 15:01:01 +08:00
|
|
|
std::vector<CCString *>::iterator stringCacheIterator;
|
|
|
|
for (stringCacheIterator = this->mStringCache.begin(); stringCacheIterator != this->mStringCache.end(); stringCacheIterator++) {
|
|
|
|
(*stringCacheIterator)->release();
|
|
|
|
}
|
|
|
|
this->mStringCache.clear();
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
setAnimationManager(NULL);
|
2012-06-14 15:01:01 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
bool CCBReader::initWithData(CCData *pData, CCObject *pOwner)
|
|
|
|
{
|
|
|
|
// Setup action manager
|
|
|
|
CCBAnimationManager *pActionManager = new CCBAnimationManager();
|
|
|
|
setAnimationManager(pActionManager);
|
|
|
|
pActionManager->release();
|
|
|
|
|
|
|
|
// Setup byte array
|
|
|
|
mData = pData;
|
|
|
|
CC_SAFE_RETAIN(mData);
|
|
|
|
mBytes = mData->getBytes();
|
|
|
|
mCurrentByte = 0;
|
|
|
|
mCurrentBit = 0;
|
|
|
|
|
|
|
|
mOwner = pOwner;
|
|
|
|
CC_SAFE_RETAIN(mOwner);
|
|
|
|
|
|
|
|
// Setup resolution scale and container size
|
|
|
|
mActionManager->setRootContainerSize(CCDirector::sharedDirector()->getWinSize());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCBAnimationManager* CCBReader::getAnimationManager()
|
|
|
|
{
|
|
|
|
return mActionManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCBReader::setAnimationManager(CCBAnimationManager *pAnimationManager)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(mActionManager);
|
|
|
|
mActionManager = pAnimationManager;
|
|
|
|
CC_SAFE_RETAIN(mActionManager);
|
2012-06-12 09:31:11 +08:00
|
|
|
}
|
|
|
|
|
2012-06-05 06:52:49 +08:00
|
|
|
CCBMemberVariableAssigner * CCBReader::getCCBMemberVariableAssigner() {
|
|
|
|
return this->mCCBMemberVariableAssigner;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCBSelectorResolver * CCBReader::getCCBSelectorResolver() {
|
|
|
|
return this->mCCBSelectorResolver;
|
|
|
|
}
|
|
|
|
|
2012-09-18 17:04:10 +08:00
|
|
|
set<string>* CCBReader::getAnimatedProperties()
|
2012-09-17 14:27:17 +08:00
|
|
|
{
|
|
|
|
return mAnimatedProps;
|
2012-06-05 07:16:42 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
set<string>& CCBReader::getLoadedSpriteSheet()
|
|
|
|
{
|
|
|
|
return mLoadedSpriteSheets;
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCObject* CCBReader::getOwner()
|
|
|
|
{
|
|
|
|
return mOwner;
|
2012-06-14 15:01:01 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCNode* CCBReader::readNodeGraphFromFile(const char *pCCBFileName)
|
|
|
|
{
|
|
|
|
return this->readNodeGraphFromFile(pCCBFileName, NULL);
|
2012-06-14 15:01:01 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCNode* CCBReader::readNodeGraphFromFile(const char* pCCBFileName, CCObject* pOwner)
|
|
|
|
{
|
|
|
|
return this->readNodeGraphFromFile(pCCBFileName, pOwner, CCDirector::sharedDirector()->getWinSize());
|
|
|
|
}
|
2012-06-12 09:31:11 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCNode * CCBReader::readNodeGraphFromFile(const char * pCCBFileName, CCObject *pOwner, const CCSize &parentSize)
|
|
|
|
{
|
|
|
|
return this->readNodeGraphFromFile(pCCBFileName, pOwner, parentSize, NULL);
|
|
|
|
}
|
2012-06-12 09:31:11 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCNode* CCBReader::readNodeGraphFromFile(const char *pCCBFileName, CCObject *pOwner, CCBAnimationManager **ppAnimationManager)
|
|
|
|
{
|
|
|
|
return this->readNodeGraphFromFile(pCCBFileName, pOwner, CCDirector::sharedDirector()->getWinSize(), ppAnimationManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCNode* CCBReader::readNodeGraphFromFile(const char *pCCBFileName, CCObject *pOwner, const CCSize &parentSize, CCBAnimationManager **ppAnimationManager)
|
|
|
|
{
|
|
|
|
const char *pPath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pCCBFileName);
|
|
|
|
unsigned long size;
|
2012-06-06 08:15:28 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
unsigned char * pBytes = CCFileUtils::sharedFileUtils()->getFileData(pPath, "rb", &size);
|
|
|
|
CCData *data = new CCData(pBytes, size);
|
2012-05-31 02:28:50 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCNode *ret = this->readNodeGraphFromData(data, pOwner, parentSize, ppAnimationManager);
|
|
|
|
|
|
|
|
data->release();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2012-06-14 15:01:01 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCNode* CCBReader::readNodeGraphFromData(CCData *pData, CCObject *pOwner, const CCSize &parentSize)
|
|
|
|
{
|
|
|
|
return this->readNodeGraphFromData(pData, pOwner, parentSize, NULL);
|
|
|
|
}
|
2012-06-06 08:15:28 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCNode* CCBReader::readNodeGraphFromData(CCData *pData, CCObject *pOwner, const CCSize &parentSize, CCBAnimationManager **ppAnimationManager)
|
|
|
|
{
|
|
|
|
initWithData(pData, pOwner);
|
|
|
|
mActionManager->setRootContainerSize(parentSize);
|
|
|
|
|
|
|
|
CCNode *pNodeGraph = readFileWithCleanUp(true);
|
|
|
|
|
|
|
|
if (pNodeGraph && mActionManager->getAutoPlaySequenceId() != -1)
|
|
|
|
{
|
|
|
|
// Auto play animations
|
|
|
|
mActionManager->runAnimations(mActionManager->getAutoPlaySequenceId(), 0);
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
2012-09-17 14:27:17 +08:00
|
|
|
|
|
|
|
// Return action manager by reference
|
|
|
|
if (ppAnimationManager)
|
|
|
|
{
|
|
|
|
*ppAnimationManager = mActionManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pNodeGraph;
|
|
|
|
}
|
2012-06-06 08:15:28 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCScene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName)
|
|
|
|
{
|
|
|
|
return createSceneWithNodeGraphFromFile(pCCBFileName, NULL);
|
|
|
|
}
|
2012-06-06 08:15:28 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCScene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName, CCObject *pOwner)
|
|
|
|
{
|
|
|
|
return createSceneWithNodeGraphFromFile(pCCBFileName, pOwner, CCDirector::sharedDirector()->getWinSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
CCScene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName, CCObject *pOwner, const CCSize &parentSize)
|
|
|
|
{
|
|
|
|
return createSceneWithNodeGraphFromFile(pCCBFileName, pOwner, parentSize, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCScene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName, CCObject *pOwner, const CCSize &parentSize, CCBAnimationManager **ppAnimationManager)
|
|
|
|
{
|
|
|
|
CCNode *pNode = readNodeGraphFromFile(pCCBFileName, pOwner, parentSize, ppAnimationManager);
|
|
|
|
CCScene *pScene = CCScene::create();
|
|
|
|
pScene->addChild(pNode);
|
|
|
|
|
|
|
|
return pScene;
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCBReader::readHeader() {
|
|
|
|
/* If no bytes loaded, don't crash about it. */
|
|
|
|
if(this->mBytes == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read magic bytes */
|
|
|
|
int magicBytes = *((int*)(this->mBytes + this->mCurrentByte));
|
|
|
|
this->mCurrentByte += 4;
|
|
|
|
|
2012-06-14 05:33:39 +08:00
|
|
|
if(CC_SWAP_INT32_LITTLE_TO_HOST(magicBytes) != 'ccbi') {
|
2012-05-31 02:28:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read version. */
|
|
|
|
int version = this->readInt(false);
|
|
|
|
if(version != kCCBVersion) {
|
|
|
|
CCLog("WARNING! Incompatible ccbi file version (file: %d reader: %d)", version, kCCBVersion);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
void CCBReader::cleanUpNodeGraph(CCNode *pNode)
|
|
|
|
{
|
|
|
|
pNode->setUserObject(NULL);
|
|
|
|
|
|
|
|
CCObject *pChild = NULL;
|
|
|
|
CCARRAY_FOREACH(pNode->getChildren(), pChild)
|
|
|
|
{
|
|
|
|
cleanUpNodeGraph((CCNode*)pChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCNode* CCBReader::readFileWithCleanUp(bool bCleanUp)
|
|
|
|
{
|
|
|
|
if (! readHeader())
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! readStringCache())
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! readSequences())
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCNode *pNode = readNodeGraph();
|
|
|
|
|
|
|
|
if (bCleanUp)
|
|
|
|
{
|
|
|
|
cleanUpNodeGraph(pNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pNode;
|
|
|
|
}
|
|
|
|
|
2012-05-31 02:28:50 +08:00
|
|
|
bool CCBReader::readStringCache() {
|
|
|
|
int numStrings = this->readInt(false);
|
|
|
|
|
|
|
|
for(int i = 0; i < numStrings; i++) {
|
2012-06-05 08:45:25 +08:00
|
|
|
this->readStringCacheEntry();
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-05 08:45:25 +08:00
|
|
|
void CCBReader::readStringCacheEntry() {
|
2012-05-31 02:28:50 +08:00
|
|
|
int b0 = this->readByte();
|
|
|
|
int b1 = this->readByte();
|
|
|
|
|
|
|
|
int numBytes = b0 << 8 | b1;
|
|
|
|
|
2012-06-20 11:48:31 +08:00
|
|
|
const unsigned char * src = (const unsigned char *) (this->mBytes + this->mCurrentByte);
|
2012-07-23 22:49:11 +08:00
|
|
|
CCString * string = CCString::createWithData(src, (unsigned long)numBytes);
|
2012-06-14 15:01:01 +08:00
|
|
|
string->retain();
|
2012-05-31 02:28:50 +08:00
|
|
|
|
|
|
|
this->mCurrentByte += numBytes;
|
|
|
|
|
2012-06-06 08:15:28 +08:00
|
|
|
this->mStringCache.push_back(string);
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char CCBReader::readByte() {
|
|
|
|
unsigned char byte = this->mBytes[this->mCurrentByte];
|
|
|
|
this->mCurrentByte++;
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCBReader::readBool() {
|
2012-06-20 11:48:31 +08:00
|
|
|
return 0 == this->readByte() ? false : true;
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
2012-06-19 03:03:06 +08:00
|
|
|
int CCBReader::readInt(bool pSigned) {
|
2012-05-31 02:28:50 +08:00
|
|
|
int numBits = 0;
|
|
|
|
while(!this->getBit()) {
|
|
|
|
numBits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long current = 0;
|
|
|
|
for(int a = numBits - 1; a >= 0; a--) {
|
|
|
|
if(this->getBit()) {
|
2012-08-25 00:22:02 +08:00
|
|
|
current |= 1LL << a;
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
}
|
2012-08-25 00:22:02 +08:00
|
|
|
current |= 1LL << numBits;
|
2012-05-31 02:28:50 +08:00
|
|
|
|
|
|
|
int num;
|
2012-06-19 03:03:06 +08:00
|
|
|
if(pSigned) {
|
2012-05-31 02:28:50 +08:00
|
|
|
int s = current % 2;
|
|
|
|
if(s) {
|
|
|
|
num = (int)(current / 2);
|
|
|
|
} else {
|
|
|
|
num = (int)(-current / 2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
num = current - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->alignBits();
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float CCBReader::readFloat() {
|
|
|
|
unsigned char type = this->readByte();
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case kCCBFloat0:
|
|
|
|
return 0;
|
|
|
|
case kCCBFloat1:
|
|
|
|
return 1;
|
|
|
|
case kCCBFloatMinus1:
|
|
|
|
return -1;
|
|
|
|
case kCCBFloat05:
|
|
|
|
return 0.5f;
|
|
|
|
case kCCBFloatInteger:
|
2012-06-20 11:48:31 +08:00
|
|
|
return (float)this->readInt(true);
|
2012-05-31 02:28:50 +08:00
|
|
|
default:
|
|
|
|
/* using a memcpy since the compiler isn't
|
|
|
|
* doing the float ptr math correctly on device.
|
|
|
|
* TODO still applies in C++ ? */
|
|
|
|
float * pF = (float*)(this->mBytes + this->mCurrentByte);
|
|
|
|
float f = 0;
|
|
|
|
memcpy(&f, pF, sizeof(float));
|
|
|
|
this->mCurrentByte += 4;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CCBReader::getBit() {
|
|
|
|
bool bit;
|
|
|
|
unsigned char byte = *(this->mBytes + this->mCurrentByte);
|
|
|
|
if(byte & (1 << this->mCurrentBit)) {
|
|
|
|
bit = true;
|
|
|
|
} else {
|
|
|
|
bit = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->mCurrentBit++;
|
|
|
|
|
|
|
|
if(this->mCurrentBit >= 8) {
|
|
|
|
this->mCurrentBit = 0;
|
|
|
|
this->mCurrentByte++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCBReader::alignBits() {
|
|
|
|
if(this->mCurrentBit) {
|
|
|
|
this->mCurrentBit = 0;
|
|
|
|
this->mCurrentByte++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:01:01 +08:00
|
|
|
CCString * CCBReader::readCachedString() {
|
2012-05-31 02:28:50 +08:00
|
|
|
int i = this->readInt(false);
|
2012-06-12 09:31:11 +08:00
|
|
|
return this->mStringCache[i];
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCNode * CCBReader::readNodeGraph(CCNode * pParent) {
|
2012-06-05 06:52:49 +08:00
|
|
|
/* Read class name. */
|
2012-06-14 15:01:01 +08:00
|
|
|
CCString * className = this->readCachedString();
|
2012-05-31 02:28:50 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
// Read assignment type and name
|
2012-05-31 02:28:50 +08:00
|
|
|
int memberVarAssignmentType = this->readInt(false);
|
2012-06-14 15:01:01 +08:00
|
|
|
CCString * memberVarAssignmentName;
|
2012-06-05 06:52:49 +08:00
|
|
|
if(memberVarAssignmentType != kCCBTargetTypeNone) {
|
2012-06-14 15:01:01 +08:00
|
|
|
memberVarAssignmentName = this->readCachedString();
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
2012-06-05 06:52:49 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCNodeLoader *ccNodeLoader = this->mCCNodeLoaderLibrary->getCCNodeLoader(className);
|
|
|
|
if (! ccNodeLoader)
|
|
|
|
{
|
|
|
|
CCLog("no corresponding node loader for %s", className->getCString());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCNode *node = ccNodeLoader->loadCCNode(pParent, this);
|
2012-06-05 06:52:49 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
// Set root node
|
|
|
|
if (! mActionManager->getRootNode())
|
|
|
|
{
|
|
|
|
mActionManager->setRootNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read animated properties
|
|
|
|
CCDictionary *seqs = CCDictionary::create();
|
2012-09-18 17:04:10 +08:00
|
|
|
mAnimatedProps = new set<string>();
|
2012-09-17 14:27:17 +08:00
|
|
|
|
|
|
|
int numSequence = readInt(false);
|
|
|
|
for (int i = 0; i < numSequence; ++i)
|
|
|
|
{
|
|
|
|
int seqId = readInt(false);
|
|
|
|
CCDictionary *seqNodeProps = CCDictionary::create();
|
|
|
|
|
|
|
|
int numProps = readInt(false);
|
|
|
|
|
|
|
|
for (int j = 0; j < numProps; ++j)
|
|
|
|
{
|
|
|
|
CCBSequenceProperty *seqProp = new CCBSequenceProperty();
|
|
|
|
seqProp->autorelease();
|
|
|
|
|
|
|
|
seqProp->setName(readCachedString()->getCString());
|
|
|
|
seqProp->setType(readInt(false));
|
2012-09-18 17:04:10 +08:00
|
|
|
mAnimatedProps->insert(seqProp->getName());
|
2012-09-17 14:27:17 +08:00
|
|
|
|
|
|
|
int numKeyframes = readInt(false);
|
|
|
|
|
|
|
|
for (int k = 0; k < numKeyframes; ++k)
|
|
|
|
{
|
|
|
|
CCBKeyframe *keyframe = readKeyframe(seqProp->getType());
|
|
|
|
|
|
|
|
seqProp->getKeyframes()->addObject(keyframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
seqNodeProps->setObject(seqProp, seqProp->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
seqs->setObject(seqNodeProps, seqId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seqs->count() > 0)
|
|
|
|
{
|
|
|
|
mActionManager->addNode(node, seqs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read properties
|
|
|
|
ccNodeLoader->parseProperties(node, pParent, this);
|
|
|
|
|
|
|
|
// Handle sub ccb files (remove middle node)
|
|
|
|
if (dynamic_cast<CCBFile*>(node))
|
|
|
|
{
|
|
|
|
CCBFile *ccbFileNode = (CCBFile*)node;
|
|
|
|
|
|
|
|
CCNode *embeddedNode = ccbFileNode->getCCBFileNode();
|
|
|
|
embeddedNode->setPosition(ccbFileNode->getPosition());
|
|
|
|
embeddedNode->setRotation(ccbFileNode->getRotation());
|
|
|
|
embeddedNode->setScale(ccbFileNode->getScale());
|
|
|
|
embeddedNode->setTag(ccbFileNode->getTag());
|
|
|
|
embeddedNode->setVisible(true);
|
|
|
|
embeddedNode->ignoreAnchorPointForPosition(ccbFileNode->isIgnoreAnchorPointForPosition());
|
|
|
|
|
|
|
|
ccbFileNode->setCCBFileNode(NULL);
|
|
|
|
|
|
|
|
node = embeddedNode;
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
#ifdef CCB_ENABLE_JAVASCRIPT
|
|
|
|
/*
|
|
|
|
if (memberVarAssignmentType && memberVarAssignmentName && ![memberVarAssignmentName isEqualToString:@""])
|
|
|
|
{
|
|
|
|
[[JSCocoa sharedController] setObject:node withName:memberVarAssignmentName];
|
|
|
|
}*/
|
|
|
|
#else
|
2012-06-05 06:52:49 +08:00
|
|
|
if(memberVarAssignmentType != kCCBTargetTypeNone) {
|
|
|
|
CCObject * target = NULL;
|
2012-09-17 14:27:17 +08:00
|
|
|
if(memberVarAssignmentType == kCCBTargetTypeDocumentRoot)
|
|
|
|
{
|
|
|
|
target = mActionManager->getRootNode();
|
|
|
|
}
|
|
|
|
else if(memberVarAssignmentType == kCCBTargetTypeOwner)
|
|
|
|
{
|
2012-06-06 08:15:28 +08:00
|
|
|
target = this->mOwner;
|
2012-06-05 06:52:49 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 08:15:28 +08:00
|
|
|
if(target != NULL) {
|
2012-06-14 05:19:13 +08:00
|
|
|
bool assigned = false;
|
|
|
|
|
|
|
|
CCBMemberVariableAssigner * targetAsCCBMemberVariableAssigner = dynamic_cast<CCBMemberVariableAssigner *>(target);
|
|
|
|
|
|
|
|
if(targetAsCCBMemberVariableAssigner != NULL) {
|
|
|
|
assigned = targetAsCCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!assigned && this->mCCBMemberVariableAssigner != NULL) {
|
2012-06-05 06:52:49 +08:00
|
|
|
this->mCCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName, node);
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-17 14:27:17 +08:00
|
|
|
#endif // CCB_ENABLE_JAVASCRIPT
|
2012-09-18 17:04:10 +08:00
|
|
|
|
|
|
|
delete mAnimatedProps;
|
|
|
|
mAnimatedProps = NULL;
|
2012-06-05 06:52:49 +08:00
|
|
|
|
|
|
|
/* Read and add children. */
|
2012-05-31 02:28:50 +08:00
|
|
|
int numChildren = this->readInt(false);
|
|
|
|
for(int i = 0; i < numChildren; i++) {
|
|
|
|
CCNode * child = this->readNodeGraph(node);
|
|
|
|
node->addChild(child);
|
|
|
|
}
|
2012-06-06 08:15:28 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
// Call onNodeLoaded
|
2012-06-14 05:19:13 +08:00
|
|
|
CCNodeLoaderListener * nodeAsCCNodeLoaderListener = dynamic_cast<CCNodeLoaderListener *>(node);
|
|
|
|
if(nodeAsCCNodeLoaderListener != NULL) {
|
|
|
|
nodeAsCCNodeLoaderListener->onNodeLoaded(node, ccNodeLoader);
|
|
|
|
} else if(this->mCCNodeLoaderListener != NULL) {
|
2012-06-13 02:59:49 +08:00
|
|
|
this->mCCNodeLoaderListener->onNodeLoaded(node, ccNodeLoader);
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCBKeyframe* CCBReader::readKeyframe(int type)
|
|
|
|
{
|
|
|
|
CCBKeyframe *keyframe = new CCBKeyframe();
|
|
|
|
keyframe->autorelease();
|
|
|
|
|
|
|
|
keyframe->setTime(readFloat());
|
|
|
|
|
|
|
|
int easingType = readInt(false);
|
|
|
|
float easingOpt = 0;
|
|
|
|
CCObject *value = NULL;
|
|
|
|
|
|
|
|
if (easingType == kCCBKeyframeEasingCubicIn
|
|
|
|
|| easingType == kCCBKeyframeEasingCubicOut
|
|
|
|
|| easingType == kCCBKeyframeEasingCubicInOut
|
|
|
|
|| easingType == kCCBKeyframeEasingElasticIn
|
|
|
|
|| easingType == kCCBKeyframeEasingElasticOut
|
|
|
|
|| easingType == kCCBKeyframeEasingElasticInOut)
|
|
|
|
{
|
|
|
|
easingOpt = readFloat();
|
|
|
|
}
|
|
|
|
keyframe->setEasingType(easingType);
|
|
|
|
keyframe->setEasingOpt(easingOpt);
|
|
|
|
|
|
|
|
if (type == kCCBPropTypeCheck)
|
|
|
|
{
|
|
|
|
value = CCBValue::create(readBool());
|
|
|
|
}
|
|
|
|
else if (type == kCCBPropTypeByte)
|
|
|
|
{
|
|
|
|
value = CCBValue::create(readByte());
|
|
|
|
}
|
|
|
|
else if (type == kCCBPropTypeColor3)
|
|
|
|
{
|
|
|
|
int r = readByte();
|
|
|
|
int g = readByte();
|
|
|
|
int b = readByte();
|
|
|
|
|
|
|
|
ccColor3B c = ccc3(r,g,b);
|
|
|
|
value = ccColor3BWapper::create(c);
|
|
|
|
}
|
|
|
|
else if (type == kCCBPropTypeDegrees)
|
|
|
|
{
|
|
|
|
value = CCBValue::create(readFloat());
|
|
|
|
}
|
|
|
|
else if (type == kCCBPropTypeScaleLock || type == kCCBPropTypePosition)
|
|
|
|
{
|
|
|
|
float a = readFloat();
|
|
|
|
float b = readFloat();
|
|
|
|
|
|
|
|
value = CCArray::create(CCBValue::create(a),
|
|
|
|
CCBValue::create(b),
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
else if (type == kCCBPropTypeSpriteFrame)
|
|
|
|
{
|
|
|
|
CCString *spriteSheet = readCachedString();
|
|
|
|
CCString *spriteFile = readCachedString();
|
|
|
|
|
|
|
|
CCSpriteFrame* spriteFrame;
|
|
|
|
CCString empty("");
|
|
|
|
if (spriteSheet->isEqual(&empty))
|
|
|
|
{
|
|
|
|
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(spriteFile->getCString());
|
|
|
|
CCRect bounds = CCRectMake(0, 0, texture->getContentSize().width, texture->getContentSize().height);
|
|
|
|
spriteFrame = CCSpriteFrame::createWithTexture(texture, bounds);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCSpriteFrameCache* frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
|
|
|
|
|
|
|
|
// Load the sprite sheet only if it is not loaded
|
|
|
|
if (mLoadedSpriteSheets.find(spriteSheet->getCString()) == mLoadedSpriteSheets.end())
|
|
|
|
{
|
|
|
|
frameCache->addSpriteFramesWithFile(spriteSheet->getCString());
|
|
|
|
mLoadedSpriteSheets.insert(spriteSheet->getCString());
|
|
|
|
}
|
|
|
|
|
|
|
|
spriteFrame = frameCache->spriteFrameByName(spriteFile->getCString());
|
|
|
|
}
|
|
|
|
value = spriteFrame;
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
2012-09-17 14:27:17 +08:00
|
|
|
|
|
|
|
keyframe->setValue(value);
|
|
|
|
|
|
|
|
return keyframe;
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
CCNode * CCBReader::readNodeGraph() {
|
|
|
|
return this->readNodeGraph(NULL);
|
2012-06-05 08:45:25 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
bool CCBReader::readSequences()
|
|
|
|
{
|
|
|
|
CCArray *sequences = mActionManager->getSequences();
|
|
|
|
|
|
|
|
int numSeqs = readInt(false);
|
|
|
|
|
|
|
|
for (int i = 0; i < numSeqs; i++)
|
|
|
|
{
|
|
|
|
CCBSequence *seq = new CCBSequence();
|
|
|
|
seq->autorelease();
|
|
|
|
|
|
|
|
seq->setDuration(readFloat());
|
|
|
|
seq->setName(readCachedString()->getCString());
|
|
|
|
seq->setSequenceId(readInt(false));
|
|
|
|
seq->setChainedSequenceId(readInt(true));
|
|
|
|
|
|
|
|
sequences->addObject(seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
mActionManager->setAutoPlaySequenceId(readInt(true));
|
|
|
|
return true;
|
2012-06-05 08:45:25 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:01:01 +08:00
|
|
|
CCString * CCBReader::lastPathComponent(CCString * pPath) {
|
|
|
|
std::string path(pPath->getCString());
|
2012-06-05 08:45:25 +08:00
|
|
|
int slashPos = path.find_last_of("/");
|
2012-06-06 08:15:28 +08:00
|
|
|
if(slashPos != std::string::npos) {
|
2012-06-20 11:48:31 +08:00
|
|
|
return CCString::create(path.substr(slashPos + 1, path.length() - slashPos).c_str());
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
2012-06-20 11:48:31 +08:00
|
|
|
return CCString::create(path.c_str());
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:01:01 +08:00
|
|
|
CCString * CCBReader::deletePathExtension(CCString * pPath) {
|
|
|
|
std::string path(pPath->getCString());
|
2012-06-05 08:45:25 +08:00
|
|
|
int dotPos = path.find_last_of(".");
|
2012-06-06 08:15:28 +08:00
|
|
|
if(dotPos != std::string::npos) {
|
2012-06-20 11:48:31 +08:00
|
|
|
return CCString::create(path.substr(0, dotPos).c_str());
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
2012-06-20 11:48:31 +08:00
|
|
|
return CCString::create(path.c_str());
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:01:01 +08:00
|
|
|
CCString * CCBReader::toLowerCase(CCString * pString) {
|
|
|
|
std::string copy(pString->getCString());
|
2012-05-31 02:28:50 +08:00
|
|
|
std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
|
2012-06-20 11:48:31 +08:00
|
|
|
return CCString::create(copy.c_str());
|
2012-06-12 09:31:11 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:01:01 +08:00
|
|
|
CCString * CCBReader::concat(CCString * pStringA, CCString * pStringB) {
|
2012-06-16 12:48:20 +08:00
|
|
|
int concatenatedLength = pStringA->length() + pStringB->length();
|
|
|
|
char* concatenated = (char*) malloc(concatenatedLength+1);
|
|
|
|
CCString* pRet = NULL;
|
2012-06-14 15:01:01 +08:00
|
|
|
strcpy(concatenated, pStringA->getCString());
|
|
|
|
strcat(concatenated, pStringB->getCString());
|
|
|
|
|
|
|
|
concatenated[concatenatedLength] = '\0';
|
2012-06-20 11:48:31 +08:00
|
|
|
pRet = CCString::create(concatenated);
|
2012-06-16 12:48:20 +08:00
|
|
|
CC_SAFE_FREE(concatenated);
|
|
|
|
return pRet;
|
2012-05-31 02:28:50 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:01:01 +08:00
|
|
|
bool CCBReader::endsWith(CCString * pString, CCString * pEnding) {
|
|
|
|
std::string string(pString->getCString());
|
|
|
|
std::string ending(pEnding->getCString());
|
2012-06-06 08:15:28 +08:00
|
|
|
if(string.length() >= ending.length()) {
|
2012-06-05 08:45:25 +08:00
|
|
|
return (string.compare(string.length() - ending.length(), ending.length(), ending) == 0);
|
2012-05-31 02:28:50 +08:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-07-19 17:22:36 +08:00
|
|
|
|
2012-09-17 14:27:17 +08:00
|
|
|
/************************************************************************
|
|
|
|
Static functions
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
float CCBReader::getResolutionScale()
|
|
|
|
{
|
|
|
|
// Init resolution scale
|
|
|
|
if (CCApplication::sharedApplication()->getTargetPlatform() == kTargetIpad)
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-19 17:22:36 +08:00
|
|
|
NS_CC_EXT_END;
|