Added CCBFileLoader.cpp. Fixed a bug in CCNodeLoader::onHandlePropTypeSize.

This commit is contained in:
Nicolas Gramlich 2012-05-31 14:57:13 -07:00
parent 39442e9bb6
commit b63559d86d
6 changed files with 76 additions and 22 deletions

View File

@ -0,0 +1,18 @@
#import "CCBFileLoader.h"
using namespace cocos2d;
using namespace cocos2d::extension;
#define PROPERTY_CCBFILE "ccbFile"
CCNode * CCBFileLoader::createCCNode(CCNode * pParent, CCBReader * pCCBReader) {
return CCNode::node();
}
void CCBFileLoader::onHandlePropTypeCCBFile(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCNode * pCCBFileNode, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_CCBFILE) == 0) {
pNode->addChild(pCCBFileNode);
} else {
CCNodeLoader::onHandlePropTypeCCBFile(pNode, pParent, pPropertyName, pCCBFileNode, pCCBReader);
}
}

View File

@ -0,0 +1,20 @@
#ifndef _CCBFILE_LOADER_H_
#define _CCBFILE_LOADER_H_
#include "CCNodeLoader.h"
NS_CC_EXT_BEGIN
/* Forward declaration. */
class CCBReader;
class CCBFileLoader : public CCNodeLoader {
protected:
virtual CCNode * createCCNode(CCNode *, CCBReader *);
virtual void onHandlePropTypeCCBFile(CCNode *, CCNode *, std::string, CCNode *, CCBReader *);
};
NS_CC_EXT_END
#endif

View File

