使用cocostudio能够装载编辑好的UI,可是过于复杂。特别是在加截UI后,发现触屏事件有些问题。
假设直接使用程序写死载入UI又过于麻烦。花点时间,添加了一个基于ini的UI配置类,眼下仅仅实现了CCSprite和plist的载入。其他的能够后面慢慢加
头文件
#ifndef _X_UI_H_
#define _X_UI_H_
#include <cocos2d.h>
namespace zdh
{
USING_NS_CC;
void CreateByXUI(CCNode * paramParent, const char * paramFileName);
}
#endif
#define _X_UI_H_
#include <cocos2d.h>
namespace zdh
{
USING_NS_CC;
void CreateByXUI(CCNode * paramParent, const char * paramFileName);
}
#endif
源文件
#include "xui.h"
#include "xini.h"
#include "xlog.h"
namespace zdh
{
namespace xui
{
//--------------------------------------------------------------------------------------
int GetIntValue(XIniText::TSection * paramSection, const char * paramKeyName)
{
auto pV = paramSection->getEntry(paramKeyName);
if (isNULL(pV)) return 0;
else return pV->getValue().getField().ToIntDef(0);
}
//--------------------------------------------------------------------------------------
int GetDoubleValue(XIniText::TSection * paramSection, const char * paramKeyName)
{
auto pV = paramSection->getEntry(paramKeyName);
if (isNULL(pV)) return 0;
else return pV->getValue().getField().ToIntDef(0);
}
//--------------------------------------------------------------------------------------
const XAnsiString & GetStringValue(XIniText::TSection * paramSection, const char * paramKeyName)
{
static const XAnsiString strEmpty;
auto pV = paramSection->getEntry(paramKeyName);
if (isNULL(pV)) return strEmpty;
else return pV->getValue().getField();
}
};
//--------------------------------------------------------------------------------------
void CreateSpriteByXUI(CCNode * paramParent, XIniText::TSection * paramSpriteSection)
{
XInt ix = xui::GetIntValue(paramSpriteSection, "x");
XInt iy = xui::GetIntValue(paramSpriteSection, "y");
XInt izOrder = xui::GetIntValue(paramSpriteSection, "zOrder");
const XAnsiString & pImageName = xui::GetStringValue(paramSpriteSection, "image");
XInt iTag = xui::GetIntValue(paramSpriteSection, "tag");
CCSprite * pSprite = NULL;
if (pImageName[0] == ':') //假设是从Cache中读取
{
pSprite = CCSprite::createWithSpriteFrameName(pImageName.c_str()+1);
}
else
{
pSprite = CCSprite::create(pImageName.c_str());
}
pSprite->setPosition(ix, iy);
pSprite->setAnchorPoint(0, 0);
pSprite->setTag(iTag);
pSprite->setZOrder(izOrder);
paramParent->addChild(pSprite, izOrder);
}
void LoadSpriteFrameByPList(CCNode * /*paramParent*/, XIniText::TSection * paramSection)
{
const XAnsiString & pPListName = xui::GetStringValue(paramSection, "filename");
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(pPListName.c_str());
}
//--------------------------------------------------------------------------------------
void CreateByXUI(CCNode * paramParent, const char * paramFileName)
{
std::string strFullFileName = CCFileUtils::sharedFileUtils()->fullPathForFilename(paramFileName);
unsigned long dwGetSize = 0;
const unsigned char * pData = CCFileUtils::sharedFileUtils()->getFileData(strFullFileName.c_str(), "rb", &dwGetSize);
ZDH_INFO("Load XUI:%s size=%u", paramFileName, dwGetSize);
if (dwGetSize == 0)
{
if (isNotNULL(pData)) delete[] pData;
return;
}
std::string strData((const char *)pData, dwGetSize);
std::stringstream ss(strData);
XIniText stIni;
if (!stIni.Load(ss))
{
ZDH_INFO("Load XUI Fail, %s", paramFileName);
return;
}
for (int s = 0; s < stIni.getSectionCount(); s++)
{
auto pSection = stIni.getSection(s);
auto pType = pSection->getEntry("type");
if (isNULL(pType))
{
ZDH_INFO("Section=[%s] not exist key:\"type\"", pSection->getSectionName().c_str());
continue;
}
const XAnsiString & paramTypeValue = pType->getValue().getField();
if (paramTypeValue == "CCSprite")
{
CreateSpriteByXUI(paramParent, pSection);
}
else if (paramTypeValue == "plist")
{
LoadSpriteFrameByPList(paramParent, pSection);
}
}
}
}
#include "xini.h"
#include "xlog.h"
namespace zdh
{
namespace xui
{
//--------------------------------------------------------------------------------------
int GetIntValue(XIniText::TSection * paramSection, const char * paramKeyName)
{
auto pV = paramSection->getEntry(paramKeyName);
if (isNULL(pV)) return 0;
else return pV->getValue().getField().ToIntDef(0);
}
//--------------------------------------------------------------------------------------
int GetDoubleValue(XIniText::TSection * paramSection, const char * paramKeyName)
{
auto pV = paramSection->getEntry(paramKeyName);
if (isNULL(pV)) return 0;
else return pV->getValue().getField().ToIntDef(0);
}
//--------------------------------------------------------------------------------------
const XAnsiString & GetStringValue(XIniText::TSection * paramSection, const char * paramKeyName)
{
static const XAnsiString strEmpty;
auto pV = paramSection->getEntry(paramKeyName);
if (isNULL(pV)) return strEmpty;
else return pV->getValue().getField();
}
};
//--------------------------------------------------------------------------------------
void CreateSpriteByXUI(CCNode * paramParent, XIniText::TSection * paramSpriteSection)
{
XInt ix = xui::GetIntValue(paramSpriteSection, "x");
XInt iy = xui::GetIntValue(paramSpriteSection, "y");
XInt izOrder = xui::GetIntValue(paramSpriteSection, "zOrder");
const XAnsiString & pImageName = xui::GetStringValue(paramSpriteSection, "image");
XInt iTag = xui::GetIntValue(paramSpriteSection, "tag");
CCSprite * pSprite = NULL;
if (pImageName[0] == ':') //假设是从Cache中读取
{
pSprite = CCSprite::createWithSpriteFrameName(pImageName.c_str()+1);
}
else
{
pSprite = CCSprite::create(pImageName.c_str());
}
pSprite->setPosition(ix, iy);
pSprite->setAnchorPoint(0, 0);
pSprite->setTag(iTag);
pSprite->setZOrder(izOrder);
paramParent->addChild(pSprite, izOrder);
}
void LoadSpriteFrameByPList(CCNode * /*paramParent*/, XIniText::TSection * paramSection)
{
const XAnsiString & pPListName = xui::GetStringValue(paramSection, "filename");
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(pPListName.c_str());
}
//--------------------------------------------------------------------------------------
void CreateByXUI(CCNode * paramParent, const char * paramFileName)
{
std::string strFullFileName = CCFileUtils::sharedFileUtils()->fullPathForFilename(paramFileName);
unsigned long dwGetSize = 0;
const unsigned char * pData = CCFileUtils::sharedFileUtils()->getFileData(strFullFileName.c_str(), "rb", &dwGetSize);
ZDH_INFO("Load XUI:%s size=%u", paramFileName, dwGetSize);
if (dwGetSize == 0)
{
if (isNotNULL(pData)) delete[] pData;
return;
}
std::string strData((const char *)pData, dwGetSize);
std::stringstream ss(strData);
XIniText stIni;
if (!stIni.Load(ss))
{
ZDH_INFO("Load XUI Fail, %s", paramFileName);
return;
}
for (int s = 0; s < stIni.getSectionCount(); s++)
{
auto pSection = stIni.getSection(s);
auto pType = pSection->getEntry("type");
if (isNULL(pType))
{
ZDH_INFO("Section=[%s] not exist key:\"type\"", pSection->getSectionName().c_str());
continue;
}
const XAnsiString & paramTypeValue = pType->getValue().getField();
if (paramTypeValue == "CCSprite")
{
CreateSpriteByXUI(paramParent, pSection);
}
else if (paramTypeValue == "plist")
{
LoadSpriteFrameByPList(paramParent, pSection);
}
}
}
}
配置文件
#支持UTF-8格式
[gk_label.png]
type = CCSprite
image = gk_label.png
tag = 1
x = 18
y = 914
zOrder = 1
[mb_label.png]
type = CCSprite
image = :mb_label.png ·#冒号开头表示从CCSpriteFrameCache载入图片
tag = 1
x = 348
y = 916
zOrder = 1
[score_label.png]
type = CCSprite
image = score_label.png
tag = 1
x = 258
y = 855
zOrder = 1
[game_star.plist]
#批量装载
type = plist
filename = game_star.plist
[gk_label.png]
type = CCSprite
image = gk_label.png
tag = 1
x = 18
y = 914
zOrder = 1
[mb_label.png]
type = CCSprite
image = :mb_label.png ·#冒号开头表示从CCSpriteFrameCache载入图片
tag = 1
x = 348
y = 916
zOrder = 1
[score_label.png]
type = CCSprite
image = score_label.png
tag = 1
x = 258
y = 855
zOrder = 1
[game_star.plist]
#批量装载
type = plist
filename = game_star.plist
相关用到的TTextIni和XAnsiString,參考我的开源码