axmol/extensions/libccs30/WidgetReader/ArmatureNodeReader/ArmatureNodeReader.cpp

256 lines
7.1 KiB
C++
Raw Normal View History

2020-10-17 16:32:16 +08:00
#include "libccs30/WidgetReader/ArmatureNodeReader/ArmatureNodeReader.h"
2019-11-23 20:27:39 +08:00
#include "platform/CCFileUtils.h"
2019-11-24 23:15:56 +08:00
2019-11-23 20:27:39 +08:00
#include "flatbuffers/flatbuffers.h"
2020-10-17 16:32:16 +08:00
#include "libccs30/WidgetReader/NodeReader/NodeReader.h"
#include "libccs30/CSParseBinary_generated.h"
#include "libccs30/WidgetReader/ArmatureNodeReader/CSArmatureNode_generated.h"
#include "libeditor/CCArmature.h"
#if defined(CC_BUILD_WITH_DRANGBONES) && CC_BUILD_WITH_DRANGBONES
#include "DragonBones/CCDragonBonesHeaders.h"
#endif
2019-11-23 20:27:39 +08:00
USING_NS_CC;
using namespace cocostudio;
using namespace flatbuffers;
IMPLEMENT_CLASS_NODE_READER_INFO(ArmatureNodeReader)
ArmatureNodeReader::ArmatureNodeReader()
{
}
ArmatureNodeReader::~ArmatureNodeReader()
{
}
static ArmatureNodeReader* _instanceArmatureNodeReader = nullptr;
ArmatureNodeReader* ArmatureNodeReader::getInstance()
{
if (_instanceArmatureNodeReader == nullptr)
{
_instanceArmatureNodeReader = new (std::nothrow) ArmatureNodeReader();
}
return _instanceArmatureNodeReader;
}
void ArmatureNodeReader::destroyInstance()
{
2019-11-24 23:15:56 +08:00
CC_SAFE_DELETE(_instanceArmatureNodeReader);
2019-11-23 20:27:39 +08:00
}
2019-11-24 23:15:56 +08:00
Offset<Table> ArmatureNodeReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
2019-11-23 20:27:39 +08:00
flatbuffers::FlatBufferBuilder *builder)
{
auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
bool isloop = false;
bool isAutoPlay = false;
2019-11-24 23:15:56 +08:00
std::string currentAnimationName;
std::string currentArmatureName;
2019-11-23 20:27:39 +08:00
int type = 0;
2020-08-04 10:55:30 +08:00
std::string path;
2019-11-23 20:27:39 +08:00
2019-11-24 23:15:56 +08:00
float armatureScale = 1.0f;
float timeScale = 1.0f;
/*int textureInfoFileType = 0;
std::string textureInfoFilePath;*/
auto attribute = objectData.first_attribute();
2019-11-23 20:27:39 +08:00
while (attribute)
{
2019-11-24 23:15:56 +08:00
std::string attriname = attribute.name();
std::string value = attribute.value();
2019-11-23 20:27:39 +08:00
if (attriname == "IsLoop")
{
isloop = (value == "True") ? true : false;
}
else if (attriname == "IsAutoPlay")
{
isAutoPlay = (value == "True") ? true : false;
2019-11-24 23:15:56 +08:00
}
2019-11-23 20:27:39 +08:00
else if (attriname == "CurrentAnimationName")
{
2019-11-24 23:15:56 +08:00
currentAnimationName = value;
2019-11-23 20:27:39 +08:00
}
2019-11-24 23:15:56 +08:00
else if (attriname == "CurrentArmatureName")
{
currentArmatureName = value;
}
else if (attriname == "ArmatureScale")
{
armatureScale = atof(value.c_str());
}
else if (attriname == "TimeScale")
{
timeScale = atof(value.c_str());
}
2019-11-23 20:27:39 +08:00
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2019-11-23 20:27:39 +08:00
}
2019-11-24 23:15:56 +08:00
auto child = objectData.first_child();
2019-11-23 20:27:39 +08:00
while (child)
{
2019-11-24 23:15:56 +08:00
std::string attriname = child.name();
2019-11-23 20:27:39 +08:00
if (attriname == "FileData")
{
2019-11-24 23:15:56 +08:00
attribute = child.first_attribute();
2019-11-23 20:27:39 +08:00
while (attribute)
{
2019-11-24 23:15:56 +08:00
attriname = attribute.name();
std::string value = attribute.value();
2019-11-23 20:27:39 +08:00
if (attriname == "Type")
{
2019-11-24 23:15:56 +08:00
type = 0;
2019-11-23 20:27:39 +08:00
}
else if (attriname == "Path")
{
2019-11-24 23:15:56 +08:00
path = value;
2019-11-23 20:27:39 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2019-11-23 20:27:39 +08:00
}
2019-11-24 23:15:56 +08:00
}
child = child.next_sibling();
2019-11-23 20:27:39 +08:00
}
auto options = CreateCSArmatureNodeOption(*builder,
nodeOptions,
CreateResourceItemData(*builder,
2019-11-24 23:15:56 +08:00
type,
builder->CreateString(path)),
2019-11-23 20:27:39 +08:00
isloop,
isAutoPlay,
2019-11-24 23:15:56 +08:00
builder->CreateString(currentAnimationName),
builder->CreateString(currentArmatureName),
timeScale,
armatureScale);
2019-11-23 20:27:39 +08:00
return *(Offset<Table>*)(&options);
}
void ArmatureNodeReader::setPropsWithFlatBuffers(cocos2d::Node *node,
const flatbuffers::Table *nodeOptions)
{
2019-11-24 23:15:56 +08:00
Node** ppResult = (Node**)(node);
2019-11-23 20:27:39 +08:00
auto options = (flatbuffers::CSArmatureNodeOption*)nodeOptions;
2019-11-24 23:15:56 +08:00
bool fileExist = false;
2020-08-04 10:55:30 +08:00
std::string errorFilePath;
2019-11-24 23:15:56 +08:00
std::string filepath(options->fileData()->path()->c_str());
if (FileUtils::getInstance()->isFileExist(filepath))
{
fileExist = true;
#if defined(CC_BUILD_WITH_DRANGBONES) && CC_BUILD_WITH_DRANGBONES
2019-11-24 23:15:56 +08:00
auto filep = filepath.rfind('.');
if (filep != std::string::npos && strcmp(&filepath[filep], ".json") == 0)
{ // Currently, adjust by file ext, regard as DragonBones 4.5/5.0
// 4.5 texture info is fixed as texture.png, texture.json
// 5.o texture info is _tex.json _tex.png
auto sharedFactory = dragonBones::CCFactory::getFactory();
const auto dragonBonesData = sharedFactory->loadDragonBonesData(filepath, filepath);
if (dragonBonesData != nullptr) {
auto slash = filepath.rfind('/');
2019-11-24 23:15:56 +08:00
if (slash == std::string::npos)
slash = filepath.rfind("\\");
if (slash != std::string::npos) {
auto folder = filepath.substr(0, slash + 1);
// try dragonBones 5.0 firstly;
std::string commonName;
auto _skePos = filepath.find("_ske");
if(_skePos != std::string::npos) {
commonName = filepath.substr(slash + 1, _skePos - slash - 1);
}
auto succeed = sharedFactory->loadTextureAtlasData(folder + commonName + "_tex.json", filepath) != nullptr;
if (succeed || sharedFactory->loadTextureAtlasData(folder + "texture.json", filepath) != nullptr)
{
std::string designArmatureName(options->currentArmatureName()->c_str());
auto armatureNode = sharedFactory->buildArmatureDisplay(designArmatureName);
armatureNode->setScale(options->armatureScale());
armatureNode->getAnimation()->timeScale = options->timeScale();
std::string currentname = options->currentAnimationName()->c_str();
armatureNode->getAnimation()->play(currentname, options->isLoop() ? -1 : 1);
*ppResult = armatureNode;
}
}
}
}
else
#endif
{
2019-11-24 23:15:56 +08:00
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(filepath);
std::string dirpath = fullpath.substr(0, fullpath.find_last_of('/'));
2019-11-24 23:15:56 +08:00
FileUtils::getInstance()->addSearchPath(dirpath);
ArmatureDataManager::getInstance()->addArmatureFileInfo(filepath);
auto armatureNode = Armature::create(getArmatureName(filepath));
std::string currentname = options->currentAnimationName()->c_str();
if (options->isAutoPlay())
armatureNode->getAnimation()->play(currentname, -1, options->isLoop());
else
{
armatureNode->getAnimation()->play(currentname);
armatureNode->getAnimation()->gotoAndPause(0);
}
*ppResult = armatureNode;
}
}
else
{
*ppResult = Node::create();
errorFilePath = filepath;
fileExist = false;
}
2019-11-23 20:27:39 +08:00
}
cocos2d::Node* ArmatureNodeReader::createNodeWithFlatBuffers(const flatbuffers::Table *nodeOptions)
{
2019-11-24 23:15:56 +08:00
Node* node = nullptr;// auto node = Armature::create();
2019-11-23 20:27:39 +08:00
// self
auto options = (flatbuffers::CSArmatureNodeOption*)nodeOptions;
2019-11-24 23:15:56 +08:00
setPropsWithFlatBuffers((Node*)(&node), (Table*)options);
2019-11-23 20:27:39 +08:00
// super node
auto NodeReader = NodeReader::getInstance();
NodeReader->setPropsWithFlatBuffers(node, (Table*)options->nodeOptions());
return node;
}
std::string ArmatureNodeReader::getArmatureName(const std::string& exporJsonPath)
{
//FileUtils.getFileData(exporJsonPath, "r", size) // need read armature name in exportJsonPath
2019-11-24 23:15:56 +08:00
size_t end = exporJsonPath.find_last_of(".");
size_t start = exporJsonPath.find_last_of('\\') + 1;
size_t start1 = exporJsonPath.find_last_of('/') + 1;
2019-11-23 20:27:39 +08:00
if (start < start1)
start = start1;
if (start == -1)
start = 0;
return exporJsonPath.substr(start, end - start);
2019-11-24 23:15:56 +08:00
}