CCScriptSupport.cpp 6.3 KB

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