fixed #1639: Added custom property support for CCBReader.

This commit is contained in:
James Chen 2013-01-21 18:37:17 +08:00
parent 7aa1c0152e
commit c0d568da9b
8 changed files with 175 additions and 38 deletions

View File

@ -2,6 +2,7 @@
#define _CCB_CCBMEMBERVARIABLEASSIGNER_H_
#include "cocos2d.h"
#include "CCBValue.h"
NS_CC_EXT_BEGIN
@ -17,11 +18,30 @@ NS_CC_EXT_BEGIN
return true; \
}
class CCBMemberVariableAssigner {
public:
virtual ~CCBMemberVariableAssigner() {};
/**
* The callback function of assigning member variable.
* @note The member variable must be CCNode or its subclass.
* @param pTarget The custom class.
* @param pMemberVariableName The name of the member variable.
* @param pNode The member variable.
* @return Whether the assignment was successful.
*/
virtual bool onAssignCCBMemberVariable(CCObject* pTarget, const char* pMemberVariableName, CCNode* pNode) = 0;
/**
* The callback function of assigning custom properties.
* @note The member variable must be CCNode or its subclass.
* @param pTarget The custom class.
* @param pMemberVariableName The name of the member variable.
* @param pValue The value of the property.
* @return Whether the assignment was successful.
*/
virtual bool onAssignCCBCustomProperty(CCObject* pTarget, const char* pMemberVariableName, CCBValue* pCCBValue) { return false; };
};
NS_CC_EXT_END

View File

@ -635,32 +635,39 @@ CCNode * CCBReader::readNodeGraph(CCNode * pParent) {
[[JSCocoa sharedController] setObject:node withName:memberVarAssignmentName];
}*/
#else
if(memberVarAssignmentType != kCCBTargetTypeNone) {
if(!jsControlled) {
CCObject * target = NULL;
if(memberVarAssignmentType == kCCBTargetTypeDocumentRoot)
if (memberVarAssignmentType != kCCBTargetTypeNone)
{
if(!jsControlled)
{
target = mActionManager->getRootNode();
}
else if(memberVarAssignmentType == kCCBTargetTypeOwner)
{
target = this->mOwner;
}
if(target != NULL) {
bool assigned = false;
CCBMemberVariableAssigner * targetAsCCBMemberVariableAssigner = dynamic_cast<CCBMemberVariableAssigner *>(target);
if(targetAsCCBMemberVariableAssigner != NULL) {
assigned = targetAsCCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node);
CCObject * target = NULL;
if(memberVarAssignmentType == kCCBTargetTypeDocumentRoot)
{
target = mActionManager->getRootNode();
}
else if(memberVarAssignmentType == kCCBTargetTypeOwner)
{
target = this->mOwner;
}
if(!assigned && this->mCCBMemberVariableAssigner != NULL) {
this->mCCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node);
if(target != NULL)
{
CCBMemberVariableAssigner * targetAsCCBMemberVariableAssigner = dynamic_cast<CCBMemberVariableAssigner *>(target);
bool assigned = false;
if (memberVarAssignmentType != kCCBTargetTypeNone)
{
if(targetAsCCBMemberVariableAssigner != NULL) {
assigned = targetAsCCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node);
}
if(!assigned && this->mCCBMemberVariableAssigner != NULL) {
assigned = this->mCCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node);
}
}
}
}
} else {
else
{
if(memberVarAssignmentType == kCCBTargetTypeDocumentRoot) {
mActionManager->addDocumentOutletName(memberVarAssignmentName);
mActionManager->addDocumentOutletNode(node);
@ -670,6 +677,36 @@ CCNode * CCBReader::readNodeGraph(CCNode * pParent) {
}
}
}
// Assign custom properties.
if (ccNodeLoader->getCustomProperties()->count() > 0) {
bool customAssigned = false;
if(!jsControlled)
{
CCObject * target = mActionManager->getRootNode();
if(target != NULL)
{
CCBMemberVariableAssigner * targetAsCCBMemberVariableAssigner = dynamic_cast<CCBMemberVariableAssigner *>(target);
if(targetAsCCBMemberVariableAssigner != NULL) {
CCDictionary* pCustomPropeties = ccNodeLoader->getCustomProperties();
CCDictElement* pElement;
CCDICT_FOREACH(pCustomPropeties, pElement)
{
customAssigned = targetAsCCBMemberVariableAssigner->onAssignCCBCustomProperty(target, pElement->getStrKey(), (CCBValue*)pElement->getObject());
if(!customAssigned && this->mCCBMemberVariableAssigner != NULL)
{
customAssigned = this->mCCBMemberVariableAssigner->onAssignCCBCustomProperty(target, pElement->getStrKey(), (CCBValue*)pElement->getObject());
}
}
}
}
}
}
#endif // CCB_ENABLE_JAVASCRIPT
delete mAnimatedProps;

View File

@ -80,13 +80,13 @@ CCBValue* CCBValue::create(unsigned char byte)
return ret;
}
CCBValue* CCBValue::create(const void *pPointer)
CCBValue* CCBValue::create(const char *pStringValue)
{
CCBValue *ret = new CCBValue();
if (ret)
{
ret->mValue.pointer = pPointer;
ret->mType = kPointerValue;
ret->m_strValue = pStringValue;
ret->mType = kStringValue;
ret->autorelease();
}
@ -121,11 +121,16 @@ unsigned char CCBValue::getByteValue()
return (unsigned char)(mValue.nValue);
}
const void* CCBValue::getPointer()
const char* CCBValue::getStringValue()
{
assert(mType == kPointerValue);
assert(mType == kStringValue);
return mValue.pointer;
return m_strValue.c_str();
}
int CCBValue::getType()
{
return mType;
}
NS_CC_EXT_END

