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

386 lines
14 KiB
C++
Raw Normal View History

2014-03-04 16:51:35 +08:00
#include "cocostudio/WidgetReader/TextFieldReader/TextFieldReader.h"
2014-11-21 15:15:38 +08:00
2014-03-11 17:13:54 +08:00
#include "ui/UITextField.h"
2016-04-18 15:09:21 +08:00
#include "platform/CCFileUtils.h"
#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
{
static TextFieldReader* instanceTextFieldReader = nullptr;
2014-03-04 16:51:35 +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";
2014-11-21 15:15:38 +08:00
IMPLEMENT_CLASS_NODE_READER_INFO(TextFieldReader)
2014-03-04 16:51:35 +08:00
TextFieldReader::TextFieldReader()
{
}
TextFieldReader::~TextFieldReader()
{
}
TextFieldReader* TextFieldReader::getInstance()
{
if (!instanceTextFieldReader)
{
instanceTextFieldReader = new (std::nothrow) TextFieldReader();
2014-03-04 16:51:35 +08:00
}
return instanceTextFieldReader;
}
2015-03-30 16:46:33 +08:00
void TextFieldReader::destroyInstance()
{
CC_SAFE_DELETE(instanceTextFieldReader);
}
2014-06-23 10:02:09 +08:00
void TextFieldReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode* cocoNode)
{
2014-06-11 09:35:24 +08:00
this->beginSetBasicProperties(widget);
TextField* textField = static_cast<TextField*>(widget);
2014-07-01 16:31:17 +08:00
stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
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-11 09:35:24 +08:00
else if(key == P_PlaceHolder){
textField->setPlaceHolder(value);
}else if(key == P_Text){
2014-09-12 17:06:13 +08:00
textField->setString(value);
}else if(key == P_FontSize){
textField->setFontSize(valueToInt(value));
}else if(key == P_FontName){
2019-11-24 23:15:56 +08:00
auto fontData = cocos2d::wext::makeResourceData(value);
cocos2d::wext::onBeforeLoadObjectAsset(textField, fontData, 0);
textField->setFontName(fontData.file);
}else if(key == P_TouchSizeWidth){
textField->setTouchSize(Size(valueToFloat(value), textField->getTouchSize().height));
}else if(key == P_TouchSizeHeight){
textField->setTouchSize(Size(textField->getTouchSize().width, valueToFloat(value)));
}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
2014-06-11 09:35:24 +08:00
this->endSetBasicProperties(widget);
}
2014-03-04 16:51:35 +08:00
void TextFieldReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
{
WidgetReader::setPropsFromJsonDictionary(widget, options);
2014-03-06 16:15:03 +08:00
TextField* textField = static_cast<TextField*>(widget);
bool ph = DICTOOL->checkObjectExist_json(options, P_PlaceHolder);
2014-03-04 16:51:35 +08:00
if (ph)
{
2014-07-01 10:57:12 +08:00
textField->setPlaceHolder(DICTOOL->getStringValue_json(options, P_PlaceHolder,"input words here"));
2014-03-04 16:51:35 +08:00
}
2014-09-12 17:06:13 +08:00
textField->setString(DICTOOL->getStringValue_json(options, P_Text,"Text Tield"));
2014-07-01 10:57:12 +08:00
textField->setFontSize(DICTOOL->getIntValue_json(options, P_FontSize,20));
2014-12-26 16:20:04 +08:00
std::string jsonPath = GUIReader::getInstance()->getFilePath();
std::string fontName = DICTOOL->getStringValue_json(options, P_FontName, "");
std::string fontFilePath = jsonPath.append(fontName);
if (FileUtils::getInstance()->isFileExist(fontFilePath))
textField->setFontName(fontFilePath);
else
textField->setFontName(fontName);
2014-07-01 10:57:12 +08:00
bool tsw = DICTOOL->checkObjectExist_json(options, P_TouchSizeWidth);
bool tsh = DICTOOL->checkObjectExist_json(options, P_TouchSizeHeight);
2014-03-04 16:51:35 +08:00
if (tsw && tsh)
{
textField->setTouchSize(Size(DICTOOL->getFloatValue_json(options, P_TouchSizeWidth), DICTOOL->getFloatValue_json(options,P_TouchSizeHeight)));
2014-03-04 16:51:35 +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);
2014-03-04 16:51:35 +08:00
textField->setMaxLengthEnabled(maxLengthEnable);
if (maxLengthEnable)
{
2014-07-01 10:57:12 +08:00
int maxLength = DICTOOL->getIntValue_json(options, P_MaxLength,10);
2014-03-04 16:51:35 +08:00
textField->setMaxLength(maxLength);
}
bool passwordEnable = DICTOOL->getBooleanValue_json(options, P_PasswordEnable);
2014-03-04 16:51:35 +08:00
textField->setPasswordEnabled(passwordEnable);
if (passwordEnable)
{
2014-07-01 10:57:12 +08:00
textField->setPasswordStyleText(DICTOOL->getStringValue_json(options, P_PasswordStyleText,"*"));
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> TextFieldReader::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);
std::string path = "";
std::string plistFile = "";
int resourceType = 0;
std::string fontName = "";
2014-11-21 15:15:38 +08:00
int fontSize = 20;
std::string text = "";
bool isLocalized = false;
2014-11-21 15:15:38 +08:00
std::string placeHolder = "Text Field";
bool passwordEnabled = false;
std::string passwordStyleText = "*";
bool maxLengthEnabled = false;
int maxLength = 10;
int areaWidth = 0;
int areaHeight = 0;
2014-10-09 18:28:09 +08:00
bool isCustomSize = false;
// 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 == "PlaceHolderText")
{
2014-11-21 15:15:38 +08:00
placeHolder = value;
2014-10-09 18:28:09 +08:00
}
else if (name == "LabelText")
{
2014-11-21 15:15:38 +08:00
text = value;
2014-10-09 18:28:09 +08:00
}
else if (name == "IsLocalized")
{
isLocalized = (value == "True") ? true : false;
}
2014-10-09 18:28:09 +08:00
else if (name == "FontSize")
{
2014-11-21 15:15:38 +08:00
fontSize = atoi(value.c_str());
2014-10-09 18:28:09 +08:00
}
else if (name == "FontName")
{
2014-11-21 15:15:38 +08:00
fontName = value;
2014-10-09 18:28:09 +08:00
}
else if (name == "MaxLengthEnable")
{
2014-11-21 15:15:38 +08:00
maxLengthEnabled = (value == "True") ? true : false;
2014-10-09 18:28:09 +08:00
}
else if (name == "MaxLengthText")
{
2014-11-21 15:15:38 +08:00
maxLength = atoi(value.c_str());
2014-10-09 18:28:09 +08:00
}
else if (name == "PasswordEnable")
{
2014-11-21 15:15:38 +08:00
passwordEnabled = (value == "True") ? true : false;
2014-10-09 18:28:09 +08:00
}
else if (name == "PasswordStyleText")
{
2014-11-21 15:15:38 +08:00
passwordStyleText = value;
2014-10-09 18:28:09 +08:00
}
else if (name == "IsCustomSize")
{
2014-11-21 15:15:38 +08:00
isCustomSize = (value == "True") ? true : false;
2014-10-09 18:28:09 +08:00
}
2014-11-21 15:15:38 +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
2014-11-21 15:15:38 +08:00
if (name == "FontResource")
2014-10-09 18:28:09 +08:00
{
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")
{
plistFile = value;
}
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 = 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,
2019-11-24 23:15:56 +08:00
isCustomSize,
isLocalized /*pitfall v3.15 use this order */);
2014-11-21 15:15:38 +08:00
return *(Offset<Table>*)(&options);
}
void TextFieldReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textFieldOptions)
{
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);
2019-11-24 23:15:56 +08:00
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);
}
2014-11-21 15:15:38 +08:00
int fontSize = options->fontSize();
textField->setFontSize(fontSize);
std::string fontName = options->fontName()->c_str();
textField->setFontName(fontName);
bool maxLengthEnabled = options->maxLengthEnabled() != 0;
2014-11-21 15:15:38 +08:00
textField->setMaxLengthEnabled(maxLengthEnabled);
if (maxLengthEnabled)
{
int maxLength = options->maxLength();
textField->setMaxLength(maxLength);
}
bool passwordEnabled = options->passwordEnabled() != 0;
2014-11-21 15:15:38 +08:00
textField->setPasswordEnabled(passwordEnabled);
if (passwordEnabled)
{
std::string passwordStyleText = options->passwordStyleText()->c_str();
textField->setPasswordStyleText(passwordStyleText.c_str());
}
bool fileExist = false;
std::string errorFilePath = "";
2019-11-24 23:15:56 +08:00
auto resourceData = cocos2d::wext::makeResourceData(options->fontResource());
std::string& path = resourceData.file;
cocos2d::wext::onBeforeLoadObjectAsset(textField, resourceData, 0);
2014-11-21 15:15:38 +08:00
if (path != "")
2014-10-09 18:28:09 +08:00
{
if (FileUtils::getInstance()->isFileExist(path))
{
fileExist = true;
}
else
{
errorFilePath = path;
fileExist = false;
}
if (fileExist)
{
textField->setFontName(path);
}
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());
2014-12-08 15:48:24 +08:00
textField->setUnifySizeEnabled(false);
textField->ignoreContentAdaptWithSize(false);
auto widgetOptions = options->widgetOptions();
2014-12-08 15:48:24 +08:00
if (!textField->isIgnoreContentAdaptWithSize())
{
2014-12-10 12:36:14 +08:00
((Label*)(textField->getVirtualRenderer()))->setLineBreakWithoutSpace(true);
2014-12-08 15:48:24 +08:00
Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
textField->setContentSize(contentSize);
}
2014-11-21 15:15:38 +08:00
}
Node* TextFieldReader::createNodeWithFlatBuffers(const flatbuffers::Table *textFieldOptions)
2014-11-21 15:15:38 +08:00
{
2019-11-24 23:15:56 +08:00
TextField* textField = wext::aTextField();// TextField::create();
2014-11-21 15:15:38 +08:00
setPropsWithFlatBuffers(textField, (Table*)textFieldOptions);
return textField;
2014-10-09 18:28:09 +08:00
}
2014-03-04 16:51:35 +08:00
}