CCScriptSupport.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "base/CCScriptSupport.h"
  23. #if CC_ENABLE_SCRIPT_BINDING
  24. #include "base/CCScheduler.h"
  25. #include "2d/CCNode.h"
  26. bool CC_DLL cc_assert_script_compatible(const char *msg)
  27. {
  28. cocos2d::ScriptEngineProtocol* engine = cocos2d::ScriptEngineManager::getInstance()->getScriptEngine();
  29. if (engine && engine->handleAssert(msg))
  30. {
  31. return true;
  32. }
  33. return false;
  34. }
  35. NS_CC_BEGIN
  36. //
  37. // // ScriptHandlerEntry
  38. ScriptHandlerEntry* ScriptHandlerEntry::create(int handler)
  39. {
  40. ScriptHandlerEntry* entry = new (std::nothrow) ScriptHandlerEntry(handler);
  41. entry->autorelease();
  42. return entry;
  43. }
  44. ScriptHandlerEntry::~ScriptHandlerEntry(void)
  45. {
  46. if (_handler != 0 )
  47. {
  48. ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_handler);
  49. LUALOG("[LUA] Remove event handler: %d", _handler);
  50. _handler = 0;
  51. }
  52. }
  53. //
  54. // // SchedulerScriptHandlerEntry
  55. SchedulerScriptHandlerEntry* SchedulerScriptHandlerEntry::create(int handler, float interval, bool paused)
  56. {
  57. SchedulerScriptHandlerEntry* entry = new (std::nothrow) SchedulerScriptHandlerEntry(handler);
  58. entry->init(interval, paused);
  59. entry->autorelease();
  60. return entry;
  61. }
  62. bool SchedulerScriptHandlerEntry::init(float interval, bool paused)
  63. {
  64. _timer = new (std::nothrow) TimerScriptHandler();
  65. _timer->initWithScriptHandler(_handler, interval);
  66. _paused = paused;
  67. LUALOG("[LUA] ADD script schedule: %d, entryID: %d", _handler, _entryId);
  68. return true;
  69. }
  70. SchedulerScriptHandlerEntry::~SchedulerScriptHandlerEntry(void)
  71. {
  72. _timer->release();
  73. LUALOG("[LUA] DEL script schedule %d, entryID: %d", _handler, _entryId);
  74. }
  75. //
  76. // // TouchScriptHandlerEntry
  77. TouchScriptHandlerEntry* TouchScriptHandlerEntry::create(int handler,
  78. bool isMultiTouches,
  79. int priority,
  80. bool swallowsTouches)
  81. {
  82. TouchScriptHandlerEntry* entry = new (std::nothrow) TouchScriptHandlerEntry(handler);
  83. entry->init(isMultiTouches, priority, swallowsTouches);
  84. entry->autorelease();
  85. return entry;
  86. }
  87. TouchScriptHandlerEntry::~TouchScriptHandlerEntry(void)
  88. {
  89. }
  90. bool TouchScriptHandlerEntry::init(bool isMultiTouches, int priority, bool swallowsTouches)
  91. {
  92. _isMultiTouches = isMultiTouches;
  93. _priority = priority;
  94. _swallowsTouches = swallowsTouches;
  95. return true;
  96. }
  97. //
  98. // // ScriptEngineManager
  99. static ScriptEngineManager* s_pSharedScriptEngineManager = nullptr;
  100. ScriptEngineManager::~ScriptEngineManager(void)
  101. {
  102. removeScriptEngine();
  103. }
  104. void ScriptEngineManager::setScriptEngine(ScriptEngineProtocol *scriptEngine)
  105. {
  106. if (_scriptEngine != scriptEngine)
  107. {
  108. removeScriptEngine();
  109. _scriptEngine = scriptEngine;
  110. }
  111. }
  112. void ScriptEngineManager::removeScriptEngine(void)
  113. {
  114. if (_scriptEngine)
  115. {
  116. delete _scriptEngine;
  117. _scriptEngine = nullptr;
  118. }
  119. }
  120. ScriptEngineManager* ScriptEngineManager::getInstance()
  121. {
  122. if (!s_pSharedScriptEngineManager)
  123. {
  124. s_pSharedScriptEngineManager = new (std::nothrow) ScriptEngineManager();
  125. }
  126. return s_pSharedScriptEngineManager;
  127. }
  128. void ScriptEngineManager::destroyInstance()
  129. {
  130. if (s_pSharedScriptEngineManager)
  131. {
  132. delete s_pSharedScriptEngineManager;
  133. s_pSharedScriptEngineManager = nullptr;
  134. }
  135. }
  136. bool ScriptEngineManager::sendActionEventToJS(Action* actionObject, int eventType, void* param)
  137. {
  138. auto scriptEngine = getInstance()->getScriptEngine();
  139. ActionObjectScriptData data(actionObject,(int*)&eventType, param);
  140. ScriptEvent scriptEvent(kScriptActionEvent,(void*)&data);
  141. if (scriptEngine->sendEvent(&scriptEvent))
  142. return true;
  143. return false;
  144. }
  145. bool ScriptEngineManager::sendNodeEventToJS(Node* node, int action)
  146. {
  147. auto scriptEngine = getInstance()->getScriptEngine();
  148. if (scriptEngine->isCalledFromScript())
  149. {
  150. // Should only be invoked at root class Node
  151. scriptEngine->setCalledFromScript(false);
  152. }
  153. else
  154. {
  155. BasicScriptData data(node,(void*)&action);
  156. ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
  157. if (scriptEngine->sendEvent(&scriptEvent))
  158. return true;
  159. }
  160. return false;
  161. }
  162. bool ScriptEngineManager::sendNodeEventToJSExtended(Node* node, int action)
  163. {
  164. auto scriptEngine = getInstance()->getScriptEngine();
  165. if (!scriptEngine->isCalledFromScript())
  166. {
  167. BasicScriptData data(node,(void*)&action);
  168. ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
  169. if (scriptEngine->sendEvent(&scriptEvent))
  170. return true;
  171. }
  172. return false;
  173. }
  174. void ScriptEngineManager::sendNodeEventToLua(Node* node, int action)
  175. {
  176. auto scriptEngine = getInstance()->getScriptEngine();
  177. BasicScriptData data(node,(void*)&action);
  178. ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
  179. scriptEngine->sendEvent(&scriptEvent);
  180. }
  181. NS_CC_END
  182. #endif // #if CC_ENABLE_SCRIPT_BINDING