CCActionManagerEx.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/CCActionManagerEx.h"
  21. #include "editor-support/cocostudio/CocoLoader.h"
  22. using namespace cocos2d;
  23. namespace cocostudio {
  24. static ActionManagerEx* sharedActionManager = nullptr;
  25. ActionManagerEx* ActionManagerEx::getInstance()
  26. {
  27. if (!sharedActionManager) {
  28. sharedActionManager = new (std::nothrow) ActionManagerEx();
  29. }
  30. return sharedActionManager;
  31. }
  32. void ActionManagerEx::destroyInstance()
  33. {
  34. if(sharedActionManager != nullptr)
  35. {
  36. sharedActionManager->releaseActions();
  37. CC_SAFE_DELETE(sharedActionManager);
  38. }
  39. }
  40. ActionManagerEx::ActionManagerEx()
  41. {
  42. }
  43. ActionManagerEx::~ActionManagerEx()
  44. {
  45. _actionDic.clear();
  46. }
  47. void ActionManagerEx::initWithDictionary(const char* jsonName,const rapidjson::Value &dic, Ref* root, int version)
  48. {
  49. std::string path = jsonName;
  50. this->_studioVersionNumber = version;
  51. ssize_t pos = path.find_last_of("/");
  52. std::string fileName = path.substr(pos+1,path.length());
  53. cocos2d::Vector<ActionObject*> actionList;
  54. int actionCount = DICTOOL->getArrayCount_json(dic, "actionlist");
  55. for (int i=0; i<actionCount; i++) {
  56. ActionObject* action = new (std::nothrow) ActionObject();
  57. action->autorelease();
  58. const rapidjson::Value &actionDic = DICTOOL->getDictionaryFromArray_json(dic, "actionlist", i);
  59. action->initWithDictionary(actionDic,root);
  60. actionList.pushBack(action);
  61. }
  62. _actionDic[fileName] = actionList;
  63. }
  64. void ActionManagerEx::initWithBinary(const char* file,
  65. cocos2d::Ref *root,
  66. CocoLoader* cocoLoader,
  67. stExpCocoNode* pCocoNode)
  68. {
  69. std::string path = file;
  70. ssize_t pos = path.find_last_of("/");
  71. std::string fileName = path.substr(pos+1,path.length());
  72. cocos2d::Vector<ActionObject*> actionList;
  73. stExpCocoNode *stChildArray = pCocoNode->GetChildArray(cocoLoader);
  74. stExpCocoNode *actionNode = nullptr;
  75. for (int i=0; i < pCocoNode->GetChildNum(); ++i) {
  76. std::string key = stChildArray[i].GetName(cocoLoader);
  77. if (key == "actionlist") {
  78. actionNode = &stChildArray[i];
  79. break;
  80. }
  81. }
  82. if (nullptr != actionNode)
  83. {
  84. int actionCount = actionNode->GetChildNum();
  85. for (int i = 0; i < actionCount; ++i) {
  86. ActionObject* action = new (std::nothrow) ActionObject();
  87. action->autorelease();
  88. action->initWithBinary(cocoLoader, &actionNode->GetChildArray(cocoLoader)[i], root);
  89. actionList.pushBack(action);
  90. }
  91. }
  92. _actionDic[fileName] = actionList;
  93. }
  94. ActionObject* ActionManagerEx::getActionByName(const char* jsonName,const char* actionName)
  95. {
  96. std::string path = jsonName;
  97. ssize_t pos = path.find_last_of("/");
  98. std::string fileName = path.substr(pos+1,path.length());
  99. auto iterator = _actionDic.find(fileName);
  100. if (iterator == _actionDic.end())
  101. {
  102. return nullptr;
  103. }
  104. auto actionList = iterator->second;
  105. for (int i = 0; i < actionList.size(); i++)
  106. {
  107. ActionObject* action = actionList.at(i);
  108. if (strcmp(actionName, action->getName()) == 0)
  109. {
  110. return action;
  111. }
  112. }
  113. return nullptr;
  114. }
  115. ActionObject* ActionManagerEx::playActionByName(const char* jsonName,const char* actionName)
  116. {
  117. ActionObject* action = getActionByName(jsonName,actionName);
  118. if (action)
  119. {
  120. action->play();
  121. }
  122. return action;
  123. }
  124. ActionObject* ActionManagerEx::playActionByName(const char* jsonName,const char* actionName, CallFunc* func)
  125. {
  126. ActionObject* action = getActionByName(jsonName,actionName);
  127. if (action)
  128. {
  129. action->play(func);
  130. }
  131. return action;
  132. }
  133. ActionObject* ActionManagerEx::stopActionByName(const char* jsonName,const char* actionName)
  134. {
  135. ActionObject* action = getActionByName(jsonName,actionName);
  136. if (action)
  137. {
  138. action->stop();
  139. }
  140. return action;
  141. }
  142. void ActionManagerEx::releaseActions()
  143. {
  144. for (auto& iter : _actionDic)
  145. {
  146. cocos2d::Vector<ActionObject*> objList = iter.second;
  147. ssize_t listCount = objList.size();
  148. for (ssize_t i = 0; i < listCount; i++) {
  149. ActionObject* action = objList.at(i);
  150. if (action != nullptr) {
  151. action->stop();
  152. }
  153. }
  154. objList.clear();
  155. }
  156. _actionDic.clear();
  157. }
  158. int ActionManagerEx::getStudioVersionNumber() const
  159. {
  160. return this->_studioVersionNumber;
  161. }
  162. }