使用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,參考我的开源码

[cocos2dx笔记012]一定简易的UI配置类的更多相关文章

  1. [cocos2dx笔记008]cocos2d 用luabridge手动绑定类

    基于cocos2dx 2.2.2版本号.这几天使用了cocostudio实现了,动画.骨骼动画.UI编辑.粒子效果,尽管有些不足,但已经算是很好了.今天尝试用lua.这个很easy.创建的时候.设置语 ...

  2. [cocos2dx笔记004] android添加�静态库project

    (基于2.2.2版本号) 这步比較麻烦,不像vs2013那样好设置.參考了非常多文章.还是不能解决这个问题.仅仅有慢慢摸索了.在这里贴出来,希望能让大家能少走点弯路. 还是其于前面的文章提到的myli ...

  3. firefox 扩展开发笔记(三):高级ui交互编程

    firefox 扩展开发笔记(三):高级ui交互编程 前言 前两篇链接 1:firefox 扩展开发笔记(一):jpm 使用实践以及调试 2:firefox 扩展开发笔记(二):进阶开发之移动设备模拟 ...

  4. Linux协议栈代码阅读笔记(二)网络接口的配置

    Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...

  5. Git 笔记二-Git安装与初始配置

    git 笔记二-Git安装与初始配置 Git的安装 由于我日常生活和工作基本上都是在Windows上,因此此处只说windows上的安装.Windows上的安装和其他程序一样,只需要到http://g ...

  6. Java程序猿JavaScript学习笔记(14——扩大jQuery UI)

    计划和完成这个例子中,音符的顺序如下: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScr ...

  7. 《软件测试自动化之道》读书笔记 之 基于反射的UI测试

    <软件测试自动化之道>读书笔记 之 基于反射的UI测试 2014-09-24 测试自动化程序的任务待测程序测试程序  启动待测程序  设置窗体的属性  获取窗体的属性  设置控件的属性  ...

  8. 《软件测试自动化之道》读书笔记 之 基于Windows的UI测试

    <软件测试自动化之道>读书笔记 之 基于Windows的UI测试 2014-09-25 测试自动化程序的任务待测程序测试程序  启动待测程序  获得待测程序主窗体的句柄  获得有名字控件的 ...

  9. 《软件测试自动化之道》读书笔记 之 底层的Web UI 测试

    <软件测试自动化之道>读书笔记 之 底层的Web UI 测试 2014-09-28 测试自动化程序的任务待测程序测试程序  启动IE并连接到这个实例  如何判断待测web程序完全加载到浏览 ...

随机推荐

  1. python-day01 pip 在线安装,标识符规则,注释,变量名,类型

    1.python第三方库安装: 在线安装:pip install 库名 pip install 库名 -i 国内源网站地址 离线安装:xxx.tar.gz/rar/zip 解压安装 2.标识符规则: ...

  2. Winform 异步调用

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. Nginx报错-找不到路径

    前言 最近在git bash里输入命令启动Nginx服务,总提示找不到路径,令我困惑不已        我反复检查安装路径和输入命令,确认无误    小技巧:复制路径可直接ctrl+c后在git ba ...

  4. 自己写的_top、_parent以及对iframe和frameset的理解

    iframe可以写在<body>标签里,如<body><iframe src="xxx"  name="xxx" style=&q ...

  5. ios -使用NSLayoutConstraint实现多个view等宽等高等间距

    @interface ViewController () { UIView *firstView; UIView *secondView; UIView *thirdView; } @end @imp ...

  6. 基于CXF搭建webService

    1.导入相关jar包,具体哪些包我记不太清了 2.在applicationContext中加入相关配置信息,如下所示: <beans xmlns="http://www.springf ...

  7. android 国际化 横屏(land) 竖屏(port)margin外边距和padding内边距

    android 国际化 横屏(land) 竖屏(port) 边距又分为内边距和外边距,即margin和padding.

  8. MatLab之HDL coder

    1 Workflow The workflow for applying HDL code generation to the hardware design process requires the ...

  9. Arduino 操作共阴极RGB LED

    一.原理图 注:电阻选用1k的 二. 实物图 三.完整事例代码,三种颜色不停的交替闪烁 实验结果自己运行观察

  10. C# MVC 获得程序运行路径

    string filePath = System.Web.HttpContext.Current.Request.MapPath("~/Upload"); //由虚拟路径指定的服务 ...