TriggerObj.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/TriggerObj.h"
  21. #include "base/CCEventListenerCustom.h"
  22. using namespace cocos2d;
  23. namespace cocostudio {
  24. BaseTriggerCondition::BaseTriggerCondition(void)
  25. {
  26. }
  27. BaseTriggerCondition::~BaseTriggerCondition(void)
  28. {
  29. }
  30. bool BaseTriggerCondition::init()
  31. {
  32. return true;
  33. }
  34. bool BaseTriggerCondition::detect()
  35. {
  36. return true;
  37. }
  38. void BaseTriggerCondition::serialize(const rapidjson::Value& /*val*/)
  39. {
  40. }
  41. void BaseTriggerCondition::serialize(cocostudio::CocoLoader* /*cocoLoader*/, cocostudio::stExpCocoNode* /*cocoNode*/)
  42. {
  43. }
  44. void BaseTriggerCondition::removeAll()
  45. {
  46. }
  47. BaseTriggerAction::BaseTriggerAction(void)
  48. {
  49. }
  50. BaseTriggerAction::~BaseTriggerAction(void)
  51. {
  52. }
  53. bool BaseTriggerAction::init()
  54. {
  55. return true;
  56. }
  57. void BaseTriggerAction::done()
  58. {
  59. }
  60. void BaseTriggerAction::serialize(const rapidjson::Value& /*val*/)
  61. {
  62. }
  63. void BaseTriggerAction::serialize(cocostudio::CocoLoader* /*cocoLoader*/, cocostudio::stExpCocoNode* /*cocoNode*/)
  64. {
  65. }
  66. void BaseTriggerAction::removeAll()
  67. {
  68. }
  69. TriggerObj::TriggerObj(void)
  70. :_id(UINT_MAX)
  71. ,_enabled(true)
  72. {
  73. }
  74. TriggerObj::~TriggerObj(void)
  75. {
  76. }
  77. bool TriggerObj::init()
  78. {
  79. return true;
  80. }
  81. TriggerObj* TriggerObj::create()
  82. {
  83. TriggerObj * pRet = new (std::nothrow) TriggerObj();
  84. if (pRet && pRet->init())
  85. {
  86. pRet->autorelease();
  87. }
  88. else
  89. {
  90. CC_SAFE_DELETE(pRet);
  91. }
  92. return pRet;
  93. }
  94. bool TriggerObj::detect()
  95. {
  96. if (!_enabled || _cons.empty())
  97. {
  98. return true;
  99. }
  100. bool ret = false;
  101. for (const auto& con : _cons)
  102. {
  103. ret = ret || con->detect();
  104. }
  105. return ret;
  106. }
  107. void TriggerObj::done()
  108. {
  109. if (!_enabled || _acts.empty())
  110. {
  111. return;
  112. }
  113. for (const auto& act : _acts)
  114. {
  115. act->done();
  116. }
  117. }
  118. void TriggerObj::removeAll()
  119. {
  120. for (const auto& con : _cons)
  121. {
  122. con->removeAll();
  123. }
  124. for (const auto& act : _acts)
  125. {
  126. act->removeAll();
  127. }
  128. for (const auto& lis : _listeners)
  129. {
  130. TriggerMng::getInstance()->removeEventListener(lis);
  131. }
  132. _cons.clear();
  133. _acts.clear();
  134. _listeners.clear();
  135. }
  136. void TriggerObj::serialize(const rapidjson::Value &val)
  137. {
  138. _id = (unsigned int)(DICTOOL->getIntValue_json(val, "id"));
  139. int count = DICTOOL->getArrayCount_json(val, "conditions");
  140. for (int i = 0; i < count; ++i)
  141. {
  142. const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(val, "conditions", i);
  143. const char *classname = DICTOOL->getStringValue_json(subDict, "classname");
  144. if (classname == nullptr)
  145. {
  146. continue;
  147. }
  148. BaseTriggerCondition *con = dynamic_cast<BaseTriggerCondition*>(ObjectFactory::getInstance()->createObject(classname));
  149. if(con == nullptr)
  150. {
  151. CCLOG("class %s can not be implemented!", classname);
  152. CCASSERT(con != nullptr, "con can't be nullptr!");
  153. }
  154. CCASSERT(con != nullptr, "con can't be nullptr!");
  155. con->serialize(subDict);
  156. con->init();
  157. _cons.pushBack(con);
  158. }
  159. count = DICTOOL->getArrayCount_json(val, "actions");
  160. for (int i = 0; i < count; ++i)
  161. {
  162. const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(val, "actions", i);
  163. const char *classname = DICTOOL->getStringValue_json(subDict, "classname");
  164. if (classname == nullptr)
  165. {
  166. continue;
  167. }
  168. BaseTriggerAction *act = dynamic_cast<BaseTriggerAction*>(ObjectFactory::getInstance()->createObject(classname));
  169. if(act == nullptr)
  170. {
  171. CCLOG("class %s can not be implemented!", classname);
  172. CCASSERT(act != nullptr, "act can't be nullptr!");
  173. }
  174. act->serialize(subDict);
  175. act->init();
  176. _acts.pushBack(act);
  177. }
  178. int length = DICTOOL->getArrayCount_json(val, "events");
  179. for (int i = 0; i < length; ++i)
  180. {
  181. const rapidjson::Value &sub = DICTOOL->getSubDictionary_json(val, "events", i);
  182. int event = DICTOOL->getIntValue_json(sub, "id");
  183. if (event < 0)
  184. {
  185. continue;
  186. }
  187. char buf[10];
  188. sprintf(buf, "%d", event);
  189. std::string custom_event_name(buf);
  190. EventListenerCustom* listener = EventListenerCustom::create(custom_event_name, [=](EventCustom* /*evt*/){
  191. if (detect())
  192. {
  193. done();
  194. }
  195. });
  196. _listeners.pushBack(listener);
  197. TriggerMng::getInstance()->addEventListenerWithFixedPriority(listener, 1);
  198. }
  199. }
  200. void TriggerObj::serialize(cocostudio::CocoLoader *pCocoLoader, cocostudio::stExpCocoNode *pCocoNode)
  201. {
  202. int length = pCocoNode->GetChildNum();
  203. int count = 0;
  204. int num = 0;
  205. stExpCocoNode *pTriggerObjArray = pCocoNode->GetChildArray(pCocoLoader);
  206. for (int i0 = 0; i0 < length; ++i0)
  207. {
  208. std::string key = pTriggerObjArray[i0].GetName(pCocoLoader);
  209. const char* str0 = pTriggerObjArray[i0].GetValue(pCocoLoader);
  210. if (key.compare("id") == 0)
  211. {
  212. if (str0 != nullptr)
  213. {
  214. _id = atoi(str0);
  215. }
  216. }
  217. else if (key.compare("conditions") == 0)
  218. {
  219. count = pTriggerObjArray[i0].GetChildNum();
  220. stExpCocoNode *pConditionsArray = pTriggerObjArray[i0].GetChildArray(pCocoLoader);
  221. for (int i1 = 0; i1 < count; ++i1)
  222. {
  223. num = pConditionsArray[i1].GetChildNum();
  224. stExpCocoNode *pConditionArray = pConditionsArray[i1].GetChildArray(pCocoLoader);
  225. const char *classname = pConditionArray[0].GetValue(pCocoLoader);
  226. if (classname == nullptr)
  227. {
  228. continue;
  229. }
  230. BaseTriggerCondition *con = dynamic_cast<BaseTriggerCondition*>(ObjectFactory::getInstance()->createObject(classname));
  231. CCAssert(con != nullptr, "class named classname can not implement!");
  232. con->serialize(pCocoLoader, &pConditionArray[1]);
  233. con->init();
  234. _cons.pushBack(con);
  235. }
  236. }
  237. else if (key.compare("actions") == 0)
  238. {
  239. count = pTriggerObjArray[i0].GetChildNum();
  240. stExpCocoNode *pActionsArray = pTriggerObjArray[i0].GetChildArray(pCocoLoader);
  241. for (int i2 = 0; i2 < count; ++i2)
  242. {
  243. num = pActionsArray[i2].GetChildNum();
  244. stExpCocoNode *pActionArray = pActionsArray[i2].GetChildArray(pCocoLoader);
  245. const char *classname = pActionArray[0].GetValue(pCocoLoader);
  246. if (classname == nullptr)
  247. {
  248. continue;
  249. }
  250. BaseTriggerAction *act = dynamic_cast<BaseTriggerAction*>(ObjectFactory::getInstance()->createObject(classname));
  251. CCAssert(act != nullptr, "class named classname can not implement!");
  252. act->serialize(pCocoLoader, &pActionArray[1]);
  253. act->init();
  254. _acts.pushBack(act);
  255. }
  256. }
  257. else if (key.compare("events") == 0)
  258. {
  259. count = pTriggerObjArray[i0].GetChildNum();
  260. stExpCocoNode *pEventsArray = pTriggerObjArray[i0].GetChildArray(pCocoLoader);
  261. for (int i3 = 0; i3 < count; ++i3)
  262. {
  263. num = pEventsArray[i3].GetChildNum();
  264. stExpCocoNode *pEventArray = pEventsArray[i3].GetChildArray(pCocoLoader);
  265. const char *str1 = pEventArray[0].GetValue(pCocoLoader);
  266. if (str1 == nullptr)
  267. {
  268. continue;
  269. }
  270. int event = atoi(str1);
  271. if (event < 0)
  272. {
  273. continue;
  274. }
  275. char buf[10];
  276. sprintf(buf, "%d", event);
  277. std::string custom_event_name(buf);
  278. EventListenerCustom* listener = EventListenerCustom::create(custom_event_name, [=](EventCustom* /*evt*/){
  279. if (detect())
  280. {
  281. done();
  282. }
  283. });
  284. _listeners.pushBack(listener);
  285. TriggerMng::getInstance()->addEventListenerWithFixedPriority(listener, 1);
  286. }
  287. }
  288. }
  289. }
  290. unsigned int TriggerObj::getId()
  291. {
  292. return _id;
  293. }
  294. void TriggerObj::setEnabled(bool enabled)
  295. {
  296. _enabled = enabled;
  297. }
  298. }