CCActionObject.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/CCActionObject.h"
  21. #include "editor-support/cocostudio/CocoLoader.h"
  22. #include "base/CCDirector.h"
  23. #include "base/CCScheduler.h"
  24. #include "2d/CCActionInstant.h"
  25. #include "base/ccUtils.h"
  26. using namespace cocos2d;
  27. namespace cocostudio {
  28. ActionObject::ActionObject()
  29. : _name("")
  30. , _loop(false)
  31. , _bPause(false)
  32. , _bPlaying(false)
  33. , _fUnitTime(0.1f)
  34. , _currentTime(0.0f)
  35. , _pScheduler(nullptr)
  36. , _CallBack(nullptr)
  37. , _fTotalTime(0.0f)
  38. {
  39. _pScheduler = Director::getInstance()->getScheduler();
  40. CC_SAFE_RETAIN(_pScheduler);
  41. }
  42. ActionObject::~ActionObject()
  43. {
  44. _loop = false;
  45. _pScheduler->unscheduleAllForTarget(this);
  46. _actionNodeList.clear();
  47. CC_SAFE_RELEASE(_pScheduler);
  48. CC_SAFE_RELEASE(_CallBack);
  49. }
  50. void ActionObject::setName(const char* name)
  51. {
  52. _name.assign(name);
  53. }
  54. const char* ActionObject::getName()
  55. {
  56. return _name.c_str();
  57. }
  58. void ActionObject::setLoop(bool bLoop)
  59. {
  60. _loop = bLoop;
  61. }
  62. bool ActionObject::getLoop()
  63. {
  64. return _loop;
  65. }
  66. void ActionObject::setUnitTime(float fTime)
  67. {
  68. _fUnitTime = fTime;
  69. for(const auto &e : _actionNodeList)
  70. {
  71. e->setUnitTime(_fUnitTime);
  72. }
  73. }
  74. float ActionObject::getUnitTime()
  75. {
  76. return _fUnitTime;
  77. }
  78. float ActionObject::getCurrentTime()
  79. {
  80. return _currentTime;
  81. }
  82. void ActionObject::setCurrentTime(float fTime)
  83. {
  84. _currentTime = fTime;
  85. }
  86. float ActionObject::getTotalTime()
  87. {
  88. return _fTotalTime;
  89. }
  90. bool ActionObject::isPlaying()
  91. {
  92. return _bPlaying;
  93. }
  94. void ActionObject::initWithDictionary(const rapidjson::Value& dic, Ref* root)
  95. {
  96. setName(DICTOOL->getStringValue_json(dic, "name"));
  97. setLoop(DICTOOL->getBooleanValue_json(dic, "loop"));
  98. setUnitTime(DICTOOL->getFloatValue_json(dic, "unittime"));
  99. int actionNodeCount = DICTOOL->getArrayCount_json(dic, "actionnodelist");
  100. int maxLength = 0;
  101. for (int i=0; i<actionNodeCount; i++) {
  102. ActionNode* actionNode = new (std::nothrow) ActionNode();
  103. actionNode->autorelease();
  104. const rapidjson::Value& actionNodeDic = DICTOOL->getDictionaryFromArray_json(dic, "actionnodelist", i);
  105. actionNode->initWithDictionary(actionNodeDic,root);
  106. actionNode->setUnitTime(getUnitTime());
  107. _actionNodeList.pushBack(actionNode);
  108. int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex();
  109. if(length > maxLength)
  110. maxLength = length;
  111. }
  112. _fTotalTime = maxLength*_fUnitTime;
  113. }
  114. void ActionObject::initWithBinary(CocoLoader *cocoLoader,
  115. stExpCocoNode *cocoNode,
  116. cocos2d::Ref *root)
  117. {
  118. stExpCocoNode *stChildNode = cocoNode->GetChildArray(cocoLoader);
  119. stExpCocoNode *actionNodeList = nullptr;
  120. int count = cocoNode->GetChildNum();
  121. for (int i = 0; i < count; ++i) {
  122. std::string key = stChildNode[i].GetName(cocoLoader);
  123. std::string value = stChildNode[i].GetValue(cocoLoader);
  124. if (key == "name") {
  125. setName(value.c_str());
  126. }else if (key == "loop"){
  127. setLoop(valueToBool(value));
  128. }else if(key == "unittime"){
  129. setUnitTime(valueToFloat(value));
  130. }else if (key == "actionnodelist"){
  131. actionNodeList = &stChildNode[i];
  132. }
  133. }
  134. if(nullptr != actionNodeList)
  135. {
  136. int actionNodeCount = actionNodeList->GetChildNum();
  137. stExpCocoNode *actionNodeArray = actionNodeList->GetChildArray(cocoLoader);
  138. int maxLength = 0;
  139. for (int i=0; i<actionNodeCount; i++) {
  140. ActionNode* actionNode = new (std::nothrow) ActionNode();
  141. actionNode->autorelease();
  142. actionNode->initWithBinary(cocoLoader, &actionNodeArray[i] , root);
  143. actionNode->setUnitTime(getUnitTime());
  144. _actionNodeList.pushBack(actionNode);
  145. int length = actionNode->getLastFrameIndex() - actionNode->getFirstFrameIndex();
  146. if(length > maxLength)
  147. maxLength = length;
  148. }
  149. _fTotalTime = maxLength* _fUnitTime;
  150. }
  151. }
  152. int ActionObject::valueToInt(const std::string& value)
  153. {
  154. return atoi(value.c_str());
  155. }
  156. bool ActionObject::valueToBool(const std::string& value)
  157. {
  158. int intValue = valueToInt(value);
  159. if (1 == intValue) {
  160. return true;
  161. }else{
  162. return false;
  163. }
  164. }
  165. float ActionObject::valueToFloat(const std::string& value)
  166. {
  167. return utils::atof(value.c_str());
  168. }
  169. void ActionObject::addActionNode(ActionNode* node)
  170. {
  171. if (node == nullptr)
  172. {
  173. return;
  174. }
  175. _actionNodeList.pushBack(node);
  176. node->setUnitTime(_fUnitTime);
  177. }
  178. void ActionObject::removeActionNode(ActionNode* node)
  179. {
  180. if (node == nullptr)
  181. {
  182. return;
  183. }
  184. _actionNodeList.eraseObject(node);
  185. }
  186. void ActionObject::play()
  187. {
  188. stop();
  189. this->updateToFrameByTime(0.0f);
  190. for(const auto &e : _actionNodeList)
  191. {
  192. e->playAction();
  193. }
  194. if (_loop)
  195. {
  196. _pScheduler->schedule(CC_SCHEDULE_SELECTOR(ActionObject::simulationActionUpdate), this, 0.0f , CC_REPEAT_FOREVER, 0.0f, false);
  197. }
  198. else
  199. {
  200. _pScheduler->schedule(CC_SCHEDULE_SELECTOR(ActionObject::simulationActionUpdate), this, 0.0f, false);
  201. }
  202. }
  203. void ActionObject::play(CallFunc* func)
  204. {
  205. this->play();
  206. this->_CallBack = func;
  207. CC_SAFE_RETAIN(_CallBack);
  208. }
  209. void ActionObject::pause()
  210. {
  211. _bPause = true;
  212. _bPlaying = false;
  213. }
  214. void ActionObject::stop()
  215. {
  216. for(const auto &e : _actionNodeList)
  217. {
  218. e->stopAction();
  219. }
  220. _bPlaying = false;
  221. _pScheduler->unschedule(CC_SCHEDULE_SELECTOR(ActionObject::simulationActionUpdate), this);
  222. _bPause = false;
  223. }
  224. void ActionObject::updateToFrameByTime(float fTime)
  225. {
  226. _currentTime = fTime;
  227. for(const auto &e : _actionNodeList)
  228. {
  229. e->updateActionToTimeLine(fTime);
  230. }
  231. }
  232. void ActionObject::simulationActionUpdate(float /*dt*/)
  233. {
  234. bool isEnd = true;
  235. for(const auto &e : _actionNodeList)
  236. {
  237. if (!e->isActionDoneOnce())
  238. {
  239. isEnd = false;
  240. break;
  241. }
  242. }
  243. if (isEnd)
  244. {
  245. if (_CallBack != nullptr)
  246. {
  247. _CallBack->execute();
  248. }
  249. if (_loop)
  250. {
  251. this->play();
  252. }
  253. else
  254. {
  255. _bPlaying = false;
  256. _pScheduler->unschedule(CC_SCHEDULE_SELECTOR(ActionObject::simulationActionUpdate), this);
  257. }
  258. }
  259. }
  260. }