• <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又過于麻煩。花點時間,增加了一個基于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
            久久人人添人人爽添人人片牛牛| 久久精品国产乱子伦| 国产亚洲成人久久| 国产一区二区三精品久久久无广告 | 亚洲AV无码1区2区久久| 国产情侣久久久久aⅴ免费| 久久精品国产久精国产| 日本国产精品久久| 精品久久久久久亚洲精品| 国产激情久久久久影院老熟女免费 | 久久久久综合国产欧美一区二区| 久久国产AVJUST麻豆| 国产午夜久久影院| 久久午夜免费视频| 91久久福利国产成人精品| 亚洲AV无码一区东京热久久| 久久er国产精品免费观看8| 久久婷婷五月综合色高清| 久久人人爽人人爽人人片AV东京热| 人妻少妇久久中文字幕 | 99久久精品影院老鸭窝| 伊人久久精品影院| 精品国产一区二区三区久久蜜臀| 亚洲国产精品无码久久久秋霞2 | 国产一区二区三区久久| 久久久久久曰本AV免费免费| 国产成人精品久久综合| 国产精品禁18久久久夂久| 亚洲国产成人精品无码久久久久久综合| 久久精品国产精品亚洲毛片| 久久婷婷国产剧情内射白浆| 久久男人中文字幕资源站| 91精品日韩人妻无码久久不卡| 久久综合综合久久综合| 久久久久久精品成人免费图片| 久久久久国色AV免费观看| 国产国产成人久久精品| 丰满少妇人妻久久久久久4| 久久精品国产精品青草| 大香网伊人久久综合网2020| 久久综合狠狠综合久久激情 |