1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/* Copyright (C) 2018 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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/>.
*/
#include "precompiled.h"
#include "JSInterface_GUITypes.h"
#include "ps/CStr.h"
#include "scriptinterface/ScriptInterface.h"
/**** GUISize ****/
JSClass JSI_GUISize::JSI_class = {
"GUISize", 0,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, JSI_GUISize::construct, nullptr
};
JSFunctionSpec JSI_GUISize::JSI_methods[] =
{
JS_FS("toString", JSI_GUISize::toString, 0, 0),
JS_FS_END
};
bool JSI_GUISize::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("GUISize"));
if (args.length() == 8)
{
JS_SetProperty(cx, obj, "left", args[0]);
JS_SetProperty(cx, obj, "top", args[1]);
JS_SetProperty(cx, obj, "right", args[2]);
JS_SetProperty(cx, obj, "bottom", args[3]);
JS_SetProperty(cx, obj, "rleft", args[4]);
JS_SetProperty(cx, obj, "rtop", args[5]);
JS_SetProperty(cx, obj, "rright", args[6]);
JS_SetProperty(cx, obj, "rbottom", args[7]);
}
else if (args.length() == 4)
{
JS::RootedValue zero(cx, JSVAL_ZERO);
JS_SetProperty(cx, obj, "left", args[0]);
JS_SetProperty(cx, obj, "top", args[1]);
JS_SetProperty(cx, obj, "right", args[2]);
JS_SetProperty(cx, obj, "bottom", args[3]);
JS_SetProperty(cx, obj, "rleft", zero);
JS_SetProperty(cx, obj, "rtop", zero);
JS_SetProperty(cx, obj, "rright", zero);
JS_SetProperty(cx, obj, "rbottom", zero);
}
else
{
JS::RootedValue zero(cx, JSVAL_ZERO);
JS_SetProperty(cx, obj, "left", zero);
JS_SetProperty(cx, obj, "top", zero);
JS_SetProperty(cx, obj, "right", zero);
JS_SetProperty(cx, obj, "bottom", zero);
JS_SetProperty(cx, obj, "rleft", zero);
JS_SetProperty(cx, obj, "rtop", zero);
JS_SetProperty(cx, obj, "rright", zero);
JS_SetProperty(cx, obj, "rbottom", zero);
}
args.rval().setObject(*obj);
return true;
}
// Produces "10", "-10", "50%", "50%-10", "50%+10", etc
CStr ToPercentString(double pix, double per)
{
if (per == 0)
return 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)
{
UNUSED2(argc);
JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
CStr buffer;
try
{
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface;
double val, valr;
#define SIDE(side) \
pScriptInterface->GetProperty(rec.thisv(), #side, val); \
pScriptInterface->GetProperty(rec.thisv(), "r"#side, valr); \
buffer += ToPercentString(val, valr);
SIDE(left);
buffer += " ";
SIDE(top);
buffer += " ";
SIDE(right);
buffer += " ";
SIDE(bottom);
#undef SIDE
}
catch (PSERROR_Scripting_ConversionFailed&)
{
rec.rval().setString(JS_NewStringCopyZ(cx, "<Error converting value to numbers>"));
return true;
}
rec.rval().setString(JS_NewStringCopyZ(cx, buffer.c_str()));
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_FS("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;
}
/**** GUIMouse ****/
JSClass JSI_GUIMouse::JSI_class = {
"GUIMouse", 0,
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, JSI_GUIMouse::construct, nullptr
};
JSFunctionSpec JSI_GUIMouse::JSI_methods[] =
{
JS_FS("toString", JSI_GUIMouse::toString, 0, 0),
JS_FS_END
};
bool JSI_GUIMouse::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("GUIMouse"));
if (args.length() == 3)
{
JS_SetProperty(cx, obj, "x", args[0]);
JS_SetProperty(cx, obj, "y", args[1]);
JS_SetProperty(cx, obj, "buttons", args[2]);
}
else
{
JS::RootedValue zero (cx, JS::NumberValue(0));
JS_SetProperty(cx, obj, "x", zero);
JS_SetProperty(cx, obj, "y", zero);
JS_SetProperty(cx, obj, "buttons", zero);
}
args.rval().setObject(*obj);
return true;
}
bool JSI_GUIMouse::toString(JSContext* cx, uint argc, JS::Value* vp)
{
UNUSED2(argc);
JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
i32 x, y, buttons;
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface;
pScriptInterface->GetProperty(rec.thisv(), "x", x);
pScriptInterface->GetProperty(rec.thisv(), "y", y);
pScriptInterface->GetProperty(rec.thisv(), "buttons", buttons);
char buffer[256];
snprintf(buffer, 256, "%d %d %d", x, y, buttons);
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);
scriptInterface.DefineCustomObjectType(&JSI_GUIMouse::JSI_class, JSI_GUIMouse::construct, 1, nullptr, JSI_GUIMouse::JSI_methods, NULL, NULL);
}
|