TriggerMng.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/TriggerMng.h"
  21. #include "json/prettywriter.h"
  22. #include "json/stringbuffer.h"
  23. #include "base/CCDirector.h"
  24. #include "base/CCEventDispatcher.h"
  25. #include "base/ccUtils.h"
  26. #include "base/CCEventCustom.h"
  27. using namespace cocos2d;
  28. namespace cocostudio {
  29. TriggerMng* TriggerMng::_sharedTriggerMng = nullptr;
  30. TriggerMng::TriggerMng(void)
  31. : _movementDispatches(new std::unordered_map<Armature*, ArmatureMovementDispatcher*>)
  32. {
  33. _eventDispatcher = Director::getInstance()->getEventDispatcher();
  34. _eventDispatcher->retain();
  35. }
  36. TriggerMng::~TriggerMng(void)
  37. {
  38. removeAll();
  39. _triggerObjs.clear();
  40. removeAllArmatureMovementCallBack();
  41. CC_SAFE_DELETE(_movementDispatches);
  42. CC_SAFE_RELEASE(_eventDispatcher);
  43. }
  44. const char* TriggerMng::triggerMngVersion()
  45. {
  46. return "1.0.0.0";
  47. }
  48. TriggerMng* TriggerMng::getInstance()
  49. {
  50. if (nullptr == _sharedTriggerMng)
  51. {
  52. _sharedTriggerMng = new (std::nothrow) TriggerMng();
  53. }
  54. return _sharedTriggerMng;
  55. }
  56. void TriggerMng::destroyInstance()
  57. {
  58. CC_SAFE_DELETE(_sharedTriggerMng);
  59. }
  60. void TriggerMng::parse(const rapidjson::Value &root)
  61. {
  62. CCLOG("%s", triggerMngVersion());
  63. int count = DICTOOL->getArrayCount_json(root, "Triggers");
  64. #if CC_ENABLE_SCRIPT_BINDING
  65. ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine();
  66. bool useBindings = engine != nullptr;
  67. if (useBindings)
  68. {
  69. if (count > 0)
  70. {
  71. const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(root, "Triggers");
  72. rapidjson::StringBuffer buffer;
  73. rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  74. subDict.Accept(writer);
  75. engine->parseConfig(ScriptEngineProtocol::ConfigType::COCOSTUDIO, buffer.GetString());
  76. }
  77. }
  78. else
  79. #endif // #if CC_ENABLE_SCRIPT_BINDING
  80. {
  81. for (int i = 0; i < count; ++i)
  82. {
  83. const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(root, "Triggers", i);
  84. TriggerObj *obj = TriggerObj::create();
  85. obj->serialize(subDict);
  86. _triggerObjs.insert(std::pair<unsigned int, TriggerObj*>(obj->getId(), obj));
  87. obj->retain();
  88. }
  89. }
  90. }
  91. void TriggerMng::parse(cocostudio::CocoLoader *pCocoLoader, cocostudio::stExpCocoNode *pCocoNode)
  92. {
  93. CCLOG("%s", triggerMngVersion());
  94. int count = pCocoNode[13].GetChildNum();
  95. stExpCocoNode *pTriggersArray = pCocoNode[13].GetChildArray(pCocoLoader);
  96. #if CC_ENABLE_SCRIPT_BINDING
  97. ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine();
  98. bool useBindings = engine != nullptr;
  99. if (useBindings)
  100. {
  101. if (count > 0 )
  102. {
  103. rapidjson::Document document;
  104. buildJson(document, pCocoLoader, pCocoNode);
  105. rapidjson::StringBuffer buffer;
  106. rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  107. document.Accept(writer);
  108. engine->parseConfig(ScriptEngineProtocol::ConfigType::COCOSTUDIO, buffer.GetString());
  109. }
  110. }
  111. else
  112. #endif // #if CC_ENABLE_SCRIPT_BINDING
  113. {
  114. for (int i = 0; i < count; ++i)
  115. {
  116. TriggerObj *obj = TriggerObj::create();
  117. obj->serialize(pCocoLoader, &pTriggersArray[i]);
  118. _triggerObjs.insert(std::pair<unsigned int, TriggerObj*>(obj->getId(), obj));
  119. obj->retain();
  120. }
  121. }
  122. }
  123. TriggerObj* TriggerMng::getTriggerObj(unsigned int id) const
  124. {
  125. auto iter = _triggerObjs.find(id);
  126. if (iter == _triggerObjs.end())
  127. {
  128. return nullptr;
  129. }
  130. return iter->second;
  131. }
  132. void TriggerMng::removeAll(void)
  133. {
  134. auto etIter = _triggerObjs.begin();
  135. for (;etIter != _triggerObjs.end(); ++etIter)
  136. {
  137. etIter->second->removeAll();
  138. CC_SAFE_DELETE(etIter->second);
  139. }
  140. _triggerObjs.clear();
  141. }
  142. bool TriggerMng::removeTriggerObj(TriggerObj *Obj)
  143. {
  144. if (Obj == nullptr)
  145. {
  146. return false;
  147. }
  148. return removeTriggerObj(Obj->getId());
  149. }
  150. bool TriggerMng::removeTriggerObj(unsigned int id)
  151. {
  152. TriggerObj *obj = getTriggerObj(id);
  153. if (obj == nullptr)
  154. {
  155. return false;
  156. }
  157. obj->removeAll();
  158. _triggerObjs.erase(id);
  159. return true;
  160. }
  161. bool TriggerMng::isEmpty(void) const
  162. {
  163. return _triggerObjs.empty();
  164. }
  165. void TriggerMng::buildJson(rapidjson::Document &document, cocostudio::CocoLoader *pCocoLoader, cocostudio::stExpCocoNode *pCocoNode)
  166. {
  167. int count = pCocoNode[13].GetChildNum();
  168. int length = 0;
  169. int num = 0;
  170. int size = 0;
  171. int extent = 0;
  172. int border = 0;
  173. std::string key0;
  174. stExpCocoNode *pTriggersArray = pCocoNode[13].GetChildArray(pCocoLoader);
  175. document.SetArray();
  176. rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
  177. for (int i0 = 0; i0 < count; ++i0)
  178. {
  179. rapidjson::Value vElemItem(rapidjson::kObjectType);
  180. border = pTriggersArray[i0].GetChildNum();
  181. stExpCocoNode *pTriggerArray = pTriggersArray[i0].GetChildArray(pCocoLoader);
  182. for (int i1 = 0; i1 < border; ++i1)
  183. {
  184. std::string key1 = pTriggerArray[i1].GetName(pCocoLoader);
  185. const char *str1 = pTriggerArray[i1].GetValue(pCocoLoader);
  186. if (key1.compare("actions") == 0)
  187. {
  188. rapidjson::Value actionsItem(rapidjson::kArrayType);
  189. length = pTriggerArray[i1].GetChildNum();
  190. stExpCocoNode *pActionsArray = pTriggerArray[i1].GetChildArray(pCocoLoader);
  191. for (int i2 = 0; i2 < length; ++i2)
  192. {
  193. rapidjson::Value action(rapidjson::kObjectType);
  194. num = pActionsArray[i2].GetChildNum();
  195. stExpCocoNode *pActionArray = pActionsArray[i2].GetChildArray(pCocoLoader);
  196. for (int i3 = 0; i3 < num; ++i3)
  197. {
  198. std::string key2 = pActionArray[i3].GetName(pCocoLoader);
  199. const char *str2 = pActionArray[i3].GetValue(pCocoLoader);
  200. if (key2.compare("classname") == 0)
  201. {
  202. if (str2 != nullptr)
  203. {
  204. action.AddMember("classname", rapidjson::Value(str2,allocator), allocator);
  205. }
  206. }
  207. else if (key2.compare("dataitems") == 0)
  208. {
  209. rapidjson::Value dataitems(rapidjson::kArrayType);
  210. size = pActionArray[i3].GetChildNum();
  211. stExpCocoNode *pDataItemsArray = pActionArray[i3].GetChildArray(pCocoLoader);
  212. for (int i4 = 0; i4 < size; ++i4)
  213. {
  214. rapidjson::Value dataitem(rapidjson::kObjectType);
  215. extent = pDataItemsArray[i4].GetChildNum();
  216. stExpCocoNode *pDataItemArray = pDataItemsArray[i4].GetChildArray(pCocoLoader);
  217. for (int i5 = 0; i5 < extent; ++i5)
  218. {
  219. std::string key3 = pDataItemArray[i5].GetName(pCocoLoader);
  220. const char *str3 = pDataItemArray[i5].GetValue(pCocoLoader);
  221. if (key3.compare("key") == 0)
  222. {
  223. if (str3 != nullptr)
  224. {
  225. dataitem.AddMember("key", rapidjson::Value(str3,allocator), allocator);
  226. }
  227. }
  228. else
  229. {
  230. rapidjson::Type type = pDataItemArray[i5].GetType(pCocoLoader);
  231. if (type == rapidjson::kStringType)
  232. {
  233. dataitem.AddMember("value", rapidjson::Value(str3,allocator), allocator);
  234. }
  235. else
  236. {
  237. int nV = atoi(str3);
  238. float fV = utils::atof(str3);
  239. if (fabs(nV - fV) < 0.0000001)
  240. {
  241. dataitem.AddMember("value", nV, allocator);
  242. }
  243. else
  244. {
  245. dataitem.AddMember("value", fV, allocator);
  246. }
  247. }
  248. }
  249. }
  250. dataitems.PushBack(dataitem, allocator);
  251. }
  252. action.AddMember("dataitems", dataitems, allocator);
  253. }
  254. }
  255. actionsItem.PushBack(action, allocator);
  256. }
  257. vElemItem.AddMember("actions", actionsItem, allocator);
  258. }
  259. else if (key1.compare("conditions") == 0)
  260. {
  261. rapidjson::Value condsItem(rapidjson::kArrayType);
  262. length = pTriggerArray[i1].GetChildNum();
  263. stExpCocoNode *pConditionsArray = pTriggerArray[i1].GetChildArray(pCocoLoader);
  264. for (int i6 = 0; i6 < length; ++i6)
  265. {
  266. rapidjson::Value cond(rapidjson::kObjectType);
  267. num = pConditionsArray[i6].GetChildNum();
  268. stExpCocoNode *pConditionArray = pConditionsArray[i6].GetChildArray(pCocoLoader);
  269. for (int i7 = 0; i7 < num; ++i7)
  270. {
  271. std::string key4 = pConditionArray[i7].GetName(pCocoLoader);
  272. const char *str4 = pConditionArray[i7].GetValue(pCocoLoader);
  273. if (key4.compare("classname") == 0)
  274. {
  275. if (str4 != nullptr)
  276. {
  277. cond.AddMember("classname", rapidjson::Value(str4,allocator), allocator);
  278. }
  279. }
  280. else if (key4.compare("dataitems") == 0)
  281. {
  282. rapidjson::Value dataitems(rapidjson::kArrayType);
  283. size = pConditionArray[i7].GetChildNum();
  284. stExpCocoNode *pDataItemsArray = pConditionArray[i7].GetChildArray(pCocoLoader);
  285. for (int i8 = 0; i8 < size; ++i8)
  286. {
  287. rapidjson::Value dataitem(rapidjson::kObjectType);
  288. extent = pDataItemsArray[i8].GetChildNum();
  289. stExpCocoNode *pDataItemArray = pDataItemsArray[i8].GetChildArray(pCocoLoader);
  290. for (int i9 = 0; i9 < extent; ++i9)
  291. {
  292. std::string key5 = pDataItemArray[i9].GetName(pCocoLoader);
  293. const char *str5 = pDataItemArray[i9].GetValue(pCocoLoader);
  294. if (key5.compare("key") == 0)
  295. {
  296. if (str5 != nullptr)
  297. {
  298. dataitem.AddMember("key", rapidjson::Value(str5,allocator), allocator);
  299. }
  300. }
  301. else
  302. {
  303. rapidjson::Type type = pDataItemArray[i9].GetType(pCocoLoader);
  304. if (type == rapidjson::kStringType)
  305. {
  306. dataitem.AddMember("value", rapidjson::Value(str5,allocator), allocator);
  307. }
  308. else
  309. {
  310. int nV = atoi(str5);
  311. float fV = utils::atof(str5);
  312. if (fabs(nV - fV) < 0.0000001)
  313. {
  314. dataitem.AddMember("value", nV, allocator);
  315. }
  316. else
  317. {
  318. dataitem.AddMember("value", fV, allocator);
  319. }
  320. }
  321. }
  322. }
  323. dataitems.PushBack(dataitem, allocator);
  324. }
  325. cond.AddMember("dataitems", dataitems, allocator);
  326. }
  327. }
  328. condsItem.PushBack(cond, allocator);
  329. }
  330. vElemItem.AddMember("conditions", condsItem, allocator);
  331. }
  332. else if (key1.compare("events") == 0)
  333. {
  334. rapidjson::Value eventsItem(rapidjson::kArrayType);
  335. length = pTriggerArray[i1].GetChildNum();
  336. stExpCocoNode *pEventsArray = pTriggerArray[i1].GetChildArray(pCocoLoader);
  337. for (int i10 = 0; i10 < length; ++i10)
  338. {
  339. rapidjson::Value event(rapidjson::kObjectType);
  340. stExpCocoNode *pEventArray = pEventsArray->GetChildArray(pCocoLoader);
  341. std::string key6 = pEventArray[0].GetName(pCocoLoader);
  342. const char *str6 = pEventArray[0].GetValue(pCocoLoader);
  343. if (key6.compare("id") == 0 && str6 != nullptr)
  344. {
  345. event.AddMember("id", atoi(str6), allocator);
  346. eventsItem.PushBack(event, allocator);
  347. }
  348. }
  349. vElemItem.AddMember("events", eventsItem, allocator);
  350. }
  351. else if (key1.compare("id") == 0)
  352. {
  353. if (str1 != nullptr)
  354. {
  355. vElemItem.AddMember("id", atoi(str1), allocator);
  356. }
  357. }
  358. }
  359. document.PushBack(vElemItem, allocator);
  360. }
  361. }
  362. void TriggerMng::addArmatureMovementCallBack(Armature *pAr, Ref *pTarget, SEL_MovementEventCallFunc mecf)
  363. {
  364. if (pAr == nullptr || _movementDispatches == nullptr || pTarget == nullptr || mecf == nullptr)
  365. {
  366. return;
  367. }
  368. auto iter = _movementDispatches->find(pAr);
  369. ArmatureMovementDispatcher *amd = nullptr;
  370. if (iter == _movementDispatches->end())
  371. {
  372. amd = new (std::nothrow) ArmatureMovementDispatcher();
  373. pAr->getAnimation()->setMovementEventCallFunc(CC_CALLBACK_0(ArmatureMovementDispatcher::animationEvent, amd, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
  374. amd->addAnimationEventCallBack(pTarget, mecf);
  375. _movementDispatches->emplace(pAr, amd);
  376. }
  377. else
  378. {
  379. amd = iter->second;
  380. amd->addAnimationEventCallBack(pTarget, mecf);
  381. }
  382. }
  383. void TriggerMng::removeArmatureMovementCallBack(Armature *pAr, Ref *pTarget, SEL_MovementEventCallFunc mecf)
  384. {
  385. if (pAr == nullptr || _movementDispatches == nullptr || pTarget == nullptr || mecf == nullptr)
  386. {
  387. return;
  388. }
  389. auto iter =_movementDispatches->find(pAr);
  390. ArmatureMovementDispatcher *amd = nullptr;
  391. if (iter == _movementDispatches->end())
  392. {
  393. return;
  394. }
  395. else
  396. {
  397. amd = iter->second;
  398. amd->removeAnnimationEventCallBack(pTarget, mecf);
  399. }
  400. }
  401. void TriggerMng::removeArmatureAllMovementCallBack(Armature *pAr)
  402. {
  403. if (pAr == nullptr)
  404. {
  405. return;
  406. }
  407. auto iter = _movementDispatches->find(pAr);
  408. if (iter == _movementDispatches->end())
  409. {
  410. return;
  411. }
  412. else
  413. {
  414. CC_SAFE_DELETE(iter->second);
  415. _movementDispatches->erase(iter);
  416. }
  417. }
  418. void TriggerMng::removeAllArmatureMovementCallBack()
  419. {
  420. auto iter = _movementDispatches->begin();
  421. while (iter != _movementDispatches->end())
  422. {
  423. removeArmatureAllMovementCallBack(iter->first);
  424. }
  425. _movementDispatches->clear();
  426. }
  427. void TriggerMng::dispatchEvent(cocos2d::EventCustom* tEvent)
  428. {
  429. _eventDispatcher->dispatchEvent(tEvent);
  430. }
  431. void TriggerMng::removeEventListener(cocos2d::EventListener* listener)
  432. {
  433. _eventDispatcher->removeEventListener(listener);
  434. }
  435. void TriggerMng::addEventListenerWithFixedPriority(cocos2d::EventListener* listener, int fixedPriority)
  436. {
  437. _eventDispatcher->addEventListenerWithFixedPriority(listener, fixedPriority);
  438. }
  439. ArmatureMovementDispatcher::ArmatureMovementDispatcher(void)
  440. : _mapEventAnimation(nullptr)
  441. {
  442. _mapEventAnimation = new (std::nothrow) std::unordered_map<Ref*, SEL_MovementEventCallFunc> ;
  443. }
  444. ArmatureMovementDispatcher::~ArmatureMovementDispatcher(void)
  445. {
  446. _mapEventAnimation->clear();
  447. CC_SAFE_DELETE(_mapEventAnimation);
  448. }
  449. void ArmatureMovementDispatcher::animationEvent(Armature *armature, MovementEventType movementType, const std::string& movementID)
  450. {
  451. for (auto iter = _mapEventAnimation->begin(); iter != _mapEventAnimation->end(); ++iter)
  452. {
  453. (iter->first->*iter->second)(armature, movementType, movementID);
  454. }
  455. }
  456. void ArmatureMovementDispatcher::addAnimationEventCallBack(Ref *pTarget, SEL_MovementEventCallFunc mecf)
  457. {
  458. _mapEventAnimation->emplace(pTarget, mecf);
  459. }
  460. void ArmatureMovementDispatcher::removeAnnimationEventCallBack(Ref *pTarget, SEL_MovementEventCallFunc /*mecf*/)
  461. {
  462. _mapEventAnimation->erase(pTarget);
  463. }
  464. }