axmol/extensions/cocostudio/WidgetReader/ArmatureNodeReader/ArmatureNodeReader.cpp

246 lines
8.2 KiB
C++
Raw Normal View History

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