axmol/extensions/cocostudio/WidgetReader/TextFieldReader/TextFieldExReader.cpp

373 lines
11 KiB
C++
Raw Normal View History

2019-11-24 23:15:56 +08:00
#include "WidgetReader/TextFieldReader/TextFieldExReader.h"
2019-11-24 23:15:56 +08:00
2020-08-04 00:14:35 +08:00
#include "ui/UITextFieldEx.h"
2019-11-24 23:15:56 +08:00
#include "platform/CCFileUtils.h"
#include "CocoLoader.h"
#include "CSParseBinary_generated.h"
#include "LocalizationManager.h"
2019-11-24 23:15:56 +08:00
#include "WidgetReader/NodeReader/NodeReader.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;
2021-12-25 10:04:45 +08:00
inline Color4B Color4BFromFb(const flatbuffers::Color* pColor)
2019-11-24 23:15:56 +08:00
{
return Color4B(pColor->r(), pColor->g(), pColor->b(), pColor->a());
}
2021-12-25 10:04:45 +08:00
inline Color3B Color3BFromFb(const flatbuffers::Color* pColor)
2019-11-24 23:15:56 +08:00
{
return Color3B(pColor->r(), pColor->g(), pColor->b());
}
namespace cocostudio
{
2021-12-25 10:04:45 +08:00
static TextFieldExReader* instanceTextFieldExReader = nullptr;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
static const char* P_PlaceHolder = "placeHolder";
static const char* P_Text = "text";
static const char* P_FontSize = "fontSize";
static const char* P_FontName = "fontName";
static const char* P_TouchSizeWidth = "touchSizeWidth";
static const char* P_TouchSizeHeight = "touchSizeHeight";
static const char* P_MaxLengthEnable = "maxLengthEnable";
static const char* P_MaxLength = "maxLength";
static const char* P_PasswordEnable = "passwordEnable";
static const char* P_PasswordStyleText = "passwordStyleText";
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
IMPLEMENT_CLASS_NODE_READER_INFO(TextFieldExReader)
TextFieldExReader::TextFieldExReader() {}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
TextFieldExReader::~TextFieldExReader() {}
TextFieldExReader* TextFieldExReader::getInstance()
{
if (!instanceTextFieldExReader)
{
instanceTextFieldExReader = new TextFieldExReader();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return instanceTextFieldExReader;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldExReader::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(instanceTextFieldExReader);
2021-12-25 10:04:45 +08:00
}
Offset<Table> TextFieldExReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
flatbuffers::FlatBufferBuilder* builder)
{
auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
std::string path;
std::string plistFile;
int resourceType = 0;
std::string fontName;
int fontSize = 20;
std::string text;
bool isLocalized = false;
std::string placeHolder = "Text Field Extend";
bool passwordEnabled = false;
std::string passwordStyleText = "*";
bool maxLengthEnabled = false;
int maxLength = 10;
bool isEnabled = true;
bool isEditable = true;
Color4B textColor;
Color4B placeholderColor;
Color4B cursorColor;
// 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 == "PlaceHolderText")
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
placeHolder = value;
}
else if (name == "LabelText")
{
text = value;
}
else if (name == "IsLocalized")
{
isLocalized = (value == "True") ? true : false;
}
else if (name == "FontSize")
{
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 == "MaxLengthText")
{
2021-12-31 15:49:45 +08:00
maxLength = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "PasswordEnable")
{
passwordEnabled = (value == "True") ? true : false;
}
else if (name == "PasswordStyleText")
{
passwordStyleText = value;
}
else if (name == "Enabled")
{
isEnabled = (value == "True") ? true : false;
}
else if (name == "Editable")
{
isEditable = (value == "True") ? true : false;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
// child elements
auto child = objectData.first_child();
while (child)
2019-11-24 23:15:56 +08:00
{
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")
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
attribute = child.first_attribute();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
while (attribute)
2019-11-24 23:15:56 +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();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (name == "Path")
{
path = value;
}
else if (name == "Type")
{
resourceType = 0;
}
else if (name == "Plist")
{
plistFile = value;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
}
}
else if (name == "TextColor")
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
attribute = child.first_attribute();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
while (attribute)
2019-11-24 23:15:56 +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();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (name == "A")
2019-11-24 23:15:56 +08:00
{
2021-12-31 15:49:45 +08:00
textColor.a = atoi(value.data());
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "R")
{
2021-12-31 15:49:45 +08:00
textColor.r = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "G")
{
2021-12-31 15:49:45 +08:00
textColor.g = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "B")
2019-11-24 23:15:56 +08:00
{
2021-12-31 15:49:45 +08:00
textColor.b = atoi(value.data());
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
else if (name == "PlaceholderColor")
{
attribute = child.first_attribute();
while (attribute)
2019-11-24 23:15:56 +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();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (name == "A")
{
2021-12-31 15:49:45 +08:00
placeholderColor.a = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "R")
{
2021-12-31 15:49:45 +08:00
placeholderColor.r = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "G")
{
2021-12-31 15:49:45 +08:00
placeholderColor.g = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "B")
2019-11-24 23:15:56 +08:00
{
2021-12-31 15:49:45 +08:00
placeholderColor.b = atoi(value.data());
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
else if (name == "CursorColor")
{
attribute = child.first_attribute();
while (attribute)
2019-11-24 23:15:56 +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();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (name == "A")
2019-11-24 23:15:56 +08:00
{
2021-12-31 15:49:45 +08:00
cursorColor.a = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "R")
{
2021-12-31 15:49:45 +08:00
cursorColor.r = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "G")
{
2021-12-31 15:49:45 +08:00
cursorColor.g = atoi(value.data());
2021-12-25 10:04:45 +08:00
}
else if (name == "B")
{
2021-12-31 15:49:45 +08:00
cursorColor.b = atoi(value.data());
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
}
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
child = child.next_sibling();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
Color f_textColor(textColor.a, textColor.r, textColor.g, textColor.b);
Color f_placeholderColor(placeholderColor.a, placeholderColor.r, placeholderColor.g, placeholderColor.b);
Color f_cursorColor(255, cursorColor.r, cursorColor.g, cursorColor.b);
auto options = CreateTextFieldExOptions(
*builder, widgetOptions,
CreateResourceData(*builder, builder->CreateString(path), builder->CreateString(plistFile), resourceType),
builder->CreateString(fontName), fontSize, builder->CreateString(text), builder->CreateString(placeHolder),
passwordEnabled, builder->CreateString(passwordStyleText), maxLength, &f_textColor, &f_placeholderColor,
&f_cursorColor, isEnabled, isEditable, isLocalized);
return *(Offset<Table>*)(&options);
}
void TextFieldExReader::setPropsWithFlatBuffers(axis::Node* node, const flatbuffers::Table* textFieldOptions)
2021-12-25 10:04:45 +08:00
{
TextFieldEx** pTextField = (TextFieldEx**)(node);
auto options = (TextFieldExOptions*)textFieldOptions;
std::string placeholder = options->placeholderText()->c_str();
std::string text = options->text()->c_str();
bool isLocalized = options->isLocalized();
if (isLocalized)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
ILocalizationManager* lm = LocalizationHelper::getCurrentManager();
std::string localizedTxt = lm->getLocalizationString(text);
std::string::size_type newlineIndex = localizedTxt.find("\n");
if (newlineIndex != std::string::npos)
localizedTxt = localizedTxt.substr(0, newlineIndex);
text = (localizedTxt);
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
int fontSize = options->fontSize();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
std::string fontName = options->fontName()->c_str();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool fileExist = false;
std::string errorFilePath;
auto resourceDataDic = (options->fontResource());
std::string path = resourceDataDic->path()->c_str();
if (path != "")
{
if (FileUtils::getInstance()->isFileExist(path))
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
fileExist = true;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
else
{
errorFilePath = path;
fileExist = false;
}
if (fileExist)
{
fontName = (path);
}
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// bool maxLengthEnabled = options->maxLengthEnabled() != 0;
// textField->setMaxLengthEnabled(maxLengthEnabled);
auto textField = TextFieldEx::create(placeholder, fontName, fontSize);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
*pTextField = textField;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
int maxLength = options->maxLength();
textField->setMaxLength(maxLength);
bool passwordEnabled = options->passwordEnabled() != 0;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
textField->setPasswordEnabled(passwordEnabled);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
/*if (passwordEnabled)
{
std::string passwordStyleText = options->passwordStyleText()->c_str();
textField->setPasswordStyleText(passwordStyleText.c_str());
}*/
textField->setString(text);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
textField->setTextColor(Color4BFromFb(options->textColor()));
textField->setPlaceholderColor(Color4BFromFb(options->placeholderColor()));
textField->setCursorColor(Color3BFromFb(options->cursorColor()));
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto widgetReader = NodeReader::getInstance();
widgetReader->setPropsWithFlatBuffers(textField, (Table*)options->nodeOptions());
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// textField->setUnifySizeEnabled(false);
// textField->ignoreContentAdaptWithSize(false);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto widgetOptions = options->nodeOptions();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
textField->setContentSize(contentSize);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
textField->setEnabled(options->enabled());
textField->setEditable(options->editable());
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
textField->enableIME(nullptr);
/*if (!textField->isIgnoreContentAdaptWithSize())
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
((Label*)(textField->getVirtualRenderer()))->setLineBreakWithoutSpace(true);
}*/
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
Node* TextFieldExReader::createNodeWithFlatBuffers(const flatbuffers::Table* textFieldOptions)
{
TextFieldEx* textField = nullptr; // TextFieldEx::create("dummy", "Courier New", 18);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
setPropsWithFlatBuffers((Node*)&textField, (Table*)textFieldOptions);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return textField;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio