1
1
forked from 0ad/0ad

Delete JSClass JSI_GUIColor / GUIColor from af9c336b43, refs #5387, D1699.

* JSI_GUIColor::construct and JSI_IGUIObject::setProperty hunk are
duplicates of FromJSVal, getProperty of ToJSVal, following 7c2e9027c2.
* The constructor, it's fallback magenta and toString have never been
used by JS.
* A JS color class providing some methods (such as found in color.js,
refs #5387, and the unused toString redundant with rgbToGuiColor, see
also eee8217b45) is more inviting to changes and maintenance if defined
in JS and can still be instantiated in C++, like the Vector2D (see also
65b02395b3).
* PredefinedColors (such as "red" or "green") can be obtained from the
prototype without defining the class in C++.
* Minimize ugliness by removing macrosity, refs 8ceb41212d.

Rename alpha to a in guiToRgbColor from eee8217b45 for consistency with
the C++ conversions (opaqueness had been skipped in the only callers of
this JS function yet).
Delete unused GUISTDTYPE Mouse in header forgotton in 8734efca94.

This was SVN commit r22534.
This commit is contained in:
elexis 2019-07-23 14:22:27 +00:00
parent 7a823ca671
commit 9be8a560a9
4 changed files with 7 additions and 105 deletions

View File

@ -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
};
}

View File

@ -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);
}

View File

@ -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

View File

@ -168,21 +168,7 @@ bool JSI_IGUIObject::getProperty(JSContext* cx, JS::HandleObject obj, JS::Handle
{
CColor color;
GUI<CColor>::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<CColor>::SetSetting(e, propName, color);
}
else