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

127 lines
4.4 KiB
C++
Raw Normal View History

2014-03-04 16:51:35 +08:00
#include "TextReader.h"
2014-03-11 17:13:54 +08:00
#include "ui/UIText.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 TextReader* instanceTextReader = NULL;
IMPLEMENT_CLASS_WIDGET_READER_INFO(TextReader)
TextReader::TextReader()
{
}
TextReader::~TextReader()
{
}
TextReader* TextReader::getInstance()
{
if (!instanceTextReader)
{
instanceTextReader = new TextReader();
}
return instanceTextReader;
}
void TextReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *pCocoLoader, stExpCocoNode *pCocoNode)
{
2014-06-11 09:35:24 +08:00
this->beginSetBasicProperties(widget);
stExpCocoNode *stChildArray = pCocoNode->GetChildArray();
Text* label = static_cast<Text*>(widget);
for (int i = 0; i < pCocoNode->GetChildNum(); ++i) {
std::string key = stChildArray[i].GetName(pCocoLoader);
std::string value = stChildArray[i].GetValue();
2014-06-19 15:16:56 +08:00
//read all basic properties of widget
CC_BASIC_PROPERTY_BINARY_READER
//read all color related properties of widget
CC_COLOR_PROPERTY_BINARY_READER
2014-06-19 15:16:56 +08:00
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 == "touchScaleEnable") {
label->setTouchScaleChangeEnabled(valueToBool(value));
}
else if(key == "text"){
label->setString(value);
}else if(key == "fontSize"){
label->setFontSize(valueToInt(value));
}else if(key == "fontName"){
label->setFontName(value);
}else if(key == "areaWidth"){
label->setTextAreaSize(Size(valueToFloat(value), label->getTextAreaSize().height));
}else if(key == "areaHeight"){
label->setTextAreaSize(Size(label->getTextAreaSize().width, valueToFloat(value)));
}else if(key == "hAlignment"){
label->setTextHorizontalAlignment((TextHAlignment)valueToInt(value));
}else if(key == "vAlignment"){
label->setTextVerticalAlignment((TextVAlignment)valueToInt(value));
}
} //end of for loop
2014-06-11 09:35:24 +08:00
this->endSetBasicProperties(widget);
}
2014-03-04 16:51:35 +08:00
void TextReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
std::string jsonPath = GUIReader::getInstance()->getFilePath();
Text* label = static_cast<Text*>(widget);
bool touchScaleChangeAble = DICTOOL->getBooleanValue_json(options, "touchScaleEnable");
label->setTouchScaleChangeEnabled(touchScaleChangeAble);
const char* text = DICTOOL->getStringValue_json(options, "text");
2014-05-14 15:26:14 +08:00
label->setString(text);
2014-03-04 16:51:35 +08:00
bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
if (fs)
{
label->setFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
}
bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
if (fn)
{
2014-06-17 16:05:44 +08:00
std::string fontName = DICTOOL->getStringValue_json(options, "fontName");
std::string fontFilePath = jsonPath.append(fontName);
label->setFontName(fontFilePath);
2014-03-04 16:51:35 +08:00
}
bool aw = DICTOOL->checkObjectExist_json(options, "areaWidth");
bool ah = DICTOOL->checkObjectExist_json(options, "areaHeight");
if (aw && ah)
{
Size size = Size(DICTOOL->getFloatValue_json(options, "areaWidth"),DICTOOL->getFloatValue_json(options,"areaHeight"));
label->setTextAreaSize(size);
}
bool ha = DICTOOL->checkObjectExist_json(options, "hAlignment");
if (ha)
{
label->setTextHorizontalAlignment((TextHAlignment)DICTOOL->getIntValue_json(options, "hAlignment"));
}
bool va = DICTOOL->checkObjectExist_json(options, "vAlignment");
if (va)
{
label->setTextVerticalAlignment((TextVAlignment)DICTOOL->getIntValue_json(options, "vAlignment"));
}
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
}
}