IGUIObject ScriptEventi: optional return value for signalling if the event has been handled

Comments by: badosu, elexis
Differential Revision: https://code.wildfiregames.com/D2727
This was SVN commit r23652.
This commit is contained in:
Imarok 2020-05-10 14:01:00 +00:00
parent 8e916489d6
commit 1ed64439ea
2 changed files with 40 additions and 3 deletions

View file

@ -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<CStr, JS::Heap<JSObject*> >::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()

View file

@ -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.
*/