1 /* Copyright (C) 2019 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "JSInterface_IGUIObject.h"
23 #include "gui/CGUISetting.h"
24 #include "gui/ObjectBases/IGUIObject.h"
25 #include "scriptinterface/ScriptExtraHeaders.h"
26 #include "scriptinterface/ScriptInterface.h"
28 JSClass
JSI_IGUIObject::JSI_class
= {
29 "GUIObject", JSCLASS_HAS_PRIVATE
,
31 JSI_IGUIObject::deleteProperty
,
32 JSI_IGUIObject::getProperty
,
33 JSI_IGUIObject::setProperty
,
34 nullptr, nullptr, nullptr, nullptr,
35 nullptr, nullptr, nullptr, nullptr
38 JSFunctionSpec
JSI_IGUIObject::JSI_methods
[] =
40 JS_FN("toString", JSI_IGUIObject::toString
, 0, 0),
41 JS_FN("focus", JSI_IGUIObject::focus
, 0, 0),
42 JS_FN("blur", JSI_IGUIObject::blur
, 0, 0),
43 JS_FN("getComputedSize", JSI_IGUIObject::getComputedSize
, 0, 0),
47 void JSI_IGUIObject::RegisterScriptClass(ScriptInterface
& scriptInterface
)
49 scriptInterface
.DefineCustomObjectType(&JSI_class
, nullptr, 0, nullptr, JSI_methods
, nullptr, nullptr);
52 bool JSI_IGUIObject::getProperty(JSContext
* cx
, JS::HandleObject obj
, JS::HandleId id
, JS::MutableHandleValue vp
)
55 ScriptInterface
* pScriptInterface
= ScriptInterface::GetScriptInterfaceAndCBData(cx
)->pScriptInterface
;
57 IGUIObject
* e
= ScriptInterface::GetPrivate
<IGUIObject
>(cx
, obj
, &JSI_IGUIObject::JSI_class
);
61 JS::RootedValue
idval(cx
);
62 if (!JS_IdToValue(cx
, id
, &idval
))
66 if (!ScriptInterface::FromJSVal(cx
, idval
, propName
))
69 // Skip registered functions and inherited properties
70 // including JSInterfaces of derived classes
71 if (propName
== "constructor" ||
72 propName
== "prototype" ||
73 propName
== "toString" ||
74 propName
== "toJSON" ||
75 propName
== "focus" ||
77 propName
== "getTextSize" ||
78 propName
== "getComputedSize"
82 // Use onWhatever to access event handlers
83 if (propName
.substr(0, 2) == "on")
85 CStr
eventName(CStr(propName
.substr(2)).LowerCase());
86 std::map
<CStr
, JS::Heap
<JSObject
*>>::iterator it
= e
->m_ScriptHandlers
.find(eventName
);
87 if (it
== e
->m_ScriptHandlers
.end())
90 vp
.setObject(*it
->second
.get());
94 if (propName
== "parent")
96 IGUIObject
* parent
= e
->GetParent();
99 vp
.set(JS::ObjectValue(*parent
->GetJSObject()));
101 vp
.set(JS::NullValue());
105 else if (propName
== "children")
107 ScriptInterface::CreateArray(cx
, vp
);
109 for (size_t i
= 0; i
< e
->m_Children
.size(); ++i
)
110 pScriptInterface
->SetPropertyInt(vp
, i
, e
->m_Children
[i
]);
114 else if (propName
== "name")
116 ScriptInterface::ToJSVal(cx
, vp
, e
->GetName());
119 else if (e
->SettingExists(propName
))
121 e
->m_Settings
[propName
]->ToJSVal(cx
, vp
);
125 JS_ReportError(cx
, "Property '%s' does not exist!", propName
.c_str());
129 bool JSI_IGUIObject::setProperty(JSContext
* cx
, JS::HandleObject obj
, JS::HandleId id
, JS::MutableHandleValue vp
, JS::ObjectOpResult
& result
)
131 IGUIObject
* e
= ScriptInterface::GetPrivate
<IGUIObject
>(cx
, obj
, &JSI_IGUIObject::JSI_class
);
133 return result
.fail(JSMSG_NOT_NONNULL_OBJECT
);
135 JSAutoRequest
rq(cx
);
136 JS::RootedValue
idval(cx
);
137 if (!JS_IdToValue(cx
, id
, &idval
))
138 return result
.fail(JSMSG_NOT_NONNULL_OBJECT
);
140 std::string propName
;
141 if (!ScriptInterface::FromJSVal(cx
, idval
, propName
))
142 return result
.fail(JSMSG_UNDEFINED_PROP
);
144 if (propName
== "name")
147 if (!ScriptInterface::FromJSVal(cx
, vp
, value
))
148 return result
.fail(JSMSG_UNDEFINED_PROP
);
150 return result
.succeed();
153 JS::RootedObject
vpObj(cx
);
155 vpObj
= &vp
.toObject();
157 // Use onWhatever to set event handlers
158 if (propName
.substr(0, 2) == "on")
160 if (vp
.isPrimitive() || vp
.isNull() || !JS_ObjectIsFunction(cx
, &vp
.toObject()))
162 JS_ReportError(cx
, "on- event-handlers must be functions");
163 return result
.fail(JSMSG_NOT_FUNCTION
);
166 CStr
eventName(CStr(propName
.substr(2)).LowerCase());
167 e
->SetScriptHandler(eventName
, vpObj
);
169 return result
.succeed();
172 if (e
->SettingExists(propName
))
173 return e
->m_Settings
[propName
]->FromJSVal(cx
, vp
, true) ? result
.succeed() : result
.fail(JSMSG_TYPE_ERR_BAD_ARGS
);
175 JS_ReportError(cx
, "Property '%s' does not exist!", propName
.c_str());
176 return result
.fail(JSMSG_UNDEFINED_PROP
);
179 bool JSI_IGUIObject::deleteProperty(JSContext
* cx
, JS::HandleObject obj
, JS::HandleId id
, JS::ObjectOpResult
& result
)
181 IGUIObject
* e
= ScriptInterface::GetPrivate
<IGUIObject
>(cx
, obj
, &JSI_IGUIObject::JSI_class
);
183 return result
.fail(JSMSG_NOT_NONNULL_OBJECT
);
185 JSAutoRequest
rq(cx
);
186 JS::RootedValue
idval(cx
);
187 if (!JS_IdToValue(cx
, id
, &idval
))
188 return result
.fail(JSMSG_NOT_NONNULL_OBJECT
);
190 std::string propName
;
191 if (!ScriptInterface::FromJSVal(cx
, idval
, propName
))
192 return result
.fail(JSMSG_UNDEFINED_PROP
);
195 if (propName
.substr(0, 2) == "on")
197 CStr
eventName(CStr(propName
.substr(2)).LowerCase());
198 e
->UnsetScriptHandler(eventName
);
199 return result
.succeed();
202 JS_ReportError(cx
, "Only event handlers can be deleted from GUI objects!");
203 return result
.fail(JSMSG_UNDEFINED_PROP
);
206 bool JSI_IGUIObject::toString(JSContext
* cx
, uint argc
, JS::Value
* vp
)
208 // No JSAutoRequest needed for these calls
209 JS::CallArgs args
= JS::CallArgsFromVp(argc
, vp
);
210 IGUIObject
* e
= ScriptInterface::GetPrivate
<IGUIObject
>(cx
, args
, &JSI_IGUIObject::JSI_class
);
214 ScriptInterface::ToJSVal(cx
, args
.rval(), "[GUIObject: " + e
->GetName() + "]");
218 bool JSI_IGUIObject::focus(JSContext
* cx
, uint argc
, JS::Value
* vp
)
220 // No JSAutoRequest needed for these calls
221 JS::CallArgs args
= JS::CallArgsFromVp(argc
, vp
);
222 IGUIObject
* e
= ScriptInterface::GetPrivate
<IGUIObject
>(cx
, args
, &JSI_IGUIObject::JSI_class
);
226 e
->GetGUI().SetFocusedObject(e
);
227 args
.rval().setUndefined();
231 bool JSI_IGUIObject::blur(JSContext
* cx
, uint argc
, JS::Value
* vp
)
233 // No JSAutoRequest needed for these calls
234 JS::CallArgs args
= JS::CallArgsFromVp(argc
, vp
);
235 IGUIObject
* e
= ScriptInterface::GetPrivate
<IGUIObject
>(cx
, args
, &JSI_IGUIObject::JSI_class
);
239 e
->GetGUI().SetFocusedObject(nullptr);
240 args
.rval().setUndefined();
244 bool JSI_IGUIObject::getComputedSize(JSContext
* cx
, uint argc
, JS::Value
* vp
)
246 JSAutoRequest
rq(cx
);
247 JS::CallArgs args
= JS::CallArgsFromVp(argc
, vp
);
249 IGUIObject
* e
= ScriptInterface::GetPrivate
<IGUIObject
>(cx
, args
, &JSI_IGUIObject::JSI_class
);
253 e
->UpdateCachedSize();
254 ScriptInterface::ToJSVal(cx
, args
.rval(), e
->m_CachedActualSize
);