axmol/extensions/cocostudio/WidgetReader/TextReader/TextReader.cpp

557 lines
17 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
#include "WidgetReader/TextReader/TextReader.h"
2019-11-23 20:27:39 +08:00
#include "ui/UIText.h"
2019-11-24 23:15:56 +08:00
#include "2d/CCLabel.h"
2019-11-23 20:27:39 +08:00
#include "platform/CCFileUtils.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_TouchScaleEnable = "touchScaleEnable";
static const char* P_Text = "text";
static const char* P_FontSize = "fontSize";
static const char* P_FontName = "fontName";
static const char* P_AreaWidth = "areaWidth";
static const char* P_AreaHeight = "areaHeight";
static const char* P_HAlignment = "hAlignment";
static const char* P_VAlignment = "vAlignment";
static TextReader* instanceTextReader = nullptr;
IMPLEMENT_CLASS_NODE_READER_INFO(TextReader)
TextReader::TextReader() {}
TextReader::~TextReader() {}
TextReader* TextReader::getInstance()
{
if (!instanceTextReader)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
instanceTextReader = new TextReader();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
return instanceTextReader;
}
void TextReader::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(instanceTextReader);
2021-12-25 10:04:45 +08:00
}
2022-08-08 18:02:17 +08:00
void TextReader::setPropsFromBinary(ax::ui::Widget* widget, CocoLoader* cocoLoader, stExpCocoNode* cocoNode)
2021-12-25 10:04:45 +08:00
{
this->beginSetBasicProperties(widget);
stExpCocoNode* stChildArray = cocoNode->GetChildArray(cocoLoader);
Text* label = static_cast<Text*>(widget);
std::string binaryFilePath{GUIReader::getInstance()->getFilePath()};
2021-12-25 10:04:45 +08:00
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_TouchScaleEnable) { label->setTouchScaleChangeEnabled(valueToBool(value)); }
else if (key == P_Text) { label->setString(value); }
else if (key == P_FontSize) { label->setFontSize(valueToInt(value)); }
else if (key == P_FontName)
{
auto& fontFilePath = binaryFilePath.append(value);
label->setFontName(fontFilePath);
}
else if (key == P_AreaWidth)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
label->setTextAreaSize(Size(valueToFloat(value), label->getTextAreaSize().height));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (key == P_AreaHeight)
{
label->setTextAreaSize(Size(label->getTextAreaSize().width, valueToFloat(value)));
}
else if (key == P_HAlignment) { label->setTextHorizontalAlignment((TextHAlignment)valueToInt(value)); }
else if (key == P_VAlignment) { label->setTextVerticalAlignment((TextVAlignment)valueToInt(value)); }
} // end of for loop
this->endSetBasicProperties(widget);
}
void TextReader::setPropsFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
std::string fontFilePath{GUIReader::getInstance()->getFilePath()};
2021-12-25 10:04:45 +08:00
Text* label = static_cast<Text*>(widget);
bool touchScaleChangeAble = DICTOOL->getBooleanValue_json(options, P_TouchScaleEnable);
label->setTouchScaleChangeEnabled(touchScaleChangeAble);
const char* text = DICTOOL->getStringValue_json(options, P_Text, "Text Label");
label->setString(text);
label->setFontSize(DICTOOL->getIntValue_json(options, P_FontSize, 20));
auto fontName = DICTOOL->getStringValue_json(options, P_FontName, "");
2021-12-25 10:04:45 +08:00
fontFilePath.append(fontName);
2021-12-25 10:04:45 +08:00
if (FileUtils::getInstance()->isFileExist(fontFilePath))
{
label->setFontName(fontFilePath);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
label->setFontName(fontName);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
bool aw = DICTOOL->checkObjectExist_json(options, P_AreaWidth);
bool ah = DICTOOL->checkObjectExist_json(options, P_AreaHeight);
if (aw && ah)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Size size =
Size(DICTOOL->getFloatValue_json(options, P_AreaWidth), DICTOOL->getFloatValue_json(options, P_AreaHeight));
label->setTextAreaSize(size);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
bool ha = DICTOOL->checkObjectExist_json(options, P_HAlignment);
if (ha)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
label->setTextHorizontalAlignment((TextHAlignment)DICTOOL->getIntValue_json(options, P_HAlignment));
}
bool va = DICTOOL->checkObjectExist_json(options, P_VAlignment);
if (va)
{
label->setTextVerticalAlignment((TextVAlignment)DICTOOL->getIntValue_json(options, P_VAlignment));
}
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
}
Offset<Table> TextReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
flatbuffers::FlatBufferBuilder* builder)
{
auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
bool touchScaleEnabled = false;
bool isCustomSize = false;
std::string fontName;
int fontSize = 20;
std::string text = "Text Label";
bool isLocalized = false;
int areaWidth = 0;
int areaHeight = 0;
int h_alignment = 0;
int v_alignment = 0;
bool outlineEnabled = false;
Color4B outlineColor = Color4B::BLACK;
int outlineSize = 1;
bool shadowEnabled = false;
Color4B shadowColor = Color4B::BLACK;
Size shadowOffset = Size(2, -2);
int shadowBlurRadius = 0;
// since x-studio reader 10.0.593.0
bool glowEnabled = false;
Color4B glowColor = Color4B::BLACK;
bool boldEnabled = false, underlineEnabled = false, italicsEnabled = false, strikethroughEnabled = false;
std::string path;
std::string plistFile;
int resourceType = 0;
// attributes
auto attribute = objectData.first_attribute();
while (attribute)
{
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 == "TouchScaleChangeAble")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
touchScaleEnabled = (value == "True") ? true : false;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else 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
else if (name == "FontSize")
2019-11-23 20:27:39 +08:00
{
2021-12-31 15:49:45 +08:00
fontSize = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "FontName")
{
fontName = value;
}
else if (name == "AreaWidth")
{
2021-12-31 15:49:45 +08:00
areaWidth = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "AreaHeight")
{
2021-12-31 15:49:45 +08:00
areaHeight = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "HorizontalAlignmentType")
{
if (value == "HT_Left")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
h_alignment = 0;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (value == "HT_Center")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
h_alignment = 1;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (value == "HT_Right")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
h_alignment = 2;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
else if (name == "VerticalAlignmentType")
{
if (value == "VT_Top")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
v_alignment = 0;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (value == "VT_Center")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
v_alignment = 1;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (value == "VT_Bottom")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
v_alignment = 2;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
else if (name == "IsCustomSize")
{
isCustomSize = (value == "True") ? true : false;
}
else if (name == "OutlineEnabled")
{
outlineEnabled = (value == "True") ? true : false;
}
else if (name == "OutlineSize")
{
2021-12-31 15:49:45 +08:00
outlineSize = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "ShadowEnabled")
{
shadowEnabled = (value == "True") ? true : false;
}
else if (name == "ShadowOffsetX")
{
2021-12-31 15:49:45 +08:00
shadowOffset.width = atof(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "ShadowOffsetY")
{
2021-12-31 15:49:45 +08:00
shadowOffset.height = atof(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "ShadowBlurRadius")
{
2021-12-31 15:49:45 +08:00
shadowBlurRadius = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "GlowEnabled")
{
glowEnabled = (value == "True") ? true : false;
}
else if (name == "BoldEnabled")
{
boldEnabled = (value == "True") ? true : false;
}
else if (name == "UnderlineEnabled")
{
underlineEnabled = (value == "True") ? true : false;
}
else if (name == "ItalicsEnabled")
{
italicsEnabled = (value == "True") ? true : false;
}
else if (name == "StrikethroughEnabled")
{
strikethroughEnabled = (value == "True") ? true : false;
}
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 == "FontResource")
{
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")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
path = value;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Type")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
resourceType = 0;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Plist")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
plistFile = 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
}
else if (name == "OutlineColor")
{
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 == "A")
2019-11-23 20:27:39 +08:00
{
2021-12-31 15:49:45 +08:00
outlineColor.a = atoi(value.data());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "R")
2019-11-23 20:27:39 +08:00
{
2021-12-31 15:49:45 +08:00
outlineColor.r = atoi(value.data());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "G")
2019-11-23 20:27:39 +08:00
{
2021-12-31 15:49:45 +08:00
outlineColor.g = atoi(value.data());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "B")
{
2021-12-31 15:49:45 +08:00
outlineColor.b = atoi(value.data());
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
else if (name == "ShadowColor")
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 == "A")
2019-11-23 20:27:39 +08:00
{
2021-12-31 15:49:45 +08:00
shadowColor.a = atoi(value.data());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "R")
{
2021-12-31 15:49:45 +08:00
shadowColor.r = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "G")
2019-11-23 20:27:39 +08:00
{
2021-12-31 15:49:45 +08:00
shadowColor.g = atoi(value.data());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "B")
{
2021-12-31 15:49:45 +08:00
shadowColor.b = atoi(value.data());
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
}
else if (name == "GlowColor")
{
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 == "A")
{
2021-12-31 15:49:45 +08:00
glowColor.a = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "R")
{
2021-12-31 15:49:45 +08:00
glowColor.r = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "G")
{
2021-12-31 15:49:45 +08:00
glowColor.g = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "B")
2019-11-23 20:27:39 +08:00
{
2021-12-31 15:49:45 +08:00
glowColor.b = atoi(value.data());
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
flatbuffers::Color f_outlineColor(outlineColor.a, outlineColor.r, outlineColor.g, outlineColor.b);
flatbuffers::Color f_shadowColor(shadowColor.a, shadowColor.r, shadowColor.g, shadowColor.b);
flatbuffers::Color f_glowColor(glowColor.a, glowColor.r, glowColor.g, glowColor.b);
auto options = CreateTextOptions(
*builder, widgetOptions,
CreateResourceData(*builder, builder->CreateString(path), builder->CreateString(plistFile), resourceType),
builder->CreateString(fontName), fontSize, builder->CreateString(text), areaWidth, areaHeight, h_alignment,
v_alignment, touchScaleEnabled, isCustomSize, outlineEnabled, &f_outlineColor, outlineSize, shadowEnabled,
&f_shadowColor, shadowOffset.width, shadowOffset.height, shadowBlurRadius, isLocalized, glowEnabled,
&f_glowColor, boldEnabled, underlineEnabled, italicsEnabled, strikethroughEnabled);
return *(Offset<Table>*)(&options);
}
2022-08-08 18:02:17 +08:00
void TextReader::setPropsWithFlatBuffers(ax::Node* node, const flatbuffers::Table* textOptions)
2021-12-25 10:04:45 +08:00
{
Text* label = static_cast<Text*>(node);
auto options = (TextOptions*)textOptions;
bool touchScaleEnabled = options->touchScaleEnable() != 0;
label->setTouchScaleChangeEnabled(touchScaleEnabled);
int fontSize = options->fontSize();
label->setFontSize(fontSize);
Size areaSize = Size(options->areaWidth(), options->areaHeight());
if (!areaSize.equals(Size::ZERO))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
label->setTextAreaSize(areaSize);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
auto resourceDataDic = (options->fontResource());
std::string path = resourceDataDic->path()->c_str();
if (!path.empty() && FileUtils::getInstance()->isFileExist(path))
{
label->setFontName(path);
}
else
{
path = options->fontName()->c_str();
label->setFontName(path);
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
TextHAlignment h_alignment = (TextHAlignment)options->hAlignment();
label->setTextHorizontalAlignment(h_alignment);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
TextVAlignment v_alignment = (TextVAlignment)options->vAlignment();
label->setTextVerticalAlignment((TextVAlignment)v_alignment);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
bool outlineEnabled = options->outlineEnabled() != 0;
if (outlineEnabled)
{
auto f_outlineColor = options->outlineColor();
if (f_outlineColor)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Color4B outlineColor(f_outlineColor->r(), f_outlineColor->g(), f_outlineColor->b(), f_outlineColor->a());
label->enableOutline(outlineColor, options->outlineSize());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
bool shadowEnabled = options->shadowEnabled() != 0;
if (shadowEnabled)
{
auto f_shadowColor = options->shadowColor();
if (f_shadowColor)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Color4B shadowColor(f_shadowColor->r(), f_shadowColor->g(), f_shadowColor->b(), f_shadowColor->a());
label->enableShadow(shadowColor, Size(options->shadowOffsetX(), options->shadowOffsetY()),
options->shadowBlurRadius());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (options->glowEnabled() != 0)
{
auto f_glowColor = options->glowColor();
if (f_glowColor)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Color4B glowColor(f_glowColor->r(), f_glowColor->g(), f_glowColor->b(), f_glowColor->a());
label->enableGlow(glowColor);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +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();
label->setString(lm->getLocalizationString(text));
}
else
{
label->setString(text);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
// Save node color before set widget properties
auto oldColor = node->getColor();
auto widgetReader = WidgetReader::getInstance();
widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
// restore node color and set color to text to fix shadow & outline color won't show correct bug
node->setColor(oldColor);
auto optionsWidget = (WidgetOptions*)options->widgetOptions();
auto f_color = optionsWidget->color();
Color4B color(f_color->r(), f_color->g(), f_color->b(), f_color->a());
((Text*)node)->setTextColor(color);
label->setUnifySizeEnabled(false);
bool IsCustomSize = options->isCustomSize() != 0;
label->ignoreContentAdaptWithSize(!IsCustomSize);
auto widgetOptions = options->widgetOptions();
if (!label->isIgnoreContentAdaptWithSize())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
label->setContentSize(contentSize);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2022-08-08 18:02:17 +08:00
auto labelRenderer = dynamic_cast<ax::Label*>(label->getVirtualRenderer());
2021-12-25 10:04:45 +08:00
if (options->boldEnabled())
labelRenderer->enableBold();
if (options->underlineEnabled())
labelRenderer->enableUnderline();
if (options->italicsEnabled())
labelRenderer->enableItalics();
if (options->strikethroughEnabled())
labelRenderer->enableStrikethrough();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
Node* TextReader::createNodeWithFlatBuffers(const flatbuffers::Table* textOptions)
{
Text* text = Text::create();
setPropsWithFlatBuffers(text, (Table*)textOptions);
return text;
}
} // namespace cocostudio