axmol/extensions/libccs30/WidgetReader/TextAtlasReader/TextAtlasReader.cpp

305 lines
11 KiB
C++
Raw Normal View History

2014-03-04 16:51:35 +08:00
2020-10-17 16:32:16 +08:00
#include "libccs30/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"
2020-10-17 16:32:16 +08:00
#include "libeditor/CocoLoader.h"
#include "libccs30/CSParseBinary_generated.h"
#include "libccs30/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
{
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";
2015-10-09 16:59:11 +08:00
static TextAtlasReader* instanceTextAtlasReader = nullptr;
2014-03-04 16:51:35 +08:00
2014-11-21 15:15:38 +08:00
IMPLEMENT_CLASS_NODE_READER_INFO(TextAtlasReader)
2014-03-04 16:51:35 +08:00
TextAtlasReader::TextAtlasReader()
{
}
TextAtlasReader::~TextAtlasReader()
{
}
TextAtlasReader* TextAtlasReader::getInstance()
{
2015-10-09 16:59:11 +08:00
if (!instanceTextAtlasReader)
2014-03-04 16:51:35 +08:00
{
2015-10-09 16:59:11 +08:00
instanceTextAtlasReader = new (std::nothrow) TextAtlasReader();
2014-03-04 16:51:35 +08:00
}
2015-10-09 16:59:11 +08:00
return instanceTextAtlasReader;
2014-03-04 16:51:35 +08:00
}
2015-03-30 16:46:33 +08:00
void TextAtlasReader::destroyInstance()
{
2015-10-09 16:59:11 +08:00
CC_SAFE_DELETE(instanceTextAtlasReader);
2015-03-30 16:46:33 +08:00
}
2014-06-23 10:02:09 +08:00
void TextAtlasReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
{
2014-06-11 09:35:24 +08:00
this->beginSetBasicProperties(widget);
TextAtlas* labelAtlas = static_cast<TextAtlas*>(widget);
2014-07-01 16:31:17 +08:00
stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
2019-11-24 23:15:56 +08:00
//Widget::TextureResType type;
//std::string charMapFileName;
std::string stringValue;
std::string startCharMap;
float itemWidth;
float itemHeight;
2020-08-04 10:55:30 +08:00
int resourceType = 0;
std::string charMapFile;
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_StringValue) {
stringValue = value;
}
else if(key == P_CharMapFileData){
2014-07-01 16:31:17 +08:00
stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
2016-09-12 09:45:34 +08:00
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);
}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);
}
2014-06-11 09:35:24 +08:00
this->endSetBasicProperties(widget);
}
2014-03-04 16:51:35 +08:00
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);
2014-07-01 14:01:27 +08:00
// 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)
2014-03-04 16:51:35 +08:00
{
2014-07-01 14:01:27 +08:00
case 0:
2014-03-04 16:51:35 +08:00
{
2014-07-01 14:01:27 +08:00
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;
2014-03-04 16:51:35 +08:00
}
2014-07-01 14:01:27 +08:00
case 1:
CCLOG("Wrong res type of LabelAtlas!");
break;
default:
break;
2014-03-04 16:51:35 +08:00
}
2014-07-01 14:01:27 +08:00
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
2019-11-24 23:15:56 +08:00
Offset<Table> TextAtlasReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
2014-11-21 15:15:38 +08:00
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);
2014-10-09 18:28:09 +08:00
2020-08-04 10:55:30 +08:00
std::string path;
std::string plistFile;
2014-10-09 18:28:09 +08:00
int resourceType = 0;
2014-11-21 15:15:38 +08:00
std::string stringValue = "0123456789";
int itemWidth = 0;
int itemHeight = 0;
2020-08-04 10:55:30 +08:00
std::string startCharMap;
2014-10-09 18:28:09 +08:00
// attributes
2019-11-24 23:15:56 +08:00
auto attribute = objectData.first_attribute();
2014-10-09 18:28:09 +08:00
while (attribute)
{
2019-11-24 23:15:56 +08:00
std::string name = attribute.name();
std::string value = attribute.value();
2014-10-09 18:28:09 +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")
{
2014-11-21 15:15:38 +08:00
startCharMap = value;
2014-10-09 18:28:09 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-10-09 18:28:09 +08:00
}
// child elements
2019-11-24 23:15:56 +08:00
auto child = objectData.first_child();
2014-10-09 18:28:09 +08:00
while (child)
{
2019-11-24 23:15:56 +08:00
std::string name = child.name();
2014-10-09 18:28:09 +08:00
if (name == "LabelAtlasFileImage_CNB")
{
2020-08-04 10:55:30 +08:00
std::string texture;
std::string texturePng;
2014-11-21 15:15:38 +08:00
2019-11-24 23:15:56 +08:00
attribute = child.first_attribute();
2014-10-09 18:28:09 +08:00
while (attribute)
{
2019-11-24 23:15:56 +08:00
name = attribute.name();
std::string value = attribute.value();
2014-10-09 18:28:09 +08:00
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;
2014-11-21 15:15:38 +08:00
texture = value;
2014-10-09 18:28:09 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-10-09 18:28:09 +08:00
}
}
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-10-09 18:28:09 +08:00
}
2014-11-21 15:15:38 +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();
2014-11-21 15:15:38 +08:00
switch (cmfType)
{
case 0:
{
2020-08-04 10:55:30 +08:00
std::string cmfPath = cmftDic->path()->c_str();
2015-03-24 15:59:21 +08:00
bool fileExist = false;
2020-08-04 10:55:30 +08:00
std::string errorFilePath;
2015-03-24 15:59:21 +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;
}
2014-11-21 15:15:38 +08:00
break;
}
2015-03-24 15:59:21 +08:00
2014-11-21 15:15:38 +08:00
case 1:
CCLOG("Wrong res type of LabelAtlas!");
break;
2015-03-24 15:59:21 +08:00
2014-11-21 15:15:38 +08:00
default:
break;
}
auto widgetReader = WidgetReader::getInstance();
widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
labelAtlas->ignoreContentAdaptWithSize(true);
2014-11-21 15:15:38 +08:00
}
Node* TextAtlasReader::createNodeWithFlatBuffers(const flatbuffers::Table *textAtlasOptions)
2014-11-21 15:15:38 +08:00
{
2020-08-04 00:14:35 +08:00
TextAtlas* textAtlas = TextAtlas::create();
2014-11-21 15:15:38 +08:00
setPropsWithFlatBuffers(textAtlas, (Table*)textAtlasOptions);
2014-10-09 18:28:09 +08:00
2014-11-21 15:15:38 +08:00
return textAtlas;
2014-10-09 18:28:09 +08:00
}
2014-03-04 16:51:35 +08:00
}