2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
2019-11-25 01:35:26 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
Copyright (c) 2019-2020 simdsoft, @HALX99
|
2021-10-09 13:48:56 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
|
|
|
|
|
|
|
https://adxe.org
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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:
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "ActionTimeline/CSLoader.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "base/ObjectFactory.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "ui/CocosGUI.h"
|
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
|
|
|
#include "2d/CCParticleSystemQuad.h"
|
2020-08-28 17:26:30 +08:00
|
|
|
#include "2d/CCFastTMXTiledMap.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "ActionTimeline/CCActionTimelineCache.h"
|
|
|
|
#include "ActionTimeline/CCActionTimeline.h"
|
|
|
|
#include "ActionTimeline/CCActionTimelineNode.h"
|
|
|
|
#include "CCSGUIReader.h"
|
|
|
|
#include "CCComAudio.h"
|
|
|
|
#include "CSParseBinary_generated.h"
|
|
|
|
|
|
|
|
#include "WidgetReader/NodeReaderProtocol.h"
|
|
|
|
#include "WidgetReader/NodeReaderDefine.h"
|
|
|
|
|
|
|
|
#include "WidgetReader/NodeReader/NodeReader.h"
|
|
|
|
#include "WidgetReader/SingleNodeReader/SingleNodeReader.h"
|
|
|
|
#include "WidgetReader/SpriteReader/SpriteReader.h"
|
|
|
|
#include "WidgetReader/ParticleReader/ParticleReader.h"
|
|
|
|
#include "WidgetReader/GameMapReader/GameMapReader.h"
|
|
|
|
#include "WidgetReader/ProjectNodeReader/ProjectNodeReader.h"
|
|
|
|
#include "WidgetReader/ComAudioReader/ComAudioReader.h"
|
|
|
|
|
|
|
|
#include "WidgetReader/ButtonReader/ButtonReader.h"
|
|
|
|
#include "WidgetReader/CheckBoxReader/CheckBoxReader.h"
|
|
|
|
#include "WidgetReader/ImageViewReader/ImageViewReader.h"
|
|
|
|
#include "WidgetReader/TextBMFontReader/TextBMFontReader.h"
|
|
|
|
#include "WidgetReader/TextReader/TextReader.h"
|
|
|
|
#include "WidgetReader/TextFieldReader/TextFieldReader.h"
|
|
|
|
#include "WidgetReader/TextAtlasReader/TextAtlasReader.h"
|
|
|
|
#include "WidgetReader/LoadingBarReader/LoadingBarReader.h"
|
|
|
|
#include "WidgetReader/SliderReader/SliderReader.h"
|
|
|
|
#include "WidgetReader/LayoutReader/LayoutReader.h"
|
|
|
|
#include "WidgetReader/ScrollViewReader/ScrollViewReader.h"
|
|
|
|
#include "WidgetReader/PageViewReader/PageViewReader.h"
|
|
|
|
#include "WidgetReader/ListViewReader/ListViewReader.h"
|
|
|
|
#include "WidgetReader/ArmatureNodeReader/ArmatureNodeReader.h"
|
|
|
|
#include "WidgetReader/Node3DReader/Node3DReader.h"
|
|
|
|
#include "WidgetReader/Sprite3DReader/Sprite3DReader.h"
|
|
|
|
#include "WidgetReader/UserCameraReader/UserCameraReader.h"
|
|
|
|
#include "WidgetReader/Particle3DReader/Particle3DReader.h"
|
|
|
|
#include "WidgetReader/GameNode3DReader/GameNode3DReader.h"
|
|
|
|
#include "WidgetReader/Light3DReader/Light3DReader.h"
|
|
|
|
#include "WidgetReader/TabControlReader/TabControlReader.h"
|
|
|
|
|
|
|
|
#include "WidgetReader/SkeletonReader/BoneNodeReader.h"
|
|
|
|
#include "WidgetReader/SkeletonReader/SkeletonNodeReader.h"
|
2020-08-03 20:31:47 +08:00
|
|
|
|
2020-08-04 21:13:26 +08:00
|
|
|
#if defined(CC_BUILD_WITH_SPINE) && CC_BUILD_WITH_SPINE
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "WidgetReader/SpineSkeletonReader/SpineSkeletonReader.h"
|
2020-08-04 21:13:26 +08:00
|
|
|
#endif
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "WidgetReader/RichTextReader/RichTextReader.h"
|
|
|
|
#include "WidgetReader/RadioButtonReader/RadioButtonReader.h"
|
|
|
|
#include "WidgetReader/RadioButtonReader/RadioButtonGroupReader.h"
|
|
|
|
#include "WidgetReader/TextFieldReader/TextFieldExReader.h"
|
|
|
|
#include "CCComExtensionData.h"
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "flatbuffers/flatbuffers.h"
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "FlatBuffersSerialize.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "WidgetCallBackHandlerProtocol.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace cocos2d::ui;
|
|
|
|
using namespace cocostudio;
|
|
|
|
using namespace cocostudio::timeline;
|
|
|
|
/* peterson */
|
|
|
|
using namespace flatbuffers;
|
|
|
|
/**/
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* ClassName_Node = "Node";
|
|
|
|
static const char* ClassName_SubGraph = "SubGraph";
|
|
|
|
static const char* ClassName_Sprite = "Sprite";
|
|
|
|
static const char* ClassName_Particle = "Particle";
|
2019-11-23 20:27:39 +08:00
|
|
|
static const char* ClassName_TMXTiledMap = "TMXTiledMap";
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* ClassName_Panel = "Panel";
|
|
|
|
static const char* ClassName_Button = "Button";
|
|
|
|
static const char* ClassName_CheckBox = "CheckBox";
|
|
|
|
static const char* ClassName_ImageView = "ImageView";
|
|
|
|
static const char* ClassName_TextAtlas = "TextAtlas";
|
|
|
|
static const char* ClassName_LabelAtlas = "LabelAtlas";
|
2019-11-24 23:15:56 +08:00
|
|
|
static const char* ClassName_LabelBMFont = "LabelBMFont";
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* ClassName_TextBMFont = "TextBMFont";
|
|
|
|
static const char* ClassName_Text = "Text";
|
|
|
|
static const char* ClassName_LoadingBar = "LoadingBar";
|
|
|
|
static const char* ClassName_TextField = "TextField";
|
|
|
|
static const char* ClassName_Slider = "Slider";
|
|
|
|
static const char* ClassName_Layout = "Layout";
|
|
|
|
static const char* ClassName_ScrollView = "ScrollView";
|
|
|
|
static const char* ClassName_ListView = "ListView";
|
|
|
|
static const char* ClassName_PageView = "PageView";
|
|
|
|
static const char* ClassName_Widget = "Widget";
|
|
|
|
static const char* ClassName_Label = "Label";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
static const char* ClassName_ComAudio = "ComAudio";
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* NODE = "nodeTree";
|
|
|
|
static const char* CHILDREN = "children";
|
|
|
|
static const char* CLASSNAME = "classname";
|
|
|
|
static const char* FILE_PATH = "fileName";
|
|
|
|
static const char* PLIST_FILE = "plistFile";
|
|
|
|
static const char* TMX_FILE = "tmxFile";
|
|
|
|
static const char* TMX_STRING = "tmxString";
|
2019-11-24 23:15:56 +08:00
|
|
|
static const char* RESOURCE_PATH = "resourcePath";
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* COMPONENTS = "components";
|
|
|
|
static const char* COMPONENT_TYPE = "componentType";
|
|
|
|
static const char* COMPONENT_NAME = "componentName";
|
|
|
|
static const char* COMPONENT_ENABLED = "componentEnabled";
|
2019-11-24 23:15:56 +08:00
|
|
|
static const char* COMPONENT_AUDIO_FILE_PATH = "comAudioFilePath";
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* COMPONENT_LOOP = "comAudioloop";
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* TAG = "tag";
|
2019-11-24 23:15:56 +08:00
|
|
|
static const char* ACTION_TAG = "actionTag";
|
|
|
|
|
|
|
|
static const char* OPTIONS = "options";
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* WIDTH = "width";
|
|
|
|
static const char* HEIGHT = "height";
|
|
|
|
static const char* X = "x";
|
|
|
|
static const char* Y = "y";
|
|
|
|
static const char* SCALE_X = "scaleX";
|
|
|
|
static const char* SCALE_Y = "scaleY";
|
|
|
|
static const char* SKEW_X = "skewX";
|
|
|
|
static const char* SKEW_Y = "skewY";
|
|
|
|
static const char* ROTATION = "rotation";
|
2019-11-24 23:15:56 +08:00
|
|
|
static const char* ROTATION_SKEW_X = "rotationSkewX";
|
|
|
|
static const char* ROTATION_SKEW_Y = "rotationSkewY";
|
2021-12-25 10:04:45 +08:00
|
|
|
static const char* ANCHOR_X = "anchorPointX";
|
|
|
|
static const char* ANCHOR_Y = "anchorPointY";
|
|
|
|
static const char* ALPHA = "opacity";
|
|
|
|
static const char* RED = "colorR";
|
|
|
|
static const char* GREEN = "colorG";
|
|
|
|
static const char* BLUE = "colorB";
|
|
|
|
static const char* ZORDER = "ZOrder";
|
|
|
|
static const char* PARTICLE_NUM = "particleNum";
|
|
|
|
static const char* FLIPX = "flipX";
|
|
|
|
static const char* FLIPY = "flipY";
|
|
|
|
static const char* VISIBLE = "visible";
|
|
|
|
|
|
|
|
static const char* TEXTURES = "textures";
|
2019-11-23 20:27:39 +08:00
|
|
|
static const char* TEXTURES_PNG = "texturesPng";
|
|
|
|
|
2019-11-24 23:15:56 +08:00
|
|
|
static const char* MONO_COCOS2D_VERSION = "cocos2dVersion";
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// CSLoader
|
|
|
|
static CSLoader* _sharedCSLoader = nullptr;
|
|
|
|
|
|
|
|
CSLoader* CSLoader::getInstance()
|
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
if (!_sharedCSLoader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_sharedCSLoader = new CSLoader();
|
2019-11-23 20:27:39 +08:00
|
|
|
_sharedCSLoader->init();
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return _sharedCSLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSLoader::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_sharedCSLoader);
|
|
|
|
ActionTimelineCache::destroyInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
CSLoader::CSLoader()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _recordJsonPath(true), _jsonPath(""), _monoCocos2dxVersion(""), _rootNode(nullptr), _csBuildID("10.0.3000.0")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(NodeReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(SingleNodeReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(SpriteReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(ParticleReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(GameMapReader);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
CREATE_CLASS_NODE_READER_INFO(ButtonReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(CheckBoxReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(ImageViewReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(TextBMFontReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(TextReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(TextFieldReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(TextAtlasReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(LoadingBarReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(SliderReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(LayoutReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(ScrollViewReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(PageViewReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(ListViewReader);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
CREATE_CLASS_NODE_READER_INFO(ArmatureNodeReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(Node3DReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(Sprite3DReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(UserCameraReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(Particle3DReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(GameNode3DReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(Light3DReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(TabControlReader);
|
|
|
|
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(BoneNodeReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(SkeletonNodeReader);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-25 01:35:26 +08:00
|
|
|
/// Added by x-studio
|
2019-11-24 23:15:56 +08:00
|
|
|
CREATE_CLASS_NODE_READER_INFO(RichTextReader);
|
2020-08-04 21:13:26 +08:00
|
|
|
#if defined(CC_BUILD_WITH_SPINE) && CC_BUILD_WITH_SPINE
|
2019-11-24 23:15:56 +08:00
|
|
|
CREATE_CLASS_NODE_READER_INFO(SpineSkeletonReader);
|
2020-08-04 21:13:26 +08:00
|
|
|
#endif
|
2019-11-24 23:15:56 +08:00
|
|
|
CREATE_CLASS_NODE_READER_INFO(RadioButtonReader);
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(RadioButtonGroupReader);
|
|
|
|
|
|
|
|
CREATE_CLASS_NODE_READER_INFO(TextFieldExReader);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void CSLoader::purge() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void CSLoader::init()
|
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
|
|
|
_funcs.insert(Pair(ClassName_Node, std::bind(&CSLoader::loadSimpleNode, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_SubGraph, std::bind(&CSLoader::loadSubGraph, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_Sprite, std::bind(&CSLoader::loadSprite, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_Particle, std::bind(&CSLoader::loadParticle, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_TMXTiledMap, std::bind(&CSLoader::loadTMXTiledMap, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_LabelAtlas, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_LabelBMFont, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_Panel, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_Button, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_CheckBox, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_ImageView, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_TextAtlas, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_TextBMFont, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_Text, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_LoadingBar, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_TextField, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_Slider, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_Layout, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_ScrollView, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_ListView, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_PageView, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_Widget, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
_funcs.insert(Pair(ClassName_Label, std::bind(&CSLoader::loadWidget, this, _1)));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_componentFuncs.insert(ComponentPair(ClassName_ComAudio, std::bind(&CSLoader::loadComAudio, this, _1)));
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::createNode(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
auto path = filename;
|
2021-12-28 11:07:12 +08:00
|
|
|
size_t pos = path.find_last_of('.');
|
2021-12-27 13:52:08 +08:00
|
|
|
auto suffix = path.substr(pos + 1, path.length());
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
CSLoader* load = CSLoader::getInstance();
|
|
|
|
|
|
|
|
if (suffix == "csb")
|
|
|
|
{
|
|
|
|
return load->createNodeWithFlatBuffersFile(filename);
|
|
|
|
}
|
|
|
|
else if (suffix == "json" || suffix == "ExportJson")
|
|
|
|
{
|
|
|
|
return load->createNodeFromJson(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::createNode(std::string_view filename, const ccNodeLoadCallback& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 11:07:12 +08:00
|
|
|
auto path = filename;
|
|
|
|
size_t pos = path.find_last_of('.');
|
|
|
|
auto suffix = path.substr(pos + 1, path.length());
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
CSLoader* load = CSLoader::getInstance();
|
|
|
|
|
|
|
|
if (suffix == "csb")
|
|
|
|
{
|
|
|
|
return load->createNodeWithFlatBuffersFile(filename, callback);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::createNodeWithVisibleSize(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto node = createNode(filename);
|
|
|
|
if (node != nullptr)
|
|
|
|
{
|
|
|
|
Size frameSize = Director::getInstance()->getVisibleSize();
|
|
|
|
node->setContentSize(frameSize);
|
|
|
|
ui::Helper::doLayout(node);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::createNodeWithVisibleSize(std::string_view filename, const ccNodeLoadCallback& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto node = createNode(filename, callback);
|
|
|
|
if (node != nullptr)
|
|
|
|
{
|
|
|
|
Size frameSize = Director::getInstance()->getVisibleSize();
|
|
|
|
node->setContentSize(frameSize);
|
|
|
|
ui::Helper::doLayout(node);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string_view CSLoader::getExtentionName(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 11:07:12 +08:00
|
|
|
auto path = name;
|
|
|
|
size_t pos = path.find_last_of('.');
|
|
|
|
auto result = path.substr(pos + 1, path.length());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
ActionTimeline* CSLoader::createTimeline(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
auto suffix = getExtentionName(filename);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ActionTimelineCache* cache = ActionTimelineCache::getInstance();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (suffix == "csb")
|
|
|
|
{
|
|
|
|
return cache->createActionWithFlatBuffersFile(filename);
|
|
|
|
}
|
|
|
|
else if (suffix == "json" || suffix == "ExportJson")
|
|
|
|
{
|
|
|
|
return cache->createActionFromJson(filename);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
ActionTimeline* CSLoader::createTimeline(const Data& data, std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
auto suffix = getExtentionName(filename);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ActionTimelineCache* cache = ActionTimelineCache::getInstance();
|
|
|
|
|
|
|
|
if (suffix == "csb")
|
|
|
|
{
|
|
|
|
return cache->createActionWithDataBuffer(data, filename);
|
|
|
|
}
|
|
|
|
else if (suffix == "json" || suffix == "ExportJson")
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string_view content((char*)data.getBytes(), data.getSize());
|
2019-11-23 20:27:39 +08:00
|
|
|
return cache->createActionFromContent(filename, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-26 23:26:34 +08:00
|
|
|
ActionTimelineNode* CSLoader::createActionTimelineNode(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Node* root = createNode(filename);
|
|
|
|
ActionTimeline* action = createTimeline(filename);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if(root && action)
|
|
|
|
{
|
|
|
|
root->runAction(action);
|
|
|
|
action->gotoFrameAndPlay(0);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ActionTimelineNode* node = ActionTimelineNode::create(root, action);
|
|
|
|
return node;
|
|
|
|
}
|
2021-12-26 23:26:34 +08:00
|
|
|
ActionTimelineNode* CSLoader::createActionTimelineNode(std::string_view filename, int startIndex, int endIndex, bool
|
2021-12-25 10:04:45 +08:00
|
|
|
loop)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ActionTimelineNode* node = createActionTimelineNode(filename);
|
|
|
|
ActionTimeline* action = node->getActionTimeline();
|
|
|
|
if(action)
|
|
|
|
action->gotoFrameAndPlay(startIndex, endIndex, loop);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::createNodeFromJson(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_recordJsonPath)
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
auto jsonPath = filename.substr(0, filename.find_last_of('/') + 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
GUIReader::getInstance()->setFilePath(jsonPath);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_jsonPath = jsonPath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GUIReader::getInstance()->setFilePath("");
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Node* node = loadNodeWithFile(filename);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::loadNodeWithFile(std::string_view fileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// Read content from file
|
|
|
|
std::string contentStr = FileUtils::getInstance()->getStringFromFile(fileName);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Node* node = loadNodeWithContent(contentStr);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Load animation data from file
|
|
|
|
ActionTimelineCache::getInstance()->loadAnimationActionWithContent(fileName, contentStr);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::loadNodeWithContent(std::string_view content)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
rapidjson::Document doc;
|
2021-12-27 13:52:08 +08:00
|
|
|
doc.Parse<0>(content.data(), content.length());
|
2019-11-23 20:27:39 +08:00
|
|
|
if (doc.HasParseError())
|
|
|
|
{
|
|
|
|
CCLOG("GetParseError %d\n", doc.GetParseError());
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// cocos2dx version mono editor is based on
|
|
|
|
_monoCocos2dxVersion = DICTOOL->getStringValue_json(doc, MONO_COCOS2D_VERSION, "");
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// decode plist
|
|
|
|
int length = DICTOOL->getArrayCount_json(doc, TEXTURES);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < length; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string plist = DICTOOL->getStringValueFromArray_json(doc, TEXTURES, i);
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string png = DICTOOL->getStringValueFromArray_json(doc, TEXTURES_PNG, i);
|
|
|
|
plist = _jsonPath + plist;
|
|
|
|
png = _jsonPath + png;
|
2019-11-23 20:27:39 +08:00
|
|
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist, png);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// decode node tree
|
|
|
|
const rapidjson::Value& subJson = DICTOOL->getSubDictionary_json(doc, NODE);
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* root = loadNode(subJson);
|
2019-11-23 20:27:39 +08:00
|
|
|
root->release();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* CSLoader::loadNode(const rapidjson::Value& json)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* node = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string nodeType = DICTOOL->getStringValue_json(json, CLASSNAME);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
NodeCreateFunc func = _funcs[nodeType];
|
|
|
|
if (func != nullptr)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& options = DICTOOL->getSubDictionary_json(json, OPTIONS);
|
2021-12-25 10:04:45 +08:00
|
|
|
node = func(options);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// component
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
const rapidjson::Value& components = DICTOOL->getSubDictionary_json(options, COMPONENTS);
|
2021-12-25 10:04:45 +08:00
|
|
|
int componentSize = DICTOOL->getArrayCount_json(options, COMPONENTS, 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
for (int i = 0; i < componentSize; ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& dic = DICTOOL->getSubDictionary_json(components, COMPONENTS, i);
|
|
|
|
Component* component = loadComponent(dic);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (component)
|
|
|
|
{
|
|
|
|
node->addComponent(component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
|
|
|
if (node)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int length = DICTOOL->getArrayCount_json(json, CHILDREN, 0);
|
2019-11-24 23:15:56 +08:00
|
|
|
for (int i = 0; i < length; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& dic = DICTOOL->getSubDictionary_json(json, CHILDREN, i);
|
|
|
|
Node* child = loadNode(dic);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
PageView* pageView = dynamic_cast<PageView*>(node);
|
|
|
|
ListView* listView = dynamic_cast<ListView*>(node);
|
|
|
|
if (pageView)
|
|
|
|
{
|
|
|
|
Layout* layout = dynamic_cast<Layout*>(child);
|
|
|
|
if (layout)
|
|
|
|
{
|
|
|
|
pageView->addPage(layout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (listView)
|
|
|
|
{
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
|
|
|
if (widget)
|
|
|
|
{
|
|
|
|
listView->pushBackCustomItem(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_monoCocos2dxVersion != "3.x")
|
|
|
|
{
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
|
|
|
Widget* parent = dynamic_cast<Widget*>(node);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (widget && parent && !dynamic_cast<Layout*>(parent))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (widget->getPositionType() == ui::Widget::PositionType::PERCENT)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
widget->setPositionPercent(
|
|
|
|
Vec2(widget->getPositionPercent().x + parent->getAnchorPoint().x,
|
|
|
|
widget->getPositionPercent().y + parent->getAnchorPoint().y));
|
|
|
|
widget->setPosition(Vec2(widget->getPositionX() + parent->getAnchorPointInPoints().x,
|
|
|
|
widget->getPositionY() + parent->getAnchorPointInPoints().y));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size parentSize = parent->getContentSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
widget->setPosition(
|
|
|
|
Vec2(widget->getPositionX() + parentSize.width * parent->getAnchorPoint().x,
|
|
|
|
widget->getPositionY() + parentSize.height * parent->getAnchorPoint().y));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
node->addChild(child);
|
|
|
|
}
|
|
|
|
child->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Not supported NodeType: %s", nodeType.c_str());
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSLoader::initNode(Node* node, const rapidjson::Value& json)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float width = DICTOOL->getFloatValue_json(json, WIDTH);
|
|
|
|
float height = DICTOOL->getFloatValue_json(json, HEIGHT);
|
|
|
|
float x = DICTOOL->getFloatValue_json(json, X);
|
|
|
|
float y = DICTOOL->getFloatValue_json(json, Y);
|
|
|
|
float scalex = DICTOOL->getFloatValue_json(json, SCALE_X, 1);
|
|
|
|
float scaley = DICTOOL->getFloatValue_json(json, SCALE_Y, 1);
|
|
|
|
float rotation = DICTOOL->getFloatValue_json(json, ROTATION);
|
2019-11-23 20:27:39 +08:00
|
|
|
float rotationSkewX = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_X);
|
|
|
|
float rotationSkewY = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_Y);
|
2019-11-25 01:35:26 +08:00
|
|
|
float skewx = DICTOOL->getFloatValue_json(json, SKEW_X);
|
|
|
|
float skewy = DICTOOL->getFloatValue_json(json, SKEW_Y);
|
|
|
|
float anchorx = DICTOOL->getFloatValue_json(json, ANCHOR_X, 0.5f);
|
|
|
|
float anchory = DICTOOL->getFloatValue_json(json, ANCHOR_Y, 0.5f);
|
|
|
|
uint8_t alpha = (uint8_t)DICTOOL->getIntValue_json(json, ALPHA, 255);
|
|
|
|
uint8_t red = (uint8_t)DICTOOL->getIntValue_json(json, RED, 255);
|
|
|
|
uint8_t green = (uint8_t)DICTOOL->getIntValue_json(json, GREEN, 255);
|
|
|
|
uint8_t blue = (uint8_t)DICTOOL->getIntValue_json(json, BLUE, 255);
|
|
|
|
int zorder = DICTOOL->getIntValue_json(json, ZORDER);
|
|
|
|
int tag = DICTOOL->getIntValue_json(json, TAG);
|
|
|
|
int actionTag = DICTOOL->getIntValue_json(json, ACTION_TAG);
|
|
|
|
bool visible = DICTOOL->getBooleanValue_json(json, VISIBLE);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (x != 0 || y != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setPosition(Point(x, y));
|
2019-11-24 23:15:56 +08:00
|
|
|
if (scalex != 1)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setScaleX(scalex);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (scaley != 1)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setScaleY(scaley);
|
|
|
|
if (rotation != 0)
|
|
|
|
node->setRotation(rotation);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (rotationSkewX != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setRotationSkewX(rotationSkewX);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (rotationSkewY != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setRotationSkewY(rotationSkewY);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (skewx != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setSkewX(skewx);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (skewy != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setSkewY(skewy);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (anchorx != 0.5f || anchory != 0.5f)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setAnchorPoint(Point(anchorx, anchory));
|
2019-11-24 23:15:56 +08:00
|
|
|
if (width != 0 || height != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setContentSize(Size(width, height));
|
2019-11-24 23:15:56 +08:00
|
|
|
if (zorder != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setLocalZOrder(zorder);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (visible != true)
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setVisible(visible);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
|
|
|
if (alpha != 255)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
node->setOpacity(alpha);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
if (red != 255 || green != 255 || blue != 255)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
node->setColor(Color3B(red, green, blue));
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
node->setTag(tag);
|
|
|
|
node->setUserObject(ActionTimelineData::create(actionTag));
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* CSLoader::loadSimpleNode(const rapidjson::Value& json)
|
|
|
|
{
|
|
|
|
Node* node = Node::create();
|
|
|
|
// fix memory leak for v3.3
|
2021-12-25 10:04:45 +08:00
|
|
|
// node->retain();
|
2019-11-23 20:27:39 +08:00
|
|
|
initNode(node, json);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* CSLoader::loadSubGraph(const rapidjson::Value& json)
|
|
|
|
{
|
|
|
|
const char* filePath = DICTOOL->getStringValue_json(json, FILE_PATH);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Node* node = nullptr;
|
|
|
|
if (filePath && strcmp("", filePath) != 0)
|
|
|
|
{
|
|
|
|
node = createNode(filePath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node = Node::create();
|
|
|
|
}
|
|
|
|
// fix memory leak for v3.3
|
2021-12-25 10:04:45 +08:00
|
|
|
// node->retain();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
initNode(node, json);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* CSLoader::loadSprite(const rapidjson::Value& json)
|
|
|
|
{
|
|
|
|
const char* filePath = DICTOOL->getStringValue_json(json, FILE_PATH);
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* sprite = nullptr;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
|
|
|
if (filePath != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string path = filePath;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (!spriteFrame)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
path = _jsonPath + path;
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite = Sprite::create(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprite = Sprite::createWithSpriteFrame(spriteFrame);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
|
|
|
if (!sprite)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
sprite = Sprite::create();
|
|
|
|
CCLOG("filePath is empty. Create a sprite with no texture");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprite = Sprite::create();
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// fix memory leak for v3.3
|
2021-12-25 10:04:45 +08:00
|
|
|
// sprite->retain();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
initNode(sprite, json);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
|
|
|
bool flipX = DICTOOL->getBooleanValue_json(json, FLIPX);
|
|
|
|
bool flipY = DICTOOL->getBooleanValue_json(json, FLIPY);
|
|
|
|
|
|
|
|
if (flipX != false)
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setFlippedX(flipX);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (flipY != false)
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setFlippedY(flipY);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* CSLoader::loadParticle(const rapidjson::Value& json)
|
|
|
|
{
|
|
|
|
const char* filePath = DICTOOL->getStringValue_json(json, PLIST_FILE);
|
2021-12-25 10:04:45 +08:00
|
|
|
int num = DICTOOL->getIntValue_json(json, PARTICLE_NUM);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ParticleSystemQuad* particle = ParticleSystemQuad::create(filePath);
|
|
|
|
particle->setTotalParticles(num);
|
|
|
|
// fix memory leak for v3.3
|
2021-12-25 10:04:45 +08:00
|
|
|
// particle->retain();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
initNode(particle, json);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return particle;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* CSLoader::loadTMXTiledMap(const rapidjson::Value& json)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* tmxFile = DICTOOL->getStringValue_json(json, TMX_FILE);
|
|
|
|
const char* tmxString = DICTOOL->getStringValue_json(json, TMX_STRING);
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* resourcePath = DICTOOL->getStringValue_json(json, RESOURCE_PATH);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
TMXTiledMap* tmx = nullptr;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (tmxFile && strcmp("", tmxFile) != 0)
|
|
|
|
{
|
|
|
|
tmx = TMXTiledMap::create(tmxFile);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if ((tmxString && strcmp("", tmxString) != 0) && (resourcePath && strcmp("", resourcePath) != 0))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
tmx = TMXTiledMap::createWithXML(tmxString, resourcePath);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return tmx;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* CSLoader::loadWidget(const rapidjson::Value& json)
|
|
|
|
{
|
|
|
|
const char* str = DICTOOL->getStringValue_json(json, CLASSNAME);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (str == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string classname = str;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2020-10-17 16:32:16 +08:00
|
|
|
WidgetPropertiesReader0300 widgetPropertiesReader;
|
2019-11-23 20:27:39 +08:00
|
|
|
Widget* widget = nullptr;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (isWidget(classname))
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string readerName{getGUIClassName(classname)};
|
2019-11-23 20:27:39 +08:00
|
|
|
readerName.append("Reader");
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string_view guiClassName = getGUIClassName(classname);
|
2021-12-28 11:07:12 +08:00
|
|
|
widget = dynamic_cast<Widget*>(ObjectFactory::getInstance()->createObject(guiClassName));
|
2019-11-23 20:27:39 +08:00
|
|
|
// fix memory leak for v3.3
|
2021-12-25 10:04:45 +08:00
|
|
|
// widget->retain();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
WidgetReaderProtocol* reader =
|
|
|
|
dynamic_cast<WidgetReaderProtocol*>(ObjectFactory::getInstance()->createObject(readerName));
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (reader && widget)
|
|
|
|
{
|
2020-10-17 16:32:16 +08:00
|
|
|
widgetPropertiesReader.setPropsForAllWidgetFromJsonDictionary(reader, widget, json);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isCustomWidget(classname))
|
|
|
|
{
|
|
|
|
widget = dynamic_cast<Widget*>(ObjectFactory::getInstance()->createObject(classname));
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// fix memory leak for v3.3
|
|
|
|
// widget->retain();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
// 1st., custom widget parse properties of parent widget with parent widget reader
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string_view readerName = getWidgetReaderClassName(widget);
|
2021-12-25 10:04:45 +08:00
|
|
|
WidgetReaderProtocol* reader =
|
|
|
|
dynamic_cast<WidgetReaderProtocol*>(ObjectFactory::getInstance()->createObject(readerName));
|
2019-11-23 20:27:39 +08:00
|
|
|
if (reader && widget)
|
|
|
|
{
|
2020-10-17 16:32:16 +08:00
|
|
|
widgetPropertiesReader.setPropsForAllWidgetFromJsonDictionary(reader, widget, json);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// 2nd., custom widget parse with custom reader
|
|
|
|
const char* customProperty = DICTOOL->getStringValue_json(json, "customProperty");
|
|
|
|
rapidjson::Document customJsonDict;
|
|
|
|
customJsonDict.Parse<0>(customProperty);
|
|
|
|
if (customJsonDict.HasParseError())
|
|
|
|
{
|
|
|
|
CCLOG("GetParseError %d\n", customJsonDict.GetParseError());
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2020-10-17 16:32:16 +08:00
|
|
|
widgetPropertiesReader.setPropsForAllCustomWidgetFromJsonDictionary(classname, widget, customJsonDict);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Widget or WidgetReader doesn't exists!!! Please check your protocol buffers file.");
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (widget)
|
|
|
|
{
|
|
|
|
float rotationSkewX = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_X);
|
|
|
|
float rotationSkewY = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_Y);
|
2021-12-25 10:04:45 +08:00
|
|
|
float skewx = DICTOOL->getFloatValue_json(json, SKEW_X);
|
|
|
|
float skewy = DICTOOL->getFloatValue_json(json, SKEW_Y);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (rotationSkewX != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
widget->setRotationSkewX(rotationSkewX);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (rotationSkewY != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
widget->setRotationSkewY(rotationSkewY);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (skewx != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
widget->setSkewX(skewx);
|
2019-11-24 23:15:56 +08:00
|
|
|
if (skewy != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
widget->setSkewY(skewy);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int actionTag = DICTOOL->getIntValue_json(json, ACTION_TAG);
|
|
|
|
widget->setUserObject(ActionTimelineData::create(actionTag));
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Component* CSLoader::loadComponent(const rapidjson::Value& json)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Component* component = nullptr;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string componentType = DICTOOL->getStringValue_json(json, COMPONENT_TYPE);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ComponentCreateFunc func = _componentFuncs[componentType];
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (func != nullptr)
|
|
|
|
{
|
|
|
|
component = func(json);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Component* CSLoader::loadComAudio(const rapidjson::Value& json)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ComAudio* audio = ComAudio::create();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* name = DICTOOL->getStringValue_json(json, COMPONENT_NAME);
|
2021-12-25 10:04:45 +08:00
|
|
|
bool enabled = DICTOOL->getBooleanValue_json(json, COMPONENT_ENABLED);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
audio->setName(name);
|
|
|
|
audio->setEnabled(enabled);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* filePath = DICTOOL->getStringValue_json(json, COMPONENT_AUDIO_FILE_PATH);
|
2021-12-25 10:04:45 +08:00
|
|
|
bool loop = DICTOOL->getBooleanValue_json(json, COMPONENT_LOOP);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
audio->setFile(filePath);
|
|
|
|
audio->setLoop(loop);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return audio;
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Node* CSLoader::createNode(const Data& data)
|
|
|
|
{
|
|
|
|
return createNode(data, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <typename _Elem, typename _Fty>
|
|
|
|
inline void fast_split(_Elem* s, typename std::remove_const<_Elem>::type delim, const _Fty& op)
|
2019-11-24 23:15:56 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto _Start = s; // the start of every string
|
|
|
|
auto _Ptr = s; // source string iterator
|
2019-11-24 23:15:56 +08:00
|
|
|
while (*_Ptr != '\0')
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (delim == *_Ptr /* && _Ptr != _Start*/)
|
2019-11-24 23:15:56 +08:00
|
|
|
{
|
|
|
|
if (_Ptr != _Start)
|
|
|
|
op(_Start, _Ptr);
|
|
|
|
_Start = _Ptr + 1;
|
|
|
|
}
|
|
|
|
++_Ptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_Start != _Ptr)
|
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
op(_Start, _Ptr);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* CSLoader::createNode(const Data& data, const ccNodeLoadCallback& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CSLoader* loader = CSLoader::getInstance();
|
|
|
|
Node* node = nullptr;
|
2019-11-24 23:15:56 +08:00
|
|
|
do
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CC_BREAK_IF(data.isNull() || data.getSize() <= 0);
|
|
|
|
auto csparsebinary = GetCSParseBinary(data.getBytes());
|
|
|
|
CC_BREAK_IF(nullptr == csparsebinary);
|
|
|
|
auto csBuildId = csparsebinary->version();
|
|
|
|
if (csBuildId)
|
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
int readerVersion = 0, writterVersion = 0;
|
|
|
|
// parse writter version
|
|
|
|
int revisionIndex = 0;
|
|
|
|
fast_split(csBuildId->c_str(), '.', [&](const char* start, const char* end) {
|
2021-12-25 10:04:45 +08:00
|
|
|
auto endv = const_cast<char*>(end);
|
2019-11-24 23:15:56 +08:00
|
|
|
char charS = *endv;
|
|
|
|
switch (++revisionIndex)
|
|
|
|
{
|
|
|
|
case 3:
|
2021-12-25 10:04:45 +08:00
|
|
|
*endv = '\0';
|
2019-11-24 23:15:56 +08:00
|
|
|
writterVersion = atoi(start);
|
2021-12-25 10:04:45 +08:00
|
|
|
*endv = charS;
|
2019-11-24 23:15:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// parse reader version
|
|
|
|
revisionIndex = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
fast_split(&loader->_csBuildID.front(), '.', [&](char* start, char* end) {
|
|
|
|
auto endv = const_cast<char*>(end);
|
2019-11-24 23:15:56 +08:00
|
|
|
char charS = *endv;
|
|
|
|
switch (++revisionIndex)
|
|
|
|
{
|
|
|
|
case 3:
|
2021-12-25 10:04:45 +08:00
|
|
|
*endv = '\0';
|
2019-11-24 23:15:56 +08:00
|
|
|
readerVersion = atoi(start);
|
2021-12-25 10:04:45 +08:00
|
|
|
*endv = charS;
|
2019-11-24 23:15:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
CCASSERT(readerVersion >= writterVersion,
|
2021-12-25 10:04:45 +08:00
|
|
|
StringUtils::format(
|
|
|
|
"%s%s%s%s%s%s%s%s%s%s", "The reader build id of your Cocos exported file(", csBuildId->c_str(),
|
|
|
|
") and the reader build id in your Cocos2d-x(", loader->_csBuildID.c_str(),
|
|
|
|
") are not match.\n", "Please get the correct reader(build id ", csBuildId->c_str(), ")from ",
|
|
|
|
"http://www.cocos2d-x.org/filedown/cocos-reader", " and replace it in your Cocos2d-x")
|
|
|
|
.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// decode plist
|
2021-12-25 10:04:45 +08:00
|
|
|
auto textures = csparsebinary->textures();
|
2019-11-23 20:27:39 +08:00
|
|
|
int textureSize = csparsebinary->textures()->size();
|
|
|
|
CCLOG("textureSize = %d", textureSize);
|
|
|
|
for (int i = 0; i < textureSize; ++i)
|
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
std::string plist = textures->Get(i)->c_str();
|
|
|
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
node = loader->nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
loader->reconstructNestNode(node);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::createNodeWithFlatBuffersFile(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return createNodeWithFlatBuffersFile(filename, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::createNodeWithFlatBuffersFile(std::string_view filename, const ccNodeLoadCallback& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Node* node = nodeWithFlatBuffersFile(filename, callback);
|
|
|
|
|
|
|
|
reconstructNestNode(node);
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
inline void CSLoader::reconstructNestNode(cocos2d::Node* node)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
/* To reconstruct nest node as WidgetCallBackHandlerProtocol. */
|
2021-12-25 10:04:45 +08:00
|
|
|
auto callbackHandler = dynamic_cast<WidgetCallBackHandlerProtocol*>(node);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (callbackHandler)
|
|
|
|
{
|
|
|
|
_callbackHandlers.popBack();
|
|
|
|
if (_callbackHandlers.empty())
|
|
|
|
{
|
|
|
|
_rootNode = nullptr;
|
|
|
|
CCLOG("Call back handler container has been clear.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_rootNode = _callbackHandlers.back();
|
2021-12-27 13:52:08 +08:00
|
|
|
CCLOG("after pop back _rootNode name = %s", _rootNode->getName().data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::nodeWithFlatBuffersFile(std::string_view fileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return nodeWithFlatBuffersFile(fileName, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::nodeWithFlatBuffersFile(std::string_view fileName, const ccNodeLoadCallback& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_ASSERT(FileUtils::getInstance()->isFileExist(fullPath));
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Data buf = FileUtils::getInstance()->getDataFromFile(fullPath);
|
|
|
|
|
|
|
|
if (buf.isNull())
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
CCLOG("CSLoader::nodeWithFlatBuffersFile - failed read file: %s", fileName.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_ASSERT(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto csparsebinary = GetCSParseBinary(buf.getBytes());
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto csBuildId = csparsebinary->version();
|
|
|
|
if (csBuildId)
|
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
int readerVersion = 0, writterVersion = 0;
|
|
|
|
// parse writter version
|
|
|
|
int revisionIndex = 0;
|
|
|
|
fast_split(csBuildId->c_str(), '.', [&](const char* start, const char* end) {
|
2021-12-25 10:04:45 +08:00
|
|
|
auto endv = const_cast<char*>(end);
|
2019-11-24 23:15:56 +08:00
|
|
|
char charS = *endv;
|
|
|
|
switch (++revisionIndex)
|
|
|
|
{
|
|
|
|
case 3:
|
2021-12-25 10:04:45 +08:00
|
|
|
*endv = '\0';
|
2019-11-24 23:15:56 +08:00
|
|
|
writterVersion = atoi(start);
|
2021-12-25 10:04:45 +08:00
|
|
|
*endv = charS;
|
2019-11-24 23:15:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// parse reader version
|
|
|
|
revisionIndex = 0;
|
|
|
|
fast_split(&_csBuildID.front(), '.', [&](char* start, char* end) {
|
2021-12-25 10:04:45 +08:00
|
|
|
auto endv = const_cast<char*>(end);
|
2019-11-24 23:15:56 +08:00
|
|
|
char charS = *endv;
|
|
|
|
switch (++revisionIndex)
|
|
|
|
{
|
|
|
|
case 3:
|
2021-12-25 10:04:45 +08:00
|
|
|
*endv = '\0';
|
2019-11-24 23:15:56 +08:00
|
|
|
readerVersion = atoi(start);
|
2021-12-25 10:04:45 +08:00
|
|
|
*endv = charS;
|
2019-11-24 23:15:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
CCASSERT(readerVersion >= writterVersion,
|
2021-12-25 10:04:45 +08:00
|
|
|
StringUtils::format(
|
|
|
|
"%s%s%s%s%s%s%s%s%s%s", "The reader build id of your Cocos exported file(", csBuildId->c_str(),
|
|
|
|
") and the reader build id in your Cocos2d-x(", _csBuildID.c_str(), ") are not match.\n",
|
|
|
|
"Please get the correct reader(build id ", csBuildId->c_str(), ")from ",
|
|
|
|
"http://www.cocos2d-x.org/filedown/cocos-reader", " and replace it in your Cocos2d-x")
|
|
|
|
.c_str());
|
|
|
|
|
|
|
|
if (readerVersion < writterVersion)
|
|
|
|
{
|
|
|
|
auto exceptionMsg =
|
|
|
|
StringUtils::format("error: The csloader version not match, require version is:%s, but %s provided!",
|
|
|
|
csBuildId->c_str(), _csBuildID.c_str());
|
2019-11-24 23:15:56 +08:00
|
|
|
throw std::logic_error(exceptionMsg.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// decode plist
|
2021-12-25 10:04:45 +08:00
|
|
|
auto textures = csparsebinary->textures();
|
2019-11-23 20:27:39 +08:00
|
|
|
int textureSize = textures->size();
|
|
|
|
for (int i = 0; i < textureSize; ++i)
|
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
std::string plist = textures->Get(i)->c_str();
|
|
|
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Node* node = nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* CSLoader::nodeWithFlatBuffers(const flatbuffers::NodeTree* nodetree)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return nodeWithFlatBuffers(nodetree, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* CSLoader::nodeWithFlatBuffers(const flatbuffers::NodeTree* nodetree, const ccNodeLoadCallback& callback)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (nodetree == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
{
|
|
|
|
Node* node = nullptr;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string classname = nodetree->classname()->c_str();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto options = nodetree->options();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (classname == "ProjectNode")
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto reader = ProjectNodeReader::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto projectNodeOptions = (ProjectNodeOptions*)options->data();
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string filePath = projectNodeOptions->fileName()->c_str();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
cocostudio::timeline::ActionTimeline* action = nullptr;
|
2020-10-17 16:32:16 +08:00
|
|
|
if (!filePath.empty() && FileUtils::getInstance()->isFileExist(filePath))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Data buf = FileUtils::getInstance()->getDataFromFile(filePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
node = createNode(buf, callback);
|
|
|
|
action = createTimeline(buf, filePath);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-04 00:14:35 +08:00
|
|
|
node = Node::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
reader->setPropsWithFlatBuffers(node, (const flatbuffers::Table*)options->data());
|
2019-11-23 20:27:39 +08:00
|
|
|
if (action)
|
|
|
|
{
|
|
|
|
action->setTimeSpeed(projectNodeOptions->innerActionSpeed());
|
|
|
|
node->runAction(action);
|
|
|
|
action->gotoFrameAndPause(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (classname == "SimpleAudio")
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
node = Node::create();
|
|
|
|
auto reader = ComAudioReader::getInstance();
|
2019-11-24 23:15:56 +08:00
|
|
|
Component* component = reader->createComAudioWithFlatBuffers((const flatbuffers::Table*)options->data());
|
2019-11-23 20:27:39 +08:00
|
|
|
if (component)
|
|
|
|
{
|
|
|
|
component->setName(PlayableFrame::PLAYABLE_EXTENTION);
|
|
|
|
node->addComponent(component);
|
2019-11-24 23:15:56 +08:00
|
|
|
reader->setPropsWithFlatBuffers(node, (const flatbuffers::Table*)options->data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string customClassName = nodetree->customClassName()->c_str();
|
|
|
|
if (customClassName != "")
|
|
|
|
{
|
|
|
|
classname = customClassName;
|
|
|
|
}
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string readername{getGUIClassName(classname)};
|
2019-11-23 20:27:39 +08:00
|
|
|
readername.append("Reader");
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
NodeReaderProtocol* reader =
|
|
|
|
dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
|
|
|
|
if (reader == nullptr)
|
|
|
|
reader = dynamic_cast<NodeReaderProtocol*>(
|
|
|
|
ObjectFactory::getInstance()->createObject("CustomRootNodeReader"));
|
2019-11-24 23:15:56 +08:00
|
|
|
if (reader != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
if (!customClassName.empty())
|
|
|
|
reader->setCurrentCustomClassName(customClassName.c_str());
|
|
|
|
|
|
|
|
node = reader->createNodeWithFlatBuffers((const flatbuffers::Table*)options->data());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto exceptionMsg = StringUtils::format(
|
|
|
|
R"(error: Missing custom reader class name:%s, please config at your project fiile xxx.xsxproj like follow:
|
2019-11-24 23:15:56 +08:00
|
|
|
<Project>
|
|
|
|
<publish-opts>
|
|
|
|
<custom-readers>
|
|
|
|
<item>%s</item>
|
|
|
|
</custom-readers>
|
|
|
|
</publish-opts>
|
|
|
|
</Project>
|
2021-12-25 10:04:45 +08:00
|
|
|
)",
|
|
|
|
readername.c_str(), readername.c_str());
|
2019-11-24 23:15:56 +08:00
|
|
|
throw std::logic_error(exceptionMsg.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Widget* widget = dynamic_cast<Widget*>(node);
|
|
|
|
if (widget)
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
auto callbackName = widget->getCallbackName();
|
|
|
|
auto callbackType = widget->getCallbackType();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bindCallback(callbackName, callbackType, widget, _rootNode);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/* To reconstruct nest node as WidgetCallBackHandlerProtocol. */
|
2021-12-25 10:04:45 +08:00
|
|
|
auto callbackHandler = dynamic_cast<WidgetCallBackHandlerProtocol*>(node);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (callbackHandler)
|
|
|
|
{
|
|
|
|
_callbackHandlers.pushBack(node);
|
|
|
|
_rootNode = _callbackHandlers.back();
|
|
|
|
}
|
|
|
|
/**/
|
|
|
|
// _loadingNodeParentHierarchy.push_back(node);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// If node is invalid, there is no necessity to process children of node.
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto children = nodetree->children();
|
2021-12-25 10:04:45 +08:00
|
|
|
int size = children->size();
|
2019-11-23 20:27:39 +08:00
|
|
|
for (int i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto subNodeTree = children->Get(i);
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* child = nodeWithFlatBuffers(subNodeTree, callback);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (child)
|
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
if (auto pageView = dynamic_cast<PageView*>(node))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Layout* layout = dynamic_cast<Layout*>(child);
|
|
|
|
if (layout)
|
|
|
|
{
|
|
|
|
pageView->addPage(layout);
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
else if (auto listView = dynamic_cast<ListView*>(node))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
|
|
|
if (widget)
|
|
|
|
{
|
|
|
|
listView->pushBackCustomItem(widget);
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
else if (auto radioButtonGroup = dynamic_cast<RadioButtonGroup*>(node))
|
|
|
|
{
|
|
|
|
radioButtonGroup->addRadioButton(dynamic_cast<RadioButton*>(child));
|
|
|
|
radioButtonGroup->addChild(child);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
node->addChild(child);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
callback(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// _loadingNodeParentHierarchy.pop_back();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool CSLoader::bindCallback(std::string_view callbackName,
|
|
|
|
std::string_view callbackType,
|
2021-12-25 10:04:45 +08:00
|
|
|
cocos2d::ui::Widget* sender,
|
|
|
|
cocos2d::Node* handler)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (callbackName.empty())
|
|
|
|
return false;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto callbackHandler = dynamic_cast<WidgetCallBackHandlerProtocol*>(handler);
|
|
|
|
if (callbackHandler) // The handler can handle callback
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (callbackType == "Click")
|
|
|
|
{
|
|
|
|
Widget::ccWidgetClickCallback callbackFunc = callbackHandler->onLocateClickCallback(callbackName);
|
|
|
|
if (callbackFunc)
|
|
|
|
{
|
|
|
|
sender->addClickEventListener(callbackFunc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (callbackType == "Touch")
|
|
|
|
{
|
|
|
|
Widget::ccWidgetTouchCallback callbackFunc = callbackHandler->onLocateTouchCallback(callbackName);
|
|
|
|
if (callbackFunc)
|
|
|
|
{
|
|
|
|
sender->addTouchEventListener(callbackFunc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (callbackType == "Event")
|
|
|
|
{
|
|
|
|
Widget::ccWidgetEventCallback callbackFunc = callbackHandler->onLocateEventCallback(callbackName);
|
|
|
|
if (callbackFunc)
|
|
|
|
{
|
|
|
|
sender->addCCSEventListener(callbackFunc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2021-12-27 13:52:08 +08:00
|
|
|
CCLOG("callBackName %s cannot be found", callbackName.data());
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool CSLoader::isWidget(std::string_view type)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return (type == ClassName_Panel || type == ClassName_Button || type == ClassName_CheckBox ||
|
|
|
|
type == ClassName_ImageView || type == ClassName_TextAtlas || type == ClassName_LabelAtlas ||
|
|
|
|
type == ClassName_LabelBMFont || type == ClassName_TextBMFont || type == ClassName_Text ||
|
|
|
|
type == ClassName_LoadingBar || type == ClassName_TextField || type == ClassName_Slider ||
|
|
|
|
type == ClassName_Layout || type == ClassName_ScrollView || type == ClassName_ListView ||
|
|
|
|
type == ClassName_PageView || type == ClassName_Widget || type == ClassName_Label);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool CSLoader::isCustomWidget(std::string_view type)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(ObjectFactory::getInstance()->createObject(type));
|
|
|
|
if (widget)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string_view CSLoader::getGUIClassName(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string_view convertedClassName;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (name == "Panel")
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
convertedClassName = "Layout"sv;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (name == "TextArea")
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
convertedClassName = "Text"sv;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (name == "TextButton")
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
convertedClassName = "Button"sv;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (name == "Label")
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
convertedClassName = "Text"sv;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (name == "LabelAtlas")
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
convertedClassName = "TextAtlas"sv;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else if (name == "LabelBMFont")
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
convertedClassName = "TextBMFont"sv;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-27 13:52:08 +08:00
|
|
|
else
|
|
|
|
convertedClassName = name;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return convertedClassName;
|
|
|
|
}
|
|
|
|
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string_view CSLoader::getWidgetReaderClassName(Widget* widget)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string_view readerName;
|
2019-11-24 23:15:56 +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";
|
|
|
|
}
|
2019-11-24 23:15:56 +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-27 13:52:08 +08:00
|
|
|
else
|
|
|
|
readerName = hlookup::empty_sv;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return readerName;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void CSLoader::registReaderObject(std::string_view className, ObjectFactory::Instance ins)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ObjectFactory::TInfo t;
|
|
|
|
t._class = className;
|
2021-12-25 10:04:45 +08:00
|
|
|
t._fun = ins;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ObjectFactory::getInstance()->registerType(t);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Node* CSLoader::createNodeWithFlatBuffersForSimulator(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
|
|
|
|
fbs->_isSimulator = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
FlatBufferBuilder* builder = fbs->createFlatBuffersWithXMLFileForSimulator(filename);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto csparsebinary = GetCSParseBinary(builder->GetBufferPointer());
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// decode plist
|
2021-12-25 10:04:45 +08:00
|
|
|
auto textures = csparsebinary->textures();
|
2019-11-23 20:27:39 +08:00
|
|
|
int textureSize = csparsebinary->textures()->size();
|
|
|
|
// CCLOG("textureSize = %d", textureSize);
|
|
|
|
for (int i = 0; i < textureSize; ++i)
|
|
|
|
{
|
|
|
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str());
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto nodeTree = csparsebinary->nodeTree();
|
|
|
|
|
|
|
|
Node* node = nodeWithFlatBuffersForSimulator(nodeTree);
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_rootNode = nullptr;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
fbs->deleteFlatBufferBuilder();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* CSLoader::nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree* nodetree)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Node* node = nullptr;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string classname = nodetree->classname()->c_str();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto options = nodetree->options();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (classname == "ProjectNode")
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto reader = ProjectNodeReader::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto projectNodeOptions = (ProjectNodeOptions*)options->data();
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string filePath = projectNodeOptions->fileName()->c_str();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
cocostudio::timeline::ActionTimeline* action = nullptr;
|
|
|
|
if (filePath != "" && FileUtils::getInstance()->isFileExist(filePath))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
node = createNodeWithFlatBuffersForSimulator(filePath);
|
|
|
|
action = cocostudio::timeline::ActionTimelineCache::getInstance()->createActionWithFlatBuffersForSimulator(
|
|
|
|
filePath);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-04 00:14:35 +08:00
|
|
|
node = Node::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
reader->setPropsWithFlatBuffers(node, (const flatbuffers::Table*)options->data());
|
2019-11-23 20:27:39 +08:00
|
|
|
if (action)
|
|
|
|
{
|
|
|
|
action->setTimeSpeed(projectNodeOptions->innerActionSpeed());
|
|
|
|
node->runAction(action);
|
|
|
|
action->gotoFrameAndPause(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (classname == "SimpleAudio")
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
node = Node::create();
|
|
|
|
auto reader = ComAudioReader::getInstance();
|
2019-11-24 23:15:56 +08:00
|
|
|
Component* component = reader->createComAudioWithFlatBuffers((const flatbuffers::Table*)options->data());
|
2019-11-23 20:27:39 +08:00
|
|
|
if (component)
|
|
|
|
{
|
|
|
|
node->addComponent(component);
|
2019-11-24 23:15:56 +08:00
|
|
|
reader->setPropsWithFlatBuffers(node, (const flatbuffers::Table*)options->data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
std::string readername{getGUIClassName(classname)};
|
2019-11-23 20:27:39 +08:00
|
|
|
readername.append("Reader");
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
NodeReaderProtocol* reader =
|
|
|
|
dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
|
2019-11-23 20:27:39 +08:00
|
|
|
if (reader)
|
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
node = reader->createNodeWithFlatBuffers((const flatbuffers::Table*)options->data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Widget* widget = dynamic_cast<Widget*>(node);
|
|
|
|
if (widget)
|
|
|
|
{
|
2021-12-27 13:52:08 +08:00
|
|
|
auto callbackName = widget->getCallbackName();
|
|
|
|
auto callbackType = widget->getCallbackType();
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bindCallback(callbackName, callbackType, widget, _rootNode);
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_rootNode == nullptr)
|
|
|
|
{
|
|
|
|
_rootNode = node;
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
// _loadingNodeParentHierarchy.push_back(node);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// If node is invalid, there is no necessity to process children of node.
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto children = nodetree->children();
|
2021-12-25 10:04:45 +08:00
|
|
|
int size = children->size();
|
2019-11-23 20:27:39 +08:00
|
|
|
for (int i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
auto subNodeTree = children->Get(i);
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* child = nodeWithFlatBuffersForSimulator(subNodeTree);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
PageView* pageView = dynamic_cast<PageView*>(node);
|
|
|
|
ListView* listView = dynamic_cast<ListView*>(node);
|
|
|
|
if (pageView)
|
|
|
|
{
|
|
|
|
Layout* layout = dynamic_cast<Layout*>(child);
|
|
|
|
if (layout)
|
|
|
|
{
|
|
|
|
pageView->addPage(layout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (listView)
|
|
|
|
{
|
|
|
|
Widget* widget = dynamic_cast<Widget*>(child);
|
|
|
|
if (widget)
|
|
|
|
{
|
|
|
|
listView->pushBackCustomItem(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->addChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
|
|
|
|
// _loadingNodeParentHierarchy.pop_back();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|