Changeset View
Changeset View
Standalone View
Standalone View
source/gui/ObjectBases/IGUIObject.cpp
Show All 19 Lines | |||||
#include "IGUIObject.h" | #include "IGUIObject.h" | ||||
#include "gui/CGUI.h" | #include "gui/CGUI.h" | ||||
#include "gui/CGUISetting.h" | #include "gui/CGUISetting.h" | ||||
#include "js/Conversions.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" | ||||
#include "scriptinterface/ScriptContext.h" | |||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "soundmanager/ISoundManager.h" | #include "soundmanager/ISoundManager.h" | ||||
const CStr IGUIObject::EventNameMouseEnter = "MouseEnter"; | const CStr IGUIObject::EventNameMouseEnter = "MouseEnter"; | ||||
const CStr IGUIObject::EventNameMouseMove = "MouseMove"; | const CStr IGUIObject::EventNameMouseMove = "MouseMove"; | ||||
const CStr IGUIObject::EventNameMouseLeave = "MouseLeave"; | const CStr IGUIObject::EventNameMouseLeave = "MouseLeave"; | ||||
IGUIObject::IGUIObject(CGUI& pGUI) | IGUIObject::IGUIObject(CGUI& pGUI) | ||||
▲ Show 20 Lines • Show All 258 Lines • ▼ Show 20 Lines | float IGUIObject::GetBufferedZ() const | ||||
// In philosophy, a parentless object shouldn't be able to have a relative sizing, | // In philosophy, a parentless object shouldn't be able to have a relative sizing, | ||||
// but we'll accept it so that absolute can be used as default without a complaint. | // but we'll accept it so that absolute can be used as default without a complaint. | ||||
// Also, you could consider those objects children to the screen resolution. | // Also, you could consider those objects children to the screen resolution. | ||||
return m_Z; | return m_Z; | ||||
} | } | ||||
void IGUIObject::RegisterScriptHandler(const CStr& eventName, const CStr& Code, CGUI& pGUI) | void IGUIObject::RegisterScriptHandler(const CStr& eventName, const CStr& Code, CGUI& pGUI) | ||||
{ | { | ||||
ScriptInterface::Request rq(pGUI.GetScriptInterface()); | ScriptRequest rq(pGUI.GetScriptInterface()); | ||||
const int paramCount = 1; | const int paramCount = 1; | ||||
const char* paramNames[paramCount] = { "mouse" }; | const char* paramNames[paramCount] = { "mouse" }; | ||||
// Location to report errors from | // Location to report errors from | ||||
CStr CodeName = GetName() + " " + eventName; | CStr CodeName = GetName() + " " + eventName; | ||||
// Generate a unique name | // Generate a unique name | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
PROFILE2_EVENT("gui mouse event"); | PROFILE2_EVENT("gui mouse event"); | ||||
PROFILE2_ATTR("type: %s", eventName.c_str()); | PROFILE2_ATTR("type: %s", eventName.c_str()); | ||||
PROFILE2_ATTR("object: %s", m_Name.c_str()); | PROFILE2_ATTR("object: %s", m_Name.c_str()); | ||||
SGUIMessage msg(type); | SGUIMessage msg(type); | ||||
HandleMessage(msg); | HandleMessage(msg); | ||||
ScriptInterface::Request rq(m_pGUI.GetScriptInterface()); | ScriptRequest rq(m_pGUI.GetScriptInterface()); | ||||
// Set up the 'mouse' parameter | // Set up the 'mouse' parameter | ||||
JS::RootedValue mouse(rq.cx); | JS::RootedValue mouse(rq.cx); | ||||
const CPos& mousePos = m_pGUI.GetMousePos(); | const CPos& mousePos = m_pGUI.GetMousePos(); | ||||
ScriptInterface::CreateObject( | ScriptInterface::CreateObject( | ||||
rq, | rq, | ||||
Show All 13 Lines | void IGUIObject::ScriptEvent(const CStr& eventName) | ||||
ScriptEventWithReturn(eventName); | ScriptEventWithReturn(eventName); | ||||
} | } | ||||
bool IGUIObject::ScriptEventWithReturn(const CStr& eventName) | bool IGUIObject::ScriptEventWithReturn(const CStr& eventName) | ||||
{ | { | ||||
if (m_ScriptHandlers.find(eventName) == m_ScriptHandlers.end()) | if (m_ScriptHandlers.find(eventName) == m_ScriptHandlers.end()) | ||||
return false; | return false; | ||||
ScriptInterface::Request rq(m_pGUI.GetScriptInterface()); | ScriptRequest rq(m_pGUI.GetScriptInterface()); | ||||
JS::AutoValueVector paramData(rq.cx); | JS::AutoValueVector paramData(rq.cx); | ||||
return ScriptEventWithReturn(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); | ScriptEventWithReturn(eventName, paramData); | ||||
} | } | ||||
bool IGUIObject::ScriptEventWithReturn(const CStr& eventName, const JS::HandleValueArray& 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 false; | return false; | ||||
ScriptInterface::Request rq(m_pGUI.GetScriptInterface()); | ScriptRequest rq(m_pGUI.GetScriptInterface()); | ||||
JS::RootedObject obj(rq.cx, GetJSObject()); | JS::RootedObject obj(rq.cx, GetJSObject()); | ||||
JS::RootedValue handlerVal(rq.cx, JS::ObjectValue(*it->second)); | JS::RootedValue handlerVal(rq.cx, JS::ObjectValue(*it->second)); | ||||
JS::RootedValue result(rq.cx); | JS::RootedValue result(rq.cx); | ||||
if (!JS_CallFunctionValue(rq.cx, obj, handlerVal, paramData, &result)) | if (!JS_CallFunctionValue(rq.cx, obj, handlerVal, paramData, &result)) | ||||
{ | { | ||||
LOGERROR("Errors executing script event \"%s\"", eventName.c_str()); | LOGERROR("Errors executing script event \"%s\"", eventName.c_str()); | ||||
ScriptException::CatchPending(rq); | |||||
return false; | return false; | ||||
} | } | ||||
return JS::ToBoolean(result); | return JS::ToBoolean(result); | ||||
} | } | ||||
void IGUIObject::CreateJSObject() | void IGUIObject::CreateJSObject() | ||||
{ | { | ||||
ScriptInterface::Request rq(m_pGUI.GetScriptInterface()); | ScriptRequest rq(m_pGUI.GetScriptInterface()); | ||||
m_JSObject.init(rq.cx, m_pGUI.GetScriptInterface()->CreateCustomObject("GUIObject")); | m_JSObject.init(rq.cx, m_pGUI.GetScriptInterface()->CreateCustomObject("GUIObject")); | ||||
JS_SetPrivate(m_JSObject.get(), this); | JS_SetPrivate(m_JSObject.get(), this); | ||||
RegisterScriptFunctions(); | RegisterScriptFunctions(); | ||||
} | } | ||||
JSObject* IGUIObject::GetJSObject() | JSObject* IGUIObject::GetJSObject() | ||||
▲ Show 20 Lines • Show All 90 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator