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
Родитель a672f03382 950894de80
Коммит 08453b911d
13 изменённых файлов: 151 добавлений и 44 удалений

Просмотреть файл

@ -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)

Просмотреть файл

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

Просмотреть файл

@ -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!

Просмотреть файл

@ -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)
{
@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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();
}

Просмотреть файл

@ -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();
}

Просмотреть файл

@ -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();

Просмотреть файл

@ -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)
{

Просмотреть файл

@ -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();
}

Просмотреть файл

@ -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()

Просмотреть файл

@ -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();
}

Просмотреть файл

@ -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)
{