ArmatureNodeReader.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "editor-support/cocostudio/WidgetReader/ArmatureNodeReader/ArmatureNodeReader.h"
  2. #include "platform/CCFileUtils.h"
  3. #include "tinyxml2.h"
  4. #include "flatbuffers/flatbuffers.h"
  5. #include "editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h"
  6. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  7. #include "editor-support/cocostudio/WidgetReader/ArmatureNodeReader/CSArmatureNode_generated.h"
  8. #include "editor-support/cocostudio/CCArmature.h"
  9. USING_NS_CC;
  10. using namespace cocostudio;
  11. using namespace flatbuffers;
  12. IMPLEMENT_CLASS_NODE_READER_INFO(ArmatureNodeReader)
  13. ArmatureNodeReader::ArmatureNodeReader()
  14. {
  15. }
  16. ArmatureNodeReader::~ArmatureNodeReader()
  17. {
  18. }
  19. static ArmatureNodeReader* _instanceArmatureNodeReader = nullptr;
  20. ArmatureNodeReader* ArmatureNodeReader::getInstance()
  21. {
  22. if (_instanceArmatureNodeReader == nullptr)
  23. {
  24. _instanceArmatureNodeReader = new (std::nothrow) ArmatureNodeReader();
  25. }
  26. return _instanceArmatureNodeReader;
  27. }
  28. void ArmatureNodeReader::destroyInstance()
  29. {
  30. CC_SAFE_DELETE(_instanceArmatureNodeReader);
  31. }
  32. Offset<Table> ArmatureNodeReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  33. flatbuffers::FlatBufferBuilder *builder)
  34. {
  35. auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  36. auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
  37. bool isloop = false;
  38. bool isAutoPlay = false;
  39. std::string currentAnimationName = "";
  40. int type = 0;
  41. std::string path = "";
  42. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  43. while (attribute)
  44. {
  45. std::string attriname = attribute->Name();
  46. std::string value = attribute->Value();
  47. if (attriname == "IsLoop")
  48. {
  49. isloop = (value == "True") ? true : false;
  50. }
  51. else if (attriname == "IsAutoPlay")
  52. {
  53. isAutoPlay = (value == "True") ? true : false;
  54. }
  55. else if (attriname == "CurrentAnimationName")
  56. {
  57. currentAnimationName = value;
  58. }
  59. attribute = attribute->Next();
  60. }
  61. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  62. while (child)
  63. {
  64. std::string attriname = child->Name();
  65. if (attriname == "FileData")
  66. {
  67. attribute = child->FirstAttribute();
  68. while (attribute)
  69. {
  70. attriname = attribute->Name();
  71. std::string value = attribute->Value();
  72. if (attriname == "Type")
  73. {
  74. type = 0;
  75. }
  76. else if (attriname == "Path")
  77. {
  78. path = value;
  79. }
  80. attribute = attribute->Next();
  81. }
  82. }
  83. child = child->NextSiblingElement();
  84. }
  85. auto options = CreateCSArmatureNodeOption(*builder,
  86. nodeOptions,
  87. CreateResourceItemData(*builder,
  88. type,
  89. builder->CreateString(path)),
  90. isloop,
  91. isAutoPlay,
  92. builder->CreateString(currentAnimationName));
  93. return *(Offset<Table>*)(&options);
  94. }
  95. void ArmatureNodeReader::setPropsWithFlatBuffers(cocos2d::Node *node,
  96. const flatbuffers::Table *nodeOptions)
  97. {
  98. auto* custom = static_cast<Armature*>(node);
  99. auto options = (flatbuffers::CSArmatureNodeOption*)nodeOptions;
  100. bool fileExist = false;
  101. std::string errorFilePath = "";
  102. std::string filepath(options->fileData()->path()->c_str());
  103. if (FileUtils::getInstance()->isFileExist(filepath))
  104. {
  105. fileExist = true;
  106. std::string fullpath = FileUtils::getInstance()->fullPathForFilename(filepath);
  107. std::string dirpath = fullpath.substr(0, fullpath.find_last_of("/"));
  108. FileUtils::getInstance()->addSearchPath(dirpath);
  109. ArmatureDataManager::getInstance()->addArmatureFileInfo(fullpath);
  110. custom->init(getArmatureName(filepath));
  111. std::string currentname = options->currentAnimationName()->c_str();
  112. if (options->isAutoPlay())
  113. custom->getAnimation()->play(currentname, -1, options->isLoop());
  114. else
  115. {
  116. custom->getAnimation()->play(currentname);
  117. custom->getAnimation()->gotoAndPause(0);
  118. }
  119. }
  120. else
  121. {
  122. errorFilePath = filepath;
  123. fileExist = false;
  124. }
  125. }
  126. cocos2d::Node* ArmatureNodeReader::createNodeWithFlatBuffers(const flatbuffers::Table *nodeOptions)
  127. {
  128. auto node = Armature::create();
  129. // self
  130. auto options = (flatbuffers::CSArmatureNodeOption*)nodeOptions;
  131. setPropsWithFlatBuffers(node, (Table*)options);
  132. // super node
  133. auto NodeReader = NodeReader::getInstance();
  134. NodeReader->setPropsWithFlatBuffers(node, (Table*)options->nodeOptions());
  135. return node;
  136. }
  137. std::string ArmatureNodeReader::getArmatureName(const std::string& exporJsonPath)
  138. {
  139. //FileUtils.getFileData(exporJsonPath, "r", size) // need read armature name in exportJsonPath
  140. size_t end = exporJsonPath.find_last_of(".");
  141. size_t start = exporJsonPath.find_last_of("\\") + 1;
  142. size_t start1 = exporJsonPath.find_last_of("/") + 1;
  143. if (start < start1)
  144. start = start1;
  145. if (start == -1)
  146. start = 0;
  147. return exporJsonPath.substr(start, end - start);
  148. }