Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #5563: Wrap override onEnter function by script call detection code to solve bugs in JSB #7071

Merged
merged 4 commits into from Jun 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions cocos/2d/CCClippingNode.cpp
Expand Up @@ -142,6 +142,14 @@ bool ClippingNode::init(Node *stencil)

void ClippingNode::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

Node::onEnter();

if (_stencil != nullptr)
Expand Down
50 changes: 8 additions & 42 deletions cocos/2d/CCNode.cpp
Expand Up @@ -1050,46 +1050,12 @@ 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
if (_scriptType == kScriptTypeJavascript)
{
if (sendNodeEventToJS(this, kNodeOnEnter))
if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnEnter))
return;
}
#endif
Expand All @@ -1106,7 +1072,7 @@ void Node::onEnter()
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua)
{
sendNodeEventToLua(this, kNodeOnEnter);
ScriptEngineManager::sendNodeEventToLua(this, kNodeOnEnter);
}
#endif
}
Expand All @@ -1116,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
Expand All @@ -1128,7 +1094,7 @@ void Node::onEnterTransitionDidFinish()
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua)
{
sendNodeEventToLua(this, kNodeOnEnterTransitionDidFinish);
ScriptEngineManager::sendNodeEventToLua(this, kNodeOnEnterTransitionDidFinish);
}
#endif
}
Expand All @@ -1138,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
Expand All @@ -1149,7 +1115,7 @@ void Node::onExitTransitionDidStart()
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua)
{
sendNodeEventToLua(this, kNodeOnExitTransitionDidStart);
ScriptEngineManager::sendNodeEventToLua(this, kNodeOnExitTransitionDidStart);
}
#endif
}
Expand All @@ -1159,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
Expand All @@ -1174,7 +1140,7 @@ void Node::onExit()
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua)
{
sendNodeEventToLua(this, kNodeOnExit);
ScriptEngineManager::sendNodeEventToLua(this, kNodeOnExit);
}
#endif
}
Expand Down
8 changes: 8 additions & 0 deletions cocos/2d/CCParticleSystem.cpp
Expand Up @@ -616,6 +616,14 @@ void ParticleSystem::initParticle(tParticle* particle)

void ParticleSystem::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

Node::onEnter();

// update after action in run!
Expand Down
46 changes: 46 additions & 0 deletions cocos/base/CCScriptSupport.cpp
Expand Up @@ -28,6 +28,7 @@
#if CC_ENABLE_SCRIPT_BINDING

#include "base/CCScheduler.h"
#include "2d/CCNode.h"

bool CC_DLL cc_assert_script_compatible(const char *msg)
{
Expand Down Expand Up @@ -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
16 changes: 16 additions & 0 deletions cocos/base/CCScriptSupport.h
Expand Up @@ -450,6 +450,7 @@ class CC_DLL ScriptEngineProtocol
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
Expand Down Expand Up @@ -490,6 +491,21 @@ class CC_DLL ScriptEngineManager
* @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
Expand Down
8 changes: 8 additions & 0 deletions cocos/editor-support/cocostudio/CCArmature.cpp
Expand Up @@ -434,6 +434,14 @@ void Armature::draw(cocos2d::Renderer *renderer, const Mat4 &transform, uint32_t

void Armature::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

Node::onEnter();
scheduleUpdate();
}
Expand Down
10 changes: 9 additions & 1 deletion cocos/editor-support/spine/CCSkeleton.cpp
Expand Up @@ -276,7 +276,15 @@ Rect Skeleton::getBoundingBox () const {
}

void Skeleton::onEnter() {
Node::onEnter();
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

Node::onEnter();
scheduleUpdate();
}

Expand Down
8 changes: 8 additions & 0 deletions cocos/ui/CCProtectedNode.cpp
Expand Up @@ -337,6 +337,14 @@ void ProtectedNode::visit(Renderer* renderer, const Mat4 &parentTransform, uint3

void ProtectedNode::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

Node::onEnter();
for( const auto &child: _protectedChildren)
child->onEnter();
Expand Down
8 changes: 8 additions & 0 deletions cocos/ui/UILayout.cpp
Expand Up @@ -102,6 +102,14 @@ Layout::~Layout()

void Layout::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

Widget::onEnter();
if (_clippingStencil)
{
Expand Down
8 changes: 8 additions & 0 deletions cocos/ui/UIPageView.cpp
Expand Up @@ -68,6 +68,14 @@ PageView* PageView::create()

void PageView::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

Layout::onEnter();
scheduleUpdate();
}
Expand Down
9 changes: 8 additions & 1 deletion cocos/ui/UIScrollView.cpp
Expand Up @@ -95,9 +95,16 @@ ScrollView* ScrollView::create()

void ScrollView::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

Layout::onEnter();
scheduleUpdate();

}

bool ScrollView::init()
Expand Down
8 changes: 8 additions & 0 deletions cocos/ui/UITextField.cpp
Expand Up @@ -415,6 +415,14 @@ bool TextField::init()

void TextField::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

Widget::onEnter();
scheduleUpdate();
}
Expand Down
8 changes: 8 additions & 0 deletions extensions/GUI/CCEditBox/CCEditBox.cpp
Expand Up @@ -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 (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif

ControlButton::onEnter();
if (_editBoxImpl != NULL)
{
Expand Down