View File

@ -25,9 +25,9 @@ enum
{
kIntValue,
kFloatValue,
kPointerValue,
kBoolValue,
kUnsignedCharValue,
kStringValue
};
class CCBValue : public CCObject
@ -37,9 +37,9 @@ private:
{
int nValue;
float fValue;
const void *pointer;
} mValue;
std::string m_strValue;
int mType;
public:
@ -47,13 +47,15 @@ public:
static CCBValue* create(bool bValue);
static CCBValue* create(float fValue);
static CCBValue* create(unsigned char byte);
static CCBValue* create(const void *pPointer);
static CCBValue* create(const char* pStr);
int getIntValue();
float getFloatValue();
bool getBoolValue();
unsigned char getByteValue();
const void* getPointer();
const char* getStringValue();
int getType();
};
NS_CC_EXT_END

View File

@ -9,6 +9,21 @@ using namespace std;
NS_CC_EXT_BEGIN
CCNodeLoader::CCNodeLoader()
{
m_pCustomProperties = new CCDictionary();
}
CCNodeLoader::~CCNodeLoader()
{
CC_SAFE_RELEASE(m_pCustomProperties);
}
CCDictionary* CCNodeLoader::getCustomProperties()
{
return m_pCustomProperties;
}
CCNode * CCNodeLoader::loadCCNode(CCNode * pParent, CCBReader * pCCBReader) {
CCNode * ccNode = this->createCCNode(pParent, pCCBReader);
@ -408,8 +423,12 @@ CCSize CCNodeLoader::parsePropTypeSize(CCNode * pNode, CCNode * pParent, CCBRead
width *= resolutionScale;
height *= resolutionScale;
break;
}
default:
{
CCLog("Unknown CCB type.");
}
break;
}
@ -900,7 +919,9 @@ void CCNodeLoader::onHandlePropTypeScaleLock(CCNode * pNode, CCNode * pParent, c
}
void CCNodeLoader::onHandlePropTypeFloat(CCNode * pNode, CCNode * pParent, const char* pPropertyName, float pFloat, CCBReader * pCCBReader) {
ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
// ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
// It may be a custom property, add it to custom property dictionary.
m_pCustomProperties->setObject(CCBValue::create(pFloat), pPropertyName);
}
void CCNodeLoader::onHandlePropTypeDegrees(CCNode * pNode, CCNode * pParent, const char* pPropertyName, float pDegrees, CCBReader * pCCBReader) {
@ -919,7 +940,9 @@ void CCNodeLoader::onHandlePropTypeInteger(CCNode * pNode, CCNode * pParent, con
if(strcmp(pPropertyName, PROPERTY_TAG) == 0) {
pNode->setTag(pInteger);
} else {
ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
// ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
// It may be a custom property, add it to custom property dictionary.
m_pCustomProperties->setObject(CCBValue::create(pInteger), pPropertyName);
}
}
@ -937,7 +960,9 @@ void CCNodeLoader::onHandlePropTypeCheck(CCNode * pNode, CCNode * pParent, const
} else if(strcmp(pPropertyName, PROPERTY_IGNOREANCHORPOINTFORPOSITION) == 0) {
pNode->ignoreAnchorPointForPosition(pCheck);
} else {
ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
//ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
// It may be a custom property, add it to custom property dictionary.
m_pCustomProperties->setObject(CCBValue::create(pCheck), pPropertyName);
}
}
@ -978,7 +1003,9 @@ void CCNodeLoader::onHandlePropTypeFntFile(CCNode * pNode, CCNode * pParent, con
}
void CCNodeLoader::onHandlePropTypeString(CCNode * pNode, CCNode * pParent, const char* pPropertyName, const char * pString, CCBReader * pCCBReader) {
ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
// ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
// It may be a custom property, add it to custom property dictionary.
m_pCustomProperties->setObject(CCBValue::create(pString), pPropertyName);
}
void CCNodeLoader::onHandlePropTypeText(CCNode * pNode, CCNode * pParent, const char* pPropertyName, const char * pText, CCBReader * pCCBReader) {

View File

@ -4,6 +4,7 @@
#include "../GUI/CCControlExtension/CCInvocation.h"
#include "cocos2d.h"
#include "CCBReader.h"
#include "CCBValue.h"
NS_CC_EXT_BEGIN
@ -41,12 +42,14 @@ class CCBReader;
class CCNodeLoader : public CCObject {
public:
virtual ~CCNodeLoader() {};
CCNodeLoader();
virtual ~CCNodeLoader();
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(CCNodeLoader, loader);
virtual CCNode * loadCCNode(CCNode *, CCBReader * pCCBReader);
virtual void parseProperties(CCNode * pNode, CCNode * pParent, CCBReader * pCCBReader);
virtual CCDictionary* getCustomProperties();
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(CCNode);
@ -106,6 +109,9 @@ class CCNodeLoader : public CCObject {
virtual void onHandlePropTypeBlock(CCNode * pNode, CCNode * pParent, const char* pPropertyName, BlockData * pBlockData, CCBReader * pCCBReader);
virtual void onHandlePropTypeBlockCCControl(CCNode * pNode, CCNode * pParent, const char* pPropertyName, BlockCCControlData * pBlockCCControlData, CCBReader * pCCBReader);
virtual void onHandlePropTypeCCBFile(CCNode * pNode, CCNode * pParent, const char* pPropertyName, CCNode * pCCBFileNode, CCBReader * pCCBReader);
protected:
CCDictionary* m_pCustomProperties;
};
NS_CC_EXT_END

View File

@ -89,6 +89,40 @@ bool HelloCocosBuilderLayer::onAssignCCBMemberVariable(CCObject * pTarget, const
return false;
}
bool HelloCocosBuilderLayer::onAssignCCBCustomProperty(CCObject* pTarget, const char* pMemberVariableName, cocos2d::extension::CCBValue* pCCBValue)
{
bool bRet = false;
if (pTarget == this)
{
if (0 == strcmp(pMemberVariableName, "mCustomPropertyInt"))
{
this->mCustomPropertyInt = pCCBValue->getIntValue();
CCLog("mCustomPropertyInt = %d", mCustomPropertyInt);
bRet = true;
}
else if ( 0 == strcmp(pMemberVariableName, "mCustomPropertyFloat"))
{
this->mCustomPropertyFloat = pCCBValue->getFloatValue();
CCLog("mCustomPropertyFloat = %f", mCustomPropertyFloat);
bRet = true;
}
else if ( 0 == strcmp(pMemberVariableName, "mCustomPropertyBoolean"))
{
this->mCustomPropertyBoolean = pCCBValue->getBoolValue();
CCLog("mCustomPropertyBoolean = %d", mCustomPropertyBoolean);
bRet = true;
}
else if ( 0 == strcmp(pMemberVariableName, "mCustomPropertyString"))
{
this->mCustomPropertyString = pCCBValue->getStringValue();
CCLog("mCustomPropertyString = %s", mCustomPropertyString.c_str());
bRet = true;
}
}
return bRet;
}
void HelloCocosBuilderLayer::onMenuTestClicked(CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent) {
this->openTest("ccb/ccb/TestMenus.ccbi", "TestMenusLayer", MenuTestLayerLoader::loader());

View File

@ -31,6 +31,7 @@ class HelloCocosBuilderLayer
virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::CCObject * pTarget, const char * pSelectorName);
virtual cocos2d::extension::SEL_CCControlHandler onResolveCCBCCControlSelector(cocos2d::CCObject * pTarget, const char * pSelectorName);
virtual bool onAssignCCBMemberVariable(cocos2d::CCObject * pTarget, const char * pMemberVariableName, cocos2d::CCNode * pNode);
virtual bool onAssignCCBCustomProperty(CCObject* pTarget, const char* pMemberVariableName, cocos2d::extension::CCBValue* pCCBValue);
virtual void onNodeLoaded(cocos2d::CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader);
void onMenuTestClicked(cocos2d::CCObject * pSender, cocos2d::extension::CCControlEvent pCCControlEvent);
@ -43,6 +44,11 @@ class HelloCocosBuilderLayer
private:
cocos2d::CCSprite * mBurstSprite;
cocos2d::CCLabelTTF * mTestTitleLabelTTF;
int mCustomPropertyInt;
float mCustomPropertyFloat;
bool mCustomPropertyBoolean;
std::string mCustomPropertyString;
};
#endif