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

280 lines
8.5 KiB
C++
Raw Normal View History

2014-03-04 16:51:35 +08:00
#include "WidgetReader/TextAtlasReader/TextAtlasReader.h"
2014-11-21 15:15:38 +08:00
2014-03-11 17:13:54 +08:00
#include "ui/UITextAtlas.h"
2016-04-18 15:09:21 +08:00
#include "platform/CCFileUtils.h"
#include "CocoLoader.h"
#include "CSParseBinary_generated.h"
#include "FlatBuffersSerialize.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
{
2021-12-25 10:04:45 +08:00
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)
2014-03-04 16:51:35 +08:00
{
2021-12-25 10:04:45 +08:00
instanceTextAtlasReader = new TextAtlasReader();
2014-03-04 16:51:35 +08:00
}
2021-12-25 10:04:45 +08:00
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);
// Widget::TextureResType type;
// std::string charMapFileName;
std::string stringValue;
std::string startCharMap;
float itemWidth;
float itemHeight;
int resourceType = 0;
std::string charMapFile;
for (int i = 0; i < cocoNode->GetChildNum(); ++i)
2014-03-04 16:51:35 +08:00
{
2021-12-25 10:04:45 +08:00
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)
2014-03-04 16:51:35 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
std::string resType = backGroundChildren[2].GetValue(cocoLoader);
Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
charMapFile = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
2014-03-04 16:51:35 +08:00
}
2021-12-25 10:04:45 +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
if (resourceType == 0)
2015-03-30 16:46:33 +08:00
{
2021-12-25 10:04:45 +08:00
labelAtlas->setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap);
2015-03-30 16:46:33 +08:00
}
2021-12-25 10:04:45 +08:00
this->endSetBasicProperties(widget);
}
void TextAtlasReader::setPropsFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
2021-12-27 13:52:08 +08:00
std::string_view jsonPath = GUIReader::getInstance()->getFilePath();
2021-12-25 10:04:45 +08:00
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)
{
2021-12-25 10:04:45 +08:00
case 0:
{
2021-12-27 13:52:08 +08:00
std::string tp_c {jsonPath};
2021-12-25 10:04:45 +08:00
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;
}
2021-12-25 10:04:45 +08:00
case 1:
CCLOG("Wrong res type of LabelAtlas!");
break;
default:
break;
}
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
}
Offset<Table> TextAtlasReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
flatbuffers::FlatBufferBuilder* builder)
{
auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
std::string path;
std::string plistFile;
int resourceType = 0;
std::string stringValue = "0123456789";
int itemWidth = 0;
int itemHeight = 0;
std::string startCharMap;
// attributes
auto attribute = objectData.first_attribute();
while (attribute)
2014-03-04 16:51:35 +08:00
{
2021-12-25 10:04:45 +08:00
std::string name = attribute.name();
std::string value = attribute.value();
if (name == "LabelText")
2014-03-04 16:51:35 +08:00
{
2021-12-25 10:04:45 +08:00
stringValue = value;
2014-03-04 16:51:35 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "CharWidth")
2014-10-09 18:28:09 +08:00
{
2021-12-25 10:04:45 +08:00
itemWidth = atoi(value.c_str());
2014-10-09 18:28:09 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "CharHeight")
2014-10-09 18:28:09 +08:00
{
2021-12-25 10:04:45 +08:00
itemHeight = atoi(value.c_str());
}
else if (name == "StartChar")
{
startCharMap = value;
2014-10-09 18:28:09 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +08:00
// child elements
auto child = objectData.first_child();
while (child)
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
std::string name = child.name();
if (name == "LabelAtlasFileImage_CNB")
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
std::string texture;
std::string texturePng;
attribute = child.first_attribute();
while (attribute)
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
name = attribute.name();
std::string value = attribute.value();
if (name == "Path")
2015-03-24 15:59:21 +08:00
{
2021-12-25 10:04:45 +08:00
path = value;
2015-03-24 15:59:21 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Type")
2015-03-24 15:59:21 +08:00
{
2021-12-25 10:04:45 +08:00
resourceType = 0;
2015-03-24 15:59:21 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Plist")
{
plistFile = value;
texture = value;
}
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
}
2021-12-25 10:04:45 +08:00
child = child.next_sibling();
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +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;
auto cmftDic = (options->charMapFileData());
int cmfType = cmftDic->resourceType();
switch (cmfType)
{
case 0:
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
std::string cmfPath = cmftDic->path()->c_str();
bool fileExist = false;
std::string errorFilePath;
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;
2014-10-09 18:28:09 +08:00
}
2021-12-25 10:04:45 +08:00
case 1:
CCLOG("Wrong res type of LabelAtlas!");
break;
default:
break;
}
auto widgetReader = WidgetReader::getInstance();
widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
labelAtlas->ignoreContentAdaptWithSize(true);
2014-03-04 16:51:35 +08:00
}
2021-12-25 10:04:45 +08:00
Node* TextAtlasReader::createNodeWithFlatBuffers(const flatbuffers::Table* textAtlasOptions)
{
TextAtlas* textAtlas = TextAtlas::create();
setPropsWithFlatBuffers(textAtlas, (Table*)textAtlasOptions);
return textAtlas;
}
} // namespace cocostudio