diff --git a/cocos/base/CCScriptSupport.cpp b/cocos/base/CCScriptSupport.cpp index 43f0d482c6c7..bb3715428412 100644 --- a/cocos/base/CCScriptSupport.cpp +++ b/cocos/base/CCScriptSupport.cpp @@ -163,6 +163,33 @@ void ScriptEngineManager::destroyInstance() } } +bool ScriptEngineManager::executeFunctionToJS(Ref *owner, const std::string func) +{ + auto scriptEngine = getInstance()->getScriptEngine(); + if (scriptEngine->isCalledFromScript()) + { + // Should only be invoked at root class of the function + scriptEngine->setCalledFromScript(false); + } + else + { + return scriptEngine->executeFunctionFromNative(owner, func); + } + + return false; +} + +bool ScriptEngineManager::executeFunctionToJSExtended(Ref *owner, const std::string func) +{ + auto scriptEngine = getInstance()->getScriptEngine(); + if (!scriptEngine->isCalledFromScript()) + { + return scriptEngine->executeFunctionFromNative(owner, func); + } + + return false; +} + bool ScriptEngineManager::sendNodeEventToJS(Node* node, int action) { auto scriptEngine = getInstance()->getScriptEngine(); diff --git a/cocos/base/CCScriptSupport.h b/cocos/base/CCScriptSupport.h index 1079dd0903dc..1aa3d2316cf2 100644 --- a/cocos/base/CCScriptSupport.h +++ b/cocos/base/CCScriptSupport.h @@ -431,6 +431,8 @@ class CC_DLL ScriptEngineProtocol */ virtual int sendEvent(ScriptEvent* evt) = 0; + virtual bool executeFunctionFromNative(Ref *owner, const std::string func) = 0; + /** called by CCAssert to allow scripting engine to handle failed assertions * @return true if the assert was handled by the script engine, false otherwise. * @js NA @@ -491,6 +493,11 @@ class CC_DLL ScriptEngineManager * @lua NA */ static void destroyInstance(); + + static bool executeFunctionToJS(Ref *owner, const std::string func); + + static bool executeFunctionToJSExtended(Ref *owner, const std::string func); + /** * @js NA * @lua NA diff --git a/cocos/ui/UIWidget.cpp b/cocos/ui/UIWidget.cpp index be7cd54206b2..f26df4275906 100644 --- a/cocos/ui/UIWidget.cpp +++ b/cocos/ui/UIWidget.cpp @@ -464,6 +464,14 @@ Node* Widget::getVirtualRenderer() void Widget::onSizeChanged() { +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (ScriptEngineManager::executeFunctionToJS(this, "onSizeChanged")) + return; + } +#endif + for (auto& child : getChildren()) { Widget* widgetChild = dynamic_cast(child); diff --git a/cocos/ui/UIWidget.h b/cocos/ui/UIWidget.h index 8ea40cc9d556..2ba09bbc722e 100644 --- a/cocos/ui/UIWidget.h +++ b/cocos/ui/UIWidget.h @@ -596,10 +596,10 @@ class Widget : public ProtectedNode, public LayoutParameterProtocol *@return void */ void dispatchFocusEvent(Widget* widgetLoseFocus, Widget* widgetGetFocus); - -protected: //call back function called when size changed. virtual void onSizeChanged(); + +protected: //initializes renderer of widget. virtual void initRenderer();