Current state of the CCB(I)Reader (dysfunctional due to CCLabelBMFont problems).

This commit is contained in:
Nicolas Gramlich 2012-05-30 11:28:50 -07:00
parent d69bc23894
commit 56ea340314
14 changed files with 1654 additions and 0 deletions

View File

@ -0,0 +1,327 @@
#include "CCNodeLoader.h"
#include "CCLayerLoader.h"
#include "CCLayerColorLoader.h"
#include "CCLayerGradientLoader.h"
#include "CCLabelBMFontLoader.h"
#include "CCSpriteLoader.h"
#include "CCBReader.h"
#include <string>
using namespace cocos2d;
using namespace cocos2d::extension;
CCBReader::CCBReader() {
this->registerCCNodeLoader("CCNode", new CCNodeLoader());
this->registerCCNodeLoader("CCLayer", new CCLayerLoader());
this->registerCCNodeLoader("CCLayerColor", new CCLayerColorLoader());
this->registerCCNodeLoader("CCLayerGradient", new CCLayerGradientLoader());
this->registerCCNodeLoader("CCSprite", new CCSpriteLoader());
this->registerCCNodeLoader("CCLabelBMFont", new CCLabelBMFontLoader());
}
void CCBReader::registerCCNodeLoader(std::string pClassName, CCNodeLoader * pCCNodeLoader) {
this->mCCNodeLoaders.insert(std::pair<std::string, CCNodeLoader *>(pClassName, pCCNodeLoader));
}
CCNodeLoader * CCBReader::getCCNodeLoader(std::string pClassName) {
std::map<std::string, CCNodeLoader *>::iterator ccNodeLoadersIterator = this->mCCNodeLoaders.find(pClassName);
assert(ccNodeLoadersIterator != this->mCCNodeLoaders.end());
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());
}
CCNode * CCBReader::readNodeGraphFromFile(const char * pCCBFileName, CCNode * pOwner, CCSize pParentSize) {
const char * path = CCFileUtils::fullPathFromRelativePath(pCCBFileName);
CCFileUtils::ccLoadFileIntoMemory(path, &this->mBytes);
this->mCurrentByte = 0;
this->mCurrentBit = 0;
this->mOwner = pOwner;
this->mRootContainerSize = pParentSize;
if(!this->readHeader()) {
return NULL;
}
if(!this->readStringCache()) {
return NULL;
}
return this->readNodeGraph();
}
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;
if(magicBytes != 'ccbi') {
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;
}
bool CCBReader::readStringCache() {
int numStrings = this->readInt(false);
for(int i = 0; i < numStrings; i++) {
std::string string = this->readUTF8();
this->mStringCache.push_back(string);
}
return true;
}
std::string CCBReader::readUTF8() {
int b0 = this->readByte();
int b1 = this->readByte();
int numBytes = b0 << 8 | b1;
const char * ptr = (const char*) (this->mBytes + this->mCurrentByte);
std::string str(ptr, numBytes);
this->mCurrentByte += numBytes;
return str;
}
unsigned char CCBReader::readByte() {
unsigned char byte = this->mBytes[this->mCurrentByte];
this->mCurrentByte++;
return byte;
}
bool CCBReader::readBool() {
return this->readByte();
}
int CCBReader::readInt(bool pSign) {
int numBits = 0;
while(!this->getBit()) {
numBits++;
}
long long current = 0;
for(int a = numBits - 1; a >= 0; a--) {
if(this->getBit()) {
current |= 1 << a;
}
}
current |= 1 << numBits;
int num;
if(pSign) {
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:
return 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++ ? */
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++;
}
}
std::string CCBReader::readCachedString() {
int i = this->readInt(false);
return this->mStringCache[i];
}
CCNode * CCBReader::readNodeGraph(CCNode * pParent) {
// Read class name
std::string className = this->readCachedString();
int memberVarAssignmentType = this->readInt(false);
std::string memberVarAssignmentName;
if(memberVarAssignmentType) {
memberVarAssignmentName = this->readCachedString();
}
CCNodeLoader * ccNodeLoader = this->getCCNodeLoader(className);
CCNode * node = ccNodeLoader->loadCCNode(pParent, this);
// Set root node
if(!this->mRootNode) {
this->mRootNode = node; // TODO retain?
}
// TODO
/*
// Assign to variable (if applicable)
if (memberVarAssignmentType)
{
id target = NULL;
if (memberVarAssignmentType == kCCBTargetTypeDocumentRoot) target = rootNode;
else if (memberVarAssignmentType == kCCBTargetTypeOwner) target = owner;
if (target)
{
Ivar ivar = class_getInstanceVariable([target class],[memberVarAssignmentName UTF8String]);
if (ivar)
{
object_setIvar(target,ivar,node);
}
else
{
NSLog(@"CCBReader: Couldn't find member variable: %@", memberVarAssignmentName);
}
}
}
*/
// Read and add children
int numChildren = this->readInt(false);
for(int i = 0; i < numChildren; i++) {
CCNode * child = this->readNodeGraph(node);
node->addChild(child);
}
// TODO
/*
// Call didLoadFromCCB
if ([node respondsToSelector:@selector(didLoadFromCCB)])
{
[node performSelector:@selector(didLoadFromCCB)];
}
*/
return node;
}
CCNode * CCBReader::readNodeGraph() {
return this->readNodeGraph(NULL);
}
CCNode * CCBReader::getOwner() {
return this->mOwner;
}
CCSize CCBReader::getContainerSize(CCNode * pNode) {
if(pNode) {
return pNode->getContentSize();
} else {
return this->mRootContainerSize;
}
}
std::string CCBReader::lastPathComponent(std::string pPath) {
int slashPos = pPath.find_last_of("/");
if (slashPos != std::string::npos) {
return pPath.substr(slashPos + 1, pPath.length() - slashPos);
}
return pPath;
}
std::string CCBReader::deletePathExtension(std::string pPath) {
int dotPos = pPath.find_last_of(".");
if (dotPos != std::string::npos) {
return pPath.substr(0, dotPos);
}
return pPath;
}
bool CCBReader::isSpriteSheetLoaded(std::string pSpriteSheet) {
return this->mLoadedSpriteSheets.find(pSpriteSheet) != this->mLoadedSpriteSheets.end();
}
void CCBReader::addLoadedSpriteSheet(std::string pSpriteSheet) {
this->mLoadedSpriteSheets.insert(pSpriteSheet);
}
std::string CCBReader::toLowerCase(std::string pString) {
std::string copy(pString);
std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
return copy;
}
bool CCBReader::endsWith(std::string pString, std::string pEnding) {
if (pString.length() >= pEnding.length()) {
return (pString.compare(pString.length() - pEnding.length(), pEnding.length(), pEnding) == 0);
} else {
return false;
}
}

