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

501 lines
17 KiB
C++
Raw Normal View History

2014-03-04 16:51:35 +08:00
#include "TextReader.h"
2014-11-21 15:15:38 +08:00
2014-03-11 17:13:54 +08:00
#include "ui/UIText.h"
#include "cocostudio/CocoLoader.h"
2014-11-21 15:15:38 +08:00
#include "cocostudio/CSParseBinary_generated.h"
#include "tinyxml2.h"
2014-11-21 15:15:38 +08:00
#include "flatbuffers/flatbuffers.h"
2014-03-04 16:51:35 +08:00
2014-03-06 16:15:03 +08:00
USING_NS_CC;
using namespace ui;
2014-11-21 15:15:38 +08:00
using namespace flatbuffers;
2014-03-06 16:15:03 +08:00
2014-03-04 16:51:35 +08:00
namespace cocostudio
{
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";
static TextReader* instanceTextReader = nullptr;
2014-03-04 16:51:35 +08:00
2014-11-21 15:15:38 +08:00
IMPLEMENT_CLASS_NODE_READER_INFO(TextReader)
2014-03-04 16:51:35 +08:00
TextReader::TextReader()
{
}
TextReader::~TextReader()
{
}
TextReader* TextReader::getInstance()
{
if (!instanceTextReader)
{
instanceTextReader = new (std::nothrow) TextReader();
2014-03-04 16:51:35 +08:00
}
return instanceTextReader;
}
2015-03-30 16:46:33 +08:00
void TextReader::destroyInstance()
{
CC_SAFE_DELETE(instanceTextReader);
}
2014-06-23 10:02:09 +08:00
void TextReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
{
2014-06-11 09:35:24 +08:00
this->beginSetBasicProperties(widget);
2014-07-01 16:31:17 +08:00
stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
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-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
else if (key == P_TouchScaleEnable) {
label->setTouchScaleChangeEnabled(valueToBool(value));
}
else if(key == P_Text){
label->setString(value);
}else if(key == P_FontSize){
label->setFontSize(valueToInt(value));
}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);
if (FileUtils::getInstance()->isFileExist(fontFilePath)) {
label->setFontName(fontFilePath);
}else{
label->setFontName(value);
}
}else if(key == P_AreaWidth){
label->setTextAreaSize(Size(valueToFloat(value), label->getTextAreaSize().height));
}else if(key == P_AreaHeight){
label->setTextAreaSize(Size(label->getTextAreaSize().width, valueToFloat(value)));
}else if(key == P_HAlignment){
label->setTextHorizontalAlignment((TextHAlignment)valueToInt(value));
}else if(key == P_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, 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-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
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)
{
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);
}
bool ha = DICTOOL->checkObjectExist_json(options, P_HAlignment);
2014-03-04 16:51:35 +08:00
if (ha)
{
label->setTextHorizontalAlignment((TextHAlignment)DICTOOL->getIntValue_json(options, P_HAlignment));
2014-03-04 16:51:35 +08:00
}
bool va = DICTOOL->checkObjectExist_json(options, P_VAlignment);
2014-03-04 16:51:35 +08:00
if (va)
{
label->setTextVerticalAlignment((TextVAlignment)DICTOOL->getIntValue_json(options, P_VAlignment));
2014-03-04 16:51:35 +08:00
}
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
2014-12-01 12:46:29 +08:00
}
2014-10-09 18:28:09 +08:00
2014-11-21 15:15:38 +08:00
Offset<Table> TextReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
flatbuffers::FlatBufferBuilder *builder)
2014-10-09 18:28:09 +08:00
{
2014-11-21 15:15:38 +08:00
auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
bool touchScaleEnabled = false;
bool isCustomSize = false;
std::string fontName = "";
2014-11-21 15:15:38 +08:00
int fontSize = 20;
std::string text = "Text Label";
int areaWidth = 0;
int areaHeight = 0;
int h_alignment = 0;
int v_alignment = 0;
2015-04-08 16:54:58 +08:00
bool outlineEnabled = false;
Color4B outlineColor = Color4B::BLACK;
int outlineSize = 1;
bool shadowEnabled = false;
Color4B shadowColor = Color4B::BLACK;
Size shadowOffset = Size(2, -2);
int shadowBlurRadius = 0;
2014-11-21 15:15:38 +08:00
std::string path = "";
std::string plistFile = "";
int resourceType = 0;
2014-10-09 18:28:09 +08:00
// attributes
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
while (attribute)
{
std::string name = attribute->Name();
std::string value = attribute->Value();
if (name == "TouchScaleChangeAble")
{
2014-11-21 15:15:38 +08:00
touchScaleEnabled = (value == "True") ? true : false;
2014-10-09 18:28:09 +08:00
}
else if (name == "LabelText")
{
2014-11-21 15:15:38 +08:00
text = value;
2014-10-09 18:28:09 +08:00
}
else if (name == "FontSize")
{
2014-11-21 15:15:38 +08:00
fontSize = atoi(value.c_str());
2014-10-09 18:28:09 +08:00
}
else if (name == "FontName")
{
2014-11-21 15:15:38 +08:00
fontName = value;
2014-10-09 18:28:09 +08:00
}
else if (name == "AreaWidth")
{
areaWidth = atoi(value.c_str());
}
else if (name == "AreaHeight")
{
areaHeight = atoi(value.c_str());
}
else if (name == "HorizontalAlignmentType")
{
if (value == "HT_Left")
{
2014-11-21 15:15:38 +08:00
h_alignment = 0;
2014-10-09 18:28:09 +08:00
}
else if (value == "HT_Center")
{
2014-11-21 15:15:38 +08:00
h_alignment = 1;
2014-10-09 18:28:09 +08:00
}
else if (value == "HT_Right")
{
2014-11-21 15:15:38 +08:00
h_alignment = 2;
2014-10-09 18:28:09 +08:00
}
}
else if (name == "VerticalAlignmentType")
{
if (value == "VT_Top")
{
2014-11-21 15:15:38 +08:00
v_alignment = 0;
2014-10-09 18:28:09 +08:00
}
else if (value == "VT_Center")
{
2014-11-21 15:15:38 +08:00
v_alignment = 1;
2014-10-09 18:28:09 +08:00
}
else if (value == "VT_Bottom")
{
2014-11-21 15:15:38 +08:00
v_alignment = 2;
2014-10-09 18:28:09 +08:00
}
}
2014-11-21 15:15:38 +08:00
else if (name == "IsCustomSize")
2014-10-09 18:28:09 +08:00
{
2014-11-21 15:15:38 +08:00
isCustomSize = (value == "True") ? true : false;
2014-10-09 18:28:09 +08:00
}
2015-04-08 16:54:58 +08:00
else if (name == "OutlineEnabled")
{
outlineEnabled = (value == "True") ? true : false;
}
else if (name == "OutlineSize")
{
outlineSize = atoi(value.c_str());
}
else if (name == "ShadowEnabled")
{
shadowEnabled = (value == "True") ? true : false;
}
else if (name == "ShadowOffsetX")
{
shadowOffset.width = atof(value.c_str());
}
else if (name == "ShadowOffsetY")
{
shadowOffset.height = atof(value.c_str());
}
else if (name == "ShadowBlurRadius")
{
shadowBlurRadius = atoi(value.c_str());
}
2014-10-09 18:28:09 +08:00
attribute = attribute->Next();
}
// child elements
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
while (child)
{
std::string name = child->Name();
2014-11-21 15:15:38 +08:00
if (name == "FontResource")
2014-10-09 18:28:09 +08:00
{
2014-10-15 17:20:54 +08:00
attribute = child->FirstAttribute();
2014-10-09 18:28:09 +08:00
while (attribute)
{
2014-10-15 17:20:54 +08:00
name = attribute->Name();
2014-10-09 18:28:09 +08:00
std::string value = attribute->Value();
if (name == "Path")
{
path = value;
}
else if (name == "Type")
{
2014-11-21 15:15:38 +08:00
resourceType = 0;
2014-10-09 18:28:09 +08:00
}
else if (name == "Plist")
{
plistFile = value;
}
attribute = attribute->Next();
}
}
2015-04-08 16:54:58 +08:00
else if (name == "OutlineColor")
{
attribute = child->FirstAttribute();
while (attribute)
{
name = attribute->Name();
std::string value = attribute->Value();
if (name == "A")
{
outlineColor.a = atoi(value.c_str());
}
else if (name == "R")
{
outlineColor.r = atoi(value.c_str());
}
else if (name == "G")
{
outlineColor.g = atoi(value.c_str());
}
else if (name == "B")
{
outlineColor.b = atoi(value.c_str());
}
attribute = attribute->Next();
}
}
else if (name == "ShadowColor")
{
attribute = child->FirstAttribute();
while (attribute)
{
name = attribute->Name();
std::string value = attribute->Value();
if (name == "A")
{
shadowColor.a = atoi(value.c_str());
}
else if (name == "R")
{
shadowColor.r = atoi(value.c_str());
}
else if (name == "G")
{
shadowColor.g = atoi(value.c_str());
}
else if (name == "B")
{
shadowColor.b = atoi(value.c_str());
}
attribute = attribute->Next();
}
}
2014-10-09 18:28:09 +08:00
child = child->NextSiblingElement();
}
2015-04-08 16:54:58 +08:00
flatbuffers::Color f_outlineColor(outlineColor.a, outlineColor.r, outlineColor.g, outlineColor.b);
flatbuffers::Color f_shadowColor(shadowColor.a, shadowColor.r, shadowColor.g, shadowColor.b);
2014-11-21 15:15:38 +08:00
auto options = CreateTextOptions(*builder,
widgetOptions,
CreateResourceData(*builder,
builder->CreateString(path),
builder->CreateString(plistFile),
resourceType),
builder->CreateString(fontName),
fontSize,
builder->CreateString(text),
areaWidth,
areaHeight,
h_alignment,
v_alignment,
touchScaleEnabled,
2015-04-08 16:54:58 +08:00
isCustomSize,
outlineEnabled,
&f_outlineColor,
outlineSize,
shadowEnabled,
&f_shadowColor,
shadowOffset.width,
shadowOffset.height,
shadowBlurRadius);
2014-11-21 15:15:38 +08:00
return *(Offset<Table>*)(&options);
}
void TextReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textOptions)
{
Text* label = static_cast<Text*>(node);
auto options = (TextOptions*)textOptions;
bool touchScaleEnabled = options->touchScaleEnable() != 0;
2014-11-21 15:15:38 +08:00
label->setTouchScaleChangeEnabled(touchScaleEnabled);
2014-11-21 15:15:38 +08:00
std::string text = options->text()->c_str();
label->setString(text);
int fontSize = options->fontSize();
label->setFontSize(fontSize);
std::string fontName = options->fontName()->c_str();
label->setFontName(fontName);
Size areaSize = Size(options->areaWidth(), options->areaHeight());
if (!areaSize.equals(Size::ZERO))
2014-10-09 18:28:09 +08:00
{
2014-11-21 15:15:38 +08:00
label->setTextAreaSize(areaSize);
2014-10-09 18:28:09 +08:00
}
2014-11-21 15:15:38 +08:00
TextHAlignment h_alignment = (TextHAlignment)options->hAlignment();
label->setTextHorizontalAlignment(h_alignment);
TextVAlignment v_alignment = (TextVAlignment)options->vAlignment();
label->setTextVerticalAlignment((TextVAlignment)v_alignment);
bool fileExist = false;
std::string errorFilePath = "";
2014-11-21 15:15:38 +08:00
auto resourceData = options->fontResource();
std::string path = resourceData->path()->c_str();
if (path != "")
{
if (FileUtils::getInstance()->isFileExist(path))
{
fileExist = true;
}
else
{
errorFilePath = path;
fileExist = false;
}
if (fileExist)
{
label->setFontName(path);
}
else
{
auto alert = Label::create();
alert->setString(__String::createWithFormat("%s missed", errorFilePath.c_str())->getCString());
label->addChild(alert);
}
2014-11-21 15:15:38 +08:00
}
2015-04-08 16:54:58 +08:00
bool outlineEnabled = options->outlineEnabled();
if (outlineEnabled)
{
auto f_outlineColor = options->outlineColor();
if (f_outlineColor)
{
Color4B outlineColor(f_outlineColor->r(), f_outlineColor->g(), f_outlineColor->b(), f_outlineColor->a());
2015-04-08 16:54:58 +08:00
label->enableOutline(outlineColor, options->outlineSize());
}
}
bool shadowEnabled = options->shadowEnabled();
if (shadowEnabled)
{
auto f_shadowColor = options->shadowColor();
if (f_shadowColor)
{
Color4B shadowColor(f_shadowColor->r(), f_shadowColor->g(), f_shadowColor->b(), f_shadowColor->a());
2015-04-08 16:54:58 +08:00
label->enableShadow(shadowColor, Size(options->shadowOffsetX(), options->shadowOffsetY()), options->shadowBlurRadius());
}
}
2014-11-21 15:15:38 +08:00
auto widgetReader = WidgetReader::getInstance();
widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
2014-12-08 15:48:24 +08:00
label->setUnifySizeEnabled(false);
bool IsCustomSize = options->isCustomSize() != 0;
label->ignoreContentAdaptWithSize(!IsCustomSize);
auto widgetOptions = options->widgetOptions();
2014-12-08 15:48:24 +08:00
if (!label->isIgnoreContentAdaptWithSize())
{
Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
label->setContentSize(contentSize);
}
2014-11-21 15:15:38 +08:00
}
Node* TextReader::createNodeWithFlatBuffers(const flatbuffers::Table *textOptions)
2014-11-21 15:15:38 +08:00
{
Text* text = Text::create();
setPropsWithFlatBuffers(text, (Table*)textOptions);
2014-10-09 18:28:09 +08:00
2014-11-21 15:15:38 +08:00
return text;
2014-10-09 18:28:09 +08:00
}
2014-03-04 16:51:35 +08:00
}