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

246 lines
8.2 KiB
C++
Raw Normal View History

#include "WidgetReader/ArmatureNodeReader/ArmatureNodeReader.h"
2019-11-23 20:27:39 +08:00
#include "platform/FileUtils.h"
2019-11-24 23:15:56 +08:00
2019-11-23 20:27:39 +08:00
#include "flatbuffers/flatbuffers.h"
#include "WidgetReader/NodeReader/NodeReader.h"
#include "CSParseBinary_generated.h"
#include "WidgetReader/ArmatureNodeReader/CSArmatureNode_generated.h"
#include "Armature.h"
2022-07-16 10:43:05 +08:00
#if defined(AX_BUILD_WITH_DRANGBONES) && AX_BUILD_WITH_DRANGBONES
2021-12-25 10:04:45 +08:00
# include "DragonBones/CCDragonBonesHeaders.h"
#endif
2019-11-23 20:27:39 +08:00
USING_NS_AX;
2019-11-23 20:27:39 +08:00
using namespace cocostudio;
using namespace flatbuffers;
IMPLEMENT_CLASS_NODE_READER_INFO(ArmatureNodeReader)
2021-12-25 10:04:45 +08:00
ArmatureNodeReader::ArmatureNodeReader() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
ArmatureNodeReader::~ArmatureNodeReader() {}
2019-11-23 20:27:39 +08:00
static ArmatureNodeReader* _instanceArmatureNodeReader = nullptr;
ArmatureNodeReader* ArmatureNodeReader::getInstance()
{
2021-12-25 10:04:45 +08:00
if (_instanceArmatureNodeReader == nullptr)
{
_instanceArmatureNodeReader = new ArmatureNodeReader();
}
return _instanceArmatureNodeReader;
2019-11-23 20:27:39 +08:00
}
void ArmatureNodeReader::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_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,
2021-12-25 10:04:45 +08:00
flatbuffers::FlatBufferBuilder* builder)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
2019-11-23 20:27:39 +08:00
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;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
int type = 0;
std::string path;
2019-11-23 20:27:39 +08:00
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
}
2019-11-23 20:27:39 +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);
2019-11-23 20:27:39 +08:00
}
2022-08-08 18:02:17 +08:00
void ArmatureNodeReader::setPropsWithFlatBuffers(ax::Node* node, const flatbuffers::Table* nodeOptions)
2019-11-23 20:27:39 +08:00
{
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;
2022-07-16 10:43:05 +08:00
#if defined(AX_BUILD_WITH_DRANGBONES) && AX_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)
{ // 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;
}
2019-11-23 20:27:39 +08:00
}
2022-08-08 18:02:17 +08:00
ax::Node* ArmatureNodeReader::createNodeWithFlatBuffers(const flatbuffers::Table* nodeOptions)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Node* node = nullptr; // auto node = Armature::create();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// self
auto options = (flatbuffers::CSArmatureNodeOption*)nodeOptions;
setPropsWithFlatBuffers((Node*)(&node), (Table*)options);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// super node
auto NodeReader = NodeReader::getInstance();
NodeReader->setPropsWithFlatBuffers(node, (Table*)options->nodeOptions());
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
return node;
2019-11-23 20:27:39 +08:00
}
std::string ArmatureNodeReader::getArmatureName(std::string_view exporJsonPath)
2019-11-23 20:27:39 +08:00
{
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;
return std::string{exporJsonPath.substr(start, end - start)};
2019-11-24 23:15:56 +08:00
}