View File

@ -0,0 +1,128 @@
#ifndef _CCB_READER_H_
#define _CCB_READER_H_
#include "cocos2d.h"
#define kCCBVersion 2
#define kCCBPropTypePosition 0
#define kCCBPropTypeSize 1
#define kCCBPropTypePoint 2
#define kCCBPropTypePointLock 3
#define kCCBPropTypeScaleLock 4
#define kCCBPropTypeDegrees 5
#define kCCBPropTypeInteger 6
#define kCCBPropTypeFloat 7
#define kCCBPropTypeFloatVar 8
#define kCCBPropTypeCheck 9
#define kCCBPropTypeSpriteFrame 10
#define kCCBPropTypeTexture 11
#define kCCBPropTypeByte 12
#define kCCBPropTypeColor3 13
#define kCCBPropTypeColor4FVar 14
#define kCCBPropTypeFlip 15
#define kCCBPropTypeBlendFunc 16
#define kCCBPropTypeFntFile 17
#define kCCBPropTypeText 18
#define kCCBPropTypeFontTTF 19
#define kCCBPropTypeIntegerLabeled 20
#define kCCBPropTypeBlock 21
#define kCCBPropTypeAnimation 22
#define kCCBPropTypeCCBFile 23
#define kCCBPropTypeString 24
#define kCCBPropTypeBlockCCControl 25
#define kCCBPropTypeFloatScale 26
#define kCCBFloat0 0
#define kCCBFloat1 1
#define kCCBFloatMinus1 2
#define kCCBFloat05 3
#define kCCBFloatInteger 4
#define kCCBFloatFull 5
#define kCCBPlatformAll 0
#define kCCBPlatformIOS 1
#define kCCBPlatformMac 2
#define kCCBTargetTypeNone 0
#define kCCBTargetTypeDocumentRoot 1
#define kCCBTargetTypeOwner 2
#define kCCBPositionTypeRelativeBottomLeft 0
#define kCCBPositionTypeRelativeTopLeft 1
#define kCCBPositionTypeRelativeTopRight 2
#define kCCBPositionTypeRelativeBottomRight 3
#define kCCBPositionTypePercent 4
#define kCCBSizeTypeAbsolute 0
#define kCCBSizeTypePercent 1
#define kCCBSizeTypeRelativeContainer 2
#define kCCBSizeTypeHorizontalPercent 3
#define kCCBSzieTypeVerticalPercent 4
#define kCCBScaleTypeAbsolute 0
#define kCCBScaleTypeMultiplyResolution 1
NS_CC_EXT_BEGIN
/* Forward declaration. */
class CCNodeLoader;
/**
* @brief Parse CCBI file which is generated by CocosBuilder
*/
class CC_DLL CCBReader : public cocos2d::CCObject { // TODO Why extend CCObject? -> Also all Loaders should extend from CCObject?
private:
unsigned char * mBytes;
int mCurrentByte;
int mCurrentBit;
cocos2d::CCNode * mOwner; /* TODO Should that be any 'Object'? */
cocos2d::CCNode * mRootNode;
cocos2d::CCSize mRootContainerSize;
std::vector<std::string> mStringCache;
std::map<std::string, CCNodeLoader *> mCCNodeLoaders;
std::set<std::string> mLoadedSpriteSheets;
public:
/* Constructor. */
CCBReader();
/* Destructor. */
~CCBReader();
CCNode * readNodeGraphFromFile(const char *, cocos2d::CCNode * = NULL);
CCNode * readNodeGraphFromFile(const char *, cocos2d::CCNode *, CCSize);
void registerCCNodeLoader(std::string, CCNodeLoader *);
CCNodeLoader * getCCNodeLoader(std::string);
cocos2d::CCNode * getOwner();
cocos2d::CCSize getContainerSize(cocos2d::CCNode *);
std::string lastPathComponent(std::string);
std::string deletePathExtension(std::string);
std::string toLowerCase(std::string);
bool endsWith(std::string, std::string);
bool isSpriteSheetLoaded(std::string);
void addLoadedSpriteSheet(std::string);
/* Parse methods */
int readInt(bool pSign);
unsigned char readByte();
bool readBool();
float readFloat();
std::string readCachedString();
private:
bool readHeader();
bool readStringCache();
cocos2d::CCNode * readNodeGraph();
cocos2d::CCNode * readNodeGraph(cocos2d::CCNode *);
bool getBit();
void alignBits();
std::string readUTF8();
};
NS_CC_EXT_END
#endif

View File

@ -0,0 +1,41 @@
#import "CCLabelBMFontLoader.h"
using namespace cocos2d;
using namespace cocos2d::extension;
#define PROPERTY_COLOR "color"
#define PROPERTY_OPACITY "opacity"
#define PROPERTY_BLENDFUNC "blendFunc"
CCLabelBMFont * CCLabelBMFontLoader::createCCNode(CCNode * pParent, CCBReader * pCCBReader) {
if(pRet && pRet->initWithString(str, fntFile)) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return new CCLabelBMFont(); // TODO Is this problematic?
}
void CCLabelBMFontLoader::onHandlePropTypeColor3(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccColor3B pCCColor3B, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_COLOR) == 0) {
((CCLabelBMFont *)pNode)->setColor(pCCColor3B);
} else {
CCNodeLoader::onHandlePropTypeColor3(pNode, pParent, pPropertyName, pCCColor3B, pCCBReader);
}
}
void CCLabelBMFontLoader::onHandlePropTypeByte(CCNode * pNode, CCNode * pParent, std::string pPropertyName, unsigned char pByte, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_OPACITY) == 0) {
((CCLabelBMFont *)pNode)->setOpacity(pByte);
} else {
CCNodeLoader::onHandlePropTypeByte(pNode, pParent, pPropertyName, pByte, pCCBReader);
}
}
void CCLabelBMFontLoader::onHandlePropTypeBlendFunc(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccBlendFunc pCCBlendFunc, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_BLENDFUNC) == 0) {
((CCLabelBMFont *)pNode)->setBlendFunc(pCCBlendFunc);
} else {
CCNodeLoader::onHandlePropTypeBlendFunc(pNode, pParent, pPropertyName, pCCBlendFunc, pCCBReader);
}
}

View File

