Merge pull request #7071 from pandamicro/Script_onEnter_fix

Fixed #5563: Wrap override onEnter function by script call detection code to solve bugs in JSB
This commit is contained in:
minggo 2014-06-18 13:37:57 +08:00
commit 08453b911d
13 changed files with 151 additions and 44 deletions

View File

@ -142,6 +142,14 @@ bool ClippingNode::init(Node *stencil)
void ClippingNode::onEnter() void ClippingNode::onEnter()
{ {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Node::onEnter(); Node::onEnter();
if (_stencil != nullptr) if (_stencil != nullptr)

View File

@ -1219,46 +1219,12 @@ Mat4 Node::transform(const Mat4& parentTransform)
return ret; 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() void Node::onEnter()
{ {
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript) if (_scriptType == kScriptTypeJavascript)
{ {
if (sendNodeEventToJS(this, kNodeOnEnter)) if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnEnter))
return; return;
} }
#endif #endif
@ -1275,7 +1241,7 @@ void Node::onEnter()
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua) if (_scriptType == kScriptTypeLua)
{ {
sendNodeEventToLua(this, kNodeOnEnter); ScriptEngineManager::sendNodeEventToLua(this, kNodeOnEnter);
} }
#endif #endif
} }
@ -1285,7 +1251,7 @@ void Node::onEnterTransitionDidFinish()
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript) if (_scriptType == kScriptTypeJavascript)
{ {
if (sendNodeEventToJS(this, kNodeOnEnterTransitionDidFinish)) if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnEnterTransitionDidFinish))
return; return;
} }
#endif #endif
@ -1297,7 +1263,7 @@ void Node::onEnterTransitionDidFinish()
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua) if (_scriptType == kScriptTypeLua)
{ {
sendNodeEventToLua(this, kNodeOnEnterTransitionDidFinish); ScriptEngineManager::sendNodeEventToLua(this, kNodeOnEnterTransitionDidFinish);
} }
#endif #endif
} }
@ -1307,7 +1273,7 @@ void Node::onExitTransitionDidStart()
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript) if (_scriptType == kScriptTypeJavascript)
{ {
if (sendNodeEventToJS(this, kNodeOnExitTransitionDidStart)) if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnExitTransitionDidStart))
return; return;
} }
#endif #endif
@ -1318,7 +1284,7 @@ void Node::onExitTransitionDidStart()
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua) if (_scriptType == kScriptTypeLua)
{ {
sendNodeEventToLua(this, kNodeOnExitTransitionDidStart); ScriptEngineManager::sendNodeEventToLua(this, kNodeOnExitTransitionDidStart);
} }
#endif #endif
} }
@ -1328,7 +1294,7 @@ void Node::onExit()
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript) if (_scriptType == kScriptTypeJavascript)
{ {
if (sendNodeEventToJS(this, kNodeOnExit)) if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnExit))
return; return;
} }
#endif #endif
@ -1343,7 +1309,7 @@ void Node::onExit()
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua) if (_scriptType == kScriptTypeLua)
{ {
sendNodeEventToLua(this, kNodeOnExit); ScriptEngineManager::sendNodeEventToLua(this, kNodeOnExit);
} }
#endif #endif
} }

View File

@ -616,6 +616,14 @@ void ParticleSystem::initParticle(tParticle* particle)
void ParticleSystem::onEnter() void ParticleSystem::onEnter()
{ {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Node::onEnter(); Node::onEnter();
// update after action in run! // update after action in run!

View File

@ -28,6 +28,7 @@
#if CC_ENABLE_SCRIPT_BINDING #if CC_ENABLE_SCRIPT_BINDING
#include "base/CCScheduler.h" #include "base/CCScheduler.h"
#include "2d/CCNode.h"
bool CC_DLL cc_assert_script_compatible(const char *msg) 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 NS_CC_END
#endif // #if CC_ENABLE_SCRIPT_BINDING #endif // #if CC_ENABLE_SCRIPT_BINDING

View File

@ -450,6 +450,7 @@ public:
virtual bool parseConfig(ConfigType type, const std::string& str) = 0; virtual bool parseConfig(ConfigType type, const std::string& str) = 0;
}; };
class Node;
/** /**
ScriptEngineManager is a singleton which holds an object instance of ScriptEngineProtocl ScriptEngineManager is a singleton which holds an object instance of ScriptEngineProtocl
It helps cocos2d-x and the user code to find back LuaEngine object It helps cocos2d-x and the user code to find back LuaEngine object
@ -490,6 +491,21 @@ public:
* @lua NA * @lua NA
*/ */
static void destroyInstance(); 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 * @js NA
* @lua NA * @lua NA

View File

@ -434,6 +434,14 @@ void Armature::draw(cocos2d::Renderer *renderer, const Mat4 &transform, uint32_t
void Armature::onEnter() void Armature::onEnter()
{ {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Node::onEnter(); Node::onEnter();
scheduleUpdate(); scheduleUpdate();
} }

View File

@ -276,7 +276,15 @@ Rect Skeleton::getBoundingBox () const {
} }
void Skeleton::onEnter() { void Skeleton::onEnter() {
Node::onEnter(); #if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Node::onEnter();
scheduleUpdate(); scheduleUpdate();
} }

View File

@ -337,6 +337,14 @@ void ProtectedNode::visit(Renderer* renderer, const Mat4 &parentTransform, uint3
void ProtectedNode::onEnter() void ProtectedNode::onEnter()
{ {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Node::onEnter(); Node::onEnter();
for( const auto &child: _protectedChildren) for( const auto &child: _protectedChildren)
child->onEnter(); child->onEnter();

View File

@ -102,6 +102,14 @@ Layout::~Layout()
void Layout::onEnter() void Layout::onEnter()
{ {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Widget::onEnter(); Widget::onEnter();
if (_clippingStencil) if (_clippingStencil)
{ {

View File

@ -68,6 +68,14 @@ PageView* PageView::create()
void PageView::onEnter() void PageView::onEnter()
{ {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Layout::onEnter(); Layout::onEnter();
scheduleUpdate(); scheduleUpdate();
} }

View File

@ -95,9 +95,16 @@ ScrollView* ScrollView::create()
void ScrollView::onEnter() void ScrollView::onEnter()
{ {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Layout::onEnter(); Layout::onEnter();
scheduleUpdate(); scheduleUpdate();
} }
bool ScrollView::init() bool ScrollView::init()

View File

@ -415,6 +415,14 @@ bool TextField::init()
void TextField::onEnter() void TextField::onEnter()
{ {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
Widget::onEnter(); Widget::onEnter();
scheduleUpdate(); scheduleUpdate();
} }

View File

@ -329,6 +329,14 @@ void EditBox::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t pa
void EditBox::onEnter(void) void EditBox::onEnter(void)
{ {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
ControlButton::onEnter(); ControlButton::onEnter();
if (_editBoxImpl != NULL) if (_editBoxImpl != NULL)
{ {