2014-03-04 16:51:35 +08:00
|
|
|
|
|
|
|
|
2020-08-03 17:51:40 +08:00
|
|
|
#include "cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.h"
|
2014-11-21 15:15:38 +08:00
|
|
|
|
2014-12-17 19:00:24 +08:00
|
|
|
#include "2d/CCFontAtlasCache.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
#include "ui/UITextBMFont.h"
|
2016-04-18 15:09:21 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2020-08-03 17:51:40 +08:00
|
|
|
#include "cocostudio/CocoLoader.h"
|
|
|
|
#include "cocostudio/CSParseBinary_generated.h"
|
|
|
|
#include "cocostudio/LocalizationManager.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
|
|
|
|
{
|
2014-06-19 17:04:14 +08:00
|
|
|
static const char* P_FileNameData = "fileNameData";
|
|
|
|
static const char* P_Text = "text";
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
static TextBMFontReader* instanceTextBMFontReader = nullptr;
|
2014-03-04 16:51:35 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
IMPLEMENT_CLASS_NODE_READER_INFO(TextBMFontReader)
|
2014-03-04 16:51:35 +08:00
|
|
|
|
|
|
|
TextBMFontReader::TextBMFontReader()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TextBMFontReader::~TextBMFontReader()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TextBMFontReader* TextBMFontReader::getInstance()
|
|
|
|
{
|
|
|
|
if (!instanceTextBMFontReader)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
instanceTextBMFontReader = new (std::nothrow) TextBMFontReader();
|
2014-03-04 16:51:35 +08:00
|
|
|
}
|
|
|
|
return instanceTextBMFontReader;
|
|
|
|
}
|
|
|
|
|
2015-03-30 16:46:33 +08:00
|
|
|
void TextBMFontReader::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(instanceTextBMFontReader);
|
|
|
|
}
|
|
|
|
|
2014-06-23 10:02:09 +08:00
|
|
|
void TextBMFontReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
|
2014-06-05 10:25:08 +08:00
|
|
|
{
|
2014-06-11 09:35:24 +08:00
|
|
|
this->beginSetBasicProperties(widget);
|
2014-06-05 10:25:08 +08:00
|
|
|
|
|
|
|
TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget);
|
|
|
|
|
|
|
|
|
2014-07-01 16:31:17 +08:00
|
|
|
stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
|
2014-06-05 10:25:08 +08:00
|
|
|
|
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
|
2014-06-05 10:25:08 +08:00
|
|
|
|
2014-06-19 17:04:14 +08:00
|
|
|
else if(key == P_FileNameData){
|
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);
|
2014-06-05 10:25:08 +08:00
|
|
|
|
|
|
|
Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
|
|
|
|
|
2014-06-23 10:02:09 +08:00
|
|
|
std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
|
|
|
auto fileData = cocos2d::wext::makeResourceData(std::move(backgroundValue), (int)imageFileNameType);
|
|
|
|
cocos2d::wext::onBeforeLoadObjectAsset(labelBMFont, fileData, 0);
|
2014-06-05 10:25:08 +08:00
|
|
|
if (imageFileNameType == (Widget::TextureResType)0) {
|
2019-11-24 23:15:56 +08:00
|
|
|
labelBMFont->setFntFile(fileData.file);
|
2014-06-05 10:25:08 +08:00
|
|
|
}
|
|
|
|
|
2014-06-19 17:04:14 +08:00
|
|
|
}else if(key == P_Text){
|
2014-06-05 10:25:08 +08:00
|
|
|
labelBMFont->setString(value);
|
|
|
|
}
|
|
|
|
} //end of for loop
|
2014-06-11 09:35:24 +08:00
|
|
|
this->endSetBasicProperties(widget);
|
2014-06-05 10:25:08 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 16:51:35 +08:00
|
|
|
void TextBMFontReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
|
|
|
|
{
|
|
|
|
WidgetReader::setPropsFromJsonDictionary(widget, options);
|
|
|
|
|
|
|
|
|
|
|
|
std::string jsonPath = GUIReader::getInstance()->getFilePath();
|
|
|
|
|
|
|
|
TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget);
|
|
|
|
|
2014-06-19 17:04:14 +08:00
|
|
|
const rapidjson::Value& cmftDic = DICTOOL->getSubDictionary_json(options, P_FileNameData);
|
|
|
|
int cmfType = DICTOOL->getIntValue_json(cmftDic, P_ResourceType);
|
2014-03-04 16:51:35 +08:00
|
|
|
switch (cmfType)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
std::string tp_c = jsonPath;
|
2014-06-19 17:04:14 +08:00
|
|
|
const char* cmfPath = DICTOOL->getStringValue_json(cmftDic, P_Path);
|
2014-03-04 16:51:35 +08:00
|
|
|
const char* cmf_tp = tp_c.append(cmfPath).c_str();
|
|
|
|
labelBMFont->setFntFile(cmf_tp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
CCLOG("Wrong res type of LabelAtlas!");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-01 10:57:12 +08:00
|
|
|
const char* text = DICTOOL->getStringValue_json(options, P_Text,"Text Label");
|
2014-05-14 16:02:31 +08:00
|
|
|
labelBMFont->setString(text);
|
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> TextBMFontReader::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
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
std::string text = "Fnt Text Label";
|
2016-02-29 18:00:14 +08:00
|
|
|
bool isLocalized = false;
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
std::string path = "";
|
|
|
|
std::string plistFlie = "";
|
|
|
|
int resourceType = 0;
|
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")
|
|
|
|
{
|
|
|
|
text = value;
|
|
|
|
}
|
2016-02-29 18:00:14 +08:00
|
|
|
else if (name == "IsLocalized")
|
|
|
|
{
|
|
|
|
isLocalized = (value == "True") ? true : false;
|
|
|
|
}
|
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 == "LabelBMFontFile_CNB")
|
|
|
|
{
|
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")
|
|
|
|
{
|
2014-11-21 15:15:38 +08:00
|
|
|
plistFlie = 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 = CreateTextBMFontOptions(*builder,
|
|
|
|
widgetOptions,
|
|
|
|
CreateResourceData(*builder,
|
|
|
|
builder->CreateString(path),
|
|
|
|
builder->CreateString(plistFlie),
|
|
|
|
resourceType),
|
2016-02-29 18:00:14 +08:00
|
|
|
builder->CreateString(text),
|
|
|
|
isLocalized);
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
return *(Offset<Table>*)(&options);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextBMFontReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textBMFontOptions)
|
|
|
|
{
|
|
|
|
TextBMFont* labelBMFont = static_cast<TextBMFont*>(node);
|
|
|
|
auto options = (TextBMFontOptions*)textBMFontOptions;
|
|
|
|
|
2019-11-24 23:15:56 +08:00
|
|
|
auto cmftDic = cocos2d::wext::makeResourceData(options->fileNameData());
|
2014-12-17 19:00:24 +08:00
|
|
|
bool fileExist = false;
|
|
|
|
std::string errorFilePath = "";
|
|
|
|
std::string errorContent = "";
|
2019-11-24 23:15:56 +08:00
|
|
|
std::string& path = cmftDic.file;
|
|
|
|
int cmfType = cmftDic.type;
|
|
|
|
cocos2d::wext::onBeforeLoadObjectAsset(labelBMFont, cmftDic, 0);
|
2014-11-21 15:15:38 +08:00
|
|
|
switch (cmfType)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
2014-12-17 19:00:24 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(path))
|
|
|
|
{
|
|
|
|
FontAtlas* newAtlas = FontAtlasCache::getFontAtlasFNT(path);
|
|
|
|
if (newAtlas)
|
|
|
|
{
|
|
|
|
fileExist = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errorContent = "has problem";
|
|
|
|
fileExist = false;
|
|
|
|
}
|
|
|
|
}
|
2014-11-21 15:15:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-12-17 19:00:24 +08:00
|
|
|
if (fileExist)
|
|
|
|
{
|
|
|
|
labelBMFont->setFntFile(path);
|
|
|
|
}
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
std::string text = options->text()->c_str();
|
2016-02-29 18:00:14 +08:00
|
|
|
bool isLocalized = options->isLocalized() != 0;
|
|
|
|
if (isLocalized)
|
|
|
|
{
|
2016-03-01 14:27:12 +08:00
|
|
|
ILocalizationManager* lm = LocalizationHelper::getCurrentManager();
|
|
|
|
labelBMFont->setString(lm->getLocalizationString(text));
|
2016-02-29 18:00:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
labelBMFont->setString(text);
|
|
|
|
}
|
2014-10-09 18:28:09 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto widgetReader = WidgetReader::getInstance();
|
|
|
|
widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
|
2015-01-27 15:09:48 +08:00
|
|
|
|
|
|
|
labelBMFont->ignoreContentAdaptWithSize(true);
|
2014-11-21 15:15:38 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 11:50:42 +08:00
|
|
|
Node* TextBMFontReader::createNodeWithFlatBuffers(const flatbuffers::Table *textBMFontOptions)
|
2014-11-21 15:15:38 +08:00
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
TextBMFont* textBMFont = wext::aTextBMFont(); // TextBMFont::create();
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
setPropsWithFlatBuffers(textBMFont, (Table*)textBMFontOptions);
|
|
|
|
|
|
|
|
return textBMFont;
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 16:51:35 +08:00
|
|
|
}
|