@ -0,0 +1,22 @@
#ifndef _CCLABELBMFONT_LOADER_H_
#define _CCLABELBMFONT_LOADER_H_
#include "CCNodeLoader.h"
NS_CC_EXT_BEGIN
/* Forward declaration. */
class CCBReader;
class CCLabelBMFontLoader : public CCNodeLoader {
protected:
virtual cocos2d::CCLabelBMFont * createCCNode(cocos2d::CCNode *, CCBReader *);
virtual void onHandlePropTypeColor3(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::ccColor3B, CCBReader *);
virtual void onHandlePropTypeByte(CCNode *, CCNode *, std::string, unsigned char, CCBReader *);
virtual void onHandlePropTypeBlendFunc(CCNode *, CCNode *, std::string, ccBlendFunc, CCBReader *);
};
NS_CC_EXT_END
#endif

View File

@ -0,0 +1,36 @@
#import "CCLayerColorLoader.h"
using namespace cocos2d;
using namespace cocos2d::extension;
#define PROPERTY_COLOR "color"
#define PROPERTY_OPACITY "opacity"
#define PROPERTY_BLENDFUNC "blendFunc"
CCLayerColor * CCLayerColorLoader::createCCNode(CCNode * pParent, CCBReader * pCCBReader) {
return CCLayerColor::node();
}
void CCLayerColorLoader::onHandlePropTypeColor3(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccColor3B pCCColor3B, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_COLOR) == 0) {
((CCLayerColor *)pNode)->setColor(pCCColor3B);
} else {
CCLayerLoader::onHandlePropTypeColor3(pNode, pParent, pPropertyName, pCCColor3B, pCCBReader);
}
}
void CCLayerColorLoader::onHandlePropTypeByte(CCNode * pNode, CCNode * pParent, std::string pPropertyName, unsigned char pByte, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_OPACITY) == 0) {
((CCLayerColor *)pNode)->setOpacity(pByte);
} else {
CCLayerLoader::onHandlePropTypeByte(pNode, pParent, pPropertyName, pByte, pCCBReader);
}
}
void CCLayerColorLoader::onHandlePropTypeBlendFunc(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccBlendFunc pCCBlendFunc, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_BLENDFUNC) == 0) {
((CCLayerColor *)pNode)->setBlendFunc(pCCBlendFunc);
} else {
CCLayerLoader::onHandlePropTypeBlendFunc(pNode, pParent, pPropertyName, pCCBlendFunc, pCCBReader);
}
}

View File

@ -0,0 +1,22 @@
#ifndef _CCLAYERCOLOR_LOADER_H_
#define _CCLAYERCOLOR_LOADER_H_
#include "CCLayerLoader.h"
NS_CC_EXT_BEGIN
/* Forward declaration. */
class CCBReader;
class CCLayerColorLoader : public CCLayerLoader {
protected:
virtual cocos2d::CCLayerColor * createCCNode(cocos2d::CCNode *, CCBReader *);
virtual void onHandlePropTypeColor3(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::ccColor3B, CCBReader *);
virtual void onHandlePropTypeByte(CCNode *, CCNode *, std::string, unsigned char, CCBReader *);
virtual void onHandlePropTypeBlendFunc(CCNode *, CCNode *, std::string, ccBlendFunc, CCBReader *);
};
NS_CC_EXT_END
#endif

View File

@ -0,0 +1,55 @@
#import "CCLayerGradientLoader.h"
using namespace cocos2d;
using namespace cocos2d::extension;
#define PROPERTY_STARTCOLOR "startColor"
#define PROPERTY_ENDCOLOR "endColor"
#define PROPERTY_STARTOPACITY "startOpacity"
#define PROPERTY_ENDOPACITY "endOpacity"
#define PROPERTY_VECTOR "vector"
#define PROPERTY_BLENDFUNC "blendFunc"
CCLayerGradient * CCLayerGradientLoader::createCCNode(CCNode * pParent, CCBReader * pCCBReader) {
return CCLayerGradient::node();
}
void CCLayerGradientLoader::onHandlePropTypeColor3(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccColor3B pCCColor3B, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_STARTCOLOR) == 0) {
((CCLayerGradient *)pNode)->setStartColor(pCCColor3B);
} else if(pPropertyName.compare(PROPERTY_ENDCOLOR) == 0) {
((CCLayerGradient *)pNode)->setEndColor(pCCColor3B);
} else {
CCLayerLoader::onHandlePropTypeColor3(pNode, pParent, pPropertyName, pCCColor3B, pCCBReader);
}
}
void CCLayerGradientLoader::onHandlePropTypeByte(CCNode * pNode, CCNode * pParent, std::string pPropertyName, unsigned char pByte, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_STARTOPACITY) == 0) {
((CCLayerGradient *)pNode)->setStartOpacity(pByte);
} else if(pPropertyName.compare(PROPERTY_ENDOPACITY) == 0) {
((CCLayerGradient *)pNode)->setEndOpacity(pByte);
} else {
CCLayerLoader::onHandlePropTypeByte(pNode, pParent, pPropertyName, pByte, pCCBReader);
}
}
void CCLayerGradientLoader::onHandlePropTypeBlendFunc(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccBlendFunc pCCBlendFunc, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_BLENDFUNC) == 0) {
((CCLayerGradient *)pNode)->setBlendFunc(pCCBlendFunc);
} else {
CCLayerLoader::onHandlePropTypeBlendFunc(pNode, pParent, pPropertyName, pCCBlendFunc, pCCBReader);
}
}
void CCLayerGradientLoader::onHandlePropTypePoint(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCPoint pPoint, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_VECTOR) == 0) {
((CCLayerGradient *)pNode)->setVector(pPoint);
// TODO Not passed along the ccbi file.
// ((CCLayerGradient *)pNode)->setIsCompressedInterpolation(true);
} else {
CCLayerLoader::onHandlePropTypePoint(pNode, pParent, pPropertyName, pPoint, pCCBReader);
}
}

View File

@ -0,0 +1,23 @@
#ifndef _CCLAYERGRADIENT_LOADER_H_
#define _CCLAYERGRADIENT_LOADER_H_
#include "CCLayerLoader.h"
NS_CC_EXT_BEGIN
/* Forward declaration. */
class CCBReader;
class CCLayerGradientLoader : public CCLayerLoader {
protected:
virtual cocos2d::CCLayerGradient * createCCNode(cocos2d::CCNode *, CCBReader *);
virtual void onHandlePropTypeColor3(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::ccColor3B, CCBReader *);
virtual void onHandlePropTypeByte(CCNode *, CCNode *, std::string, unsigned char, CCBReader *);
virtual void onHandlePropTypePoint(CCNode *, CCNode *, std::string, CCPoint, CCBReader *);
virtual void onHandlePropTypeBlendFunc(CCNode *, CCNode *, std::string, ccBlendFunc, CCBReader *);
};
NS_CC_EXT_END
#endif

View File

