mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-09-21 20:06:40 +00:00
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:
@@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#include "gui/CGUI.h"
|
#include "gui/CGUI.h"
|
||||||
#include "gui/CGUISetting.h"
|
#include "gui/CGUISetting.h"
|
||||||
|
#include "js/Conversions.h"
|
||||||
#include "ps/CLogger.h"
|
#include "ps/CLogger.h"
|
||||||
#include "ps/GameSetup/Config.h"
|
#include "ps/GameSetup/Config.h"
|
||||||
#include "ps/Profile.h"
|
#include "ps/Profile.h"
|
||||||
@@ -396,21 +397,31 @@ InReaction IGUIObject::SendMouseEvent(EGUIMessageType type, const CStr& eventNam
|
|||||||
}
|
}
|
||||||
|
|
||||||
void IGUIObject::ScriptEvent(const CStr& eventName)
|
void IGUIObject::ScriptEvent(const CStr& eventName)
|
||||||
|
{
|
||||||
|
ScriptEventWithReturn(eventName);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IGUIObject::ScriptEventWithReturn(const CStr& eventName)
|
||||||
{
|
{
|
||||||
if (m_ScriptHandlers.find(eventName) == m_ScriptHandlers.end())
|
if (m_ScriptHandlers.find(eventName) == m_ScriptHandlers.end())
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
JSContext* cx = m_pGUI.GetScriptInterface()->GetContext();
|
JSContext* cx = m_pGUI.GetScriptInterface()->GetContext();
|
||||||
JSAutoRequest rq(cx);
|
JSAutoRequest rq(cx);
|
||||||
JS::AutoValueVector paramData(cx);
|
JS::AutoValueVector paramData(cx);
|
||||||
ScriptEvent(eventName, paramData);
|
return ScriptEventWithReturn(eventName, paramData);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IGUIObject::ScriptEvent(const CStr& eventName, const JS::HandleValueArray& 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);
|
std::map<CStr, JS::Heap<JSObject*> >::iterator it = m_ScriptHandlers.find(eventName);
|
||||||
if (it == m_ScriptHandlers.end())
|
if (it == m_ScriptHandlers.end())
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
JSContext* cx = m_pGUI.GetScriptInterface()->GetContext();
|
JSContext* cx = m_pGUI.GetScriptInterface()->GetContext();
|
||||||
JSAutoRequest rq(cx);
|
JSAutoRequest rq(cx);
|
||||||
@@ -419,7 +430,11 @@ void IGUIObject::ScriptEvent(const CStr& eventName, const JS::HandleValueArray&
|
|||||||
JS::RootedValue result(cx);
|
JS::RootedValue result(cx);
|
||||||
|
|
||||||
if (!JS_CallFunctionValue(cx, obj, handlerVal, paramData, &result))
|
if (!JS_CallFunctionValue(cx, obj, handlerVal, paramData, &result))
|
||||||
|
{
|
||||||
JS_ReportError(cx, "Errors executing script event \"%s\"", eventName.c_str());
|
JS_ReportError(cx, "Errors executing script event \"%s\"", eventName.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return JS::ToBoolean(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IGUIObject::CreateJSObject()
|
void IGUIObject::CreateJSObject()
|
||||||
|
|||||||
@@ -382,6 +382,17 @@ protected:
|
|||||||
*/
|
*/
|
||||||
void ScriptEvent(const CStr& eventName);
|
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.
|
* Execute the script for a particular action.
|
||||||
* Does nothing if no script has been registered for that 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);
|
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.
|
* Assigns a JS function to the event name.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user