[cocos2dx笔记012]一定简易的UI配置类

19 篇文章 0 订阅
13 篇文章 1 订阅
使用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
源文件
#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);
            }
        }
    }
}
配置文件
#支持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
相关用到的TTextIni和XAnsiString,参考 我的开源代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值