2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CCComRender.h"
|
|
|
|
#include "CocoStudio.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "platform/CCFileUtils.h"
|
2020-08-28 17:26:30 +08:00
|
|
|
#include "2d/CCFastTMXTiledMap.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "2d/CCParticleSystemQuad.h"
|
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
IMPLEMENT_CLASS_COMPONENT_INFO(ComRender)
|
|
|
|
|
|
|
|
const std::string ComRender::COMPONENT_NAME = "CCComRender";
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ComRender::ComRender() : _render(nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_name = COMPONENT_NAME;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ComRender::ComRender(cocos2d::Node* node, const char* comName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (node != nullptr)
|
|
|
|
{
|
|
|
|
_render = node;
|
|
|
|
_render->retain();
|
|
|
|
}
|
|
|
|
_name.assign(comName);
|
|
|
|
}
|
|
|
|
|
|
|
|
ComRender::~ComRender()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_render);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComRender::onEnter()
|
|
|
|
{
|
|
|
|
if (_owner != nullptr && _render->getParent() == nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_owner->addChild(_render);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComRender::onExit()
|
|
|
|
{
|
|
|
|
if (_owner != nullptr && _render->getParent() == nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_owner->removeChild(_render, true);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComRender::onAdd()
|
|
|
|
{
|
|
|
|
if (_owner != nullptr && _render->getParent() == nullptr)
|
|
|
|
{
|
|
|
|
_owner->addChild(_render);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComRender::onRemove()
|
|
|
|
{
|
|
|
|
if (_owner != nullptr && _render->getParent() == nullptr)
|
|
|
|
{
|
|
|
|
_owner->removeChild(_render, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cocos2d::Node* ComRender::getNode()
|
|
|
|
{
|
|
|
|
return _render;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ComRender::setNode(cocos2d::Node* node)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_render != nullptr)
|
|
|
|
{
|
|
|
|
_render->release();
|
|
|
|
_render = nullptr;
|
|
|
|
}
|
|
|
|
if (node != nullptr)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_render = node;
|
|
|
|
_render->retain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This method is soooo big!! We should refactor it!
|
|
|
|
bool ComRender::serialize(void* r)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(r == nullptr);
|
2021-12-25 10:04:45 +08:00
|
|
|
SerData* serData = (SerData*)(r);
|
|
|
|
const rapidjson::Value* v = serData->_rData;
|
|
|
|
stExpCocoNode* cocoNode = serData->_cocoNode;
|
|
|
|
CocoLoader* cocoLoader = serData->_cocoLoader;
|
|
|
|
const char* className = nullptr;
|
|
|
|
const char* comName = nullptr;
|
|
|
|
const char* file = nullptr;
|
|
|
|
const char* plist = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string filePath;
|
|
|
|
std::string plistPath;
|
|
|
|
int resType = 0;
|
|
|
|
if (v != nullptr)
|
|
|
|
{
|
|
|
|
className = DICTOOL->getStringValue_json(*v, "classname");
|
|
|
|
CC_BREAK_IF(className == nullptr);
|
2021-12-25 10:04:45 +08:00
|
|
|
comName = DICTOOL->getStringValue_json(*v, "name");
|
|
|
|
const rapidjson::Value& fileData = DICTOOL->getSubDictionary_json(*v, "fileData");
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData));
|
2021-12-25 10:04:45 +08:00
|
|
|
file = DICTOOL->getStringValue_json(fileData, "path");
|
2019-11-23 20:27:39 +08:00
|
|
|
plist = DICTOOL->getStringValue_json(fileData, "plistFile");
|
|
|
|
CC_BREAK_IF(file == nullptr && plist == nullptr);
|
|
|
|
resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (cocoNode != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
className = cocoNode[1].GetValue(cocoLoader);
|
|
|
|
CC_BREAK_IF(className == nullptr);
|
2021-12-25 10:04:45 +08:00
|
|
|
comName = cocoNode[2].GetValue(cocoLoader);
|
|
|
|
stExpCocoNode* pfileData = cocoNode[4].GetChildArray(cocoLoader);
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_BREAK_IF(!pfileData);
|
2021-12-25 10:04:45 +08:00
|
|
|
file = pfileData[0].GetValue(cocoLoader);
|
2019-11-23 20:27:39 +08:00
|
|
|
plist = pfileData[1].GetValue(cocoLoader);
|
|
|
|
CC_BREAK_IF(file == nullptr && plist == nullptr);
|
|
|
|
resType = atoi(pfileData[2].GetValue(cocoLoader));
|
|
|
|
}
|
|
|
|
if (comName != nullptr)
|
|
|
|
{
|
|
|
|
setName(comName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setName(className);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (file != nullptr)
|
|
|
|
{
|
|
|
|
filePath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(file));
|
|
|
|
}
|
|
|
|
if (plist != nullptr)
|
|
|
|
{
|
|
|
|
plistPath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(plist));
|
|
|
|
}
|
|
|
|
if (resType == 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (strcmp(className, "CCSprite") == 0 &&
|
|
|
|
(filePath.find(".png") != filePath.npos || filePath.find(".pvr.ccz") != filePath.npos))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_render = Sprite::create(filePath);
|
|
|
|
_render->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcmp(className, "CCTMXTiledMap") == 0 && filePath.find(".tmx") != filePath.npos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_render = TMXTiledMap::create(filePath);
|
|
|
|
_render->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcmp(className, "CCParticleSystemQuad") == 0 && filePath.find(".plist") != filePath.npos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_render = ParticleSystemQuad::create(filePath);
|
|
|
|
_render->setPosition(0.0f, 0.0f);
|
|
|
|
_render->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcmp(className, "CCArmature") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string fileExtension = FileUtils::getInstance()->getFileExtension(filePath);
|
|
|
|
if (fileExtension == ".json" || fileExtension == ".exportjson")
|
|
|
|
{
|
|
|
|
rapidjson::Document doc;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!readJson(filePath, doc))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
log("read json file[%s] error!\n", filePath.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
const rapidjson::Value& subData = DICTOOL->getDictionaryFromArray_json(doc, "armature_data", 0);
|
|
|
|
const char* name = DICTOOL->getStringValue_json(subData, "name");
|
2019-11-23 20:27:39 +08:00
|
|
|
ArmatureDataManager::getInstance()->addArmatureFileInfo(filePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
Armature* pAr = Armature::create(name);
|
|
|
|
_render = pAr;
|
2019-11-23 20:27:39 +08:00
|
|
|
_render->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* actionName = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (cocoNode != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
actionName = cocoNode[6].GetValue(
|
|
|
|
cocoLoader); // DICTOOL->getStringValue_json(*v, "selectedactionname");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
actionName = DICTOOL->getStringValue_json(*v, "selectedactionname");
|
|
|
|
}
|
|
|
|
if (actionName != nullptr && pAr->getAnimation() != nullptr)
|
|
|
|
{
|
|
|
|
pAr->getAnimation()->play(actionName);
|
|
|
|
}
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else if (fileExtension == ".csb")
|
|
|
|
{
|
|
|
|
std::string binaryFilePath = FileUtils::getInstance()->fullPathForFilename(filePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto fileData = FileUtils::getInstance()->getDataFromFile(binaryFilePath);
|
|
|
|
auto fileDataBytes = fileData.getBytes();
|
2019-11-23 20:27:39 +08:00
|
|
|
CC_BREAK_IF(fileData.isNull());
|
|
|
|
CocoLoader tCocoLoader;
|
|
|
|
if (tCocoLoader.ReadCocoBinBuff((char*)fileDataBytes))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
stExpCocoNode* tpRootCocoNode = tCocoLoader.GetRootCocoNode();
|
|
|
|
rapidjson::Type tType = tpRootCocoNode->GetType(&tCocoLoader);
|
|
|
|
if (rapidjson::kObjectType == tType)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int count = tpRootCocoNode->GetChildNum();
|
|
|
|
stExpCocoNode* tpChildArray = tpRootCocoNode->GetChildArray(&tCocoLoader);
|
2019-11-23 20:27:39 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
std::string key = tpChildArray[i].GetName(&tCocoLoader);
|
|
|
|
if (key.compare("armature_data") == 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int length = tpChildArray[i].GetChildNum();
|
|
|
|
stExpCocoNode* armature_dataArray = tpChildArray[i].GetChildArray(&tCocoLoader);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (length < 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
length = armature_dataArray[0].GetChildNum();
|
|
|
|
stExpCocoNode* armature_data = armature_dataArray[0].GetChildArray(&tCocoLoader);
|
2019-11-23 20:27:39 +08:00
|
|
|
for (int j = 0; j < length; ++j)
|
|
|
|
{
|
|
|
|
std::string key1 = armature_data[j].GetName(&tCocoLoader);
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* str1 = armature_data[j].GetValue(&tCocoLoader);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (key.compare("name") == 0)
|
|
|
|
{
|
|
|
|
if (str1 != nullptr)
|
|
|
|
{
|
|
|
|
ArmatureDataManager::getInstance()->addArmatureFileInfo(filePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
Armature* pAr = Armature::create(str1);
|
|
|
|
_render = pAr;
|
2019-11-23 20:27:39 +08:00
|
|
|
_render->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* actionName = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (cocoNode != nullptr)
|
|
|
|
{
|
|
|
|
actionName = cocoNode[6].GetValue(&tCocoLoader);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
actionName = DICTOOL->getStringValue_json(*v, "selectedactionname");
|
|
|
|
}
|
|
|
|
if (actionName != nullptr && pAr->getAnimation() != nullptr)
|
|
|
|
{
|
|
|
|
pAr->getAnimation()->play(actionName);
|
|
|
|
}
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcmp(className, "GUIComponent") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string fileExtension = FileUtils::getInstance()->getFileExtension(filePath);
|
|
|
|
if (fileExtension == ".json" || fileExtension == ".exportjson")
|
|
|
|
{
|
|
|
|
cocos2d::ui::Widget* widget = GUIReader::getInstance()->widgetFromJsonFile(filePath.c_str());
|
2021-12-25 10:04:45 +08:00
|
|
|
_render = widget;
|
2019-11-23 20:27:39 +08:00
|
|
|
_render->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else if (fileExtension == ".csb")
|
|
|
|
{
|
|
|
|
cocos2d::ui::Widget* widget = GUIReader::getInstance()->widgetFromBinaryFile(filePath.c_str());
|
2021-12-25 10:04:45 +08:00
|
|
|
_render = widget;
|
2019-11-23 20:27:39 +08:00
|
|
|
_render->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (resType == 1)
|
|
|
|
{
|
|
|
|
if (strcmp(className, "CCSprite") == 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string strPngFile = plistPath;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string::size_type pos = strPngFile.find(".plist");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (pos == strPngFile.npos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strPngFile.replace(pos, strPngFile.length(), ".png");
|
2021-08-17 12:04:45 +08:00
|
|
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plistPath, strPngFile);
|
2019-11-23 20:27:39 +08:00
|
|
|
_render = Sprite::createWithSpriteFrameName(filePath);
|
|
|
|
_render->retain();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(true);
|
|
|
|
}
|
|
|
|
} while (0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComRender* ComRender::create()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ComRender* ret = new ComRender();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ComRender* ComRender::create(cocos2d::Node* node, const char* comName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ComRender* ret = new ComRender(node, comName);
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ComRender::readJson(const std::string& fileName, rapidjson::Document& doc)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string contentStr = FileUtils::getInstance()->getStringFromFile(fileName);
|
|
|
|
doc.Parse<0>(contentStr.c_str());
|
|
|
|
CC_BREAK_IF(doc.HasParseError());
|
|
|
|
ret = true;
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|