Changeset View
Changeset View
Standalone View
Standalone View
source/gui/Scripting/JSInterface_IGUIObject.cpp
Show All 13 Lines | |||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "JSInterface_GUIProxy_impl.h" | #include "JSInterface_GUIProxy_impl.h" | ||||
#include "simulation2/system/ParamNode.h" | |||||
using GUIObjectType = IGUIObject; | using GUIObjectType = IGUIObject; | ||||
bool IGUICreateChild(JSContext* cx, uint argc, JS::Value* vp) | |||||
{ | |||||
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); | |||||
IGUIObject* e = static_cast<IGUIObject*>(js::GetProxyPrivate(args.thisv().toObjectOrNull()).toPrivate()); | |||||
if (!e) | |||||
return false; | |||||
ScriptInterface& scriptInterface = *ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; | |||||
ScriptRequest rq(scriptInterface); | |||||
CParamNode node; | |||||
node.ApplyLayer(scriptInterface, args.get(0)); | |||||
IGUIObject* child = e->CreateChild(node); | |||||
if (!child) | |||||
return false; | |||||
ScriptInterface::ToJSVal(rq, args.rval(), child); | |||||
return true; | |||||
} | |||||
bool IGUIDeleteChild(JSContext* cx, uint argc, JS::Value* vp) | |||||
{ | |||||
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); | |||||
IGUIObject* e = static_cast<IGUIObject*>(js::GetProxyPrivate(args.thisv().toObjectOrNull()).toPrivate()); | |||||
if (!e) | |||||
return false; | |||||
ScriptInterface& scriptInterface = *ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; | |||||
ScriptRequest rq(scriptInterface); | |||||
CGUI* gui = static_cast<CGUI*>(ScriptInterface::GetScriptInterfaceAndCBData(cx)->pCBData); | |||||
CStr name; | |||||
if (!ScriptInterface::FromJSVal(rq, args.get(0), name)) | |||||
{ | |||||
LOGERROR("DeleteChild: first argument (object name) was not a string."); | |||||
return false; | |||||
} | |||||
IGUIObject* child = gui->FindObjectByName(name); | |||||
// If the object does not exist, assume we already deleted it/we're good and do nothing. | |||||
if (!child) | |||||
return true; | |||||
if (!e->DeleteChild(child)) | |||||
{ | |||||
// Fail in this case - it's likely user error. | |||||
LOGERROR("Object '%s' exists but is not a child of '%s'", name, e->GetPresentableName()); | |||||
return false; | |||||
} | |||||
return true; | |||||
} | |||||
template<> | template<> | ||||
void JSI_GUIProxy<GUIObjectType>::CreateFunctions(const ScriptRequest& rq, GUIProxyProps* cache) | void JSI_GUIProxy<GUIObjectType>::CreateFunctions(const ScriptRequest& rq, GUIProxyProps* cache) | ||||
{ | { | ||||
#define func(class, func) &JSInterface_GUIProxy::apply_to<GUIObjectType, class, &class::func> | #define func(class, func) &JSInterface_GUIProxy::apply_to<GUIObjectType, class, &class::func> | ||||
cache->setFunction(rq, "toString", func(IGUIObject, toString), 0); | cache->setFunction(rq, "toString", func(IGUIObject, toString), 0); | ||||
cache->setFunction(rq, "focus", func(IGUIObject, focus), 0); | cache->setFunction(rq, "focus", func(IGUIObject, focus), 0); | ||||
cache->setFunction(rq, "blur", func(IGUIObject, blur), 0); | cache->setFunction(rq, "blur", func(IGUIObject, blur), 0); | ||||
cache->setFunction(rq, "getComputedSize", func(IGUIObject, getComputedSize), 0); | cache->setFunction(rq, "getComputedSize", func(IGUIObject, getComputedSize), 0); | ||||
#undef func | #undef func | ||||
cache->setFunction(rq, "createChild", IGUICreateChild, 1); | |||||
cache->setFunction(rq, "deleteChild", IGUIDeleteChild, 1); | |||||
} | } | ||||
template class JSI_GUIProxy<GUIObjectType>; | template class JSI_GUIProxy<GUIObjectType>; |
Wildfire Games · Phabricator