• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            專職C++

            不能停止的腳步

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              163 Posts :: 7 Stories :: 135 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(28)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            本文地址:http://m.shnenglu.com/zdhsoft/archive/2014/07/23/207760.html
            使用cocostudio可以裝載編輯好的UI,但是過于復雜。特別是在加截UI后,發現觸屏事件有些問題。如果直接使用程序寫死加載UI又過于麻煩?;c時間,增加了一個基于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,參考我的開源代碼
            posted on 2014-07-23 20:04 冬瓜 閱讀(2102) 評論(0)  編輯 收藏 引用 所屬分類: 原創 、cocos2dx
            亚洲国产成人久久综合一| 久久乐国产综合亚洲精品| 999久久久无码国产精品| 国产精品欧美久久久天天影视| 99久久综合国产精品二区| 亚洲精品99久久久久中文字幕| 久久天天躁狠狠躁夜夜躁2O2O| 99久久国产热无码精品免费久久久久 | 一本大道久久东京热无码AV | 欧美日韩精品久久久免费观看| 久久亚洲AV无码精品色午夜| 青青草国产精品久久| 久久99精品国产麻豆宅宅| 国产高清国内精品福利99久久| 国产成年无码久久久免费| 久久久久久亚洲精品无码| 国内精品久久久久久久97牛牛| 久久久高清免费视频| 777久久精品一区二区三区无码| 亚洲va中文字幕无码久久不卡| 久久天天躁狠狠躁夜夜2020 | 午夜精品久久久久久久| 无码任你躁久久久久久| 国产精品成人99久久久久91gav | 夜夜亚洲天天久久| 久久这里只有精品首页| 久久精品草草草| 久久精品国产99国产精品澳门| 国内精品久久久久| 欧美亚洲国产精品久久蜜芽| 精品久久久久久中文字幕人妻最新| 人妻无码精品久久亚瑟影视 | 久久国产欧美日韩精品| 亚洲国产一成久久精品国产成人综合| 久久精品成人| 久久福利资源国产精品999| 色天使久久综合网天天| 久久久国产打桩机| 亚洲级αV无码毛片久久精品| 国产69精品久久久久9999APGF| 99久久99久久精品国产片果冻|