2014-03-04 16:51:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
#include "TextReader.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
#include "ui/UIText.h"
|
2014-06-05 10:25:08 +08:00
|
|
|
#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
|
|
|
|
{
|
2014-06-19 17:04:14 +08:00
|
|
|
static const char* P_TouchScaleEnable = "touchScaleEnable";
|
|
|
|
static const char* P_Text = "text";
|
|
|
|
static const char* P_FontSize = "fontSize";
|
|
|
|
static const char* P_FontName = "fontName";
|
|
|
|
static const char* P_AreaWidth = "areaWidth";
|
|
|
|
static const char* P_AreaHeight = "areaHeight";
|
|
|
|
static const char* P_HAlignment = "hAlignment";
|
|
|
|
static const char* P_VAlignment = "vAlignment";
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
static TextReader* instanceTextReader = nullptr;
|
2014-03-04 16:51:35 +08:00
|
|
|
|
|
|
|
IMPLEMENT_CLASS_WIDGET_READER_INFO(TextReader)
|
|
|
|
|
|
|
|
TextReader::TextReader()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TextReader::~TextReader()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TextReader* TextReader::getInstance()
|
|
|
|
{
|
|
|
|
if (!instanceTextReader)
|
|
|
|
{
|
|
|
|
instanceTextReader = new TextReader();
|
|
|
|
}
|
|
|
|
return instanceTextReader;
|
|
|
|
}
|
|
|
|
|
2014-06-23 10:02:09 +08:00
|
|
|
void TextReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
|
2014-06-05 10:25:08 +08:00
|
|
|
{
|
2014-06-11 09:35:24 +08:00
|
|
|
this->beginSetBasicProperties(widget);
|
2014-06-05 10:25:08 +08:00
|
|
|
|
2014-07-01 16:31:17 +08:00
|
|
|
stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
|
2014-06-05 10:25:08 +08:00
|
|
|
|
|
|
|
Text* label = static_cast<Text*>(widget);
|
2014-07-02 10:08:04 +08:00
|
|
|
|
2014-07-07 15:22:14 +08:00
|
|
|
std::string binaryFilePath = GUIReader::getInstance()->getFilePath();
|
2014-07-02 10:08:04 +08:00
|
|
|
|
2014-06-05 10:25:08 +08:00
|
|
|
|
2014-06-23 10:02:09 +08:00
|
|
|
for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
|
|
|
|
std::string key = stChildArray[i].GetName(cocoLoader);
|
2014-07-01 16:31:17 +08:00
|
|
|
std::string value = stChildArray[i].GetValue(cocoLoader);
|
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 17:04:14 +08:00
|
|
|
|
|
|
|
else if (key == P_TouchScaleEnable) {
|
2014-06-05 10:25:08 +08:00
|
|
|
label->setTouchScaleChangeEnabled(valueToBool(value));
|
|
|
|
}
|
|
|
|
|
2014-06-19 17:04:14 +08:00
|
|
|
else if(key == P_Text){
|
2014-06-05 10:25:08 +08:00
|
|
|
label->setString(value);
|
2014-06-19 17:04:14 +08:00
|
|
|
}else if(key == P_FontSize){
|
2014-06-05 10:25:08 +08:00
|
|
|
label->setFontSize(valueToInt(value));
|
2014-06-19 17:04:14 +08:00
|
|
|
}else if(key == P_FontName){
|
2014-07-04 11:53:06 +08:00
|
|
|
std::string fontFilePath;
|
2014-07-07 15:22:14 +08:00
|
|
|
fontFilePath = binaryFilePath.append(value);
|
2014-07-02 10:08:04 +08:00
|
|
|
label->setFontName(fontFilePath);
|
2014-06-19 17:04:14 +08:00
|
|
|
}else if(key == P_AreaWidth){
|
2014-06-05 10:25:08 +08:00
|
|
|
label->setTextAreaSize(Size(valueToFloat(value), label->getTextAreaSize().height));
|
2014-06-19 17:04:14 +08:00
|
|
|
}else if(key == P_AreaHeight){
|
2014-06-05 10:25:08 +08:00
|
|
|
label->setTextAreaSize(Size(label->getTextAreaSize().width, valueToFloat(value)));
|
2014-06-19 17:04:14 +08:00
|
|
|
}else if(key == P_HAlignment){
|
2014-06-05 10:25:08 +08:00
|
|
|
label->setTextHorizontalAlignment((TextHAlignment)valueToInt(value));
|
2014-06-19 17:04:14 +08:00
|
|
|
}else if(key == P_VAlignment){
|
2014-06-05 10:25:08 +08:00
|
|
|
label->setTextVerticalAlignment((TextVAlignment)valueToInt(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
} //end of for loop
|
2014-06-11 09:35:24 +08:00
|
|
|
this->endSetBasicProperties(widget);
|
2014-06-05 10:25:08 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2014-06-19 17:04:14 +08:00
|
|
|
bool touchScaleChangeAble = DICTOOL->getBooleanValue_json(options, P_TouchScaleEnable);
|
2014-03-04 16:51:35 +08:00
|
|
|
label->setTouchScaleChangeEnabled(touchScaleChangeAble);
|
2014-07-01 10:57:12 +08:00
|
|
|
const char* text = DICTOOL->getStringValue_json(options, P_Text,"Text Label");
|
2014-05-14 15:26:14 +08:00
|
|
|
label->setString(text);
|
2014-07-01 10:57:12 +08:00
|
|
|
|
|
|
|
label->setFontSize(DICTOOL->getIntValue_json(options, P_FontSize,20));
|
|
|
|
|
|
|
|
std::string fontName = DICTOOL->getStringValue_json(options, P_FontName, "微软雅黑");
|
2014-07-10 19:29:39 +08:00
|
|
|
|
2014-07-01 10:57:12 +08:00
|
|
|
std::string fontFilePath = jsonPath.append(fontName);
|
2014-07-09 16:38:26 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(fontFilePath))
|
|
|
|
{
|
|
|
|
label->setFontName(fontFilePath);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
label->setFontName(fontName);
|
|
|
|
}
|
2014-07-01 10:57:12 +08:00
|
|
|
|
2014-06-19 17:04:14 +08:00
|
|
|
bool aw = DICTOOL->checkObjectExist_json(options, P_AreaWidth);
|
|
|
|
bool ah = DICTOOL->checkObjectExist_json(options, P_AreaHeight);
|
2014-03-04 16:51:35 +08:00
|
|
|
if (aw && ah)
|
|
|
|
{
|
2014-06-19 17:04:14 +08:00
|
|
|
Size size = Size(DICTOOL->getFloatValue_json(options, P_AreaWidth),DICTOOL->getFloatValue_json(options,P_AreaHeight));
|
2014-03-04 16:51:35 +08:00
|
|
|
label->setTextAreaSize(size);
|
|
|
|
}
|
2014-06-19 17:04:14 +08:00
|
|
|
bool ha = DICTOOL->checkObjectExist_json(options, P_HAlignment);
|
2014-03-04 16:51:35 +08:00
|
|
|
if (ha)
|
|
|
|
{
|
2014-06-19 17:04:14 +08:00
|
|
|
label->setTextHorizontalAlignment((TextHAlignment)DICTOOL->getIntValue_json(options, P_HAlignment));
|
2014-03-04 16:51:35 +08:00
|
|
|
}
|
2014-06-19 17:04:14 +08:00
|
|
|
bool va = DICTOOL->checkObjectExist_json(options, P_VAlignment);
|
2014-03-04 16:51:35 +08:00
|
|
|
if (va)
|
|
|
|
{
|
2014-06-19 17:04:14 +08:00
|
|
|
label->setTextVerticalAlignment((TextVAlignment)DICTOOL->getIntValue_json(options, P_VAlignment));
|
2014-03-04 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
|
|
|
|
}
|
|
|
|
}
|