axmol/extensions/cocostudio/WidgetReader/TextAtlasReader/TextAtlasReader.cpp

305 lines
11 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
2020-08-03 20:31:47 +08:00
#include "cocostudio/WidgetReader/TextAtlasReader/TextAtlasReader.h"
2019-11-23 20:27:39 +08:00
#include "ui/UITextAtlas.h"
#include "platform/CCFileUtils.h"
2020-08-03 20:31:47 +08:00
#include "cocostudio/CocoLoader.h"
#include "cocostudio/CSParseBinary_generated.h"
#include "cocostudio/FlatBuffersSerialize.h"
2019-11-23 20:27:39 +08:00
#include "flatbuffers/flatbuffers.h"
USING_NS_CC;
using namespace ui;
using namespace flatbuffers;
namespace cocostudio
{
static const char* P_StringValue = "stringValue";
static const char* P_CharMapFileData = "charMapFileData";
static const char* P_ItemWidth = "itemWidth";
static const char* P_ItemHeight = "itemHeight";
static const char* P_StartCharMap = "startCharMap";
static TextAtlasReader* instanceTextAtlasReader = nullptr;
IMPLEMENT_CLASS_NODE_READER_INFO(TextAtlasReader)
TextAtlasReader::TextAtlasReader()
{
}
TextAtlasReader::~TextAtlasReader()
{
}
TextAtlasReader* TextAtlasReader::getInstance()
{
if (!instanceTextAtlasReader)
{
instanceTextAtlasReader = new (std::nothrow) TextAtlasReader();
}
return instanceTextAtlasReader;
}
void TextAtlasReader::destroyInstance()
{
CC_SAFE_DELETE(instanceTextAtlasReader);
}
void TextAtlasReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
{
this->beginSetBasicProperties(widget);
TextAtlas* labelAtlas = static_cast<TextAtlas*>(widget);
stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
2019-11-24 23:15:56 +08:00
//Widget::TextureResType type;
//std::string charMapFileName;
2019-11-23 20:27:39 +08:00
std::string stringValue;
std::string startCharMap;
float itemWidth;
float itemHeight;
2020-08-04 10:55:30 +08:00
int resourceType = 0;
std::string charMapFile;
2019-11-23 20:27:39 +08:00
for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
std::string key = stChildArray[i].GetName(cocoLoader);
std::string value = stChildArray[i].GetValue(cocoLoader);
//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_StringValue) {
stringValue = value;
}
else if(key == P_CharMapFileData){
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
std::string resType = backGroundChildren[2].GetValue(cocoLoader);
Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
2020-08-04 10:55:30 +08:00
charMapFile = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
2019-11-23 20:27:39 +08:00
}else if(key == P_ItemWidth){
itemWidth = valueToFloat(value);
}else if(key == P_ItemHeight){
itemHeight = valueToFloat(value);
}else if(key == P_StartCharMap){
startCharMap = value;
}
} //end of for loop
2020-08-04 10:55:30 +08:00
if (resourceType == 0) {
labelAtlas->setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap);
2019-11-23 20:27:39 +08:00
}
this->endSetBasicProperties(widget);
}
void TextAtlasReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
std::string jsonPath = GUIReader::getInstance()->getFilePath();
TextAtlas* labelAtlas = static_cast<TextAtlas*>(widget);
// bool sv = DICTOOL->checkObjectExist_json(options, P_StringValue);
// bool cmf = DICTOOL->checkObjectExist_json(options, P_CharMapFile);
// bool iw = DICTOOL->checkObjectExist_json(options, P_ItemWidth);
// bool ih = DICTOOL->checkObjectExist_json(options, P_ItemHeight);
// bool scm = DICTOOL->checkObjectExist_json(options, P_StartCharMap);
const rapidjson::Value& cmftDic = DICTOOL->getSubDictionary_json(options, P_CharMapFileData);
int cmfType = DICTOOL->getIntValue_json(cmftDic, P_ResourceType);
switch (cmfType)
{
case 0:
{
std::string tp_c = jsonPath;
const char* cmfPath = DICTOOL->getStringValue_json(cmftDic, P_Path);
const char* cmf_tp = tp_c.append(cmfPath).c_str();
labelAtlas->setProperty(DICTOOL->getStringValue_json(options, P_StringValue,"12345678"),
cmf_tp,
DICTOOL->getIntValue_json(options, P_ItemWidth,24),
DICTOOL->getIntValue_json(options,P_ItemHeight,32),
DICTOOL->getStringValue_json(options, P_StartCharMap));
break;
}
case 1:
CCLOG("Wrong res type of LabelAtlas!");
break;
default:
break;
}
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
}
2019-11-24 23:15:56 +08:00
Offset<Table> TextAtlasReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
2019-11-23 20:27:39 +08:00
flatbuffers::FlatBufferBuilder *builder)
{
auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
2020-08-04 10:55:30 +08:00
std::string path;
std::string plistFile;
2019-11-23 20:27:39 +08:00
int resourceType = 0;
std::string stringValue = "0123456789";
int itemWidth = 0;
int itemHeight = 0;
2020-08-04 10:55:30 +08:00
std::string startCharMap;
2019-11-23 20:27:39 +08:00
// attributes
2019-11-24 23:15:56 +08:00
auto attribute = objectData.first_attribute();
2019-11-23 20:27:39 +08:00
while (attribute)
{
2019-11-24 23:15:56 +08:00
std::string name = attribute.name();
std::string value = attribute.value();
2019-11-23 20:27:39 +08:00
if (name == "LabelText")
{
stringValue = value;
}
else if (name == "CharWidth")
{
itemWidth = atoi(value.c_str());
}
else if (name == "CharHeight")
{
itemHeight = atoi(value.c_str());
}
else if (name == "StartChar")
{
startCharMap = value;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2019-11-23 20:27:39 +08:00
}
// child elements
2019-11-24 23:15:56 +08:00
auto child = objectData.first_child();
2019-11-23 20:27:39 +08:00
while (child)
{
2019-11-24 23:15:56 +08:00
std::string name = child.name();
2019-11-23 20:27:39 +08:00
if (name == "LabelAtlasFileImage_CNB")
{
2020-08-04 10:55:30 +08:00
std::string texture;
std::string texturePng;
2019-11-23 20:27:39 +08:00
2019-11-24 23:15:56 +08:00
attribute = child.first_attribute();
2019-11-23 20:27:39 +08:00
while (attribute)
{
2019-11-24 23:15:56 +08:00
name = attribute.name();
std::string value = attribute.value();
2019-11-23 20:27:39 +08:00
if (name == "Path")
{
path = value;
}
else if (name == "Type")
{
resourceType = 0;
}
else if (name == "Plist")
{
plistFile = value;
texture = value;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2019-11-23 20:27:39 +08:00
}
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2019-11-23 20:27:39 +08:00
}
auto options = CreateTextAtlasOptions(*builder,
widgetOptions,
CreateResourceData(*builder,
builder->CreateString(path),
builder->CreateString(plistFile),
resourceType),
builder->CreateString(stringValue),
builder->CreateString(startCharMap),
itemWidth,
itemHeight
);
return *(Offset<Table>*)(&options);
}
void TextAtlasReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textAtlasOptions)
{
TextAtlas* labelAtlas = static_cast<TextAtlas*>(node);
auto options = (TextAtlasOptions*)textAtlasOptions;
2020-08-04 10:55:30 +08:00
auto cmftDic = (options->charMapFileData());
int cmfType = cmftDic->resourceType();
2019-11-23 20:27:39 +08:00
switch (cmfType)
{
case 0:
{
2020-08-04 10:55:30 +08:00
std::string cmfPath = cmftDic->path()->c_str();
2019-11-23 20:27:39 +08:00
bool fileExist = false;
2020-08-04 10:55:30 +08:00
std::string errorFilePath;
2019-11-23 20:27:39 +08:00
if (FileUtils::getInstance()->isFileExist(cmfPath))
{
fileExist = true;
std::string stringValue = options->stringValue()->c_str();
int itemWidth = options->itemWidth();
int itemHeight = options->itemHeight();
labelAtlas->setProperty(stringValue,
cmfPath,
itemWidth,
itemHeight,
options->startCharMap()->c_str());
}
else
{
errorFilePath = cmfPath;
fileExist = false;
}
break;
}
case 1:
CCLOG("Wrong res type of LabelAtlas!");
break;
default:
break;
}
auto widgetReader = WidgetReader::getInstance();
widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
labelAtlas->ignoreContentAdaptWithSize(true);
}
Node* TextAtlasReader::createNodeWithFlatBuffers(const flatbuffers::Table *textAtlasOptions)
{
2020-08-04 00:14:35 +08:00
TextAtlas* textAtlas = TextAtlas::create();
2019-11-23 20:27:39 +08:00
setPropsWithFlatBuffers(textAtlas, (Table*)textAtlasOptions);
return textAtlas;
}
}