@ -0,0 +1,30 @@
#import "CCLayerLoader.h"
using namespace cocos2d;
using namespace cocos2d::extension;
#define PROPERTY_TOUCH_ENABLED "isTouchEnabled"
#define PROPERTY_ACCELEROMETER_ENABLED "isAccelerometerEnabled"
#define PROPERTY_MOUSE_ENABLED "isMouseEnabled"
#define PROPERTY_KEYBOARD_ENABLED "isKeyboardEnabled"
CCLayer * CCLayerLoader::createCCNode(CCNode * pParent, CCBReader * pCCBReader) {
return CCLayer::node();
}
void CCLayerLoader::onHandlePropTypeCheck(CCNode * pNode, CCNode * pParent, std::string pPropertyName, bool pCheck, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_TOUCH_ENABLED) == 0) {
((CCLayer *)pNode)->setIsTouchEnabled(pCheck);
} else if(pPropertyName.compare(PROPERTY_ACCELEROMETER_ENABLED) == 0) {
((CCLayer *)pNode)->setIsAccelerometerEnabled(pCheck);
} else if(pPropertyName.compare(PROPERTY_MOUSE_ENABLED) == 0) {
// TODO XXX
CCLOG("The property '%s' is not supported!", PROPERTY_MOUSE_ENABLED);
} else if(pPropertyName.compare(PROPERTY_KEYBOARD_ENABLED) == 0) {
// TODO XXX
CCLOG("The property '%s' is not supported!", PROPERTY_KEYBOARD_ENABLED);
// This comes closest: ((CCLayer *)pNode)->setIsKeypadEnabled(pCheck);
} else {
CCNodeLoader::onHandlePropTypeCheck(pNode, pParent, pPropertyName, pCheck, pCCBReader);
}
}

View File

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

View File

