diff --git a/binaries/data/mods/public/gui/common/color.js b/binaries/data/mods/public/gui/common/color.js index bf57d08797..2387c37bf3 100644 --- a/binaries/data/mods/public/gui/common/color.js +++ b/binaries/data/mods/public/gui/common/color.js @@ -34,7 +34,7 @@ function guiToRgbColor(string) "r": +color[0], "g": +color[1], "b": +color[2], - "alpha": color.length == 4 ? +color[3] : undefined + "a": color.length == 4 ? +color[3] : undefined }; } diff --git a/source/gui/scripting/JSInterface_GUITypes.cpp b/source/gui/scripting/JSInterface_GUITypes.cpp index 33ec29fad1..cebc25e7a1 100644 --- a/source/gui/scripting/JSInterface_GUITypes.cpp +++ b/source/gui/scripting/JSInterface_GUITypes.cpp @@ -22,7 +22,6 @@ #include "ps/CStr.h" #include "scriptinterface/ScriptInterface.h" -/**** GUISize ****/ JSClass JSI_GUISize::JSI_class = { "GUISize", 0, nullptr, nullptr, @@ -127,79 +126,7 @@ bool JSI_GUISize::toString(JSContext* cx, uint argc, JS::Value* vp) return true; } - -/**** GUIColor ****/ - - -JSClass JSI_GUIColor::JSI_class = { - "GUIColor", 0, - nullptr, nullptr, - nullptr, nullptr, - nullptr, nullptr, nullptr, nullptr, - nullptr, nullptr, JSI_GUIColor::construct, nullptr -}; - -JSFunctionSpec JSI_GUIColor::JSI_methods[] = -{ - JS_FN("toString", JSI_GUIColor::toString, 0, 0), - JS_FS_END -}; - -bool JSI_GUIColor::construct(JSContext* cx, uint argc, JS::Value* vp) -{ - JSAutoRequest rq(cx); - JS::CallArgs args = JS::CallArgsFromVp(argc, vp); - - ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; - JS::RootedObject obj(cx, pScriptInterface->CreateCustomObject("GUIColor")); - - if (args.length() == 4) - { - JS_SetProperty(cx, obj, "r", args[0]); - JS_SetProperty(cx, obj, "g", args[1]); - JS_SetProperty(cx, obj, "b", args[2]); - JS_SetProperty(cx, obj, "a", args[3]); - } - else - { - // Nice magenta: - JS::RootedValue c(cx, JS::NumberValue(1.0)); - JS_SetProperty(cx, obj, "r", c); - JS_SetProperty(cx, obj, "b", c); - JS_SetProperty(cx, obj, "a", c); - c = JS::NumberValue(0.0); - JS_SetProperty(cx, obj, "g", c); - } - - args.rval().setObject(*obj); - return true; -} - -bool JSI_GUIColor::toString(JSContext* cx, uint argc, JS::Value* vp) -{ - UNUSED2(argc); - JS::CallReceiver rec = JS::CallReceiverFromVp(vp); - - double r, g, b, a; - ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; - pScriptInterface->GetProperty(rec.thisv(), "r", r); - pScriptInterface->GetProperty(rec.thisv(), "g", g); - pScriptInterface->GetProperty(rec.thisv(), "b", b); - pScriptInterface->GetProperty(rec.thisv(), "a", a); - char buffer[256]; - // Convert to integers, to be compatible with the GUI's string SetSetting - snprintf(buffer, 256, "%d %d %d %d", - (int)(255.0 * r), - (int)(255.0 * g), - (int)(255.0 * b), - (int)(255.0 * a)); - rec.rval().setString(JS_NewStringCopyZ(cx, buffer)); - return true; -} - -// Initialise all the types at once: void JSI_GUITypes::init(ScriptInterface& scriptInterface) { scriptInterface.DefineCustomObjectType(&JSI_GUISize::JSI_class, JSI_GUISize::construct, 1, nullptr, JSI_GUISize::JSI_methods, NULL, NULL); - scriptInterface.DefineCustomObjectType(&JSI_GUIColor::JSI_class, JSI_GUIColor::construct, 1, nullptr, JSI_GUIColor::JSI_methods, NULL, NULL); } diff --git a/source/gui/scripting/JSInterface_GUITypes.h b/source/gui/scripting/JSInterface_GUITypes.h index c6e47473e9..d23c0c8557 100644 --- a/source/gui/scripting/JSInterface_GUITypes.h +++ b/source/gui/scripting/JSInterface_GUITypes.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2018 Wildfire Games. +/* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -31,8 +31,6 @@ } GUISTDTYPE(Size) -GUISTDTYPE(Color) -GUISTDTYPE(Mouse) #undef GUISTDTYPE // avoid unnecessary pollution diff --git a/source/gui/scripting/JSInterface_IGUIObject.cpp b/source/gui/scripting/JSInterface_IGUIObject.cpp index 2054a85cf5..2fe1a0fdaf 100644 --- a/source/gui/scripting/JSInterface_IGUIObject.cpp +++ b/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -168,21 +168,7 @@ bool JSI_IGUIObject::getProperty(JSContext* cx, JS::HandleObject obj, JS::Handle { CColor color; GUI::GetSetting(e, propName, color); - JS::RootedObject obj(cx, pScriptInterface->CreateCustomObject("GUIColor")); - vp.setObject(*obj); - JS::RootedValue c(cx); - // Attempt to minimise ugliness through macrosity -#define P(x) \ - c = JS::NumberValue(color.x); \ - if (c.isNull()) \ - return false; \ - JS_SetProperty(cx, obj, #x, c) - - P(r); - P(g); - P(b); - P(a); -#undef P + ScriptInterface::ToJSVal(cx, vp, color); break; } @@ -539,21 +525,12 @@ bool JSI_IGUIObject::setProperty(JSContext* cx, JS::HandleObject obj, JS::Handle return false; } } - else if (vp.isObject() && JS_InstanceOf(cx, vpObj, &JSI_GUIColor::JSI_class, NULL)) + else if (vp.isObject()) { CColor color; - JS::RootedValue t(cx); - double s; -#define PROP(x) \ - JS_GetProperty(cx, vpObj, #x, &t); \ - s = t.toDouble(); \ - color.x = (float)s - - PROP(r); - PROP(g); - PROP(b); - PROP(a); -#undef PROP + if (!ScriptInterface::FromJSVal(cx, vp, color) + // Exception has been thrown already + return false; GUI::SetSetting(e, propName, color); } else