Changeset View
Changeset View
Standalone View
Standalone View
source/gui/scripting/JSInterface_IGUITextOwner.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_IGUITextOwner.h" | #include "JSInterface_IGUITextOwner.h" | ||||
#include "gui/IGUIObject.h" | |||||
#include "gui/IGUITextOwner.h" | #include "gui/IGUITextOwner.h" | ||||
#include "gui/scripting/JSInterface_IGUIObject.h" | |||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
JSFunctionSpec JSI_IGUITextOwner::JSI_methods[] = | JSFunctionSpec JSI_IGUITextOwner::JSI_methods[] = | ||||
{ | { | ||||
JS_FN("getTextSize", GetTextSize, 0, 0), | JS_FN("getTextSize", [](JSContext* cx, uint UNUSED(argc), JS::Value* vp){ return JSI_IGUIObject::performObjectFunction(cx, vp, GetTextSize); }, 0, 0), | ||||
JS_FS_END | JS_FS_END | ||||
}; | }; | ||||
void JSI_IGUITextOwner::RegisterScriptFunctions(JSContext* cx, JS::HandleObject obj) | void JSI_IGUITextOwner::RegisterScriptFunctions(JSContext* cx, JS::HandleObject obj) | ||||
{ | { | ||||
JS_DefineFunctions(cx, obj, JSI_methods); | JS_DefineFunctions(cx, obj, JSI_methods); | ||||
} | } | ||||
bool JSI_IGUITextOwner::GetTextSize(JSContext* cx, uint UNUSED(argc), JS::Value* vp) | bool JSI_IGUITextOwner::GetTextSize(JSContext* cx, IGUIObject& obj, JS::MutableHandleValue rval) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | |||||
JS::CallReceiver rec = JS::CallReceiverFromVp(vp); | |||||
JS::RootedObject thisObj(cx, &rec.thisv().toObject()); | |||||
IGUIObject* obj = static_cast<IGUIObject*>(JS_GetInstancePrivate(cx, thisObj, &JSI_IGUIObject::JSI_class, nullptr)); | |||||
if (!obj) | |||||
{ | |||||
JS_ReportError(cx, "This is not an IGUIObject!"); | |||||
return false; | |||||
} | |||||
// Avoid dynamic_cast for performance reasons | // Avoid dynamic_cast for performance reasons | ||||
IGUITextOwner* objText = static_cast<IGUITextOwner*>(obj->GetTextOwner()); | IGUITextOwner* objText = static_cast<IGUITextOwner*>(obj.GetTextOwner()); | ||||
if (!objText) | if (!objText) | ||||
{ | { | ||||
JS_ReportError(cx, "This IGUIObject is not an IGUITextOwner!"); | JS_ReportError(cx, "This IGUIObject is not an IGUITextOwner!"); | ||||
return false; | return false; | ||||
} | } | ||||
ScriptInterface::ToJSVal(cx, rec.rval(), objText->CalculateTextSize()); | ScriptInterface::ToJSVal(cx, rval, objText->CalculateTextSize()); | ||||
return true; | return true; | ||||
} | } |
Wildfire Games · Phabricator