Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/gui/Scripting/JSInterface_GUISize.cpp
Show All 37 Lines | |||||
void JSI_GUISize::RegisterScriptClass(ScriptInterface& scriptInterface) | void JSI_GUISize::RegisterScriptClass(ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.DefineCustomObjectType(&JSI_GUISize::JSI_class, JSI_GUISize::construct, 0, nullptr, JSI_GUISize::JSI_methods, nullptr, nullptr); | scriptInterface.DefineCustomObjectType(&JSI_GUISize::JSI_class, JSI_GUISize::construct, 0, nullptr, JSI_GUISize::JSI_methods, nullptr, nullptr); | ||||
} | } | ||||
bool JSI_GUISize::construct(JSContext* cx, uint argc, JS::Value* vp) | bool JSI_GUISize::construct(JSContext* cx, uint argc, JS::Value* vp) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | |||||
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); | ||||
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; | ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; | ||||
JS::RootedObject obj(cx, pScriptInterface->CreateCustomObject("GUISize")); | ScriptInterface::Request rq(*pScriptInterface); | ||||
JS::RootedObject obj(rq.cx, pScriptInterface->CreateCustomObject("GUISize")); | |||||
if (args.length() == 8) | if (args.length() == 8) | ||||
{ | { | ||||
JS_SetProperty(cx, obj, "left", args[0]); | JS_SetProperty(rq.cx, obj, "left", args[0]); | ||||
JS_SetProperty(cx, obj, "top", args[1]); | JS_SetProperty(rq.cx, obj, "top", args[1]); | ||||
JS_SetProperty(cx, obj, "right", args[2]); | JS_SetProperty(rq.cx, obj, "right", args[2]); | ||||
JS_SetProperty(cx, obj, "bottom", args[3]); | JS_SetProperty(rq.cx, obj, "bottom", args[3]); | ||||
JS_SetProperty(cx, obj, "rleft", args[4]); | JS_SetProperty(rq.cx, obj, "rleft", args[4]); | ||||
JS_SetProperty(cx, obj, "rtop", args[5]); | JS_SetProperty(rq.cx, obj, "rtop", args[5]); | ||||
JS_SetProperty(cx, obj, "rright", args[6]); | JS_SetProperty(rq.cx, obj, "rright", args[6]); | ||||
JS_SetProperty(cx, obj, "rbottom", args[7]); | JS_SetProperty(rq.cx, obj, "rbottom", args[7]); | ||||
} | } | ||||
else if (args.length() == 4) | else if (args.length() == 4) | ||||
{ | { | ||||
JS::RootedValue zero(cx, JS::NumberValue(0)); | JS::RootedValue zero(rq.cx, JS::NumberValue(0)); | ||||
JS_SetProperty(cx, obj, "left", args[0]); | JS_SetProperty(rq.cx, obj, "left", args[0]); | ||||
JS_SetProperty(cx, obj, "top", args[1]); | JS_SetProperty(rq.cx, obj, "top", args[1]); | ||||
JS_SetProperty(cx, obj, "right", args[2]); | JS_SetProperty(rq.cx, obj, "right", args[2]); | ||||
JS_SetProperty(cx, obj, "bottom", args[3]); | JS_SetProperty(rq.cx, obj, "bottom", args[3]); | ||||
JS_SetProperty(cx, obj, "rleft", zero); | JS_SetProperty(rq.cx, obj, "rleft", zero); | ||||
JS_SetProperty(cx, obj, "rtop", zero); | JS_SetProperty(rq.cx, obj, "rtop", zero); | ||||
JS_SetProperty(cx, obj, "rright", zero); | JS_SetProperty(rq.cx, obj, "rright", zero); | ||||
JS_SetProperty(cx, obj, "rbottom", zero); | JS_SetProperty(rq.cx, obj, "rbottom", zero); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
JS::RootedValue zero(cx, JS::NumberValue(0)); | JS::RootedValue zero(rq.cx, JS::NumberValue(0)); | ||||
JS_SetProperty(cx, obj, "left", zero); | JS_SetProperty(rq.cx, obj, "left", zero); | ||||
JS_SetProperty(cx, obj, "top", zero); | JS_SetProperty(rq.cx, obj, "top", zero); | ||||
JS_SetProperty(cx, obj, "right", zero); | JS_SetProperty(rq.cx, obj, "right", zero); | ||||
JS_SetProperty(cx, obj, "bottom", zero); | JS_SetProperty(rq.cx, obj, "bottom", zero); | ||||
JS_SetProperty(cx, obj, "rleft", zero); | JS_SetProperty(rq.cx, obj, "rleft", zero); | ||||
JS_SetProperty(cx, obj, "rtop", zero); | JS_SetProperty(rq.cx, obj, "rtop", zero); | ||||
JS_SetProperty(cx, obj, "rright", zero); | JS_SetProperty(rq.cx, obj, "rright", zero); | ||||
JS_SetProperty(cx, obj, "rbottom", zero); | JS_SetProperty(rq.cx, obj, "rbottom", zero); | ||||
} | } | ||||
args.rval().setObject(*obj); | args.rval().setObject(*obj); | ||||
return true; | return true; | ||||
} | } | ||||
// Produces "10", "-10", "50%", "50%-10", "50%+10", etc | // Produces "10", "-10", "50%", "50%-10", "50%+10", etc | ||||
CStr JSI_GUISize::ToPercentString(double pix, double per) | CStr JSI_GUISize::ToPercentString(double pix, double per) | ||||
{ | { | ||||
if (per == 0) | if (per == 0) | ||||
return CStr::FromDouble(pix); | return CStr::FromDouble(pix); | ||||
return CStr::FromDouble(per)+"%"+(pix == 0.0 ? CStr() : pix > 0.0 ? CStr("+")+CStr::FromDouble(pix) : CStr::FromDouble(pix)); | return CStr::FromDouble(per)+"%"+(pix == 0.0 ? CStr() : pix > 0.0 ? CStr("+")+CStr::FromDouble(pix) : CStr::FromDouble(pix)); | ||||
} | } | ||||
bool JSI_GUISize::toString(JSContext* cx, uint argc, JS::Value* vp) | bool JSI_GUISize::toString(JSContext* cx, uint argc, JS::Value* vp) | ||||
{ | { | ||||
// JSAutoRequest not needed for the calls below | |||||
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); | ||||
CStr buffer; | CStr buffer; | ||||
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; | ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; | ||||
ScriptInterface::Request rq(*pScriptInterface); | |||||
double val, valr; | double val, valr; | ||||
#define SIDE(side) \ | #define SIDE(side) \ | ||||
pScriptInterface->GetProperty(args.thisv(), #side, val); \ | pScriptInterface->GetProperty(args.thisv(), #side, val); \ | ||||
pScriptInterface->GetProperty(args.thisv(), "r"#side, valr); \ | pScriptInterface->GetProperty(args.thisv(), "r"#side, valr); \ | ||||
buffer += ToPercentString(val, valr); | buffer += ToPercentString(val, valr); | ||||
SIDE(left); | SIDE(left); | ||||
buffer += " "; | buffer += " "; | ||||
SIDE(top); | SIDE(top); | ||||
buffer += " "; | buffer += " "; | ||||
SIDE(right); | SIDE(right); | ||||
buffer += " "; | buffer += " "; | ||||
SIDE(bottom); | SIDE(bottom); | ||||
#undef SIDE | #undef SIDE | ||||
ScriptInterface::ToJSVal(cx, args.rval(), buffer); | ScriptInterface::ToJSVal(rq, args.rval(), buffer); | ||||
return true; | return true; | ||||
} | } |
Wildfire Games · Phabricator