@ -0,0 +1,791 @@
#import "CCNodeLoader.h"
#define PROPERTY_POSITION "position"
#define PROPERTY_CONTENTSIZE "contentSize"
#define PROPERTY_ANCHORPOINT "anchorPoint"
#define PROPERTY_SCALE "scale"
#define PROPERTY_ROTATION "rotation"
#define PROPERTY_TAG "tag"
#define PROPERTY_IGNOREANCHORPOINTFORPOSITION "ignoreAnchorPointForPosition"
#define PROPERTY_VISIBLE "visible"
#define ASSERT_UNEXPECTED_PROPERTY(PROPERTY) printf("Unexpected property type: '%s'!\n", PROPERTY.c_str()); assert(false)
#define ASSERT_UNEXPECTED_PROPERTYTYPE(PROPERTYTYPE) printf("Unexpected property type: '%d'!\n", PROPERTYTYPE); assert(false)
using namespace cocos2d;
using namespace cocos2d::extension;
CCNode * CCNodeLoader::loadCCNode(CCNode * pParent, CCBReader * pCCBReader) {
CCNode * ccNode = this->createCCNode(pParent, pCCBReader);
this->parseProperties(ccNode, pParent, pCCBReader);
return ccNode;
}
CCNode * CCNodeLoader::createCCNode(CCNode * pParent, CCBReader * pCCBReader) {
return CCNode::node();
}
void CCNodeLoader::parseProperties(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
int propertyCount = pCCBReader->readInt(false);
for(int i = 0; i < propertyCount; i++) {
int type = pCCBReader->readInt(false);
std::string propertyName = pCCBReader->readCachedString();
// Check if the property can be set for this platform
bool setProp = false;
int platform = pCCBReader->readByte();
if(platform == kCCBPlatformAll) {
setProp = true;
}
#ifdef __CC_PLATFORM_IOS
if(platform == kCCBPlatformIOS) {
setProp = true;
}
#elif defined(__CC_PLATFORM_MAC)
if(platform == kCCBPlatformMac) {
setProp = true;
}
#endif
switch(type) {
case kCCBPropTypePosition: {
CCPoint position = this->parsePropTypePosition(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypePosition(pNode, pParent, propertyName, position, pCCBReader);
}
break;
}
case kCCBPropTypePoint: {
CCPoint point = this->parsePropTypePoint(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypePoint(pNode, pParent, propertyName, point, pCCBReader);
}
break;
}
case kCCBPropTypePointLock: {
CCPoint pointLock = this->parsePropTypePointLock(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypePointLock(pNode, pParent, propertyName, pointLock, pCCBReader);
}
break;
}
case kCCBPropTypeSize: {
CCSize size = this->parsePropTypeSize(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeSize(pNode, pParent, propertyName, size, pCCBReader);
}
break;
}
case kCCBPropTypeScaleLock: {
float * scaleLock = this->parsePropTypeScaleLock(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeScaleLock(pNode, pParent, propertyName, scaleLock, pCCBReader);
}
delete scaleLock; // TODO Can this just be deleted?
break;
}
case kCCBPropTypeFloat: {
float f = this->parsePropTypeFloat(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeFloat(pNode, pParent, propertyName, f, pCCBReader);
}
break;
}
case kCCBPropTypeDegrees: {
float degrees = this->parsePropTypeDegrees(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeDegrees(pNode, pParent, propertyName, degrees, pCCBReader);
}
break;
}
case kCCBPropTypeFloatScale: {
float floatScale = this->parsePropTypeFloatScale(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeFloatScale(pNode, pParent, propertyName, floatScale, pCCBReader);
}
break;
}
case kCCBPropTypeInteger: {
int integer = this->parsePropTypeInteger(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeInteger(pNode, pParent, propertyName, integer, pCCBReader);
}
break;
}
case kCCBPropTypeIntegerLabeled: {
int integerLabeled = this->parsePropTypeIntegerLabeled(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeIntegerLabeled(pNode, pParent, propertyName, integerLabeled, pCCBReader);
}
break;
}
case kCCBPropTypeFloatVar: {
float * floatVar = this->parsePropTypeFloatVar(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeFloatVar(pNode, pParent, propertyName, floatVar, pCCBReader);
}
delete floatVar; // TODO Can this just be deleted?
break;
}
case kCCBPropTypeCheck: {
bool check = this->parsePropTypeCheck(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeCheck(pNode, pParent, propertyName, check, pCCBReader);
}
break;
}
case kCCBPropTypeSpriteFrame: {
if(setProp) {
this->onHandlePropTypeSpriteFrame(pNode, pParent, propertyName, this->parsePropTypeSpriteFrame(pNode, pParent, pCCBReader), pCCBReader);
}
break;
}
case kCCBPropTypeAnimation: {
CCAnimation * ccAnimation = this->parsePropTypeAnimation(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeAnimation(pNode, pParent, propertyName, ccAnimation, pCCBReader);
}
// TODO delete ccAnimation; ???
break;
}
case kCCBPropTypeTexture: {
CCTexture2D * ccTexture2D = this->parsePropTypeTexture(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeTexture(pNode, pParent, propertyName, ccTexture2D, pCCBReader);
}
break;
}
case kCCBPropTypeByte: {
unsigned char byte = this->parsePropTypeByte(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeByte(pNode, pParent, propertyName, byte, pCCBReader);
}
break;
}
case kCCBPropTypeColor3: {
ccColor3B color3B = this->parsePropTypeColor3(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeColor3(pNode, pParent, propertyName, color3B, pCCBReader);
}
break;
}
case kCCBPropTypeColor4FVar: {
ccColor4F * color4FVar = this->parsePropTypeColor4FVar(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeColor4FVar(pNode, pParent, propertyName, color4FVar, pCCBReader);
}
delete color4FVar; // TODO Can this just be deleted?
break;
}
case kCCBPropTypeFlip: {
bool * flip = this->parsePropTypeFlip(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeFlip(pNode, pParent, propertyName, flip, pCCBReader);
}
delete flip; // TODO Can this just be deleted?
break;
}
case kCCBPropTypeBlendFunc: {
ccBlendFunc blendFunc = this->parsePropTypeBlendFunc(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeBlendFunc(pNode, pParent, propertyName, blendFunc, pCCBReader);
}
break;
}
case kCCBPropTypeFntFile: {
std::string fntFile = this->parsePropTypeFntFile(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeFntFile(pNode, pParent, propertyName, fntFile, pCCBReader);
}
break;
}
case kCCBPropTypeString: {
std::string string = this->parsePropTypeString(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeString(pNode, pParent, propertyName, string, pCCBReader);
}
break;
}
case kCCBPropTypeText: {
std::string text = this->parsePropTypeText(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeText(pNode, pParent, propertyName, text, pCCBReader);
}
break;
}
case kCCBPropTypeBlock: {
void * block = this->parsePropTypeBlock(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeBlock(pNode, pParent, propertyName, block, pCCBReader);
}
// TODO delete block; ???
break;
}
case kCCBPropTypeBlockCCControl: {
void * blockCCControl = this->parsePropTypeBlockCCControl(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeBlockCCControl(pNode, pParent, propertyName, blockCCControl, pCCBReader);
}
// TODO delete blockCCControl; ???
break;
}
case kCCBPropTypeCCBFile: {
std::string ccbFile = this->parsePropTypeCCBFile(pNode, pParent, pCCBReader);
if(setProp) {
this->onHandlePropTypeCCBFile(pNode, pParent, propertyName, ccbFile, pCCBReader);
}
break;
}
default:
ASSERT_UNEXPECTED_PROPERTYTYPE(type);
break;
}
}
}
CCPoint CCNodeLoader::parsePropTypePosition(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
float x = pCCBReader->readFloat();
float y = pCCBReader->readFloat();
int type = pCCBReader->readInt(false);
CCSize containerSize = pCCBReader->getContainerSize(pParent);
switch (type) {
case kCCBPositionTypeRelativeBottomLeft: {
/* Nothing. */
break;
}
case kCCBPositionTypeRelativeTopLeft: {
y = containerSize.height - y;
break;
}
case kCCBPositionTypeRelativeTopRight: {
x = containerSize.width - x;
y = containerSize.height - y;
break;
}
case kCCBPositionTypeRelativeBottomRight: {
x = containerSize.width - x;
break;
}
case kCCBPositionTypePercent: {
x = (int)(containerSize.width * x / 100.0f);
y = (int)(containerSize.height * y / 100.0f);
break;
}
}
return CCPoint(x, y);
}
CCPoint CCNodeLoader::parsePropTypePoint(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
float x = pCCBReader->readFloat();
float y = pCCBReader->readFloat();
return CCPoint(x, y);
}
CCPoint CCNodeLoader::parsePropTypePointLock(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
float x = pCCBReader->readFloat();
float y = pCCBReader->readFloat();
return CCPoint(x, y);
}
CCSize CCNodeLoader::parsePropTypeSize(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
float width = pCCBReader->readFloat();
float height = pCCBReader->readFloat();
int type = pCCBReader->readInt(false);
CCSize containerSize = pCCBReader->getContainerSize(pParent);
switch (type) {
case kCCBSizeTypeAbsolute: {
/* Nothing. */
break;
}
case kCCBSizeTypeRelativeContainer: {
width = containerSize.width - width;
height = containerSize.height - height;
break;
}
case kCCBSizeTypePercent: {
width = (int)(containerSize.width * width / 100.0f);
height = (int)(containerSize.height * height / 100.0f);
break;
}
case kCCBSizeTypeHorizontalPercent: {
width = (int)(containerSize.width * width / 100.0f);
break;
}
case kCCBSzieTypeVerticalPercent: {
height = (int)(containerSize.height * height / 100.0f);
break;
}
default:
break;
}
return CCSize(width, height);
}
float * CCNodeLoader::parsePropTypeScaleLock(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
float x = pCCBReader->readFloat();
float y = pCCBReader->readFloat();
int type = pCCBReader->readInt(false);
// TODO
/*
if (type == kCCBScaleTypeMultiplyResolution) {
x *= resolutionScale;
y *= resolutionScale;
}
*/
float * scaleLock = new float[2];
scaleLock[0] = x;
scaleLock[1] = y;
return scaleLock;
}
float CCNodeLoader::parsePropTypeFloat(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readFloat();
}
float CCNodeLoader::parsePropTypeDegrees(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readFloat();
}
float CCNodeLoader::parsePropTypeFloatScale(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
float f = pCCBReader->readFloat();
int type = pCCBReader->readInt(false);
// TODO
/*
if (type == kCCBScaleTypeMultiplyResolution) {
x *= resolutionScale;
y *= resolutionScale;
}
*/
return f;
}
int CCNodeLoader::parsePropTypeInteger(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readInt(true);
}
int CCNodeLoader::parsePropTypeIntegerLabeled(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readInt(true);
}
float * CCNodeLoader::parsePropTypeFloatVar(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
float f = pCCBReader->readFloat();
float fVar = pCCBReader->readFloat();
float * arr = new float[2];
arr[0] = f;
arr[1] = fVar;
return arr;
}
bool CCNodeLoader::parsePropTypeCheck(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readBool();
}
CCSpriteFrame * CCNodeLoader::parsePropTypeSpriteFrame(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
std::string spriteSheet = pCCBReader->readCachedString();
std::string spriteFile = pCCBReader->readCachedString();
CCSpriteFrame * spriteFrame;
if (spriteSheet.compare("") == 0) {
CCTexture2D * texture = CCTextureCache::sharedTextureCache()->addImage(spriteFile.c_str());
CCRect bounds = CCRect::CCRect(0, 0, texture->getContentSize().width, texture->getContentSize().height);
spriteFrame = CCSpriteFrame::frameWithTexture(texture, bounds);
} else {
CCSpriteFrameCache * frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
/* Load the sprite sheet only if it is not loaded. */
if (!pCCBReader->isSpriteSheetLoaded(spriteSheet)) {
frameCache->addSpriteFramesWithFile(spriteSheet.c_str());
pCCBReader->addLoadedSpriteSheet(spriteSheet);
}
spriteFrame = frameCache->spriteFrameByName(spriteFile.c_str());
}
return spriteFrame;
}
CCAnimation * CCNodeLoader::parsePropTypeAnimation(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
std::string animationFile = pCCBReader->readCachedString();
std::string animation = pCCBReader->readCachedString();
CCAnimation * ccAnimation = NULL;
// Support for stripping relative file paths, since ios doesn't currently
// know what to do with them, since its pulling from bundle.
// Eventually this should be handled by a client side asset manager
// interface which figured out what resources to load.
// TODO Does this problem exist in C++?
animation = pCCBReader->lastPathComponent(animation);
animationFile = pCCBReader->lastPathComponent(animationFile);
if (animation.compare("") != 0) {
CCAnimationCache * animationCache = CCAnimationCache::sharedAnimationCache();
animationCache->addAnimationsWithFile(animationFile.c_str());
ccAnimation = animationCache->animationByName(animation.c_str());
}
return ccAnimation;
}
CCTexture2D * CCNodeLoader::parsePropTypeTexture(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
std::string spriteFile = pCCBReader->readCachedString();
return CCTextureCache::sharedTextureCache()->addImage(spriteFile.c_str());
}
unsigned char CCNodeLoader::parsePropTypeByte(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readByte();
}
ccColor3B CCNodeLoader::parsePropTypeColor3(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
unsigned char red = pCCBReader->readByte();
unsigned char green = pCCBReader->readByte();
unsigned char blue = pCCBReader->readByte();
ccColor3B color = { red, green, blue };
return color;
}
ccColor4F * CCNodeLoader::parsePropTypeColor4FVar(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
float red = pCCBReader->readFloat();
float green = pCCBReader->readFloat();
float blue = pCCBReader->readFloat();
float alpha = pCCBReader->readFloat();
float redVar = pCCBReader->readFloat();
float greenVar = pCCBReader->readFloat();
float blueVar = pCCBReader->readFloat();
float alphaVar = pCCBReader->readFloat();
ccColor4F * colors = new ccColor4F[2];
colors[0].r = red;
colors[0].g = green;
colors[0].b = blue;
colors[0].a = alpha;
colors[1].r = redVar;
colors[1].g = greenVar;
colors[1].b = blueVar;
colors[1].a = alphaVar;
return colors;
}
bool * CCNodeLoader::parsePropTypeFlip(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
bool flipX = pCCBReader->readBool();
bool flipY = pCCBReader->readBool();
bool * arr = new bool[2];
arr[0] = flipX;
arr[1] = flipY;
return arr;
}
ccBlendFunc CCNodeLoader::parsePropTypeBlendFunc(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
int source = pCCBReader->readInt(false);
int destination = pCCBReader->readInt(false);
ccBlendFunc blendFunc;
blendFunc.src = source;
blendFunc.dst = destination;
return blendFunc;
}
std::string CCNodeLoader::parsePropTypeFntFile(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readCachedString();
}
std::string CCNodeLoader::parsePropTypeString(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readCachedString();
}
std::string CCNodeLoader::parsePropTypeText(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
return pCCBReader->readCachedString();
}
std::string CCNodeLoader::parsePropTypeFontTTF(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
std::string fnt = pCCBReader->readCachedString();
std::string ttfEnding("ttf");
if (pCCBReader->endsWith(pCCBReader->toLowerCase(fnt), ttfEnding)){
fnt = pCCBReader->deletePathExtension(pCCBReader->lastPathComponent(fnt));
}
return fnt;
}
void * CCNodeLoader::parsePropTypeBlock(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
std::string selectorName = pCCBReader->readCachedString();
int selectorTarget = pCCBReader->readInt(false);
// TODO Selectors?
/*
#ifdef CCB_ENABLE_JAVASCRIPT
if (selectorTarget && selectorName && ![selectorName isEqualToString:@""])
{
void (^block)(id sender);
block = ^(id sender) {
[[JSCocoa sharedController] eval:[NSString stringWithFormat:@"%@();",selectorName]];
};
NSString* setSelectorName = [NSString stringWithFormat:@"set%@:",[name capitalizedString]];
SEL setSelector = NSSelectorFromString(setSelectorName);
if ([node respondsToSelector:setSelector])
{
[node performSelector:setSelector withObject:block];
}
else
{
NSLog(@"CCBReader: Failed to set selector/target block for %@",selectorName);
}
}
#else
if (selectorTarget)
{
id target = NULL;
if (selectorTarget == kCCBTargetTypeDocumentRoot) target = rootNode;
else if (selectorTarget == kCCBTargetTypeOwner) target = owner;
if (target)
{
SEL selector = NSSelectorFromString(selectorName);
__block id t = target;
void (^block)(id sender);
block = ^(id sender) {
[t performSelector:selector withObject:sender];
};
NSString* setSelectorName = [NSString stringWithFormat:@"set%@:",[name capitalizedString]];
SEL setSelector = NSSelectorFromString(setSelectorName);
if ([node respondsToSelector:setSelector])
{
[node performSelector:setSelector withObject:block];
}
else
{
NSLog(@"CCBReader: Failed to set selector/target block for %@",selectorName);
}
}
else
{
NSLog(@"CCBReader: Failed to find target for block");
}
}
#endif
*/
return NULL;
}
void * CCNodeLoader::parsePropTypeBlockCCControl(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader) {
std::string selectorName = pCCBReader->readCachedString();
int selectorTarget = pCCBReader->readInt(false);
int ctrlEvts = pCCBReader->readInt(false);
// TODO
/*
// Since we do not know for sure that CCControl is available, use
// NSInvocation to call it's addTarget:action:forControlEvents: method
NSMethodSignature* sig = [node methodSignatureForSelector:@selector(addTarget:action:forControlEvents:)];
if (sig)
{
SEL selector = NSSelectorFromString(selectorName);
id target = NULL;
if (selectorTarget == kCCBTargetTypeDocumentRoot) target = rootNode;
else if (selectorTarget == kCCBTargetTypeOwner) target = owner;
if (selector && target)
{
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:node];
[invocation setSelector:@selector(addTarget:action:forControlEvents:)];
[invocation setArgument:&target atIndex:2];
[invocation setArgument:&selector atIndex:3];
[invocation setArgument:&ctrlEvts atIndex:4];
[invocation invoke];
}
}
else
{
NSLog(@"CCBReader: Failed to add selector/target block for CCControl");
}
*/
return NULL;
}
std::string 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");
}
void CCNodeLoader::onHandlePropTypePosition(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCPoint pPosition, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_POSITION) == 0) {
pNode->setPosition(pPosition);
} else {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
}
void CCNodeLoader::onHandlePropTypePoint(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCPoint pPoint, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_ANCHORPOINT) == 0) {
pNode->setAnchorPoint(pPoint);
} else {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
}
void CCNodeLoader::onHandlePropTypePointLock(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCPoint pPointLock, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeSize(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCSize pSize, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_CONTENTSIZE) == 0) {
pNode->setContentSize(pSize);
}
}
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]);
} else {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
}
void CCNodeLoader::onHandlePropTypeFloat(CCNode * pNode, CCNode * pParent, std::string pPropertyName, float pFloat, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeDegrees(CCNode * pNode, CCNode * pParent, std::string pPropertyName, float pDegrees, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_ROTATION) == 0) {
pNode->setRotation(pDegrees);
} else {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
}
void CCNodeLoader::onHandlePropTypeFloatScale(CCNode * pNode, CCNode * pParent, std::string pPropertyName, float pFloatScale, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeInteger(CCNode * pNode, CCNode * pParent, std::string pPropertyName, int pInteger, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_TAG) == 0) {
pNode->setTag(pInteger);
} else {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
}
void CCNodeLoader::onHandlePropTypeIntegerLabeled(CCNode * pNode, CCNode * pParent, std::string pPropertyName, int pIntegerLabeled, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeFloatVar(CCNode * pNode, CCNode * pParent, std::string pPropertyName, float * pFloatVar, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeCheck(CCNode * pNode, CCNode * pParent, std::string pPropertyName, bool pCheck, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_VISIBLE) == 0) {
pNode->setIsVisible(pCheck);
} else if(pPropertyName.compare(PROPERTY_IGNOREANCHORPOINTFORPOSITION) == 0) {
pNode->setIsRelativeAnchorPoint(!pCheck);
} else {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
}
void CCNodeLoader::onHandlePropTypeSpriteFrame(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCSpriteFrame * pCCSpriteFrame, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeAnimation(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCAnimation * pCCAnimation, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeTexture(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCTexture2D * pCCTexture2D, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeByte(CCNode * pNode, CCNode * pParent, std::string pPropertyName, unsigned char pByte, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeColor3(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccColor3B pCCColor3B, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeColor4FVar(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccColor4F * pCCColor4FVar, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeFlip(CCNode * pNode, CCNode * pParent, std::string pPropertyName, bool * pFlip, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeBlendFunc(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccBlendFunc pCCBlendFunc, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeFntFile(CCNode * pNode, CCNode * pParent, std::string pPropertyName, std::string pFntFile, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeString(CCNode * pNode, CCNode * pParent, std::string pPropertyName, std::string pString, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeText(CCNode * pNode, CCNode * pParent, std::string pPropertyName, std::string pString, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeFontTTF(CCNode * pNode, CCNode * pParent, std::string pPropertyName, std::string pFontTTF, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeBlock(CCNode * pNode, CCNode * pParent, std::string pPropertyName, void * pBlock, CCBReader * pCCBReader) {
ASSERT_UNEXPECTED_PROPERTY(pPropertyName);
}
void CCNodeLoader::onHandlePropTypeBlockCCControl(CCNode * pNode, CCNode * pParent, std::string pPropertyName, void * pBlock, CCBReader * pCCBReader) {
ASSERT_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?
}

View File

@ -0,0 +1,80 @@
#ifndef _CCNODE_LOADER_H_
#define _CCNODE_LOADER_H_
#include "cocos2d.h"
#include "CCBReader.h"
NS_CC_EXT_BEGIN
/* Forward declaration. */
class CCBReader;
class CC_DLL CCNodeLoader {
public:
virtual cocos2d::CCNode * loadCCNode(cocos2d::CCNode *, CCBReader *);
virtual void parseProperties(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
protected:
virtual cocos2d::CCNode * createCCNode(cocos2d::CCNode *, CCBReader *);
virtual cocos2d::CCPoint parsePropTypePosition(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual cocos2d::CCPoint parsePropTypePoint(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual cocos2d::CCPoint parsePropTypePointLock(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual cocos2d::CCSize parsePropTypeSize(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual float * parsePropTypeScaleLock(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual float parsePropTypeFloat(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual float parsePropTypeDegrees(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual float parsePropTypeFloatScale(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual int parsePropTypeInteger(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual int parsePropTypeIntegerLabeled(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual float * parsePropTypeFloatVar(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual bool parsePropTypeCheck(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual cocos2d::CCSpriteFrame * parsePropTypeSpriteFrame(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual cocos2d::CCAnimation * parsePropTypeAnimation(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual cocos2d::CCTexture2D * parsePropTypeTexture(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual unsigned char parsePropTypeByte(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual cocos2d::ccColor3B parsePropTypeColor3(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual cocos2d::ccColor4F * parsePropTypeColor4FVar(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual bool * parsePropTypeFlip(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual cocos2d::ccBlendFunc parsePropTypeBlendFunc(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual std::string parsePropTypeFntFile(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual std::string parsePropTypeString(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual std::string parsePropTypeText(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual std::string parsePropTypeFontTTF(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual void * parsePropTypeBlock(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual void * parsePropTypeBlockCCControl(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual std::string parsePropTypeCCBFile(cocos2d::CCNode *, cocos2d::CCNode *, CCBReader *);
virtual void onHandlePropTypePosition(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::CCPoint, CCBReader *);
virtual void onHandlePropTypePoint(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::CCPoint, CCBReader *);
virtual void onHandlePropTypePointLock(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::CCPoint, CCBReader *);
virtual void onHandlePropTypeSize(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::CCSize, CCBReader *);
virtual void onHandlePropTypeScaleLock(cocos2d::CCNode *, cocos2d::CCNode *, std::string, float *, CCBReader *);
virtual void onHandlePropTypeFloat(cocos2d::CCNode *, cocos2d::CCNode *, std::string, float, CCBReader *);
virtual void onHandlePropTypeDegrees(cocos2d::CCNode *, cocos2d::CCNode *, std::string, float, CCBReader *);
virtual void onHandlePropTypeFloatScale(cocos2d::CCNode *, cocos2d::CCNode *, std::string, float, CCBReader *);
virtual void onHandlePropTypeInteger(cocos2d::CCNode *, cocos2d::CCNode *, std::string, int, CCBReader *);
virtual void onHandlePropTypeIntegerLabeled(cocos2d::CCNode *, cocos2d::CCNode *, std::string, int, CCBReader *);
virtual void onHandlePropTypeFloatVar(cocos2d::CCNode *, cocos2d::CCNode *, std::string, float *, CCBReader *);
virtual void onHandlePropTypeCheck(cocos2d::CCNode *, cocos2d::CCNode *, std::string, bool, CCBReader *);
virtual void onHandlePropTypeSpriteFrame(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::CCSpriteFrame *, CCBReader *);
virtual void onHandlePropTypeAnimation(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::CCAnimation *, CCBReader *);
virtual void onHandlePropTypeTexture(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::CCTexture2D *, CCBReader *);
virtual void onHandlePropTypeByte(cocos2d::CCNode *, cocos2d::CCNode *, std::string, unsigned char, CCBReader *);
virtual void onHandlePropTypeColor3(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::ccColor3B, CCBReader *);
virtual void onHandlePropTypeColor4FVar(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::ccColor4F *, CCBReader *);
virtual void onHandlePropTypeFlip(cocos2d::CCNode *, cocos2d::CCNode *, std::string, bool *, CCBReader *);
virtual void onHandlePropTypeBlendFunc(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::ccBlendFunc, CCBReader *);
virtual void onHandlePropTypeFntFile(cocos2d::CCNode *, cocos2d::CCNode *, std::string, std::string, CCBReader *);
virtual void onHandlePropTypeString(cocos2d::CCNode *, cocos2d::CCNode *, std::string, std::string, CCBReader *);
virtual void onHandlePropTypeText(cocos2d::CCNode *, cocos2d::CCNode *, std::string, std::string, CCBReader *);
virtual void onHandlePropTypeFontTTF(cocos2d::CCNode *, cocos2d::CCNode *, std::string, std::string, CCBReader *);
virtual void onHandlePropTypeBlock(cocos2d::CCNode *, cocos2d::CCNode *, std::string, void *, CCBReader *);
virtual void onHandlePropTypeBlockCCControl(cocos2d::CCNode *, cocos2d::CCNode *, std::string, void *, CCBReader *);
virtual void onHandlePropTypeCCBFile(cocos2d::CCNode *, cocos2d::CCNode *, std::string, std::string, CCBReader *);
};
NS_CC_EXT_END
#endif

View File

@ -0,0 +1,55 @@
#import "CCSpriteLoader.h"
using namespace cocos2d;
using namespace cocos2d::extension;
#define PROPERTY_FLIP "flip"
#define PROPERTY_DISPLAYFRAME "displayFrame"
#define PROPERTY_COLOR "color"
#define PROPERTY_OPACITY "opacity"
#define PROPERTY_BLENDFUNC "blendFunc"
CCSprite * CCSpriteLoader::createCCNode(CCNode * pParent, CCBReader * pCCBReader) {
return CCSprite::node();
}
void CCSpriteLoader::onHandlePropTypeSpriteFrame(CCNode * pNode, CCNode * pParent, std::string pPropertyName, CCSpriteFrame * pCCSpriteFrame, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_DISPLAYFRAME) == 0) {
((CCSprite *)pNode)->setDisplayFrame(pCCSpriteFrame);
} else {
CCNodeLoader::onHandlePropTypeSpriteFrame(pNode, pParent, pPropertyName, pCCSpriteFrame, pCCBReader);
}
}
void CCSpriteLoader::onHandlePropTypeFlip(CCNode * pNode, CCNode * pParent, std::string pPropertyName, bool * pFlip, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_FLIP) == 0) {
((CCSprite *)pNode)->setFlipX(pFlip[0]);
((CCSprite *)pNode)->setFlipX(pFlip[1]);
} else {
CCNodeLoader::onHandlePropTypeCheck(pNode, pParent, pPropertyName, pFlip, pCCBReader);
}
}
void CCSpriteLoader::onHandlePropTypeColor3(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccColor3B pCCColor3B, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_COLOR) == 0) {
((CCSprite *)pNode)->setColor(pCCColor3B);
} else {
CCNodeLoader::onHandlePropTypeColor3(pNode, pParent, pPropertyName, pCCColor3B, pCCBReader);
}
}
void CCSpriteLoader::onHandlePropTypeByte(CCNode * pNode, CCNode * pParent, std::string pPropertyName, unsigned char pByte, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_OPACITY) == 0) {
((CCSprite *)pNode)->setOpacity(pByte);
} else {
CCNodeLoader::onHandlePropTypeByte(pNode, pParent, pPropertyName, pByte, pCCBReader);
}
}
void CCSpriteLoader::onHandlePropTypeBlendFunc(CCNode * pNode, CCNode * pParent, std::string pPropertyName, ccBlendFunc pCCBlendFunc, CCBReader * pCCBReader) {
if(pPropertyName.compare(PROPERTY_BLENDFUNC) == 0) {
((CCSprite *)pNode)->setBlendFunc(pCCBlendFunc);
} else {
CCNodeLoader::onHandlePropTypeBlendFunc(pNode, pParent, pPropertyName, pCCBlendFunc, pCCBReader);
}
}

View File

@ -0,0 +1,24 @@
#ifndef _CCSPRITE_LOADER_H_
#define _CCSPRITE_LOADER_H_
#include "CCNodeLoader.h"
NS_CC_EXT_BEGIN
/* Forward declaration. */
class CCBReader;
class CCSpriteLoader : public CCNodeLoader {
protected:
virtual cocos2d::CCSprite * createCCNode(cocos2d::CCNode *, CCBReader *);
virtual void onHandlePropTypeColor3(cocos2d::CCNode *, cocos2d::CCNode *, std::string, cocos2d::ccColor3B, CCBReader *);
virtual void onHandlePropTypeByte(CCNode *, CCNode *, std::string, unsigned char, CCBReader *);
virtual void onHandlePropTypeBlendFunc(CCNode *, CCNode *, std::string, ccBlendFunc, CCBReader *);
virtual void onHandlePropTypeSpriteFrame(CCNode *, CCNode *, std::string, CCSpriteFrame *, CCBReader *);
virtual void onHandlePropTypeFlip(CCNode *, CCNode *, std::string, bool *, CCBReader *);
};
NS_CC_EXT_END
#endif