当前位置:
首页
文章
前端
详情

Lite2D UI编辑器1 解析字符串属性

负责解析字符串属性 然后输出对应的Node* 

数据协议可以用json

也可以用高性能的Google FlatBuffers

Node * FactoryCreater(string type,  string style )
{
    Node * ret = nullptr;

    if (type == "sprite")
    {
        auto ret = Sprite::create("shouzhi_dianji0.png");

        //解析器
        std::unordered_map<string,string> _map_arrt;

        static string NON = "";

        int cursor = 0;
        int las1 = 0, las2 = 0;
        style.append(",");
        for (int i = 0; i < style.size(); i++)
        {
            if (style[i] == ':')
            {
                las1 = i;
            }
            if (style[i] == ',')
            {
                las2 = i;
                string key =  style.substr(cursor, las1 - cursor);
                string value= style.substr(las1 + 1, las2 - las1 - 1);
                _map_arrt.insert(std::make_pair(key, value));
                cursor = las2 + 1;
            }

        }

        //解析完毕,初始化属性
        float x = atof(_map_arrt["x"].c_str());
        float y = atof(_map_arrt["y"].c_str());
        ret->setPosition(x,y);
        float scale = 1.0f;
        if (_map_arrt["scale"] != NON)
        {
            scale = atof(_map_arrt["scale"].c_str());
        }

         
        ret->setScale(scale);

        return ret;
    }


    if (type == "label")
    {

    }

    return nullptr;
}




// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    this->addChild(FactoryCreater("sprite" , "x:100,y:200,scale:0.1"));
    this->addChild(FactoryCreater("sprite", "x:200,y:10"));



    return true;
}

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。