diff --git a/source/gui/ObjectBases/IGUIObject.cpp b/source/gui/ObjectBases/IGUIObject.cpp index 4b81b73e9e..48241210ed 100644 --- a/source/gui/ObjectBases/IGUIObject.cpp +++ b/source/gui/ObjectBases/IGUIObject.cpp @@ -21,6 +21,7 @@ #include "gui/CGUI.h" #include "gui/CGUISetting.h" +#include "js/Conversions.h" #include "ps/CLogger.h" #include "ps/GameSetup/Config.h" #include "ps/Profile.h" @@ -396,21 +397,31 @@ InReaction IGUIObject::SendMouseEvent(EGUIMessageType type, const CStr& eventNam } void IGUIObject::ScriptEvent(const CStr& eventName) +{ + ScriptEventWithReturn(eventName); +} + +bool IGUIObject::ScriptEventWithReturn(const CStr& eventName) { if (m_ScriptHandlers.find(eventName) == m_ScriptHandlers.end()) - return; + return false; JSContext* cx = m_pGUI.GetScriptInterface()->GetContext(); JSAutoRequest rq(cx); JS::AutoValueVector paramData(cx); - ScriptEvent(eventName, paramData); + return ScriptEventWithReturn(eventName, paramData); } void IGUIObject::ScriptEvent(const CStr& eventName, const JS::HandleValueArray& paramData) +{ + ScriptEventWithReturn(eventName, paramData); +} + +bool IGUIObject::ScriptEventWithReturn(const CStr& eventName, const JS::HandleValueArray& paramData) { std::map >::iterator it = m_ScriptHandlers.find(eventName); if (it == m_ScriptHandlers.end()) - return; + return false; JSContext* cx = m_pGUI.GetScriptInterface()->GetContext(); JSAutoRequest rq(cx); @@ -419,7 +430,11 @@ void IGUIObject::ScriptEvent(const CStr& eventName, const JS::HandleValueArray& JS::RootedValue result(cx); if (!JS_CallFunctionValue(cx, obj, handlerVal, paramData, &result)) + { JS_ReportError(cx, "Errors executing script event \"%s\"", eventName.c_str()); + return false; + } + return JS::ToBoolean(result); } void IGUIObject::CreateJSObject() diff --git a/source/gui/ObjectBases/IGUIObject.h b/source/gui/ObjectBases/IGUIObject.h index 5810da5584..25dc9320ea 100644 --- a/source/gui/ObjectBases/IGUIObject.h +++ b/source/gui/ObjectBases/IGUIObject.h @@ -382,6 +382,17 @@ protected: */ void ScriptEvent(const CStr& eventName); + /** + * Execute the script for a particular action. + * Does nothing if no script has been registered for that action. + * The mouse coordinates will be passed as the first argument. + * + * @param eventName Name of action + * + * @return True if the script returned something truthy. + */ + bool ScriptEventWithReturn(const CStr& eventName); + /** * Execute the script for a particular action. * Does nothing if no script has been registered for that action. @@ -391,6 +402,17 @@ protected: */ void ScriptEvent(const CStr& eventName, const JS::HandleValueArray& paramData); + /** + * Execute the script for a particular action. + * Does nothing if no script has been registered for that action. + * + * @param eventName Name of action + * @param paramData JS::HandleValueArray arguments to pass to the event. + * + * @return True if the script returned something truthy. + */ + bool ScriptEventWithReturn(const CStr& eventName, const JS::HandleValueArray& paramData); + /** * Assigns a JS function to the event name. */