diff --git a/cocos/2d/CCClippingNode.cpp b/cocos/2d/CCClippingNode.cpp index 0b933917af..fba3c00deb 100644 --- a/cocos/2d/CCClippingNode.cpp +++ b/cocos/2d/CCClippingNode.cpp @@ -145,7 +145,7 @@ void ClippingNode::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index c76b893709..a51d2ac752 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -1055,7 +1055,7 @@ void Node::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJS(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnEnter)) return; } #endif @@ -1072,7 +1072,7 @@ void Node::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeLua) { - sendNodeEventToLua(this, kNodeOnEnter); + ScriptEngineManager::sendNodeEventToLua(this, kNodeOnEnter); } #endif } @@ -1082,7 +1082,7 @@ void Node::onEnterTransitionDidFinish() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJS(this, kNodeOnEnterTransitionDidFinish)) + if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnEnterTransitionDidFinish)) return; } #endif @@ -1094,7 +1094,7 @@ void Node::onEnterTransitionDidFinish() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeLua) { - sendNodeEventToLua(this, kNodeOnEnterTransitionDidFinish); + ScriptEngineManager::sendNodeEventToLua(this, kNodeOnEnterTransitionDidFinish); } #endif } @@ -1104,7 +1104,7 @@ void Node::onExitTransitionDidStart() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJS(this, kNodeOnExitTransitionDidStart)) + if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnExitTransitionDidStart)) return; } #endif @@ -1115,7 +1115,7 @@ void Node::onExitTransitionDidStart() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeLua) { - sendNodeEventToLua(this, kNodeOnExitTransitionDidStart); + ScriptEngineManager::sendNodeEventToLua(this, kNodeOnExitTransitionDidStart); } #endif } @@ -1125,7 +1125,7 @@ void Node::onExit() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJS(this, kNodeOnExit)) + if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnExit)) return; } #endif @@ -1140,7 +1140,7 @@ void Node::onExit() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeLua) { - sendNodeEventToLua(this, kNodeOnExit); + ScriptEngineManager::sendNodeEventToLua(this, kNodeOnExit); } #endif } diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 763f93b641..cfac0a194f 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -73,55 +73,6 @@ enum { bool nodeComparisonLess(Node* n1, Node* n2); - -#if CC_ENABLE_SCRIPT_BINDING - -static bool sendNodeEventToJS(Node* node, int action) -{ - auto scriptEngine = ScriptEngineManager::getInstance()->getScriptEngine(); - - if (scriptEngine->isCalledFromScript()) - { - // Should only be invoked at root class Node - scriptEngine->setCalledFromScript(false); - } - else - { - BasicScriptData data(node,(void*)&action); - ScriptEvent scriptEvent(kNodeEvent,(void*)&data); - if (scriptEngine->sendEvent(&scriptEvent)) - return true; - } - - return false; -} -static bool sendNodeEventToJSExtended(Node* node, int action) -{ - auto scriptEngine = ScriptEngineManager::getInstance()->getScriptEngine(); - - if (!scriptEngine->isCalledFromScript()) - { - BasicScriptData data(node,(void*)&action); - ScriptEvent scriptEvent(kNodeEvent,(void*)&data); - if (scriptEngine->sendEvent(&scriptEvent)) - return true; - } - - return false; -} - -static void sendNodeEventToLua(Node* node, int action) -{ - auto scriptEngine = ScriptEngineManager::getInstance()->getScriptEngine(); - - BasicScriptData data(node,(void*)&action); - ScriptEvent scriptEvent(kNodeEvent,(void*)&data); - - scriptEngine->sendEvent(&scriptEvent); -} - -#endif - class EventListener; /** @brief Node is the base element of the Scene Graph. Elements of the Scene Graph must be Node objects or subclasses of it. diff --git a/cocos/2d/CCParticleSystem.cpp b/cocos/2d/CCParticleSystem.cpp index 7d538fb70b..25c1066098 100644 --- a/cocos/2d/CCParticleSystem.cpp +++ b/cocos/2d/CCParticleSystem.cpp @@ -619,7 +619,7 @@ void ParticleSystem::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif diff --git a/cocos/base/CCScriptSupport.cpp b/cocos/base/CCScriptSupport.cpp index f5400c436a..dda9ecca11 100644 --- a/cocos/base/CCScriptSupport.cpp +++ b/cocos/base/CCScriptSupport.cpp @@ -28,6 +28,7 @@ #if CC_ENABLE_SCRIPT_BINDING #include "base/CCScheduler.h" +#include "CCNode.h" bool CC_DLL cc_assert_script_compatible(const char *msg) { @@ -162,6 +163,51 @@ void ScriptEngineManager::destroyInstance() } } +bool ScriptEngineManager::sendNodeEventToJS(Node* node, int action) +{ + auto scriptEngine = getInstance()->getScriptEngine(); + + if (scriptEngine->isCalledFromScript()) + { + // Should only be invoked at root class Node + scriptEngine->setCalledFromScript(false); + } + else + { + BasicScriptData data(node,(void*)&action); + ScriptEvent scriptEvent(kNodeEvent,(void*)&data); + if (scriptEngine->sendEvent(&scriptEvent)) + return true; + } + + return false; +} + +bool ScriptEngineManager::sendNodeEventToJSExtended(Node* node, int action) +{ + auto scriptEngine = getInstance()->getScriptEngine(); + + if (!scriptEngine->isCalledFromScript()) + { + BasicScriptData data(node,(void*)&action); + ScriptEvent scriptEvent(kNodeEvent,(void*)&data); + if (scriptEngine->sendEvent(&scriptEvent)) + return true; + } + + return false; +} + +void ScriptEngineManager::sendNodeEventToLua(Node* node, int action) +{ + auto scriptEngine = getInstance()->getScriptEngine(); + + BasicScriptData data(node,(void*)&action); + ScriptEvent scriptEvent(kNodeEvent,(void*)&data); + + scriptEngine->sendEvent(&scriptEvent); +} + NS_CC_END #endif // #if CC_ENABLE_SCRIPT_BINDING diff --git a/cocos/base/CCScriptSupport.h b/cocos/base/CCScriptSupport.h index ee649a1f9a..1079dd0903 100644 --- a/cocos/base/CCScriptSupport.h +++ b/cocos/base/CCScriptSupport.h @@ -450,6 +450,7 @@ public: virtual bool parseConfig(ConfigType type, const std::string& str) = 0; }; +class Node; /** ScriptEngineManager is a singleton which holds an object instance of ScriptEngineProtocl It helps cocos2d-x and the user code to find back LuaEngine object @@ -490,6 +491,21 @@ public: * @lua NA */ static void destroyInstance(); + /** + * @js NA + * @lua NA + */ + static bool sendNodeEventToJS(Node* node, int action); + /** + * @js NA + * @lua NA + */ + static bool sendNodeEventToJSExtended(Node* node, int action); + /** + * @js NA + * @lua NA + */ + static void sendNodeEventToLua(Node* node, int action); /** * @js NA * @lua NA diff --git a/cocos/editor-support/cocostudio/CCArmature.cpp b/cocos/editor-support/cocostudio/CCArmature.cpp index 1b71db9347..d8b2537ff7 100644 --- a/cocos/editor-support/cocostudio/CCArmature.cpp +++ b/cocos/editor-support/cocostudio/CCArmature.cpp @@ -437,7 +437,7 @@ void Armature::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif diff --git a/cocos/editor-support/spine/CCSkeleton.cpp b/cocos/editor-support/spine/CCSkeleton.cpp index e2f52b945f..4cde3682c7 100644 --- a/cocos/editor-support/spine/CCSkeleton.cpp +++ b/cocos/editor-support/spine/CCSkeleton.cpp @@ -279,7 +279,7 @@ void Skeleton::onEnter() { #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif diff --git a/cocos/ui/CCProtectedNode.cpp b/cocos/ui/CCProtectedNode.cpp index ebbde2023e..4819db32c5 100644 --- a/cocos/ui/CCProtectedNode.cpp +++ b/cocos/ui/CCProtectedNode.cpp @@ -340,7 +340,7 @@ void ProtectedNode::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif diff --git a/cocos/ui/UILayout.cpp b/cocos/ui/UILayout.cpp index b4f1227b17..a42599b915 100644 --- a/cocos/ui/UILayout.cpp +++ b/cocos/ui/UILayout.cpp @@ -105,7 +105,7 @@ void Layout::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif diff --git a/cocos/ui/UIPageView.cpp b/cocos/ui/UIPageView.cpp index dd465c73c5..4a4ad15605 100644 --- a/cocos/ui/UIPageView.cpp +++ b/cocos/ui/UIPageView.cpp @@ -71,7 +71,7 @@ void PageView::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index 183df6d1b1..076e99ddce 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -98,7 +98,7 @@ void ScrollView::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif diff --git a/cocos/ui/UITextField.cpp b/cocos/ui/UITextField.cpp index c625aeb097..7159fa0f6b 100644 --- a/cocos/ui/UITextField.cpp +++ b/cocos/ui/UITextField.cpp @@ -418,7 +418,7 @@ void TextField::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif diff --git a/extensions/GUI/CCEditBox/CCEditBox.cpp b/extensions/GUI/CCEditBox/CCEditBox.cpp index 880a63be75..ceea82382d 100644 --- a/extensions/GUI/CCEditBox/CCEditBox.cpp +++ b/extensions/GUI/CCEditBox/CCEditBox.cpp @@ -332,7 +332,7 @@ void EditBox::onEnter(void) #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJSExtended(this, kNodeOnEnter)) + if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif