axmol/cocos/editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.cpp

325 lines
14 KiB
C++
Raw Normal View History

2014-03-04 16:51:35 +08:00
#include "ButtonReader.h"
2014-03-11 17:13:54 +08:00
#include "ui/UIButton.h"
#include "cocostudio/CocoLoader.h"
2014-03-04 16:51:35 +08:00
2014-03-06 16:15:03 +08:00
USING_NS_CC;
using namespace ui;
2014-03-04 16:51:35 +08:00
namespace cocostudio
{
static ButtonReader* instanceButtonReader = NULL;
IMPLEMENT_CLASS_WIDGET_READER_INFO(ButtonReader)
ButtonReader::ButtonReader()
{
}
ButtonReader::~ButtonReader()
{
}
ButtonReader* ButtonReader::getInstance()
{
if (!instanceButtonReader)
{
instanceButtonReader = new ButtonReader();
}
return instanceButtonReader;
}
void ButtonReader::purge()
{
CC_SAFE_DELETE(instanceButtonReader);
}
void ButtonReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *pCocoLoader, stExpCocoNode *pCocoNode)
{
WidgetReader::setPropsFromBinary(widget, pCocoLoader, pCocoNode);
Button *button = static_cast<Button*>(widget);
stExpCocoNode *stChildArray = pCocoNode->GetChildArray();
2014-06-11 09:35:24 +08:00
this->beginSetBasicProperties(widget);
float capsx = 0.0f, capsy = 0.0, capsWidth = 0.0, capsHeight = 0.0f;
int cri = 255, cgi = 255, cbi = 255;
float scale9Width = 0.0f, scale9Height = 0.0f;
for (int i = 0; i < pCocoNode->GetChildNum(); ++i) {
std::string key = stChildArray[i].GetName(pCocoLoader);
std::string value = stChildArray[i].GetValue();
2014-06-15 16:12:24 +08:00
// CCLOG("Button: key = %s, value = %d", key.c_str(), i);
2014-06-11 09:35:24 +08:00
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"){
2014-06-18 14:44:00 +08:00
_sizePercentX = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}else if(key == "sizePercentY"){
2014-06-18 14:44:00 +08:00
_sizePercentY = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}else if(key == "positionPercentX"){
2014-06-18 14:44:00 +08:00
_positionPercentX = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}else if(key == "positionPercentY"){
2014-06-18 14:44:00 +08:00
_positionPercentY = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}
else if(key == "adaptScreen"){
2014-06-18 14:44:00 +08:00
_isAdaptScreen = valueToBool(value);
2014-06-11 09:35:24 +08:00
}
else if (key == "width"){
2014-06-18 14:44:00 +08:00
_width = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}else if(key == "height"){
2014-06-18 14:44:00 +08:00
_height = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}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"){
2014-06-18 14:44:00 +08:00
_position.x = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}else if(key == "y"){
2014-06-18 14:44:00 +08:00
_position.y = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}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();
LinearLayoutParameter *linearParameter = LinearLayoutParameter::create();
RelativeLayoutParameter *relativeParameter = RelativeLayoutParameter::create();
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;
}
}
2014-06-11 09:35:24 +08:00
else if (key == "opacity") {
2014-06-18 14:44:00 +08:00
_opacity = valueToInt(value);
2014-06-11 09:35:24 +08:00
}else if(key == "colorR"){
2014-06-18 14:44:00 +08:00
_color.r = valueToInt(value);
2014-06-11 09:35:24 +08:00
}else if(key == "colorG"){
2014-06-18 14:44:00 +08:00
_color.g = valueToInt(value);
2014-06-11 09:35:24 +08:00
}else if(key == "colorB")
{
2014-06-18 14:44:00 +08:00
_color.b = valueToInt(value);
2014-06-11 09:35:24 +08:00
}else if(key == "flipX"){
widget->setFlippedX(valueToBool(value));
}else if(key == "flipY"){
widget->setFlippedY(valueToBool(value));
}else if(key == "anchorPointX"){
2014-06-18 14:44:00 +08:00
_originalAnchorPoint.x = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}else if(key == "anchorPointY"){
2014-06-18 14:44:00 +08:00
_originalAnchorPoint.y = valueToFloat(value);
2014-06-11 09:35:24 +08:00
}
else if (key == "scale9Enable") {
button->setScale9Enabled(valueToBool(value));
}
else if (key == "normalData"){
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray();
std::string resType = backGroundChildren[2].GetValue();;
Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
std::string backgroundValue = this->getResourcePath(pCocoLoader, &stChildArray[i], imageFileNameType);
button->loadTextureNormal(backgroundValue, imageFileNameType);
}
else if (key == "pressedData"){
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray();
std::string resType = backGroundChildren[2].GetValue();;
Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
std::string backgroundValue = this->getResourcePath(pCocoLoader, &stChildArray[i], imageFileNameType);
button->loadTexturePressed(backgroundValue, imageFileNameType);
}
else if (key == "disabledData"){
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray();
std::string resType = backGroundChildren[2].GetValue();;
Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
std::string backgroundValue = this->getResourcePath(pCocoLoader, &stChildArray[i], imageFileNameType);
button->loadTextureDisabled(backgroundValue, imageFileNameType);
}else if (key == "text"){
button->setTitleText(value);
}
else if(key == "capInsetsX"){
capsx = valueToFloat(value);
}else if(key == "capInsetsY"){
capsy = valueToFloat(value);
}else if(key == "capInsetsWidth"){
capsWidth = valueToFloat(value);
}else if(key == "capInsetsHeight"){
capsHeight = valueToFloat(value);
}else if(key == "scale9Width"){
scale9Width = valueToFloat(value);
}else if(key == "scale9Height"){
scale9Height = valueToFloat(value);
}else if(key == "textColorR"){
cri = valueToInt(value);
}else if(key == "textColorG"){
cgi = valueToInt(value);
}else if(key == "textColorB"){
cbi = valueToInt(value);
}else if(key == "fontSize"){
button->setTitleFontSize(valueToFloat(value));
}else if(key == "fontName"){
button->setTitleFontName(value);
}
} //end of for loop
2014-06-11 09:35:24 +08:00
this->endSetBasicProperties(widget);
if (button->isScale9Enabled()) {
button->setCapInsets(Rect(capsx, capsy, capsWidth, capsHeight));
button->setSize(Size(scale9Width, scale9Height));
}
button->setTitleColor(Color3B(cri, cgi, cbi));
2014-06-11 09:35:24 +08:00
}
2014-03-04 16:51:35 +08:00
void ButtonReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
Button* button = static_cast<Button*>(widget);
bool scale9Enable = DICTOOL->getBooleanValue_json(options, "scale9Enable");
button->setScale9Enabled(scale9Enable);
2014-04-04 16:51:07 +08:00
2014-03-04 16:51:35 +08:00
const rapidjson::Value& normalDic = DICTOOL->getSubDictionary_json(options, "normalData");
int normalType = DICTOOL->getIntValue_json(normalDic, "resourceType");
std::string normalTexturePath = this->getResourcePath(normalDic, "path", (Widget::TextureResType)normalType);
button->loadTextureNormal(normalTexturePath, (Widget::TextureResType)normalType);
2014-04-04 16:51:07 +08:00
2014-03-04 16:51:35 +08:00
const rapidjson::Value& pressedDic = DICTOOL->getSubDictionary_json(options, "pressedData");
int pressedType = DICTOOL->getIntValue_json(pressedDic, "resourceType");
2014-04-04 16:51:07 +08:00
std::string pressedTexturePath = this->getResourcePath(pressedDic, "path", (Widget::TextureResType)pressedType);
button->loadTexturePressed(pressedTexturePath, (Widget::TextureResType)pressedType);
2014-04-04 16:51:07 +08:00
2014-03-04 16:51:35 +08:00
const rapidjson::Value& disabledDic = DICTOOL->getSubDictionary_json(options, "disabledData");
int disabledType = DICTOOL->getIntValue_json(disabledDic, "resourceType");
2014-04-04 16:51:07 +08:00
std::string disabledTexturePath = this->getResourcePath(disabledDic, "path", (Widget::TextureResType)disabledType);
button->loadTextureDisabled(disabledTexturePath, (Widget::TextureResType)disabledType);
2014-04-04 16:51:07 +08:00
2014-03-04 16:51:35 +08:00
if (scale9Enable)
{
float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
button->setCapInsets(Rect(cx, cy, cw, ch));
bool sw = DICTOOL->checkObjectExist_json(options, "scale9Width");
bool sh = DICTOOL->checkObjectExist_json(options, "scale9Height");
if (sw && sh)
{
float swf = DICTOOL->getFloatValue_json(options, "scale9Width");
float shf = DICTOOL->getFloatValue_json(options, "scale9Height");
button->setSize(Size(swf, shf));
}
}
bool tt = DICTOOL->checkObjectExist_json(options, "text");
if (tt)
{
const char* text = DICTOOL->getStringValue_json(options, "text");
if (text)
{
button->setTitleText(text);
}
}
bool cr = DICTOOL->checkObjectExist_json(options, "textColorR");
bool cg = DICTOOL->checkObjectExist_json(options, "textColorG");
bool cb = DICTOOL->checkObjectExist_json(options, "textColorB");
int cri = cr?DICTOOL->getIntValue_json(options, "textColorR"):255;
int cgi = cg?DICTOOL->getIntValue_json(options, "textColorG"):255;
int cbi = cb?DICTOOL->getIntValue_json(options, "textColorB"):255;
button->setTitleColor(Color3B(cri,cgi,cbi));
bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
if (fs)
{
button->setTitleFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
}
bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
if (fn)
{
button->setTitleFontName(DICTOOL->getStringValue_json(options, "fontName"));
}
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
}
}