axmol/extensions/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.cpp

256 lines
7.2 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
#include "WidgetReader/TextBMFontReader/TextBMFontReader.h"
2019-11-23 20:27:39 +08:00
#include "2d/FontAtlasCache.h"
2019-11-23 20:27:39 +08:00
#include "ui/UITextBMFont.h"
#include "platform/FileUtils.h"
#include "CocoLoader.h"
#include "CSParseBinary_generated.h"
#include "LocalizationManager.h"
2019-11-23 20:27:39 +08:00
#include "flatbuffers/flatbuffers.h"
USING_NS_AX;
2019-11-23 20:27:39 +08:00
using namespace ui;
using namespace flatbuffers;
namespace cocostudio
{
2021-12-25 10:04:45 +08:00
static const char* P_FileNameData = "fileNameData";
static const char* P_Text = "text";
static TextBMFontReader* instanceTextBMFontReader = nullptr;
IMPLEMENT_CLASS_NODE_READER_INFO(TextBMFontReader)
TextBMFontReader::TextBMFontReader() {}
TextBMFontReader::~TextBMFontReader() {}
TextBMFontReader* TextBMFontReader::getInstance()
{
if (!instanceTextBMFontReader)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
instanceTextBMFontReader = new TextBMFontReader();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
return instanceTextBMFontReader;
}
void TextBMFontReader::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(instanceTextBMFontReader);
2021-12-25 10:04:45 +08:00
}
2022-08-08 18:02:17 +08:00
void TextBMFontReader::setPropsFromBinary(ax::ui::Widget* widget, CocoLoader* cocoLoader, stExpCocoNode* cocoNode)
2021-12-25 10:04:45 +08:00
{
this->beginSetBasicProperties(widget);
TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget);
stExpCocoNode* stChildArray = cocoNode->GetChildArray(cocoLoader);
for (int i = 0; i < cocoNode->GetChildNum(); ++i)
2019-11-23 20:27:39 +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
2022-07-16 10:43:05 +08:00
AX_BASIC_PROPERTY_BINARY_READER
2021-12-25 10:04:45 +08:00
// read all color related properties of widget
2022-07-16 10:43:05 +08:00
AX_COLOR_PROPERTY_BINARY_READER
2021-12-25 10:04:45 +08:00
else if (key == P_FileNameData)
2019-11-23 20:27:39 +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);
std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
if (imageFileNameType == (Widget::TextureResType)0)
{
labelBMFont->setFntFile(backgroundValue);
}
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (key == P_Text) { labelBMFont->setString(value); }
} // end of for loop
this->endSetBasicProperties(widget);
}
void TextBMFontReader::setPropsFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
std::string_view jsonPath = GUIReader::getInstance()->getFilePath();
2021-12-25 10:04:45 +08:00
TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget);
const rapidjson::Value& cmftDic = DICTOOL->getSubDictionary_json(options, P_FileNameData);
int cmfType = DICTOOL->getIntValue_json(cmftDic, P_ResourceType);
switch (cmfType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case 0:
2019-11-23 20:27:39 +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();
labelBMFont->setFntFile(cmf_tp);
break;
}
case 1:
2022-07-16 10:43:05 +08:00
AXLOG("Wrong res type of LabelAtlas!");
2021-12-25 10:04:45 +08:00
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
const char* text = DICTOOL->getStringValue_json(options, P_Text, "Text Label");
labelBMFont->setString(text);
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
}
Offset<Table> TextBMFontReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
flatbuffers::FlatBufferBuilder* builder)
{
auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
std::string text = "Fnt Text Label";
bool isLocalized = false;
std::string path;
std::string plistFlie;
int resourceType = 0;
// attributes
auto attribute = objectData.first_attribute();
while (attribute)
2019-11-23 20:27:39 +08:00
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2021-12-25 10:04:45 +08:00
if (name == "LabelText")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
text = value;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "IsLocalized")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
isLocalized = (value == "True") ? true : false;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
}
// child elements
auto child = objectData.first_child();
while (child)
{
2021-12-31 15:49:45 +08:00
std::string_view name = child.name();
2021-12-25 10:04:45 +08:00
if (name == "LabelBMFontFile_CNB")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
attribute = child.first_attribute();
while (attribute)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2021-12-25 10:04:45 +08:00
if (name == "Path")
{
path = value;
}
else if (name == "Type")
{
resourceType = 0;
}
else if (name == "Plist")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
plistFlie = value;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
child = child.next_sibling();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
auto options = CreateTextBMFontOptions(
*builder, widgetOptions,
CreateResourceData(*builder, builder->CreateString(path), builder->CreateString(plistFlie), resourceType),
builder->CreateString(text), isLocalized);
return *(Offset<Table>*)(&options);
}
2022-08-08 18:02:17 +08:00
void TextBMFontReader::setPropsWithFlatBuffers(ax::Node* node, const flatbuffers::Table* textBMFontOptions)
2021-12-25 10:04:45 +08:00
{
TextBMFont* labelBMFont = static_cast<TextBMFont*>(node);
auto options = (TextBMFontOptions*)textBMFontOptions;
auto cmftDic = (options->fileNameData());
bool fileExist = false;
std::string errorFilePath;
std::string errorContent;
std::string path = cmftDic->path()->c_str();
int cmfType = cmftDic->resourceType();
switch (cmfType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
case 0:
{
if (FileUtils::getInstance()->isFileExist(path))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
FontAtlas* newAtlas = FontAtlasCache::getFontAtlasFNT(path);
if (newAtlas)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
fileExist = true;
}
else
{
errorContent = "has problem";
fileExist = false;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
break;
}
default:
break;
}
if (fileExist)
{
labelBMFont->setFntFile(path);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
std::string text = options->text()->c_str();
bool isLocalized = options->isLocalized() != 0;
if (isLocalized)
{
ILocalizationManager* lm = LocalizationHelper::getCurrentManager();
labelBMFont->setString(lm->getLocalizationString(text));
}
else
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
labelBMFont->setString(text);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
auto widgetReader = WidgetReader::getInstance();
widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
labelBMFont->ignoreContentAdaptWithSize(true);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
Node* TextBMFontReader::createNodeWithFlatBuffers(const flatbuffers::Table* textBMFontOptions)
{
TextBMFont* textBMFont = TextBMFont::create();
setPropsWithFlatBuffers(textBMFont, (Table*)textBMFontOptions);
return textBMFont;
}
} // namespace cocostudio