axmol/extensions/cocostudio/CCSGUIReader.cpp

1663 lines
65 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCSGUIReader.h"
2019-11-23 20:27:39 +08:00
#include <fstream>
#include <iostream>
#include "ui/CocosGUI.h"
#include "platform/CCFileUtils.h"
#include "2d/CCSpriteFrameCache.h"
#include "base/CCDirector.h"
#include "base/ccUtils.h"
#include "CCActionManagerEx.h"
2020-11-16 22:38:18 +08:00
#include "WidgetReader/ButtonReader/ButtonReader.h"
#include "WidgetReader/CheckBoxReader/CheckBoxReader.h"
#include "WidgetReader/SliderReader/SliderReader.h"
#include "WidgetReader/ImageViewReader/ImageViewReader.h"
#include "WidgetReader/LoadingBarReader/LoadingBarReader.h"
#include "WidgetReader/TextAtlasReader/TextAtlasReader.h"
#include "WidgetReader/TextReader/TextReader.h"
#include "WidgetReader/TextBMFontReader/TextBMFontReader.h"
#include "WidgetReader/TextFieldReader/TextFieldReader.h"
#include "WidgetReader/LayoutReader/LayoutReader.h"
#include "WidgetReader/PageViewReader/PageViewReader.h"
#include "WidgetReader/ScrollViewReader/ScrollViewReader.h"
#include "WidgetReader/ListViewReader/ListViewReader.h"
#include "CocoLoader.h"
#include "pugixml/pugixml.hpp"
2020-10-17 16:32:16 +08:00
2019-11-23 20:27:39 +08:00
using namespace cocos2d;
using namespace cocos2d::ui;
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
static GUIReader* sharedReader = nullptr;
2021-12-25 10:04:45 +08:00
GUIReader::GUIReader() : m_strFilePath("")
2019-11-23 20:27:39 +08:00
{
ObjectFactory* factoryCreate = ObjectFactory::getInstance();
2021-12-25 10:04:45 +08:00
2020-11-16 22:38:18 +08:00
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ButtonReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(CheckBoxReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(SliderReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ImageViewReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(LoadingBarReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextAtlasReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextBMFontReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextFieldReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(LayoutReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(PageViewReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ScrollViewReader));
factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ListViewReader));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Button));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(CheckBox));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ImageView));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Text));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextAtlas));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextBMFont));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(LoadingBar));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Slider));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextField));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Layout));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ListView));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(PageView));
factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ScrollView));
}
2021-12-25 10:04:45 +08:00
GUIReader::~GUIReader() {}
2019-11-23 20:27:39 +08:00
GUIReader* GUIReader::getInstance()
{
if (!sharedReader)
{
2021-12-08 00:11:53 +08:00
sharedReader = new GUIReader();
2019-11-23 20:27:39 +08:00
}
return sharedReader;
}
void GUIReader::destroyInstance()
{
CC_SAFE_DELETE(sharedReader);
}
2021-12-25 10:04:45 +08:00
int GUIReader::getVersionInteger(const char* str)
2019-11-23 20:27:39 +08:00
{
std::string strVersion = str;
2021-12-25 10:04:45 +08:00
size_t length = strVersion.length();
2019-11-23 20:27:39 +08:00
if (length < 7)
{
return 0;
}
2021-12-25 10:04:45 +08:00
size_t pos = strVersion.find_first_of(".");
std::string t = strVersion.substr(0, pos);
strVersion = strVersion.substr(pos + 1, strVersion.length() - 1);
pos = strVersion.find_first_of(".");
std::string h = strVersion.substr(0, pos);
strVersion = strVersion.substr(pos + 1, strVersion.length() - 1);
pos = strVersion.find_first_of(".");
std::string te = strVersion.substr(0, pos);
strVersion = strVersion.substr(pos + 1, strVersion.length() - 1);
pos = strVersion.find_first_of(".");
std::string s = strVersion.substr(0, pos);
int it = atoi(t.c_str());
int ih = atoi(h.c_str());
2019-11-23 20:27:39 +08:00
int ite = atoi(te.c_str());
2021-12-25 10:04:45 +08:00
int is = atoi(s.c_str());
int iVersion = it * 1000 + ih * 100 + ite * 10 + is;
// CCLOG("iversion %d",iVersion);
2019-11-23 20:27:39 +08:00
return iVersion;
/************************/
}
2021-12-25 10:04:45 +08:00
void GUIReader::storeFileDesignSize(const char* fileName, const cocos2d::Size& size)
2019-11-23 20:27:39 +08:00
{
std::string keyWidth = fileName;
keyWidth.append("width");
std::string keyHeight = fileName;
keyHeight.append("height");
2021-12-25 10:04:45 +08:00
_fileDesignSizes[keyWidth] = cocos2d::Value(size.width);
2019-11-23 20:27:39 +08:00
_fileDesignSizes[keyHeight] = cocos2d::Value(size.height);
}
cocos2d::Size GUIReader::getFileDesignSize(const char* fileName) const
{
std::string keyWidth = fileName;
keyWidth.append("width");
std::string keyHeight = fileName;
keyHeight.append("height");
2020-08-18 11:33:18 +08:00
float w = _fileDesignSizes.at(keyWidth).asFloat();
float h = _fileDesignSizes.at(keyHeight).asFloat();
2019-11-23 20:27:39 +08:00
return Size(w, h);
}
2021-12-25 10:04:45 +08:00
2021-12-26 23:26:34 +08:00
void GUIReader::registerTypeAndCallBack(std::string_view classType,
2019-11-23 20:27:39 +08:00
ObjectFactory::Instance ins,
2021-12-25 10:04:45 +08:00
Ref* object,
2019-11-23 20:27:39 +08:00
SEL_ParseEvent callBack)
{
ObjectFactory* factoryCreate = ObjectFactory::getInstance();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ObjectFactory::TInfo t(classType, ins);
factoryCreate->registerType(t);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (object)
{
_mapObject.insert(ParseObjectMap::value_type(classType, object));
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (callBack)
{
_mapParseSelector.insert(ParseCallBackMap::value_type(classType, callBack));
}
}
2021-12-26 23:26:34 +08:00
void GUIReader::registerTypeAndCallBack(std::string_view classType,
2019-11-23 20:27:39 +08:00
ObjectFactory::InstanceFunc ins,
2021-12-25 10:04:45 +08:00
Ref* object,
2019-11-23 20:27:39 +08:00
SEL_ParseEvent callBack)
{
ObjectFactory* factoryCreate = ObjectFactory::getInstance();
ObjectFactory::TInfo t(classType, ins);
factoryCreate->registerType(t);
if (object)
{
_mapObject.insert(ParseObjectMap::value_type(classType, object));
}
if (callBack)
{
_mapParseSelector.insert(ParseCallBackMap::value_type(classType, callBack));
}
}
2021-12-25 10:04:45 +08:00
Widget* GUIReader::widgetFromJsonFile(const char* fileName)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
std::string jsonpath;
rapidjson::Document jsonDict;
2019-11-23 20:27:39 +08:00
jsonpath = fileName;
2021-12-25 10:04:45 +08:00
// jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
size_t pos = jsonpath.find_last_of('/');
m_strFilePath = jsonpath.substr(0, pos + 1);
2019-11-23 20:27:39 +08:00
std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath);
2021-12-25 10:04:45 +08:00
jsonDict.Parse<0>(contentStr.c_str());
2019-11-23 20:27:39 +08:00
if (jsonDict.HasParseError())
{
2021-12-25 10:04:45 +08:00
CCLOG("GetParseError %d\n", jsonDict.GetParseError());
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
Widget* widget = nullptr;
const char* fileVersion = DICTOOL->getStringValue_json(jsonDict, "version");
WidgetPropertiesReader* pReader = nullptr;
2019-11-23 20:27:39 +08:00
if (fileVersion)
{
int versionInteger = this->getVersionInteger(fileVersion);
2019-11-23 20:27:39 +08:00
if (versionInteger < 250)
{
2021-12-08 00:11:53 +08:00
pReader = new WidgetPropertiesReader0250();
2021-12-25 10:04:45 +08:00
widget = pReader->createWidget(jsonDict, this->m_strFilePath.c_str(), fileName);
2019-11-23 20:27:39 +08:00
}
else
{
2021-12-08 00:11:53 +08:00
pReader = new WidgetPropertiesReader0300();
2021-12-25 10:04:45 +08:00
widget = pReader->createWidget(jsonDict, this->m_strFilePath.c_str(), fileName);
2019-11-23 20:27:39 +08:00
}
}
else
{
2021-12-08 00:11:53 +08:00
pReader = new WidgetPropertiesReader0250();
2021-12-25 10:04:45 +08:00
widget = pReader->createWidget(jsonDict, this->m_strFilePath.c_str(), fileName);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(pReader);
return widget;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
std::string WidgetPropertiesReader::getWidgetReaderClassName(Widget* widget)
{
std::string readerName;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// 1st., custom widget parse properties of parent widget with parent widget reader
if (dynamic_cast<Button*>(widget))
{
readerName = "ButtonReader";
}
else if (dynamic_cast<CheckBox*>(widget))
{
readerName = "CheckBoxReader";
}
else if (dynamic_cast<ImageView*>(widget))
{
readerName = "ImageViewReader";
}
else if (dynamic_cast<TextAtlas*>(widget))
{
readerName = "TextAtlasReader";
}
else if (dynamic_cast<TextBMFont*>(widget))
{
readerName = "TextBMFontReader";
}
else if (dynamic_cast<Text*>(widget))
{
readerName = "TextReader";
}
else if (dynamic_cast<LoadingBar*>(widget))
{
readerName = "LoadingBarReader";
}
else if (dynamic_cast<Slider*>(widget))
{
readerName = "SliderReader";
}
else if (dynamic_cast<TextField*>(widget))
{
readerName = "TextFieldReader";
}
else if (dynamic_cast<ListView*>(widget))
{
readerName = "ListViewReader";
}
else if (dynamic_cast<PageView*>(widget))
{
readerName = "PageViewReader";
}
else if (dynamic_cast<ScrollView*>(widget))
{
readerName = "ScrollViewReader";
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
else if (dynamic_cast<Layout*>(widget))
{
readerName = "LayoutReader";
}
else if (dynamic_cast<Widget*>(widget))
{
readerName = "WidgetReader";
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return readerName;
}
2021-12-26 23:26:34 +08:00
std::string WidgetPropertiesReader::getGUIClassName(std::string_view name)
2019-11-23 20:27:39 +08:00
{
2021-12-27 13:52:08 +08:00
std::string convertedClassName;
2019-11-23 20:27:39 +08:00
if (name == "Panel")
{
convertedClassName = "Layout";
}
else if (name == "TextArea")
{
convertedClassName = "Text";
}
else if (name == "TextButton")
{
convertedClassName = "Button";
}
else if (name == "Label")
{
convertedClassName = "Text";
}
else if (name == "LabelAtlas")
{
convertedClassName = "TextAtlas";
}
else if (name == "LabelBMFont")
{
convertedClassName = "TextBMFont";
}
2021-12-27 13:52:08 +08:00
else
convertedClassName = name;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return convertedClassName;
}
2021-12-25 10:04:45 +08:00
2021-12-26 23:26:34 +08:00
cocos2d::ui::Widget* WidgetPropertiesReader::createGUI(std::string_view classname)
2019-11-23 20:27:39 +08:00
{
std::string name = this->getGUIClassName(classname);
Ref* object = ObjectFactory::getInstance()->createObject(name);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return dynamic_cast<ui::Widget*>(object);
}
2021-12-26 23:26:34 +08:00
WidgetReaderProtocol* WidgetPropertiesReader::createWidgetReaderProtocol(std::string_view classname)
2019-11-23 20:27:39 +08:00
{
Ref* object = ObjectFactory::getInstance()->createObject(classname);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return dynamic_cast<WidgetReaderProtocol*>(object);
}
2021-12-25 10:04:45 +08:00
Widget* GUIReader::widgetFromBinaryFile(const char* fileName)
2019-11-23 20:27:39 +08:00
{
std::string jsonpath;
rapidjson::Document jsonDict;
jsonpath = fileName;
2021-12-25 10:04:45 +08:00
// jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
size_t pos = jsonpath.find_last_of('/');
m_strFilePath = jsonpath.substr(0, pos + 1);
2019-11-23 20:27:39 +08:00
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
2021-12-25 10:04:45 +08:00
auto fileData = FileUtils::getInstance()->getDataFromFile(fullPath);
auto fileDataBytes = fileData.getBytes();
auto fileDataSize = fileData.getSize();
2020-10-17 16:32:16 +08:00
const char* fileVersion = "";
2021-12-25 10:04:45 +08:00
ui::Widget* widget = nullptr;
2019-11-23 20:27:39 +08:00
if (fileDataBytes != nullptr && fileDataSize > 0)
{
2021-12-25 10:04:45 +08:00
CocoLoader tCocoLoader;
if (true == tCocoLoader.ReadCocoBinBuff((char*)fileDataBytes))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* tpRootCocoNode = tCocoLoader.GetRootCocoNode();
2019-11-23 20:27:39 +08:00
rapidjson::Type tType = tpRootCocoNode->GetType(&tCocoLoader);
if (rapidjson::kObjectType == tType || rapidjson::kArrayType == tType)
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* tpChildArray = tpRootCocoNode->GetChildArray(&tCocoLoader);
for (int i = 0; i < tpRootCocoNode->GetChildNum(); ++i)
{
2019-11-23 20:27:39 +08:00
std::string key = tpChildArray[i].GetName(&tCocoLoader);
2021-12-25 10:04:45 +08:00
if (key == "version")
{
2019-11-23 20:27:39 +08:00
fileVersion = tpChildArray[i].GetValue(&tCocoLoader);
break;
}
}
2021-12-25 10:04:45 +08:00
WidgetPropertiesReader* pReader = nullptr;
2019-11-23 20:27:39 +08:00
if (fileVersion)
{
int versionInteger = this->getVersionInteger(fileVersion);
2019-11-23 20:27:39 +08:00
if (versionInteger < 250)
{
2021-12-25 10:04:45 +08:00
CCASSERT(
0,
"You current studio doesn't support binary format, please upgrade to the latest version!");
2021-12-08 00:11:53 +08:00
pReader = new WidgetPropertiesReader0250();
2021-12-25 10:04:45 +08:00
widget = pReader->createWidgetFromBinary(&tCocoLoader, tpRootCocoNode, fileName);
2019-11-23 20:27:39 +08:00
}
else
{
2021-12-08 00:11:53 +08:00
pReader = new WidgetPropertiesReader0300();
2021-12-25 10:04:45 +08:00
widget = pReader->createWidgetFromBinary(&tCocoLoader, tpRootCocoNode, fileName);
2019-11-23 20:27:39 +08:00
}
}
else
{
2021-12-08 00:11:53 +08:00
pReader = new WidgetPropertiesReader0250();
2021-12-25 10:04:45 +08:00
widget = pReader->createWidgetFromBinary(&tCocoLoader, tpRootCocoNode, fileName);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
CC_SAFE_DELETE(pReader);
2019-11-23 20:27:39 +08:00
}
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return widget;
}
2021-12-26 23:26:34 +08:00
std::string WidgetPropertiesReader::getWidgetReaderClassName(std::string_view classname)
2019-11-23 20:27:39 +08:00
{
// create widget reader to parse properties of widget
2021-12-27 13:52:08 +08:00
std::string readerName;
2019-11-23 20:27:39 +08:00
if (readerName == "Panel")
{
readerName = "Layout";
}
else if (readerName == "TextArea")
{
readerName = "Text";
}
else if (readerName == "TextButton")
{
readerName = "Button";
}
else if (readerName == "Label")
{
readerName = "Text";
}
else if (readerName == "LabelAtlas")
{
readerName = "TextAtlas";
}
else if (readerName == "LabelBMFont")
{
readerName = "TextBMFont";
}
2021-12-27 13:52:08 +08:00
else
readerName = classname;
2019-11-23 20:27:39 +08:00
readerName.append("Reader");
return readerName;
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader::setAnchorPointForWidget(cocos2d::ui::Widget* widget, const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
bool isAnchorPointXExists = DICTOOL->checkObjectExist_json(options, "anchorPointX");
float anchorPointXInFile;
2021-12-25 10:04:45 +08:00
if (isAnchorPointXExists)
{
2019-11-23 20:27:39 +08:00
anchorPointXInFile = DICTOOL->getFloatValue_json(options, "anchorPointX");
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
anchorPointXInFile = widget->getAnchorPoint().x;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool isAnchorPointYExists = DICTOOL->checkObjectExist_json(options, "anchorPointY");
float anchorPointYInFile;
2021-12-25 10:04:45 +08:00
if (isAnchorPointYExists)
{
2019-11-23 20:27:39 +08:00
anchorPointYInFile = DICTOOL->getFloatValue_json(options, "anchorPointY");
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
anchorPointYInFile = widget->getAnchorPoint().y;
}
2021-12-25 10:04:45 +08:00
if (isAnchorPointXExists || isAnchorPointYExists)
{
2019-11-23 20:27:39 +08:00
widget->setAnchorPoint(Vec2(anchorPointXInFile, anchorPointYInFile));
}
}
2021-12-25 10:04:45 +08:00
Widget* WidgetPropertiesReader0250::createWidget(const rapidjson::Value& data,
const char* fullPath,
const char* fileName)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
m_strFilePath = fullPath;
2019-11-23 20:27:39 +08:00
int texturesCount = DICTOOL->getArrayCount_json(data, "textures");
2021-12-25 10:04:45 +08:00
for (int i = 0; i < texturesCount; i++)
2019-11-23 20:27:39 +08:00
{
const char* file = DICTOOL->getStringValueFromArray_json(data, "textures", i);
2021-12-25 10:04:45 +08:00
std::string tp = fullPath;
2019-11-23 20:27:39 +08:00
tp.append(file);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(tp);
}
2021-12-25 10:04:45 +08:00
float fileDesignWidth = DICTOOL->getFloatValue_json(data, "designWidth");
2019-11-23 20:27:39 +08:00
float fileDesignHeight = DICTOOL->getFloatValue_json(data, "designHeight");
2021-12-25 10:04:45 +08:00
if (fileDesignWidth <= 0 || fileDesignHeight <= 0)
{
2019-11-23 20:27:39 +08:00
CCLOGERROR("Read design size error!\n");
Size winSize = Director::getInstance()->getWinSize();
GUIReader::getInstance()->storeFileDesignSize(fileName, winSize);
}
else
{
GUIReader::getInstance()->storeFileDesignSize(fileName, Size(fileDesignWidth, fileDesignHeight));
}
const rapidjson::Value& widgetTree = DICTOOL->getSubDictionary_json(data, "widgetTree");
2021-12-25 10:04:45 +08:00
Widget* widget = widgetFromJsonDictionary(widgetTree);
2019-11-23 20:27:39 +08:00
/* *********temp********* */
if (widget->getContentSize().equals(Size::ZERO))
{
Layout* rootWidget = dynamic_cast<Layout*>(widget);
rootWidget->setContentSize(Size(fileDesignWidth, fileDesignHeight));
}
/* ********************** */
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// widget->setFileDesignSize(Size(fileDesignWidth, fileDesignHeight));
const rapidjson::Value& actions = DICTOOL->getSubDictionary_json(data, "animation");
/* *********temp********* */
// ActionManager::getInstance()->releaseActions();
/* ********************** */
2021-12-25 10:04:45 +08:00
// CCLOG("file name == [%s]",fileName);
Ref* rootWidget = (Ref*)widget;
ActionManagerEx::getInstance()->initWithDictionary(fileName, actions, rootWidget);
2019-11-23 20:27:39 +08:00
return widget;
}
2021-12-25 10:04:45 +08:00
Widget* WidgetPropertiesReader0250::widgetFromJsonDictionary(const rapidjson::Value& data)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Widget* widget = nullptr;
const char* classname = DICTOOL->getStringValue_json(data, "classname");
2019-11-23 20:27:39 +08:00
const rapidjson::Value& uiOptions = DICTOOL->getSubDictionary_json(data, "options");
if (classname && strcmp(classname, "Button") == 0)
{
widget = cocos2d::ui::Button::create();
setPropsForButtonFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "CheckBox") == 0)
{
widget = CheckBox::create();
setPropsForCheckBoxFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "Label") == 0)
{
widget = cocos2d::ui::Text::create();
setPropsForLabelFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "LabelAtlas") == 0)
{
widget = cocos2d::ui::TextAtlas::create();
setPropsForLabelAtlasFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "LoadingBar") == 0)
{
widget = cocos2d::ui::LoadingBar::create();
setPropsForLoadingBarFromJsonDictionary(widget, uiOptions);
2021-12-25 10:04:45 +08:00
}
else if (classname && strcmp(classname, "ScrollView") == 0)
{
2019-11-23 20:27:39 +08:00
widget = cocos2d::ui::ScrollView::create();
setPropsForScrollViewFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "TextArea") == 0)
{
widget = cocos2d::ui::Text::create();
setPropsForLabelFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "TextButton") == 0)
{
widget = cocos2d::ui::Button::create();
setPropsForButtonFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "TextField") == 0)
{
widget = cocos2d::ui::TextField::create();
setPropsForTextFieldFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "ImageView") == 0)
{
widget = cocos2d::ui::ImageView::create();
setPropsForImageViewFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "Panel") == 0)
{
widget = Layout::create();
setPropsForLayoutFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "Slider") == 0)
{
widget = cocos2d::ui::Slider::create();
setPropsForSliderFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "LabelBMFont") == 0)
{
widget = cocos2d::ui::TextBMFont::create();
setPropsForLabelBMFontFromJsonDictionary(widget, uiOptions);
}
else if (classname && strcmp(classname, "DragPanel") == 0)
{
widget = cocos2d::ui::ScrollView::create();
setPropsForScrollViewFromJsonDictionary(widget, uiOptions);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int childrenCount = DICTOOL->getArrayCount_json(data, "children");
2021-12-25 10:04:45 +08:00
for (int i = 0; i < childrenCount; i++)
2019-11-23 20:27:39 +08:00
{
const rapidjson::Value& subData = DICTOOL->getDictionaryFromArray_json(data, "children", i);
2021-12-25 10:04:45 +08:00
Widget* child = widgetFromJsonDictionary(subData);
2019-11-23 20:27:39 +08:00
if (child)
{
widget->addChild(child);
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return widget;
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForWidgetFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
bool ignoreSizeExsit = DICTOOL->checkObjectExist_json(options, "ignoreSize");
if (ignoreSizeExsit)
{
widget->ignoreContentAdaptWithSize(DICTOOL->getBooleanValue_json(options, "ignoreSize"));
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float w = DICTOOL->getFloatValue_json(options, "width");
float h = DICTOOL->getFloatValue_json(options, "height");
widget->setContentSize(Size(w, h));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
widget->setTag(DICTOOL->getIntValue_json(options, "tag"));
2021-12-25 10:04:45 +08:00
widget->setActionTag(DICTOOL->getIntValue_json(options, "actiontag"));
2019-11-23 20:27:39 +08:00
widget->setTouchEnabled(DICTOOL->getBooleanValue_json(options, "touchAble"));
2021-12-25 10:04:45 +08:00
const char* name = DICTOOL->getStringValue_json(options, "name");
const char* widgetName = name ? name : "default";
2019-11-23 20:27:39 +08:00
widget->setName(widgetName);
float x = DICTOOL->getFloatValue_json(options, "x");
float y = DICTOOL->getFloatValue_json(options, "y");
2021-12-25 10:04:45 +08:00
widget->setPosition(Vec2(x, y));
2019-11-23 20:27:39 +08:00
bool sx = DICTOOL->checkObjectExist_json(options, "scaleX");
if (sx)
{
widget->setScaleX(DICTOOL->getFloatValue_json(options, "scaleX"));
}
bool sy = DICTOOL->checkObjectExist_json(options, "scaleY");
if (sy)
{
widget->setScaleY(DICTOOL->getFloatValue_json(options, "scaleY"));
}
bool rt = DICTOOL->checkObjectExist_json(options, "rotation");
if (rt)
{
widget->setRotation(DICTOOL->getFloatValue_json(options, "rotation"));
}
bool vb = DICTOOL->checkObjectExist_json(options, "visible");
if (vb)
{
widget->setVisible(DICTOOL->getBooleanValue_json(options, "visible"));
}
int z = DICTOOL->getIntValue_json(options, "ZOrder");
widget->setLocalZOrder(z);
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setColorPropsForWidgetFromJsonDictionary(Widget* widget,
const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
bool op = DICTOOL->checkObjectExist_json(options, "opacity");
if (op)
{
widget->setOpacity(DICTOOL->getIntValue_json(options, "opacity"));
}
2021-12-25 10:04:45 +08:00
bool cr = DICTOOL->checkObjectExist_json(options, "colorR");
bool cg = DICTOOL->checkObjectExist_json(options, "colorG");
bool cb = DICTOOL->checkObjectExist_json(options, "colorB");
2019-11-23 20:27:39 +08:00
int colorR = cr ? DICTOOL->getIntValue_json(options, "colorR") : 255;
int colorG = cg ? DICTOOL->getIntValue_json(options, "colorG") : 255;
int colorB = cb ? DICTOOL->getIntValue_json(options, "colorB") : 255;
widget->setColor(Color3B(colorR, colorG, colorB));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
this->setAnchorPointForWidget(widget, options);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool flipX = DICTOOL->getBooleanValue_json(options, "flipX");
bool flipY = DICTOOL->getBooleanValue_json(options, "flipY");
widget->setFlippedX(flipX);
widget->setFlippedY(flipY);
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForButtonFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForWidgetFromJsonDictionary(widget, options);
cocos2d::ui::Button* button = static_cast<Button*>(widget);
2021-12-25 10:04:45 +08:00
bool scale9Enable = DICTOOL->getBooleanValue_json(options, "scale9Enable");
2019-11-23 20:27:39 +08:00
button->setScale9Enabled(scale9Enable);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
std::string tp_n = m_strFilePath;
std::string tp_p = m_strFilePath;
std::string tp_d = m_strFilePath;
2021-12-25 10:04:45 +08:00
const char* normalFileName = DICTOOL->getStringValue_json(options, "normal");
const char* pressedFileName = DICTOOL->getStringValue_json(options, "pressed");
2019-11-23 20:27:39 +08:00
const char* disabledFileName = DICTOOL->getStringValue_json(options, "disabled");
2021-12-25 10:04:45 +08:00
const char* normalFileName_tp =
(normalFileName && (strcmp(normalFileName, "") != 0)) ? tp_n.append(normalFileName).c_str() : nullptr;
const char* pressedFileName_tp =
(pressedFileName && (strcmp(pressedFileName, "") != 0)) ? tp_p.append(pressedFileName).c_str() : nullptr;
const char* disabledFileName_tp =
(disabledFileName && (strcmp(disabledFileName, "") != 0)) ? tp_d.append(disabledFileName).c_str() : nullptr;
2019-11-23 20:27:39 +08:00
bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
if (scale9Enable)
{
float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
button->loadTextures(normalFileName, pressedFileName, disabledFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
button->loadTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp);
}
button->setCapInsets(Rect(cx, cy, cw, ch));
bool sw = DICTOOL->checkObjectExist_json(options, "scale9Width");
bool sh = DICTOOL->checkObjectExist_json(options, "scale9Height");
if (sw && sh)
{
float swf = DICTOOL->getFloatValue_json(options, "scale9Width");
float shf = DICTOOL->getFloatValue_json(options, "scale9Height");
button->setContentSize(Size(swf, shf));
}
}
else
{
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
button->loadTextures(normalFileName, pressedFileName, disabledFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
button->loadTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp);
}
}
bool tt = DICTOOL->checkObjectExist_json(options, "text");
if (tt)
{
const char* text = DICTOOL->getStringValue_json(options, "text");
if (text)
{
button->setTitleText(text);
}
}
bool cr = DICTOOL->checkObjectExist_json(options, "textColorR");
bool cg = DICTOOL->checkObjectExist_json(options, "textColorG");
bool cb = DICTOOL->checkObjectExist_json(options, "textColorB");
2021-12-25 10:04:45 +08:00
int cri = cr ? DICTOOL->getIntValue_json(options, "textColorR") : 255;
int cgi = cg ? DICTOOL->getIntValue_json(options, "textColorG") : 255;
int cbi = cb ? DICTOOL->getIntValue_json(options, "textColorB") : 255;
button->setTitleColor(Color3B(cri, cgi, cbi));
2019-11-23 20:27:39 +08:00
bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
if (fs)
{
button->setTitleFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
}
bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
if (fn)
{
2021-12-25 10:04:45 +08:00
const char* szTemp = DICTOOL->getStringValue_json(options, "fontName");
2019-11-23 20:27:39 +08:00
if (szTemp && *szTemp)
button->setTitleFontName(szTemp);
else
button->setTitleFontName(std::string(""));
}
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForCheckBoxFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForWidgetFromJsonDictionary(widget, options);
2021-12-25 10:04:45 +08:00
CheckBox* checkBox = static_cast<CheckBox*>(widget);
const char* backGroundFileName = DICTOOL->getStringValue_json(options, "backGroundBox");
2019-11-23 20:27:39 +08:00
const char* backGroundSelectedFileName = DICTOOL->getStringValue_json(options, "backGroundBoxSelected");
2021-12-25 10:04:45 +08:00
const char* frontCrossFileName = DICTOOL->getStringValue_json(options, "frontCross");
2019-11-23 20:27:39 +08:00
const char* backGroundDisabledFileName = DICTOOL->getStringValue_json(options, "backGroundBoxDisabled");
const char* frontCrossDisabledFileName = DICTOOL->getStringValue_json(options, "frontCrossDisabled");
2021-12-25 10:04:45 +08:00
std::string tp_b = m_strFilePath;
2019-11-23 20:27:39 +08:00
std::string tp_bs = m_strFilePath;
2021-12-25 10:04:45 +08:00
std::string tp_c = m_strFilePath;
2019-11-23 20:27:39 +08:00
std::string tp_bd = m_strFilePath;
std::string tp_cd = m_strFilePath;
2021-12-25 10:04:45 +08:00
const char* backGroundFileName_tp = (backGroundFileName && (strcmp(backGroundFileName, "") != 0))
? tp_b.append(backGroundFileName).c_str()
: nullptr;
const char* backGroundSelectedFileName_tp =
(backGroundSelectedFileName && (strcmp(backGroundSelectedFileName, "") != 0))
? tp_bs.append(backGroundSelectedFileName).c_str()
: nullptr;
const char* frontCrossFileName_tp = (frontCrossFileName && (strcmp(frontCrossFileName, "") != 0))
? tp_c.append(frontCrossFileName).c_str()
: nullptr;
const char* backGroundDisabledFileName_tp =
(backGroundDisabledFileName && (strcmp(backGroundDisabledFileName, "") != 0))
? tp_bd.append(backGroundDisabledFileName).c_str()
: nullptr;
const char* frontCrossDisabledFileName_tp =
(frontCrossDisabledFileName && (strcmp(frontCrossDisabledFileName, "") != 0))
? tp_cd.append(frontCrossDisabledFileName).c_str()
: nullptr;
2019-11-23 20:27:39 +08:00
bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
checkBox->loadTextures(backGroundFileName, backGroundSelectedFileName, frontCrossFileName,
backGroundDisabledFileName, frontCrossDisabledFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
2021-12-25 10:04:45 +08:00
checkBox->loadTextures(backGroundFileName_tp, backGroundSelectedFileName_tp, frontCrossFileName_tp,
backGroundDisabledFileName_tp, frontCrossDisabledFileName_tp);
2019-11-23 20:27:39 +08:00
}
checkBox->setSelected(DICTOOL->getBooleanValue_json(options, "selectedState"));
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForImageViewFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForWidgetFromJsonDictionary(widget, options);
cocos2d::ui::ImageView* imageView = static_cast<ImageView*>(widget);
2021-12-25 10:04:45 +08:00
const char* imageFileName = DICTOOL->getStringValue_json(options, "fileName");
bool scale9EnableExist = DICTOOL->checkObjectExist_json(options, "scale9Enable");
bool scale9Enable = false;
2019-11-23 20:27:39 +08:00
if (scale9EnableExist)
{
scale9Enable = DICTOOL->getBooleanValue_json(options, "scale9Enable");
}
imageView->setScale9Enabled(scale9Enable);
2021-12-25 10:04:45 +08:00
std::string tp_i = m_strFilePath;
2019-11-23 20:27:39 +08:00
const char* imageFileName_tp = nullptr;
if (imageFileName && (strcmp(imageFileName, "") != 0))
{
imageFileName_tp = tp_i.append(imageFileName).c_str();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
if (scale9Enable)
{
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
imageView->loadTexture(imageFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
imageView->loadTexture(imageFileName_tp);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool sw = DICTOOL->checkObjectExist_json(options, "scale9Width");
bool sh = DICTOOL->checkObjectExist_json(options, "scale9Height");
if (sw && sh)
{
float swf = DICTOOL->getFloatValue_json(options, "scale9Width");
float shf = DICTOOL->getFloatValue_json(options, "scale9Height");
imageView->setContentSize(Size(swf, shf));
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
imageView->setCapInsets(Rect(cx, cy, cw, ch));
}
else
{
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
imageView->loadTexture(imageFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
imageView->loadTexture(imageFileName_tp);
}
}
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForLabelFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForWidgetFromJsonDictionary(widget, options);
2021-12-25 10:04:45 +08:00
cocos2d::ui::Text* label = static_cast<cocos2d::ui::Text*>(widget);
2019-11-23 20:27:39 +08:00
bool touchScaleChangeAble = DICTOOL->getBooleanValue_json(options, "touchScaleEnable");
label->setTouchScaleChangeEnabled(touchScaleChangeAble);
const char* text = DICTOOL->getStringValue_json(options, "text");
label->setString(text);
bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
if (fs)
{
label->setFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
}
bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
if (fn)
{
2021-12-25 10:04:45 +08:00
const char* szTemp = DICTOOL->getStringValue_json(options, "fontName");
2019-11-23 20:27:39 +08:00
if (szTemp && *szTemp)
label->setFontName(szTemp);
else
label->setFontName(std::string(""));
}
bool aw = DICTOOL->checkObjectExist_json(options, "areaWidth");
bool ah = DICTOOL->checkObjectExist_json(options, "areaHeight");
if (aw && ah)
{
2021-12-25 10:04:45 +08:00
Size size =
Size(DICTOOL->getFloatValue_json(options, "areaWidth"), DICTOOL->getFloatValue_json(options, "areaHeight"));
2019-11-23 20:27:39 +08:00
label->setTextAreaSize(size);
}
bool ha = DICTOOL->checkObjectExist_json(options, "hAlignment");
if (ha)
{
label->setTextHorizontalAlignment((TextHAlignment)DICTOOL->getIntValue_json(options, "hAlignment"));
}
bool va = DICTOOL->checkObjectExist_json(options, "vAlignment");
if (va)
{
label->setTextVerticalAlignment((TextVAlignment)DICTOOL->getIntValue_json(options, "vAlignment"));
}
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForLabelAtlasFromJsonDictionary(Widget* widget,
const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForWidgetFromJsonDictionary(widget, options);
cocos2d::ui::TextAtlas* labelAtlas = static_cast<cocos2d::ui::TextAtlas*>(widget);
2021-12-25 10:04:45 +08:00
bool sv = DICTOOL->checkObjectExist_json(options, "stringValue");
bool cmf = DICTOOL->checkObjectExist_json(options, "charMapFile");
bool iw = DICTOOL->checkObjectExist_json(options, "itemWidth");
bool ih = DICTOOL->checkObjectExist_json(options, "itemHeight");
bool scm = DICTOOL->checkObjectExist_json(options, "startCharMap");
2019-11-23 20:27:39 +08:00
if (sv && cmf && iw && ih && scm && (strcmp(DICTOOL->getStringValue_json(options, "charMapFile"), "") != 0))
{
2021-12-25 10:04:45 +08:00
std::string tp_c = m_strFilePath;
const char* cmft = DICTOOL->getStringValue_json(options, "charMapFile");
2019-11-23 20:27:39 +08:00
const char* cmf_tp = tp_c.append(cmft).c_str();
2021-12-25 10:04:45 +08:00
labelAtlas->setProperty(DICTOOL->getStringValue_json(options, "stringValue"), cmf_tp,
DICTOOL->getIntValue_json(options, "itemWidth"),
DICTOOL->getIntValue_json(options, "itemHeight"),
DICTOOL->getStringValue_json(options, "startCharMap"));
labelAtlas->setProperty(DICTOOL->getStringValue_json(options, "stringValue"), cmf_tp,
DICTOOL->getIntValue_json(options, "itemWidth") / CC_CONTENT_SCALE_FACTOR(),
DICTOOL->getIntValue_json(options, "itemHeight") / CC_CONTENT_SCALE_FACTOR(),
DICTOOL->getStringValue_json(options, "startCharMap"));
}
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForLayoutFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForWidgetFromJsonDictionary(widget, options);
Layout* containerWidget = static_cast<Layout*>(widget);
2021-12-25 10:04:45 +08:00
if (!dynamic_cast<cocos2d::ui::ScrollView*>(containerWidget) &&
!dynamic_cast<cocos2d::ui::ListView*>(containerWidget))
2019-11-23 20:27:39 +08:00
{
containerWidget->setClippingEnabled(DICTOOL->getBooleanValue_json(options, "clipAble"));
}
2021-12-25 10:04:45 +08:00
Layout* panel = (Layout*)widget;
2019-11-23 20:27:39 +08:00
bool backGroundScale9Enable = DICTOOL->getBooleanValue_json(options, "backGroundScale9Enable");
panel->setBackGroundImageScale9Enabled(backGroundScale9Enable);
int cr = DICTOOL->getIntValue_json(options, "bgColorR");
int cg = DICTOOL->getIntValue_json(options, "bgColorG");
int cb = DICTOOL->getIntValue_json(options, "bgColorB");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int scr = DICTOOL->getIntValue_json(options, "bgStartColorR");
int scg = DICTOOL->getIntValue_json(options, "bgStartColorG");
int scb = DICTOOL->getIntValue_json(options, "bgStartColorB");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int ecr = DICTOOL->getIntValue_json(options, "bgEndColorR");
int ecg = DICTOOL->getIntValue_json(options, "bgEndColorG");
int ecb = DICTOOL->getIntValue_json(options, "bgEndColorB");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float bgcv1 = DICTOOL->getFloatValue_json(options, "vectorX");
float bgcv2 = DICTOOL->getFloatValue_json(options, "vectorY");
panel->setBackGroundColorVector(Vec2(bgcv1, bgcv2));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int co = DICTOOL->getIntValue_json(options, "bgColorOpacity");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int colorType = DICTOOL->getIntValue_json(options, "colorType");
panel->setBackGroundColorType(Layout::BackGroundColorType(colorType));
2021-12-25 10:04:45 +08:00
panel->setBackGroundColor(Color3B(scr, scg, scb), Color3B(ecr, ecg, ecb));
2019-11-23 20:27:39 +08:00
panel->setBackGroundColor(Color3B(cr, cg, cb));
panel->setBackGroundColorOpacity(co);
2021-12-25 10:04:45 +08:00
std::string tp_b = m_strFilePath;
2019-11-23 20:27:39 +08:00
const char* imageFileName = DICTOOL->getStringValue_json(options, "backGroundImage");
2021-12-25 10:04:45 +08:00
const char* imageFileName_tp =
(imageFileName && (strcmp(imageFileName, "") != 0)) ? tp_b.append(imageFileName).c_str() : nullptr;
2019-11-23 20:27:39 +08:00
bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
if (backGroundScale9Enable)
{
float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
panel->setBackGroundImage(imageFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
panel->setBackGroundImage(imageFileName_tp);
}
panel->setBackGroundImageCapInsets(Rect(cx, cy, cw, ch));
}
else
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
panel->setBackGroundImage(imageFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
panel->setBackGroundImage(imageFileName_tp);
}
}
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForScrollViewFromJsonDictionary(Widget* widget,
const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForLayoutFromJsonDictionary(widget, options);
cocos2d::ui::ScrollView* scrollView = static_cast<cocos2d::ui::ScrollView*>(widget);
2021-12-25 10:04:45 +08:00
float innerWidth = DICTOOL->getFloatValue_json(options, "innerWidth");
float innerHeight = DICTOOL->getFloatValue_json(options, "innerHeight");
2019-11-23 20:27:39 +08:00
scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
2021-12-25 10:04:45 +08:00
int direction = DICTOOL->getFloatValue_json(options, "direction");
2019-11-23 20:27:39 +08:00
scrollView->setDirection((ScrollView::Direction)direction);
scrollView->setBounceEnabled(DICTOOL->getBooleanValue_json(options, "bounceEnable"));
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForSliderFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForWidgetFromJsonDictionary(widget, options);
cocos2d::ui::Slider* slider = static_cast<cocos2d::ui::Slider*>(widget);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool barTextureScale9Enable = DICTOOL->getBooleanValue_json(options, "barTextureScale9Enable");
slider->setScale9Enabled(barTextureScale9Enable);
2021-12-25 10:04:45 +08:00
bool bt = DICTOOL->checkObjectExist_json(options, "barFileName");
float barLength = DICTOOL->getFloatValue_json(options, "length");
2019-11-23 20:27:39 +08:00
bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
if (bt)
{
if (barTextureScale9Enable)
{
2021-12-25 10:04:45 +08:00
std::string tp_b = m_strFilePath;
2019-11-23 20:27:39 +08:00
const char* imageFileName = DICTOOL->getStringValue_json(options, "barFileName");
2021-12-25 10:04:45 +08:00
const char* imageFileName_tp =
(imageFileName && (strcmp(imageFileName, "") != 0)) ? tp_b.append(imageFileName).c_str() : nullptr;
2019-11-23 20:27:39 +08:00
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
slider->loadBarTexture(imageFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
slider->loadBarTexture(imageFileName_tp);
}
slider->setContentSize(Size(barLength, slider->getContentSize().height));
}
else
{
2021-12-25 10:04:45 +08:00
std::string tp_b = m_strFilePath;
2019-11-23 20:27:39 +08:00
const char* imageFileName = DICTOOL->getStringValue_json(options, "barFileName");
2021-12-25 10:04:45 +08:00
const char* imageFileName_tp =
(imageFileName && (strcmp(imageFileName, "") != 0)) ? tp_b.append(imageFileName).c_str() : nullptr;
2019-11-23 20:27:39 +08:00
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
slider->loadBarTexture(imageFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
slider->loadBarTexture(imageFileName_tp);
}
}
}
std::string tp_n = m_strFilePath;
std::string tp_p = m_strFilePath;
std::string tp_d = m_strFilePath;
2021-12-25 10:04:45 +08:00
const char* normalFileName = DICTOOL->getStringValue_json(options, "ballNormal");
const char* pressedFileName = DICTOOL->getStringValue_json(options, "ballPressed");
2019-11-23 20:27:39 +08:00
const char* disabledFileName = DICTOOL->getStringValue_json(options, "ballDisabled");
2021-12-25 10:04:45 +08:00
const char* normalFileName_tp =
(normalFileName && (strcmp(normalFileName, "") != 0)) ? tp_n.append(normalFileName).c_str() : nullptr;
const char* pressedFileName_tp =
(pressedFileName && (strcmp(pressedFileName, "") != 0)) ? tp_p.append(pressedFileName).c_str() : nullptr;
const char* disabledFileName_tp =
(disabledFileName && (strcmp(disabledFileName, "") != 0)) ? tp_d.append(disabledFileName).c_str() : nullptr;
2019-11-23 20:27:39 +08:00
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
slider->loadSlidBallTextures(normalFileName, pressedFileName, disabledFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
2021-12-25 10:04:45 +08:00
slider->loadSlidBallTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp);
2019-11-23 20:27:39 +08:00
}
slider->setPercent(DICTOOL->getIntValue_json(options, "percent"));
2021-12-25 10:04:45 +08:00
std::string tp_b = m_strFilePath;
2019-11-23 20:27:39 +08:00
const char* imageFileName = DICTOOL->getStringValue_json(options, "progressBarFileName");
2021-12-25 10:04:45 +08:00
const char* imageFileName_tp =
(imageFileName && (strcmp(imageFileName, "") != 0)) ? tp_b.append(imageFileName).c_str() : nullptr;
2019-11-23 20:27:39 +08:00
if (useMergedTexture)
{
slider->loadProgressBarTexture(imageFileName, Widget::TextureResType::PLIST);
}
else
{
slider->loadProgressBarTexture(imageFileName_tp);
}
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForTextFieldFromJsonDictionary(Widget* widget, const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForWidgetFromJsonDictionary(widget, options);
cocos2d::ui::TextField* textField = static_cast<cocos2d::ui::TextField*>(widget);
2021-12-25 10:04:45 +08:00
bool ph = DICTOOL->checkObjectExist_json(options, "placeHolder");
2019-11-23 20:27:39 +08:00
if (ph)
{
textField->setPlaceHolder(DICTOOL->getStringValue_json(options, "placeHolder"));
}
textField->setString(DICTOOL->getStringValue_json(options, "text"));
bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
if (fs)
{
textField->setFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
}
bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
if (fn)
{
2021-12-25 10:04:45 +08:00
const char* szTemp = DICTOOL->getStringValue_json(options, "fontName");
2019-11-23 20:27:39 +08:00
if (szTemp && *szTemp)
textField->setFontName(szTemp);
else
textField->setFontName(std::string(""));
}
bool tsw = DICTOOL->checkObjectExist_json(options, "touchSizeWidth");
bool tsh = DICTOOL->checkObjectExist_json(options, "touchSizeHeight");
if (tsw && tsh)
{
2021-12-25 10:04:45 +08:00
textField->setTouchSize(Size(DICTOOL->getFloatValue_json(options, "touchSizeWidth"),
DICTOOL->getFloatValue_json(options, "touchSizeHeight")));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float dw = DICTOOL->getFloatValue_json(options, "width");
float dh = DICTOOL->getFloatValue_json(options, "height");
if (dw > 0.0f || dh > 0.0f)
{
2021-12-25 10:04:45 +08:00
// textField->setSize(Size(dw, dh));
2019-11-23 20:27:39 +08:00
}
bool maxLengthEnable = DICTOOL->getBooleanValue_json(options, "maxLengthEnable");
textField->setMaxLengthEnabled(maxLengthEnable);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (maxLengthEnable)
{
int maxLength = DICTOOL->getIntValue_json(options, "maxLength");
textField->setMaxLength(maxLength);
}
bool passwordEnable = DICTOOL->getBooleanValue_json(options, "passwordEnable");
textField->setPasswordEnabled(passwordEnable);
if (passwordEnable)
{
textField->setPasswordStyleText(DICTOOL->getStringValue_json(options, "passwordStyleText"));
}
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForLoadingBarFromJsonDictionary(Widget* widget,
const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
setPropsForWidgetFromJsonDictionary(widget, options);
cocos2d::ui::LoadingBar* loadingBar = static_cast<cocos2d::ui::LoadingBar*>(widget);
2021-12-25 10:04:45 +08:00
bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
std::string tp_b = m_strFilePath;
const char* imageFileName = DICTOOL->getStringValue_json(options, "texture");
const char* imageFileName_tp =
(imageFileName && (strcmp(imageFileName, "") != 0)) ? tp_b.append(imageFileName).c_str() : nullptr;
2019-11-23 20:27:39 +08:00
if (useMergedTexture)
{
2021-12-25 10:04:45 +08:00
loadingBar->loadTexture(imageFileName, Widget::TextureResType::PLIST);
2019-11-23 20:27:39 +08:00
}
else
{
loadingBar->loadTexture(imageFileName_tp);
}
loadingBar->setDirection(LoadingBar::Direction(DICTOOL->getIntValue_json(options, "direction")));
loadingBar->setPercent(DICTOOL->getIntValue_json(options, "percent"));
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForLabelBMFontFromJsonDictionary(Widget* widget,
const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setPropsForWidgetFromJsonDictionary(widget, options);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
cocos2d::ui::TextBMFont* labelBMFont = static_cast<cocos2d::ui::TextBMFont*>(widget);
2021-12-25 10:04:45 +08:00
std::string tp_c = m_strFilePath;
const char* cmft = DICTOOL->getStringValue_json(options, "fileName");
2019-11-23 20:27:39 +08:00
const char* cmf_tp = tp_c.append(cmft).c_str();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
labelBMFont->setFntFile(cmf_tp);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
const char* text = DICTOOL->getStringValue_json(options, "text");
labelBMFont->setString(text);
2021-12-25 10:04:45 +08:00
setColorPropsForWidgetFromJsonDictionary(widget, options);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0250::setPropsForAllWidgetFromJsonDictionary(WidgetReaderProtocol* /*reader*/,
Widget* /*widget*/,
const rapidjson::Value& /*options*/)
{}
2021-12-26 23:26:34 +08:00
void WidgetPropertiesReader0250::setPropsForAllCustomWidgetFromJsonDictionary(std::string_view /*classType*/,
2019-11-23 20:27:39 +08:00
cocos2d::ui::Widget* /*widget*/,
const rapidjson::Value& /*customOptions*/)
2021-12-25 10:04:45 +08:00
{}
2019-11-23 20:27:39 +08:00
Widget* WidgetPropertiesReader0250::createWidgetFromBinary(CocoLoader* /*cocoLoader*/,
2021-12-25 10:04:45 +08:00
stExpCocoNode* /*pCocoNode*/,
const char* /*fileName*/)
2019-11-23 20:27:39 +08:00
{
return nullptr;
}
2021-12-25 10:04:45 +08:00
Widget* WidgetPropertiesReader0250::widgetFromBinary(CocoLoader* /*cocoLoader*/, stExpCocoNode* /*pCocoNode*/)
2019-11-23 20:27:39 +08:00
{
return nullptr;
}
void WidgetPropertiesReader0250::setPropsForAllWidgetFromBinary(WidgetReaderProtocol* /*reader*/,
2021-12-25 10:04:45 +08:00
cocos2d::ui::Widget* /*widget*/,
CocoLoader* /*cocoLoader*/,
stExpCocoNode* /*pCocoNode*/)
2019-11-23 20:27:39 +08:00
{}
/*0.3.0.0~1.0.0.0*/
2021-12-25 10:04:45 +08:00
Widget* WidgetPropertiesReader0300::createWidget(const rapidjson::Value& data,
const char* fullPath,
const char* fileName)
2019-11-23 20:27:39 +08:00
{
m_strFilePath = fullPath;
int texturesCount = DICTOOL->getArrayCount_json(data, "textures");
2021-12-25 10:04:45 +08:00
for (int i = 0; i < texturesCount; i++)
2019-11-23 20:27:39 +08:00
{
const char* file = DICTOOL->getStringValueFromArray_json(data, "textures", i);
2021-12-25 10:04:45 +08:00
std::string tp = fullPath;
2019-11-23 20:27:39 +08:00
tp.append(file);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(tp);
}
2021-12-25 10:04:45 +08:00
float fileDesignWidth = DICTOOL->getFloatValue_json(data, "designWidth");
2019-11-23 20:27:39 +08:00
float fileDesignHeight = DICTOOL->getFloatValue_json(data, "designHeight");
2021-12-25 10:04:45 +08:00
if (fileDesignWidth <= 0 || fileDesignHeight <= 0)
{
2019-11-23 20:27:39 +08:00
CCLOGERROR("Read design size error!\n");
Size winSize = Director::getInstance()->getWinSize();
GUIReader::getInstance()->storeFileDesignSize(fileName, winSize);
}
else
{
GUIReader::getInstance()->storeFileDesignSize(fileName, Size(fileDesignWidth, fileDesignHeight));
}
const rapidjson::Value& widgetTree = DICTOOL->getSubDictionary_json(data, "widgetTree");
2021-12-25 10:04:45 +08:00
Widget* widget = widgetFromJsonDictionary(widgetTree);
2019-11-23 20:27:39 +08:00
/* *********temp********* */
if (widget->getContentSize().equals(Size::ZERO))
{
Layout* rootWidget = dynamic_cast<Layout*>(widget);
rootWidget->setContentSize(Size(fileDesignWidth, fileDesignHeight));
}
/* ********************** */
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// widget->setFileDesignSize(Size(fileDesignWidth, fileDesignHeight));
const rapidjson::Value& actions = DICTOOL->getSubDictionary_json(data, "animation");
/* *********temp********* */
// ActionManager::getInstance()->releaseActions();
/* ********************** */
2021-12-25 10:04:45 +08:00
// CCLOG("file name == [%s]",fileName);
Ref* rootWidget = (Ref*)widget;
ActionManagerEx::getInstance()->initWithDictionary(fileName, actions, rootWidget);
2019-11-23 20:27:39 +08:00
return widget;
}
2021-12-25 10:04:45 +08:00
cocos2d::ui::Widget* WidgetPropertiesReader0300::createWidgetFromBinary(CocoLoader* cocoLoader,
stExpCocoNode* cocoNode,
const char* fileName)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
stExpCocoNode* tpChildArray = cocoNode->GetChildArray(cocoLoader);
float fileDesignWidth = 0.0f;
float fileDesignHeight = 0.0f;
Widget* widget = nullptr;
for (int i = 0; i < cocoNode->GetChildNum(); ++i)
{
2019-11-23 20:27:39 +08:00
std::string key = tpChildArray[i].GetName(cocoLoader);
2021-12-25 10:04:45 +08:00
if (key == "textures")
{
2019-11-23 20:27:39 +08:00
int texturesCount = tpChildArray[i].GetChildNum();
2021-12-25 10:04:45 +08:00
for (int j = 0; j < texturesCount; j++)
2019-11-23 20:27:39 +08:00
{
std::string file;
2021-12-25 10:04:45 +08:00
stExpCocoNode* textureCountsArray = tpChildArray[i].GetChildArray(cocoLoader);
file = textureCountsArray[j].GetValue(cocoLoader);
2019-11-23 20:27:39 +08:00
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file);
}
2021-12-25 10:04:45 +08:00
}
else if (key == "designWidth")
{
fileDesignWidth = utils::atof(tpChildArray[i].GetValue(cocoLoader));
}
else if (key == "designHeight")
{
2019-11-23 20:27:39 +08:00
fileDesignHeight = utils::atof(tpChildArray[i].GetValue(cocoLoader));
2021-12-25 10:04:45 +08:00
}
else if (key == "widgetTree")
{
if (fileDesignWidth <= 0 || fileDesignHeight <= 0)
{
2019-11-23 20:27:39 +08:00
CCLOGERROR("Read design size error!\n");
Size winSize = Director::getInstance()->getWinSize();
GUIReader::getInstance()->storeFileDesignSize(fileName, winSize);
}
else
{
GUIReader::getInstance()->storeFileDesignSize(fileName, Size(fileDesignWidth, fileDesignHeight));
}
2021-12-25 10:04:45 +08:00
stExpCocoNode* widgetTreeNode = &tpChildArray[i];
rapidjson::Type tType = tpChildArray[i].GetType(cocoLoader);
2019-11-23 20:27:39 +08:00
if (rapidjson::kObjectType == tType)
{
widget = widgetFromBinary(cocoLoader, widgetTreeNode);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (widget->getContentSize().equals(Size::ZERO))
{
Layout* rootWidget = dynamic_cast<Layout*>(widget);
rootWidget->setContentSize(Size(fileDesignWidth, fileDesignHeight));
}
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/* ********************** */
/* ********************** */
2021-12-25 10:04:45 +08:00
stExpCocoNode* optionChildNode = cocoNode->GetChildArray(cocoLoader);
for (int k = 0; k < cocoNode->GetChildNum(); ++k)
{
2019-11-23 20:27:39 +08:00
std::string key = optionChildNode[k].GetName(cocoLoader);
2021-12-25 10:04:45 +08:00
if (key == "animation")
{
Ref* rootWidget = (Ref*)widget;
ActionManagerEx::getInstance()->initWithBinary(fileName, rootWidget, cocoLoader, &optionChildNode[k]);
2019-11-23 20:27:39 +08:00
break;
}
}
return widget;
}
2021-12-25 10:04:45 +08:00
Widget* WidgetPropertiesReader0300::widgetFromBinary(CocoLoader* cocoLoader, stExpCocoNode* cocoNode)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Widget* widget = nullptr;
stExpCocoNode* stChildArray = cocoNode->GetChildArray(cocoLoader);
stExpCocoNode* optionsNode = nullptr;
stExpCocoNode* childrenNode = nullptr;
int elementCount = cocoNode->GetChildNum();
2019-11-23 20:27:39 +08:00
std::string classname;
2021-12-25 10:04:45 +08:00
for (int i = 0; i < elementCount; ++i)
{
std::string key = stChildArray[i].GetName(cocoLoader);
2019-11-23 20:27:39 +08:00
std::string value = stChildArray[i].GetValue(cocoLoader);
2021-12-25 10:04:45 +08:00
if (key == "classname")
2019-11-23 20:27:39 +08:00
{
if (!value.empty())
{
classname = value;
2021-12-25 10:04:45 +08:00
widget = this->createGUI(classname);
2019-11-23 20:27:39 +08:00
}
else
{
CCLOG("Warning!!! classname not found!");
}
2021-12-25 10:04:45 +08:00
}
else if (key == "children")
{
2019-11-23 20:27:39 +08:00
childrenNode = &stChildArray[i];
2021-12-25 10:04:45 +08:00
}
else if (key == "options")
{
2019-11-23 20:27:39 +08:00
optionsNode = &stChildArray[i];
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
std::string readerName = this->getWidgetReaderClassName(classname);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
WidgetReaderProtocol* reader = this->createWidgetReaderProtocol(readerName);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (reader)
{
// widget parse with widget reader
2021-12-25 10:04:45 +08:00
setPropsForAllWidgetFromBinary(reader, widget, cocoLoader, optionsNode);
2019-11-23 20:27:39 +08:00
}
else
{
// 1st., custom widget parse properties of parent widget with parent widget reader
readerName = this->getWidgetReaderClassName(widget);
2021-12-25 10:04:45 +08:00
reader = this->createWidgetReaderProtocol(readerName);
if (reader && widget)
{
2019-11-23 20:27:39 +08:00
setPropsForAllWidgetFromBinary(reader, widget, cocoLoader, optionsNode);
// 2nd., custom widget parse with custom reader
2021-12-25 10:04:45 +08:00
// 2nd. parse custom property
const char* customProperty = nullptr;
stExpCocoNode* optionChildNode = optionsNode->GetChildArray(cocoLoader);
for (int k = 0; k < optionsNode->GetChildNum(); ++k)
{
2019-11-23 20:27:39 +08:00
std::string key = optionChildNode[k].GetName(cocoLoader);
2021-12-25 10:04:45 +08:00
if (key == "customProperty")
{
2019-11-23 20:27:39 +08:00
customProperty = optionChildNode[k].GetValue(cocoLoader);
break;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
rapidjson::Document customJsonDict;
customJsonDict.Parse<0>(customProperty);
if (customJsonDict.HasParseError())
{
CCLOG("GetParseError %d\n", customJsonDict.GetParseError());
}
setPropsForAllCustomWidgetFromJsonDictionary(classname, widget, customJsonDict);
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
CCLOG("Widget or WidgetReader doesn't exists!!! Please check your csb file.");
}
2021-12-25 10:04:45 +08:00
}
// parse children
if (nullptr != childrenNode)
{
rapidjson::Type tType22 = childrenNode->GetType(cocoLoader);
if (tType22 == rapidjson::kArrayType)
{
int childrenCount = childrenNode->GetChildNum();
2019-11-23 20:27:39 +08:00
stExpCocoNode* innerChildArray = childrenNode->GetChildArray(cocoLoader);
2021-12-25 10:04:45 +08:00
for (int i = 0; i < childrenCount; ++i)
{
rapidjson::Type tType = innerChildArray[i].GetType(cocoLoader);
if (tType == rapidjson::kObjectType)
{
Widget* child = widgetFromBinary(cocoLoader, &innerChildArray[i]);
2019-11-23 20:27:39 +08:00
if (child)
{
PageView* pageView = dynamic_cast<PageView*>(widget);
if (pageView)
{
pageView->addPage(static_cast<Layout*>(child));
}
else
{
ListView* listView = dynamic_cast<ListView*>(widget);
if (listView)
{
listView->pushBackCustomItem(child);
}
else
{
if (nullptr == dynamic_cast<Layout*>(widget))
{
if (child->getPositionType() == ui::Widget::PositionType::PERCENT)
{
2021-12-25 10:04:45 +08:00
child->setPositionPercent(
Vec2(child->getPositionPercent().x + widget->getAnchorPoint().x,
child->getPositionPercent().y + widget->getAnchorPoint().y));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
child->setPosition(
Vec2(child->getPositionX() + widget->getAnchorPointInPoints().x,
child->getPositionY() + widget->getAnchorPointInPoints().y));
2019-11-23 20:27:39 +08:00
}
widget->addChild(child);
}
}
}
}
}
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return widget;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void WidgetPropertiesReader0300::setPropsForAllWidgetFromBinary(WidgetReaderProtocol* reader,
cocos2d::ui::Widget* widget,
CocoLoader* cocoLoader,
2021-12-25 10:04:45 +08:00
stExpCocoNode* cocoNode)
2019-11-23 20:27:39 +08:00
{
reader->setPropsFromBinary(widget, cocoLoader, cocoNode);
}
2021-12-25 10:04:45 +08:00
2021-12-26 23:26:34 +08:00
void WidgetPropertiesReader0300::setPropsForAllCustomWidgetFromBinary(std::string_view /*classType*/,
2021-12-25 10:04:45 +08:00
cocos2d::ui::Widget* /*widget*/,
CocoLoader* /*cocoLoader*/,
stExpCocoNode* /*pCocoNode*/)
{
// TODO: custom property
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Widget* WidgetPropertiesReader0300::widgetFromJsonDictionary(const rapidjson::Value& data)
{
2021-12-25 10:04:45 +08:00
const char* classname = DICTOOL->getStringValue_json(data, "classname");
2019-11-23 20:27:39 +08:00
const rapidjson::Value& uiOptions = DICTOOL->getSubDictionary_json(data, "options");
2021-12-25 10:04:45 +08:00
Widget* widget = this->createGUI(classname);
// CCLOG("classname = %s", classname);
2019-11-23 20:27:39 +08:00
std::string readerName = this->getWidgetReaderClassName(classname);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
WidgetReaderProtocol* reader = this->createWidgetReaderProtocol(readerName);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (reader)
{
// widget parse with widget reader
setPropsForAllWidgetFromJsonDictionary(reader, widget, uiOptions);
}
else
{
readerName = this->getWidgetReaderClassName(widget);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
reader = dynamic_cast<WidgetReaderProtocol*>(ObjectFactory::getInstance()->createObject(readerName));
2021-12-25 10:04:45 +08:00
if (reader && widget)
{
2019-11-23 20:27:39 +08:00
setPropsForAllWidgetFromJsonDictionary(reader, widget, uiOptions);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// 2nd., custom widget parse with custom reader
const char* customProperty = DICTOOL->getStringValue_json(uiOptions, "customProperty");
rapidjson::Document customJsonDict;
customJsonDict.Parse<0>(customProperty);
if (customJsonDict.HasParseError())
{
CCLOG("GetParseError %d\n", customJsonDict.GetParseError());
}
setPropsForAllCustomWidgetFromJsonDictionary(classname, widget, customJsonDict);
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
CCLOG("Widget or WidgetReader doesn't exists!!! Please check your json file.");
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int childrenCount = DICTOOL->getArrayCount_json(data, "children");
for (int i = 0; i < childrenCount; i++)
{
const rapidjson::Value& subData = DICTOOL->getDictionaryFromArray_json(data, "children", i);
2021-12-25 10:04:45 +08:00
cocos2d::ui::Widget* child = widgetFromJsonDictionary(subData);
2019-11-23 20:27:39 +08:00
if (child)
{
PageView* pageView = dynamic_cast<PageView*>(widget);
if (pageView)
{
pageView->addPage(static_cast<Layout*>(child));
}
else
{
ListView* listView = dynamic_cast<ListView*>(widget);
if (listView)
{
listView->pushBackCustomItem(child);
}
else
{
if (nullptr == dynamic_cast<Layout*>(widget))
{
if (child->getPositionType() == ui::Widget::PositionType::PERCENT)
{
2021-12-25 10:04:45 +08:00
child->setPositionPercent(Vec2(child->getPositionPercent().x + widget->getAnchorPoint().x,
child->getPositionPercent().y + widget->getAnchorPoint().y));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
child->setPosition(Vec2(child->getPositionX() + widget->getAnchorPointInPoints().x,
child->getPositionY() + widget->getAnchorPointInPoints().y));
2019-11-23 20:27:39 +08:00
}
widget->addChild(child);
}
}
}
}
return widget;
}
2021-12-25 10:04:45 +08:00
void WidgetPropertiesReader0300::setPropsForAllWidgetFromJsonDictionary(WidgetReaderProtocol* reader,
Widget* widget,
const rapidjson::Value& options)
2019-11-23 20:27:39 +08:00
{
reader->setPropsFromJsonDictionary(widget, options);
}
2021-12-25 10:04:45 +08:00
2021-12-26 23:26:34 +08:00
void WidgetPropertiesReader0300::setPropsForAllCustomWidgetFromJsonDictionary(std::string_view classType,
2021-12-25 10:04:45 +08:00
cocos2d::ui::Widget* widget,
const rapidjson::Value& customOptions)
2019-11-23 20:27:39 +08:00
{
GUIReader* guiReader = GUIReader::getInstance();
2021-12-25 10:04:45 +08:00
2021-12-27 13:52:08 +08:00
hlookup::string_map<Ref*>* object_map = guiReader->getParseObjectMap();
2021-12-28 11:05:34 +08:00
Ref* object = (*object_map)[classType];
2021-12-25 10:04:45 +08:00
2021-12-27 13:52:08 +08:00
hlookup::string_map<SEL_ParseEvent>* selector_map = guiReader->getParseCallBackMap();
2021-12-28 11:05:34 +08:00
SEL_ParseEvent selector = (*selector_map)[classType];
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (object && selector)
{
(object->*selector)(classType, widget, customOptions);
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio