From d524ba1013157a114a4c8d5992dc8378609b6f7d Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 13 Jun 2014 17:59:23 +0800 Subject: [PATCH 1/4] Fixed #5563: Wrap override onEnter function by script call detection code to solve bugs in JSB --- cocos/2d/CCClippingNode.cpp | 15 ++++++++ cocos/2d/CCNode.cpp | 34 ------------------- cocos/2d/CCNode.h | 34 +++++++++++++++++++ cocos/2d/CCParticleSystem.cpp | 15 ++++++++ .../editor-support/cocostudio/CCArmature.cpp | 15 ++++++++ cocos/editor-support/spine/CCSkeleton.cpp | 17 +++++++++- cocos/ui/CCProtectedNode.cpp | 15 ++++++++ cocos/ui/UILayout.cpp | 15 ++++++++ cocos/ui/UIPageView.cpp | 15 ++++++++ cocos/ui/UIScrollView.cpp | 16 ++++++++- cocos/ui/UITextField.cpp | 15 ++++++++ extensions/GUI/CCEditBox/CCEditBox.cpp | 15 ++++++++ 12 files changed, 185 insertions(+), 36 deletions(-) diff --git a/cocos/2d/CCClippingNode.cpp b/cocos/2d/CCClippingNode.cpp index 15fb64a923..6071fe2088 100644 --- a/cocos/2d/CCClippingNode.cpp +++ b/cocos/2d/CCClippingNode.cpp @@ -142,6 +142,14 @@ bool ClippingNode::init(Node *stencil) void ClippingNode::onEnter() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + Node::onEnter(); if (_stencil != nullptr) @@ -152,6 +160,13 @@ void ClippingNode::onEnter() { CCLOG("ClippingNode warning: _stencil is nil."); } + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } void ClippingNode::onEnterTransitionDidFinish() diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 07d28e4455..c76b893709 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -1050,40 +1050,6 @@ Mat4 Node::transform(const Mat4& parentTransform) return ret; } - -#if CC_ENABLE_SCRIPT_BINDING - -static bool sendNodeEventToJS(Node* node, int action) -{ - auto scriptEngine = ScriptEngineManager::getInstance()->getScriptEngine(); - - if (scriptEngine->isCalledFromScript()) - { - scriptEngine->setCalledFromScript(false); - } - else - { - 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 - void Node::onEnter() { #if CC_ENABLE_SCRIPT_BINDING diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index cfac0a194f..06959b10cb 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -73,6 +73,40 @@ 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()) + { + scriptEngine->setCalledFromScript(false); + } + else + { + 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 724b196724..9ddc692c3d 100644 --- a/cocos/2d/CCParticleSystem.cpp +++ b/cocos/2d/CCParticleSystem.cpp @@ -616,10 +616,25 @@ void ParticleSystem::initParticle(tParticle* particle) void ParticleSystem::onEnter() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + Node::onEnter(); // update after action in run! this->scheduleUpdateWithPriority(1); + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } void ParticleSystem::onExit() diff --git a/cocos/editor-support/cocostudio/CCArmature.cpp b/cocos/editor-support/cocostudio/CCArmature.cpp index aa7b32335b..3cdd04fe25 100644 --- a/cocos/editor-support/cocostudio/CCArmature.cpp +++ b/cocos/editor-support/cocostudio/CCArmature.cpp @@ -434,8 +434,23 @@ void Armature::draw(cocos2d::Renderer *renderer, const Mat4 &transform, uint32_t void Armature::onEnter() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + Node::onEnter(); scheduleUpdate(); + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } void Armature::onExit() diff --git a/cocos/editor-support/spine/CCSkeleton.cpp b/cocos/editor-support/spine/CCSkeleton.cpp index ec8ed6b9ff..346b19b083 100644 --- a/cocos/editor-support/spine/CCSkeleton.cpp +++ b/cocos/editor-support/spine/CCSkeleton.cpp @@ -276,8 +276,23 @@ Rect Skeleton::getBoundingBox () const { } void Skeleton::onEnter() { - Node::onEnter(); +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + + Node::onEnter(); scheduleUpdate(); + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } void Skeleton::onExit() { diff --git a/cocos/ui/CCProtectedNode.cpp b/cocos/ui/CCProtectedNode.cpp index e49aa6bbde..91b300f1f8 100644 --- a/cocos/ui/CCProtectedNode.cpp +++ b/cocos/ui/CCProtectedNode.cpp @@ -337,9 +337,24 @@ void ProtectedNode::visit(Renderer* renderer, const Mat4 &parentTransform, uint3 void ProtectedNode::onEnter() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + Node::onEnter(); for( const auto &child: _protectedChildren) child->onEnter(); + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } void ProtectedNode::onEnterTransitionDidFinish() diff --git a/cocos/ui/UILayout.cpp b/cocos/ui/UILayout.cpp index 74495a8e1b..ba22857014 100644 --- a/cocos/ui/UILayout.cpp +++ b/cocos/ui/UILayout.cpp @@ -102,6 +102,14 @@ Layout::~Layout() void Layout::onEnter() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + Widget::onEnter(); if (_clippingStencil) { @@ -109,6 +117,13 @@ void Layout::onEnter() } _doLayoutDirty = true; _clippingRectDirty = true; + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } void Layout::onExit() diff --git a/cocos/ui/UIPageView.cpp b/cocos/ui/UIPageView.cpp index bec6b266b9..ee992a488e 100644 --- a/cocos/ui/UIPageView.cpp +++ b/cocos/ui/UIPageView.cpp @@ -68,8 +68,23 @@ PageView* PageView::create() void PageView::onEnter() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + Layout::onEnter(); scheduleUpdate(); + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } bool PageView::init() diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index ad66c67a37..aa21fde150 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -95,9 +95,23 @@ ScrollView* ScrollView::create() void ScrollView::onEnter() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + Layout::onEnter(); scheduleUpdate(); - + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } bool ScrollView::init() diff --git a/cocos/ui/UITextField.cpp b/cocos/ui/UITextField.cpp index 7199783b69..9062dd5c43 100644 --- a/cocos/ui/UITextField.cpp +++ b/cocos/ui/UITextField.cpp @@ -415,8 +415,23 @@ bool TextField::init() void TextField::onEnter() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + Widget::onEnter(); scheduleUpdate(); + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } void TextField::initRenderer() diff --git a/extensions/GUI/CCEditBox/CCEditBox.cpp b/extensions/GUI/CCEditBox/CCEditBox.cpp index 12572be246..1964c6a4f6 100644 --- a/extensions/GUI/CCEditBox/CCEditBox.cpp +++ b/extensions/GUI/CCEditBox/CCEditBox.cpp @@ -329,6 +329,14 @@ void EditBox::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t pa void EditBox::onEnter(void) { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendNodeEventToJS(this, kNodeOnEnter)) + return; + } +#endif + ControlButton::onEnter(); if (_editBoxImpl != NULL) { @@ -337,6 +345,13 @@ void EditBox::onEnter(void) #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) this->schedule(schedule_selector(EditBox::updatePosition), CHECK_EDITBOX_POSITION_INTERVAL); #endif + +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeLua) + { + sendNodeEventToLua(this, kNodeOnEnter); + } +#endif } void EditBox::updatePosition(float dt) From d00c9fe70a5c270275b83388a12ac954be4d9f5e Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 16 Jun 2014 12:50:35 +0800 Subject: [PATCH 2/4] Fixed #5563: Add sendNodeEventToJSExtended function to fix issues and remove lua event send (unnecessary) --- cocos/2d/CCClippingNode.cpp | 9 +-------- cocos/2d/CCNode.h | 15 +++++++++++++++ cocos/2d/CCParticleSystem.cpp | 9 +-------- cocos/editor-support/cocostudio/CCArmature.cpp | 9 +-------- cocos/editor-support/spine/CCSkeleton.cpp | 9 +-------- cocos/ui/CCProtectedNode.cpp | 9 +-------- cocos/ui/UILayout.cpp | 9 +-------- cocos/ui/UIPageView.cpp | 9 +-------- cocos/ui/UIScrollView.cpp | 9 +-------- cocos/ui/UITextField.cpp | 9 +-------- extensions/GUI/CCEditBox/CCEditBox.cpp | 9 +-------- 11 files changed, 25 insertions(+), 80 deletions(-) diff --git a/cocos/2d/CCClippingNode.cpp b/cocos/2d/CCClippingNode.cpp index 6071fe2088..0b933917af 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 (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif @@ -160,13 +160,6 @@ void ClippingNode::onEnter() { CCLOG("ClippingNode warning: _stencil is nil."); } - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } void ClippingNode::onEnterTransitionDidFinish() diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 06959b10cb..763f93b641 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -82,6 +82,7 @@ static bool sendNodeEventToJS(Node* node, int action) if (scriptEngine->isCalledFromScript()) { + // Should only be invoked at root class Node scriptEngine->setCalledFromScript(false); } else @@ -94,6 +95,20 @@ static bool sendNodeEventToJS(Node* node, int action) 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) { diff --git a/cocos/2d/CCParticleSystem.cpp b/cocos/2d/CCParticleSystem.cpp index 9ddc692c3d..7d538fb70b 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 (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif @@ -628,13 +628,6 @@ void ParticleSystem::onEnter() // update after action in run! this->scheduleUpdateWithPriority(1); - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } void ParticleSystem::onExit() diff --git a/cocos/editor-support/cocostudio/CCArmature.cpp b/cocos/editor-support/cocostudio/CCArmature.cpp index 3cdd04fe25..1b71db9347 100644 --- a/cocos/editor-support/cocostudio/CCArmature.cpp +++ b/cocos/editor-support/cocostudio/CCArmature.cpp @@ -437,20 +437,13 @@ void Armature::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif Node::onEnter(); scheduleUpdate(); - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } void Armature::onExit() diff --git a/cocos/editor-support/spine/CCSkeleton.cpp b/cocos/editor-support/spine/CCSkeleton.cpp index 346b19b083..e2f52b945f 100644 --- a/cocos/editor-support/spine/CCSkeleton.cpp +++ b/cocos/editor-support/spine/CCSkeleton.cpp @@ -279,20 +279,13 @@ void Skeleton::onEnter() { #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif Node::onEnter(); scheduleUpdate(); - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } void Skeleton::onExit() { diff --git a/cocos/ui/CCProtectedNode.cpp b/cocos/ui/CCProtectedNode.cpp index 91b300f1f8..ebbde2023e 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 (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif @@ -348,13 +348,6 @@ void ProtectedNode::onEnter() Node::onEnter(); for( const auto &child: _protectedChildren) child->onEnter(); - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } void ProtectedNode::onEnterTransitionDidFinish() diff --git a/cocos/ui/UILayout.cpp b/cocos/ui/UILayout.cpp index ba22857014..b4f1227b17 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 (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif @@ -117,13 +117,6 @@ void Layout::onEnter() } _doLayoutDirty = true; _clippingRectDirty = true; - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } void Layout::onExit() diff --git a/cocos/ui/UIPageView.cpp b/cocos/ui/UIPageView.cpp index ee992a488e..dd465c73c5 100644 --- a/cocos/ui/UIPageView.cpp +++ b/cocos/ui/UIPageView.cpp @@ -71,20 +71,13 @@ void PageView::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif Layout::onEnter(); scheduleUpdate(); - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } bool PageView::init() diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index aa21fde150..183df6d1b1 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -98,20 +98,13 @@ void ScrollView::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif Layout::onEnter(); scheduleUpdate(); - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } bool ScrollView::init() diff --git a/cocos/ui/UITextField.cpp b/cocos/ui/UITextField.cpp index 9062dd5c43..c625aeb097 100644 --- a/cocos/ui/UITextField.cpp +++ b/cocos/ui/UITextField.cpp @@ -418,20 +418,13 @@ void TextField::onEnter() #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { - if (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif Widget::onEnter(); scheduleUpdate(); - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } void TextField::initRenderer() diff --git a/extensions/GUI/CCEditBox/CCEditBox.cpp b/extensions/GUI/CCEditBox/CCEditBox.cpp index 1964c6a4f6..880a63be75 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 (sendNodeEventToJS(this, kNodeOnEnter)) + if (sendNodeEventToJSExtended(this, kNodeOnEnter)) return; } #endif @@ -345,13 +345,6 @@ void EditBox::onEnter(void) #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) this->schedule(schedule_selector(EditBox::updatePosition), CHECK_EDITBOX_POSITION_INTERVAL); #endif - -#if CC_ENABLE_SCRIPT_BINDING - if (_scriptType == kScriptTypeLua) - { - sendNodeEventToLua(this, kNodeOnEnter); - } -#endif } void EditBox::updatePosition(float dt) From 94fdff1da9c0cefc987c848e1a1741683e44c1d4 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 18 Jun 2014 11:51:52 +0800 Subject: [PATCH 3/4] Fixed #5563: Make sendNodeEventToXXX as static functions of ScriptEngineManager in CCScriptSupport --- cocos/2d/CCClippingNode.cpp | 2 +- cocos/2d/CCNode.cpp | 16 +++--- cocos/2d/CCNode.h | 49 ------------------- cocos/2d/CCParticleSystem.cpp | 2 +- cocos/base/CCScriptSupport.cpp | 46 +++++++++++++++++ cocos/base/CCScriptSupport.h | 16 ++++++ .../editor-support/cocostudio/CCArmature.cpp | 2 +- cocos/editor-support/spine/CCSkeleton.cpp | 2 +- cocos/ui/CCProtectedNode.cpp | 2 +- cocos/ui/UILayout.cpp | 2 +- cocos/ui/UIPageView.cpp | 2 +- cocos/ui/UIScrollView.cpp | 2 +- cocos/ui/UITextField.cpp | 2 +- extensions/GUI/CCEditBox/CCEditBox.cpp | 2 +- 14 files changed, 80 insertions(+), 67 deletions(-) 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 From 950894de80755c4aa50b70e739afbe7dd0fc4002 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 18 Jun 2014 12:47:48 +0800 Subject: [PATCH 4/4] Fixed #5563: Fix include issue --- cocos/base/CCScriptSupport.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/base/CCScriptSupport.cpp b/cocos/base/CCScriptSupport.cpp index dda9ecca11..43f0d482c6 100644 --- a/cocos/base/CCScriptSupport.cpp +++ b/cocos/base/CCScriptSupport.cpp @@ -28,7 +28,7 @@ #if CC_ENABLE_SCRIPT_BINDING #include "base/CCScheduler.h" -#include "CCNode.h" +#include "2d/CCNode.h" bool CC_DLL cc_assert_script_compatible(const char *msg) {