@ -8,6 +8,7 @@
#include "CCLabelTTFLoader.h"
#include "CCSpriteLoader.h"
#include "CCScale9SpriteLoader.h"
#include "CCBFileLoader.h"
using namespace cocos2d;
using namespace cocos2d::extension;
@ -21,6 +22,22 @@ CCBReader::CCBReader() {
this->registerCCNodeLoader("CCLabelBMFont", new CCLabelBMFontLoader());
this->registerCCNodeLoader("CCLabelTTF", new CCLabelTTFLoader());
this->registerCCNodeLoader("CCScale9Sprite", new CCScale9SpriteLoader());
this->registerCCNodeLoader("CCBFile", new CCBFileLoader());
}
CCBReader::~CCBReader() {
if(this->mBytes) {
delete this->mBytes;
this->mBytes = NULL;
}
// TODO Also delete mCCNodeLoaders, mLoadedSpritesheets, etc... ? (Keep in mind they might be copied/inherited from another CCBReader through the copy constructor!)
}
CCBReader::CCBReader(CCBReader * pCCBReader) {
/* Borrow CCNodeLoaders and LoadedSpriteSheets. */
this->mLoadedSpriteSheets = pCCBReader->mLoadedSpriteSheets;
this->mCCNodeLoaders = pCCBReader->mCCNodeLoaders;
}
void CCBReader::registerCCNodeLoader(std::string pClassName, CCNodeLoader * pCCNodeLoader) {
@ -33,13 +50,6 @@ CCNodeLoader * CCBReader::getCCNodeLoader(std::string pClassName) {
return ccNodeLoadersIterator->second;
}
CCBReader::~CCBReader() {
if(this->mBytes) {
delete this->mBytes;
this->mBytes = NULL;
}
}
CCNode * CCBReader::readNodeGraphFromFile(const char * pCCBFileName, CCNode * pOwner) {
return this->readNodeGraphFromFile(pCCBFileName, pOwner, CCDirector::sharedDirector()->getWinSize());
}
@ -47,7 +57,8 @@ CCNode * CCBReader::readNodeGraphFromFile(const char * pCCBFileName, CCNode * pO
CCNode * CCBReader::readNodeGraphFromFile(const char * pCCBFileName, CCNode * pOwner, CCSize pParentSize) {
const char * path = CCFileUtils::fullPathFromRelativePath(pCCBFileName);
CCFileUtils::ccLoadFileIntoMemory(path, &this->mBytes);
unsigned long size = 0;
this->mBytes = CCFileUtils::getFileData(path, "r", &size);
this->mCurrentByte = 0;
this->mCurrentBit = 0;

View File

@ -88,6 +88,7 @@ class CC_DLL CCBReader : public CCObject { // TODO Why extend CCObject? -> Also
public:
/* Constructor. */
CCBReader();
CCBReader(CCBReader *);
/* Destructor. */
~CCBReader();

View File

@ -243,9 +243,9 @@ void CCNodeLoader::parseProperties(CCNode * pNode, CCNode * pParent, CCBReader *
break;
}
case kCCBPropTypeCCBFile: {
std::string ccbFile = this->parsePropTypeCCBFile(pNode, pParent, pCCBReader);
CCNode * ccbFileNode = this->parsePropTypeCCBFile(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeCCBFile(pNode, pParent, propertyName, ccbFile, pCCBReader);
this->onHandlePropTypeCCBFile(pNode, pParent, propertyName, ccbFileNode, pCCBReader);
}
break;
}
@ -649,11 +649,19 @@ void * CCNodeLoader::parsePropTypeBlockCCControl(CCNode * pNode, CCNode * pParen
return NULL;
}
std::string CCNodeLoader::parsePropTypeCCBFile(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
CCNode * CCNodeLoader::parsePropTypeCCBFile(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
std::string ccbFileName = pCCBReader->readCachedString();
/* Change path extension to .ccbi. */
return pCCBReader->deletePathExtension(ccbFileName) + std::string(".ccbi");
std::string ccbiFileName = pCCBReader->deletePathExtension(ccbFileName) + std::string(".ccbi");
CCBReader * ccbReader = new CCBReader(pCCBReader);
CCNode * ccbFileNode = ccbReader->readNodeGraphFromFile(ccbiFileName.c_str(), pCCBReader->getOwner(), pParent->getContentSize());
delete ccbReader;
return ccbFileNode;
}
@ -689,7 +697,7 @@ void CCNodeLoader::onHandlePropTypeSize(CCNode * pNode, CCNode * pParent, std::s
void CCNodeLoader::onHandlePropTypeScaleLock(CCNode * pNode, CCNode * pParent, std::string pPropertyName, float * pScaleLock, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_SCALE) == 0) {
pNode->setScaleX(pScaleLock[0]);
pNode->setScaleX(pScaleLock[1]);
pNode->setScaleY(pScaleLock[1]);
} else {
ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
}
@ -793,10 +801,6 @@ void CCNodeLoader::onHandlePropTypeBlockCCControl(CCNode * pNode, CCNode * pPare
ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeCCBFile(CCNode * pNode, CCNode * pParent, std::string pPropertyName, std::string pCCBFileName, CCBReader * pCCBReader) {
// TODO check pPropertyName?
CCBReader * ccbReader = new CCBReader();
CCNode * ccNode = ccbReader->readNodeGraphFromFile(pCCBFileName.c_str(), pCCBReader->getOwner(), pParent->getContentSize());
pNode->addChild(ccNode); // TODO will this work in all cases?
void CCNodeLoader::onHandlePropTypeCCBFile(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCNode * pCCBFileNode, CCBReader * pCCBReader) {
ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
}

View File

@ -43,7 +43,7 @@ class CC_DLL CCNodeLoader {
virtual std::string parsePropTypeFontTTF(CCNode *, CCNode *, CCBReader *);
virtual void * parsePropTypeBlock(CCNode *, CCNode *, CCBReader *);
virtual void * parsePropTypeBlockCCControl(CCNode *, CCNode *, CCBReader *);
virtual std::string parsePropTypeCCBFile(CCNode *, CCNode *, CCBReader *);
virtual CCNode * parsePropTypeCCBFile(CCNode *, CCNode *, CCBReader *);
virtual void onHandlePropTypePosition(CCNode *, CCNode *, std::string, CCPoint, CCBReader *);
@ -72,7 +72,7 @@ class CC_DLL CCNodeLoader {
virtual void onHandlePropTypeFontTTF(CCNode *, CCNode *, std::string, std::string, CCBReader *);
virtual void onHandlePropTypeBlock(CCNode *, CCNode *, std::string, void *, CCBReader *);
virtual void onHandlePropTypeBlockCCControl(CCNode *, CCNode *, std::string, void *, CCBReader *);
virtual void onHandlePropTypeCCBFile(CCNode *, CCNode *, std::string, std::string, CCBReader *);
virtual void onHandlePropTypeCCBFile(CCNode *, CCNode *, std::string, CCNode *, CCBReader *);
};
NS_CC_EXT_END