From 615995af542e744b2985e0c5cb90be9be46276cc Mon Sep 17 00:00:00 2001 From: andyque Date: Wed, 18 Jun 2014 14:15:11 +0800 Subject: [PATCH] add base property parser for WidgetReader --- .../cocostudio/WidgetReader/WidgetReader.cpp | 100 ++++++++++++++++++ .../cocostudio/WidgetReader/WidgetReader.h | 5 +- 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp index 04c6c8eae2..24a325671b 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp +++ b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.cpp @@ -292,6 +292,106 @@ namespace cocostudio } } + void WidgetReader::setPropsFromBinary(cocos2d::ui::Widget *widget, cocostudio::CocoLoader *pCocoLoader, cocostudio::stExpCocoNode *pCocoNode) + { + stExpCocoNode *stChildArray = pCocoNode->GetChildArray(); + + + for (int i = 0; i < pCocoNode->GetChildNum(); ++i) { + std::string key = stChildArray[i].GetName(pCocoLoader); + std::string value = stChildArray[i].GetValue(); + + if (key == "ignoreSize") { + widget->ignoreContentAdaptWithSize(valueToBool(value)); + }else if(key == "sizeType"){ + widget->setSizeType((Widget::SizeType)valueToInt(value)); + }else if(key == "positionType"){ + widget->setPositionType((Widget::PositionType)valueToInt(value)); + }else if(key == "sizePercentX"){ + widget->setSizePercent(Vec2(valueToFloat(value), widget->getSizePercent().y)); + }else if(key == "sizePercentY"){ + widget->setSizePercent(Vec2(widget->getSizePercent().x, valueToFloat(value))); + }else if(key == "positionPercentX"){ + widget->setPositionPercent(Vec2(valueToFloat(value), widget->getPositionPercent().y)); + }else if(key == "positionPercentY"){ + widget->setPositionPercent(Vec2(widget->getPositionPercent().x, valueToFloat(value))); + }else if (key == "width"){ + widget->setSize(Size(valueToFloat(value), widget->getSize().height)); + }else if(key == "height"){ + widget->setSize(Size(widget->getSize().width, valueToFloat(value))); + }else if(key == "tag"){ + widget->setTag(valueToInt(value)); + }else if(key == "actiontag"){ + widget->setActionTag(valueToInt(value)); + }else if(key == "touchAble"){ + widget->setTouchEnabled(valueToBool(value)); + }else if(key == "name"){ + std::string widgetName = value.empty() ? "default" : value; + widget->setName(widgetName); + }else if(key == "x"){ + widget->setPosition(Vec2(valueToFloat(value), widget->getPosition().y)); + }else if(key == "y"){ + widget->setPosition(Vec2(widget->getPosition().x, valueToFloat(value))); + }else if(key == "scaleX"){ + widget->setScaleX(valueToFloat(value)); + }else if(key == "scaleY"){ + widget->setScaleY(valueToFloat(value)); + }else if(key == "rotation"){ + widget->setRotation(valueToFloat(value)); + }else if(key == "visible"){ + widget->setVisible(valueToBool(value)); + }else if(key == "ZOrder"){ + widget->setZOrder(valueToInt(value)); + }else if(key == "layoutParameter"){ + stExpCocoNode *layoutCocosNode = stChildArray[i].GetChildArray(); + + ui::LinearLayoutParameter *linearParameter = ui::LinearLayoutParameter::create(); + ui::RelativeLayoutParameter *relativeParameter = ui::RelativeLayoutParameter::create(); + ui::Margin mg; + + int paramType = -1; + for (int j = 0; j < stChildArray[i].GetChildNum(); ++j) { + std::string innerKey = layoutCocosNode[j].GetName(pCocoLoader); + std::string innerValue = layoutCocosNode[j].GetValue(); + + if (innerKey == "type") { + paramType = valueToInt(innerValue); + }else if(innerKey == "gravity"){ + linearParameter->setGravity((cocos2d::ui::LinearLayoutParameter::LinearGravity)valueToInt(innerValue)); + }else if(innerKey == "relativeName"){ + relativeParameter->setRelativeName(innerValue); + }else if(innerKey == "relativeToName"){ + relativeParameter->setRelativeToWidgetName(innerValue); + }else if(innerKey == "align"){ + relativeParameter->setAlign((cocos2d::ui::RelativeLayoutParameter::RelativeAlign)valueToInt(innerValue)); + }else if(innerKey == "marginLeft"){ + mg.left = valueToFloat(innerValue); + }else if(innerKey == "marginTop"){ + mg.top = valueToFloat(innerValue); + }else if(innerKey == "marginRight"){ + mg.right = valueToFloat(innerValue); + }else if(innerKey == "marginDown"){ + mg.bottom = valueToFloat(innerValue); + } + } + + linearParameter->setMargin(mg); + relativeParameter->setMargin(mg); + + switch (paramType) { + case 1: + widget->setLayoutParameter(linearParameter); + break; + case 2: + widget->setLayoutParameter(relativeParameter); + default: + break; + } + } + + } + } + } diff --git a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.h b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.h index 08c779f383..0dee7d533e 100644 --- a/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.h +++ b/cocos/editor-support/cocostudio/WidgetReader/WidgetReader.h @@ -54,10 +54,7 @@ namespace cocostudio virtual void setColorPropsFromJsonDictionary(cocos2d::ui::Widget* widget, const rapidjson::Value& options); - virtual void setPropsFromBinary(cocos2d::ui::Widget* widget, CocoLoader* pCocoLoader, stExpCocoNode* pCocoNode) - { - - }; + virtual void setPropsFromBinary(cocos2d::ui::Widget* widget, CocoLoader* pCocoLoader, stExpCocoNode* pCocoNode); protected: std::string getResourcePath(const rapidjson::Value& dict, const std::string& key,