Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cocos/base/CCScriptSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions cocos/base/CCScriptSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions cocos/ui/UIWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Widget*>(child);
Expand Down
4 changes: 2 additions & 2 deletions cocos/ui/UIWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down