axmol/extensions/cocostudio/WidgetReader/RichTextReader/RichTextReader.cpp

103 lines
2.7 KiB
C++
Raw Normal View History

2019-11-24 23:15:56 +08:00
#include "WidgetReader/RichTextReader/RichTextReader.h"
2019-11-24 23:15:56 +08:00
#include "2d/CCFontAtlasCache.h"
#include "ui/UIRichText.h"
#include "platform/CCFileUtils.h"
#include "CocoLoader.h"
#include "CSParseBinary_generated.h"
#include "LocalizationManager.h"
2019-11-24 23:15:56 +08:00
#include "flatbuffers/flatbuffers.h"
USING_NS_AX;
2019-11-24 23:15:56 +08:00
using namespace ui;
using namespace flatbuffers;
namespace cocostudio
{
2021-12-25 10:04:45 +08:00
static RichTextReader* instanceTextBMFontReader = nullptr;
IMPLEMENT_CLASS_NODE_READER_INFO(RichTextReader)
RichTextReader::RichTextReader() {}
RichTextReader::~RichTextReader() {}
RichTextReader* RichTextReader::getInstance()
{
if (!instanceTextBMFontReader)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
instanceTextBMFontReader = new RichTextReader();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return instanceTextBMFontReader;
}
void RichTextReader::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(instanceTextBMFontReader);
2021-12-25 10:04:45 +08:00
}
Offset<Table> RichTextReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
flatbuffers::FlatBufferBuilder* builder)
{
auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
std::string xmlDefinition = "RichText";
// bool isLocalized = false;
// attributes
auto attribute = objectData.first_attribute();
while (attribute)
2019-11-24 23:15:56 +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 == "XmlDefinition")
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
xmlDefinition = value;
break;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
/*else if (name == "IsLocalized")
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
isLocalized = (value == "True") ? true : false;
}*/
attribute = attribute.next_attribute();
}
auto options = CreateRichTextOptions(*builder,
2019-11-24 23:15:56 +08:00
widgetOptions,
builder->CreateString(xmlDefinition)/*,
isLocalized*/);
2021-12-25 10:04:45 +08:00
return *(Offset<Table>*)(&options);
}
2022-08-08 18:02:17 +08:00
void RichTextReader::setPropsWithFlatBuffers(ax::Node* node, const flatbuffers::Table* richTextOptions)
2021-12-25 10:04:45 +08:00
{
auto ppnode = (Node**)(node);
auto options = (RichTextOptions*)richTextOptions;
auto implNode = ui::RichText::createWithXML(options->xmlDefinition()->c_str());
*ppnode = implNode;
auto widgetReader = WidgetReader::getInstance();
widgetReader->setPropsWithFlatBuffers(implNode, (Table*)options->widgetOptions());
// implNode->ignoreContentAdaptWithSize(true);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
Node* RichTextReader::createNodeWithFlatBuffers(const flatbuffers::Table* options)
{
Node* node = nullptr;
// #Hack
setPropsWithFlatBuffers((Node*)(&node), (Table*)options);
return node;
}
} // namespace cocostudio