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

355 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
#include "WidgetReader/TextFieldReader/TextFieldReader.h"
2019-11-23 20:27:39 +08:00
#include "ui/UITextField.h"
#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 TextFieldReader* instanceTextFieldReader = nullptr;
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";
IMPLEMENT_CLASS_NODE_READER_INFO(TextFieldReader)
TextFieldReader::TextFieldReader() {}
TextFieldReader::~TextFieldReader() {}
TextFieldReader* TextFieldReader::getInstance()
{
if (!instanceTextFieldReader)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
instanceTextFieldReader = new TextFieldReader();
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
return instanceTextFieldReader;
}
void TextFieldReader::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(instanceTextFieldReader);
2021-12-25 10:04:45 +08:00
}
void TextFieldReader::setPropsFromBinary(axis::ui::Widget* widget, CocoLoader* cocoLoader, stExpCocoNode* cocoNode)
2021-12-25 10:04:45 +08:00
{
this->beginSetBasicProperties(widget);
TextField* textField = static_cast<TextField*>(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_PlaceHolder) { textField->setPlaceHolder(value); }
else if (key == P_Text) { textField->setString(value); }
else if (key == P_FontSize) { textField->setFontSize(valueToInt(value)); }
else if (key == P_FontName) { textField->setFontName(value); }
else if (key == P_TouchSizeWidth)
{
textField->setTouchSize(Size(valueToFloat(value), textField->getTouchSize().height));
}
else if (key == P_TouchSizeHeight)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
textField->setTouchSize(Size(textField->getTouchSize().width, valueToFloat(value)));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (key == P_MaxLengthEnable) { textField->setMaxLengthEnabled(valueToBool(value)); }
else if (key == P_MaxLength) { textField->setMaxLength(valueToInt(value)); }
else if (key == P_PasswordEnable) { textField->setPasswordEnabled(valueToBool(value)); }
else if (key == P_PasswordStyleText) { textField->setPasswordStyleText(value.c_str()); }
} // end of for loop
this->endSetBasicProperties(widget);
}
void TextFieldReader::setPropsFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
TextField* textField = static_cast<TextField*>(widget);
bool ph = DICTOOL->checkObjectExist_json(options, P_PlaceHolder);
if (ph)
{
textField->setPlaceHolder(DICTOOL->getStringValue_json(options, P_PlaceHolder, "input words here"));
}
textField->setString(DICTOOL->getStringValue_json(options, P_Text, "Text Tield"));
textField->setFontSize(DICTOOL->getIntValue_json(options, P_FontSize, 20));
std::string fontFilePath{GUIReader::getInstance()->getFilePath()};
auto fontName = DICTOOL->getStringValue_json(options, P_FontName, "");
fontFilePath.append(fontName);
2021-12-25 10:04:45 +08:00
if (FileUtils::getInstance()->isFileExist(fontFilePath))
textField->setFontName(fontFilePath);
else
textField->setFontName(fontName);
bool tsw = DICTOOL->checkObjectExist_json(options, P_TouchSizeWidth);
bool tsh = DICTOOL->checkObjectExist_json(options, P_TouchSizeHeight);
if (tsw && tsh)
{
textField->setTouchSize(Size(DICTOOL->getFloatValue_json(options, P_TouchSizeWidth),
DICTOOL->getFloatValue_json(options, P_TouchSizeHeight)));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
// float dw = DICTOOL->getFloatValue_json(options, "width");
// float dh = DICTOOL->getFloatValue_json(options, "height");
// if (dw > 0.0f || dh > 0.0f)
// {
// //textField->setSize(Size(dw, dh));
// }
bool maxLengthEnable = DICTOOL->getBooleanValue_json(options, P_MaxLengthEnable);
textField->setMaxLengthEnabled(maxLengthEnable);
if (maxLengthEnable)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
int maxLength = DICTOOL->getIntValue_json(options, P_MaxLength, 10);
textField->setMaxLength(maxLength);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
bool passwordEnable = DICTOOL->getBooleanValue_json(options, P_PasswordEnable);
textField->setPasswordEnabled(passwordEnable);
if (passwordEnable)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
textField->setPasswordStyleText(DICTOOL->getStringValue_json(options, P_PasswordStyleText, "*"));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
WidgetReader::setColorPropsFromJsonDictionary(widget, options);
}
Offset<Table> TextFieldReader::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";
bool passwordEnabled = false;
std::string passwordStyleText = "*";
bool maxLengthEnabled = false;
int maxLength = 10;
int areaWidth = 0;
int areaHeight = 0;
bool isCustomSize = false;
// 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 == "PlaceHolderText")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
placeHolder = value;
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());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "FontName")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
fontName = value;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "MaxLengthEnable")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
maxLengthEnabled = (value == "True") ? true : false;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "MaxLengthText")
2019-11-23 20:27:39 +08:00
{
2021-12-31 15:49:45 +08:00
maxLength = atoi(value.data());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "PasswordEnable")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
passwordEnabled = (value == "True") ? true : false;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "PasswordStyleText")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
passwordStyleText = value;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "IsCustomSize")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
isCustomSize = (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 == "FontResource")
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")
{
plistFile = value;
}
attribute = attribute.next_attribute();
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
child = child.next_sibling();
}
auto options = CreateTextFieldOptions(
*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), maxLengthEnabled, maxLength, areaWidth, areaHeight,
isCustomSize, isLocalized /*pitfall v3.15 use this order */);
return *(Offset<Table>*)(&options);
}
void TextFieldReader::setPropsWithFlatBuffers(axis::Node* node, const flatbuffers::Table* textFieldOptions)
2021-12-25 10:04:45 +08:00
{
TextField* textField = static_cast<TextField*>(node);
auto options = (TextFieldOptions*)textFieldOptions;
std::string placeholder = options->placeHolder()->c_str();
textField->setPlaceHolder(placeholder);
std::string text = options->text()->c_str();
bool isLocalized = options->isLocalized() != 0;
if (isLocalized)
{
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);
textField->setString(localizedTxt);
}
else
{
textField->setString(text);
}
int fontSize = options->fontSize();
textField->setFontSize(fontSize);
std::string fontName = options->fontName()->c_str();
textField->setFontName(fontName);
bool maxLengthEnabled = options->maxLengthEnabled() != 0;
textField->setMaxLengthEnabled(maxLengthEnabled);
if (maxLengthEnabled)
{
int maxLength = options->maxLength();
textField->setMaxLength(maxLength);
}
bool passwordEnabled = options->passwordEnabled() != 0;
textField->setPasswordEnabled(passwordEnabled);
if (passwordEnabled)
{
std::string passwordStyleText = options->passwordStyleText()->c_str();
textField->setPasswordStyleText(passwordStyleText.c_str());
}
bool fileExist = false;
std::string errorFilePath;
auto resourceDataDic = (options->fontResource());
std::string path = resourceDataDic->path()->c_str();
if (path != "")
{
if (FileUtils::getInstance()->isFileExist(path))
{
fileExist = true;
}
else
{
errorFilePath = path;
fileExist = false;
}
if (fileExist)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
textField->setFontName(path);
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());
textField->setUnifySizeEnabled(false);
textField->ignoreContentAdaptWithSize(false);
auto widgetOptions = options->widgetOptions();
if (!textField->isIgnoreContentAdaptWithSize())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
((Label*)(textField->getVirtualRenderer()))->setLineBreakWithoutSpace(true);
Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
textField->setContentSize(contentSize);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
Node* TextFieldReader::createNodeWithFlatBuffers(const flatbuffers::Table* textFieldOptions)
{
TextField* textField = TextField::create();
setPropsWithFlatBuffers(textField, (Table*)textFieldOptions);
return textField;
}
} // namespace cocostudio