From 490d5a37474d6f789364a76f8e2574b0b98198e2 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 16 Jul 2015 14:09:26 +0800 Subject: [PATCH] Remove cleanup from auto bindings add made it support override in JS --- cocos/2d/CCNode.cpp | 22 ++++++++++--------- cocos/2d/CCProtectedNode.cpp | 8 +++++++ cocos/2d/CCTransition.cpp | 8 +++++++ .../js-bindings/manual/cocos2d_specifics.cpp | 15 +++++++++++++ .../js-bindings/manual/cocos2d_specifics.hpp | 1 + cocos/ui/UIScale9Sprite.cpp | 8 +++++++ tools/tojs/cocos2dx.ini | 2 +- 7 files changed, 53 insertions(+), 11 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 2cff686d9c..94a868d8de 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -221,20 +221,22 @@ bool Node::init() void Node::cleanup() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnCleanup)) + return; + } + else if (_scriptType == kScriptTypeLua) + { + ScriptEngineManager::sendNodeEventToLua(this, kNodeOnCleanup); + } +#endif // #if CC_ENABLE_SCRIPT_BINDING + // actions this->stopAllActions(); this->unscheduleAllCallbacks(); -#if CC_ENABLE_SCRIPT_BINDING - if ( _scriptType != kScriptTypeNone) - { - int action = kNodeOnCleanup; - BasicScriptData data(this,(void*)&action); - ScriptEvent scriptEvent(kNodeEvent,(void*)&data); - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent); - } -#endif // #if CC_ENABLE_SCRIPT_BINDING - // timers for( const auto &child: _children) child->cleanup(); diff --git a/cocos/2d/CCProtectedNode.cpp b/cocos/2d/CCProtectedNode.cpp index 3acb2cd79a..8e60d825d5 100644 --- a/cocos/2d/CCProtectedNode.cpp +++ b/cocos/2d/CCProtectedNode.cpp @@ -63,6 +63,14 @@ ProtectedNode * ProtectedNode::create(void) void ProtectedNode::cleanup() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnCleanup)) + return; + } +#endif // #if CC_ENABLE_SCRIPT_BINDING + Node::cleanup(); // timers for( const auto &child: _protectedChildren) diff --git a/cocos/2d/CCTransition.cpp b/cocos/2d/CCTransition.cpp index 15287df224..bc70e1d59a 100644 --- a/cocos/2d/CCTransition.cpp +++ b/cocos/2d/CCTransition.cpp @@ -191,6 +191,14 @@ void TransitionScene::onExit() // custom cleanup void TransitionScene::cleanup() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnCleanup)) + return; + } +#endif // #if CC_ENABLE_SCRIPT_BINDING + Scene::cleanup(); if( _isSendCleanupToScene ) diff --git a/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp b/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp index 87603e33ed..ccc383f274 100644 --- a/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp +++ b/cocos/scripting/js-bindings/manual/cocos2d_specifics.cpp @@ -2456,6 +2456,20 @@ bool js_cocos2dx_Node_onExitTransitionDidStart(JSContext *cx, uint32_t argc, jsv return false; } +bool js_cocos2dx_Node_cleanup(JSContext *cx, uint32_t argc, jsval *vp) +{ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JS::RootedObject obj(cx, args.thisv().toObjectOrNull()); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + cocos2d::Node* cobj = (cocos2d::Node *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_Node_cleanup : Invalid Native Object"); + + ScriptingCore::getInstance()->setCalledFromScript(true); + cobj->cleanup(); + args.rval().setUndefined(); + return true; +} + bool js_cocos2dx_CCNode_setPosition(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); @@ -6145,6 +6159,7 @@ void register_cocos2dx_js_core(JSContext* cx, JS::HandleObject global) JS_DefineFunction(cx, tmpObj, "onExit", js_cocos2dx_Node_onExit, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "onEnterTransitionDidFinish", js_cocos2dx_Node_onEnterTransitionDidFinish, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "onExitTransitionDidStart", js_cocos2dx_Node_onExitTransitionDidStart, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT); + JS_DefineFunction(cx, tmpObj, "cleanup", js_cocos2dx_Node_cleanup, 0, JSPROP_ENUMERATE | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "schedule", js_CCNode_schedule, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "scheduleOnce", js_CCNode_scheduleOnce, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "scheduleUpdateWithPriority", js_cocos2dx_CCNode_scheduleUpdateWithPriority, 1, JSPROP_ENUMERATE | JSPROP_PERMANENT); diff --git a/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp b/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp index fa8c6ff9e9..5bad0a7d62 100644 --- a/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp +++ b/cocos/scripting/js-bindings/manual/cocos2d_specifics.hpp @@ -262,6 +262,7 @@ bool js_cocos2dx_Node_onEnter(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_Node_onExit(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_Node_onEnterTransitionDidFinish(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_Node_onExitTransitionDidStart(JSContext *cx, uint32_t argc, jsval *vp); +bool js_cocos2dx_Node_cleanup(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_Component_onEnter(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_Component_onExit(JSContext *cx, uint32_t argc, jsval *vp); diff --git a/cocos/ui/UIScale9Sprite.cpp b/cocos/ui/UIScale9Sprite.cpp index bc5ad0843e..3890ecd772 100644 --- a/cocos/ui/UIScale9Sprite.cpp +++ b/cocos/ui/UIScale9Sprite.cpp @@ -1385,6 +1385,14 @@ namespace ui { void Scale9Sprite::cleanup() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (ScriptEngineManager::sendNodeEventToJS(this, kNodeOnCleanup)) + return; + } +#endif // #if CC_ENABLE_SCRIPT_BINDING + Node::cleanup(); // timers for( const auto &child: _protectedChildren) diff --git a/tools/tojs/cocos2dx.ini b/tools/tojs/cocos2dx.ini index da1e96417d..1423a4ada8 100644 --- a/tools/tojs/cocos2dx.ini +++ b/tools/tojs/cocos2dx.ini @@ -103,7 +103,7 @@ skip = Node::[^setPosition$ setGLServerState description getUserObject .*UserDat CardinalSpline.*::[create actionWithDuration setPoints initWithDuration], Scheduler::[pause resume ^unschedule$ unscheduleUpdate unscheduleAllForTarget schedule isTargetPaused isScheduled], TextureCache::[addPVRTCImage], - *::[copyWith.* onEnter.* onExit.* ^description$ getObjectType onTouch.* onAcc.* onKey.* onRegisterTouchListener operator.+], + *::[copyWith.* ^cleanup$ onEnter.* onExit.* ^description$ getObjectType onTouch.* onAcc.* onKey.* onRegisterTouchListener operator.+], FileUtils::[getFileData getDataFromFile setFilenameLookupDictionary destroyInstance getFullPathCache], Application::[^application.* ^run$ getCurrentLanguageCode setAnimationInterval], Camera::[getEyeXYZ getCenterXYZ getUpXYZ],